sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1673142 - in /sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis: internal/referencing/provider/CoordinateOperationMethodsHTML.java test/HTMLGenerator.java
Date Mon, 13 Apr 2015 09:20:21 GMT
Author: desruisseaux
Date: Mon Apr 13 09:20:21 2015
New Revision: 1673142

URL: http://svn.apache.org/r1673142
Log:
Referencing documentation: Generates a slightly more compact HTML.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/HTMLGenerator.java

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java?rev=1673142&r1=1673141&r2=1673142&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/internal/referencing/provider/CoordinateOperationMethodsHTML.java
[UTF-8] Mon Apr 13 09:20:21 2015
@@ -166,32 +166,26 @@ public final class CoordinateOperationMe
      */
     public void writeIndex(final Iterable<? extends OperationMethod> methods) throws
IOException {
         printlnHTML("p", "<b>Table of content:</b>");
-        open("ul");
+        final int ul = openTag("ul");
+        int innerUL  = ul + 1;
         int category = 0;
         for (final OperationMethod method : methods) {
             final int nc = category(method);
             if (nc != category) {
-                if (category != 0) {
-                    close("ul");
-                    close("li");
-                }
-                open("li");
+                closeTags(innerUL);
+                reopenTag("li");
                 switch (nc) {
                     case PROJECTION:     println("Projections");    break;
                     case CONVERSION:     println("Conversions");    break;
                     case TRANSFORMATION: println("Tranformations"); break;
                     default: throw new AssertionError(category);
                 }
-                open("ul");
+                innerUL = openTag("ul");
                 category = nc;
             }
             printlnHTML("li", "<a href=\"#" + getAnchor(method) + "\">" + escape(method.getName().getCode())
+ "</a>");
         }
-        if (category != 0) {
-            close("ul");
-            close("li");
-        }
-        close("ul");
+        closeTags(ul);
     }
 
     /**
@@ -202,10 +196,10 @@ public final class CoordinateOperationMe
      */
     public void write(final OperationMethod method) throws IOException {
         println("h2 id=\"" + getAnchor(method) + '"', method.getName().getCode());
-        open("blockquote");
+        final int blockquote = openTag("blockquote");
         writeIdentification(method);
         writeParameters(method.getParameters());
-        close("blockquote");
+        closeTags(blockquote);
     }
 
     /**
@@ -219,7 +213,7 @@ public final class CoordinateOperationMe
      * </ul>
      */
     private void writeIdentification(final OperationMethod method) throws IOException {
-        open("table class=\"info\"");
+        final int table = openTag("table class=\"info\"");
         /*
          * ────────────────    EPSG IDENTIFIERS    ────────────────────────────────────
          */
@@ -240,10 +234,10 @@ public final class CoordinateOperationMe
             }
         }
         if (buffer.length() != 0) {
-            open("tr");
+            final int tr = openTag("tr");
             println("th", "EPSG code:");
             printlnHTML("td", buffer);
-            close("tr");
+            closeTags(tr);
         }
         /*
          * ────────────────    ALIASES    ─────────────────────────────────────────────
@@ -262,10 +256,10 @@ public final class CoordinateOperationMe
             }
         }
         if (buffer.length() != 0) {
-            open("tr");
+            final int tr = openTag("tr");
             println("th", "Aliases:");
             printlnHTML("td", buffer);
-            close("tr");
+            closeTags(tr);
         }
         /*
          * ────────────────    DOMAIN OF VALIDITY    ──────────────────────────────────
@@ -273,15 +267,14 @@ public final class CoordinateOperationMe
         buffer.setLength(0);
         final DefaultGeographicBoundingBox domain = getDomainOfValidity(method);
         if (domain != null) {
-            open("tr");
+            openTag("tr");
             println("th", "Domain of validity:");
             println("td", buffer.append(new Latitude (domain.getSouthBoundLatitude())).append("
to ")
                                 .append(new Latitude (domain.getNorthBoundLatitude())).append("
and ")
                                 .append(new Longitude(domain.getWestBoundLongitude())).append("
to ")
                                 .append(new Longitude(domain.getEastBoundLongitude())));
-            close("tr");
         }
-        close("table");
+        closeTags(table);
     }
 
     /**
@@ -297,9 +290,9 @@ public final class CoordinateOperationMe
      * </ul>
      */
     private void writeParameters(final ParameterDescriptorGroup group) throws IOException
{
-        open   ("table class=\"param\"");
+        int table = openTag("table class=\"param\"");
         println("caption", "Operation parameters:");
-        open   ("tr");
+        openTag("tr");
         println("th", "EPSG");
         println("th class=\"sep\"", "Name");
         println("th class=\"sep\"", "Remarks");
@@ -308,7 +301,7 @@ public final class CoordinateOperationMe
         final Map<String, Integer> footnotes = new LinkedHashMap<>();
         for (final GeneralParameterDescriptor gp : group.descriptors()) {
             final ParameterDescriptor<?> param = (ParameterDescriptor<?>) gp;
-            reopen ("tr", "tr");
+            reopenTag("tr");
             println("td", getFirstEpsgCode(param.getIdentifiers()));
             writeName(param);
             String remarks = toLocalizedString(param.getRemarks());
@@ -331,17 +324,15 @@ public final class CoordinateOperationMe
             }
             println("td class=\"sep\"", getDefaultValue(param, getUnit(param)));
         }
-        close("tr");
-        close("table");
+        closeTags(table);
         if (!footnotes.isEmpty()) {
-            open("table class=\"footnotes\"");
+            table = openTag("table class=\"footnotes\"");
             for (final Map.Entry<String,Integer> entry : footnotes.entrySet()) {
-                open("tr");
+                reopenTag("tr");
                 println("td", String.valueOf(toSuperScript(entry.getValue())));
                 println("td", entry.getKey());
-                close("tr");
             }
-            close("table");
+            closeTags(table);
         }
     }
 
@@ -349,8 +340,8 @@ public final class CoordinateOperationMe
      * Writes the primary name and aliases.
      */
     private void writeName(final ParameterDescriptor<?> param) throws IOException {
-        open("td class=\"sep\"");
-        open("details");
+        final int td = openTag("td class=\"sep\"");
+        openTag("details");
         final Identifier name = param.getName();
         final String codeSpace = name.getCodeSpace();
         if (Constants.EPSG.equalsIgnoreCase(codeSpace)) {
@@ -359,16 +350,13 @@ public final class CoordinateOperationMe
             printlnHTML("summary", "<span class=\"non-epsg\">" + codeSpace
                     + ":</span><code>" + name.getCode() + "</code>");
         }
-        open("table class=\"aliases\"");
+        openTag("table class=\"aliases\"");
         for (final GenericName alias : param.getAlias()) {
-            open("tr");
+            reopenTag("tr");
             println("th", alias.head().toString() + ':');
             println("td", alias.tip().toString());
-            close("tr");
         }
-        close("table");
-        close("details");
-        close("td");
+        closeTags(td);
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/HTMLGenerator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/HTMLGenerator.java?rev=1673142&r1=1673141&r2=1673142&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/HTMLGenerator.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/test/HTMLGenerator.java
[UTF-8] Mon Apr 13 09:20:21 2015
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.test;
 
+import java.util.Deque;
+import java.util.ArrayDeque;
 import java.util.Locale;
 import java.io.File;
 import java.io.FileOutputStream;
@@ -72,6 +74,11 @@ public abstract class HTMLGenerator impl
     private String margin = "";
 
     /**
+     * HTML tags currently opened.
+     */
+    private final Deque<String> openedTags;
+
+    /**
      * Creates a new instance which will write in the given file.
      * This constructor immediately writes the HTML header up to the {@code <h1>} line,
inclusive.
      *
@@ -84,6 +91,7 @@ public abstract class HTMLGenerator impl
         if (file.exists()) {
             throw new IOException("File " + file.getAbsolutePath() + " already exists.");
         }
+        openedTags = new ArrayDeque<>();
         out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), ENCODING));
         out.write("<!DOCTYPE html>");
         out.newLine();
@@ -96,17 +104,16 @@ public abstract class HTMLGenerator impl
         out.newLine();
         out.write("-->");
         out.newLine();
-        open   ("html");
-        open   ("head");
+        openTag("html");
+        final int head = openTag("head");
         out.write(margin);
         out.write("<meta charset=\"" + ENCODING + "\"/>");
         out.newLine();
         println("title", CharSequences.replace(title, "™", ""));
-        open   ("style type=\"text/css\" media=\"all\"");
+        openTag   ("style type=\"text/css\" media=\"all\"");
         println("@import url(\"./reports.css\");");
-        close  ("style");
-        close  ("head");
-        open   ("body");
+        closeTags(head);
+        openTag("body");
         println("h1", title);
     }
 
@@ -124,51 +131,72 @@ public abstract class HTMLGenerator impl
     }
 
     /**
-     * Opens a new HTML element and increase the indentation.
+     * Return the given HTML tag without the attributes. For example if {@code tag} is
+     * {@code "table class='param'"}, then this method returns only {@code "table"}.
+     */
+    private static String omitAttributes(String tag) {
+        final int s = tag.indexOf(' ');
+        if (s >= 0) {
+            tag = tag.substring(0, s);
+        }
+        return tag;
+    }
+
+    /**
+     * Opens a new HTML tag and increase the indentation.
      *
-     * @param  element The HTML element without brackets (e.g. {@code "h2"}).
+     * @param  tag The HTML tag without brackets (e.g. {@code "h2"}).
+     * @return The value to give to {@link #closeTags(int)} for closing the tags.
      * @throws IOException if an error occurred while writing to the file.
      */
-    protected final void open(final String element) throws IOException {
+    protected final int openTag(final String tag) throws IOException {
         out.write(margin);
         out.write('<');
-        out.write(element);
+        out.write(tag);
         out.write('>');
         out.newLine();
         margin = CharSequences.spaces(margin.length() + INDENTATION).toString();
+        final int openedTag = openedTags.size();
+        openedTags.add(omitAttributes(tag));
+        return openedTag;
     }
 
     /**
-     * Closes a HTML element an opens a new one on the same line.
+     * Closes the last HTML tag if it is equals to the given element, and opens a new tag
on the same line.
      *
-     * @param  previous The HTML element to close, without brackets.
-     * @param  element  The HTML element without brackets (e.g. {@code "h2"}).
+     * @param  tag The HTML tag without brackets (e.g. {@code "h2"}).
      * @throws IOException if an error occurred while writing to the file.
      */
-    protected final void reopen(final String previous, final String element) throws IOException
{
-        out.write(CharSequences.spaces(margin.length() - INDENTATION).toString());
-        out.write("</");
-        out.write(previous);
-        out.write("><");
-        out.write(element);
-        out.write('>');
-        out.newLine();
+    protected final void reopenTag(final String tag) throws IOException {
+        final String tagWithoutAttributes = omitAttributes(tag);
+        if (openedTags.getLast().equals(tagWithoutAttributes)) {
+            out.write(CharSequences.spaces(margin.length() - INDENTATION).toString());
+            out.write("</");
+            out.write(tagWithoutAttributes);
+            out.write("><");
+            out.write(tag);
+            out.write('>');
+            out.newLine();
+        } else {
+            openTag(tag);
+        }
     }
 
     /**
-     * Closes a HTML element and decrease the indentation. The {@code previous} argument
must matches
-     * the argument given to the last call to {@link #open(String)}.
+     * Closes the HTML tag identified by the given number, together will all child tags.
      *
-     * @param  previous The HTML element without brackets (e.g. {@code "h2"}).
+     * @param  openedTag The value returned by the {@link #openTag(String)} matching the
tag to close.
      * @throws IOException if an error occurred while writing to the file.
      */
-    protected final void close(final String previous) throws IOException {
-        margin = CharSequences.spaces(margin.length() - INDENTATION).toString();
-        out.write(margin);
-        out.write("</");
-        out.write(previous);
-        out.write('>');
-        out.newLine();
+    protected final void closeTags(final int openedTag) throws IOException {
+        while (openedTags.size() != openedTag) {
+            margin = CharSequences.spaces(margin.length() - INDENTATION).toString();
+            out.write(margin);
+            out.write("</");
+            out.write(openedTags.removeLast());
+            out.write('>');
+            out.newLine();
+        }
     }
 
     /**
@@ -228,8 +256,7 @@ public abstract class HTMLGenerator impl
      */
     @Override
     public void close() throws IOException {
-        close("body");
-        close("html");
+        closeTags(0);
         out.close();
     }
 



Mime
View raw message