Author: kpreisser
Date: Tue Oct  1 20:26:55 2013
New Revision: 1528211

URL: http://svn.apache.org/r1528211
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=55383
- Improve HTML markup of generated pages by Txt2HTML ant task.
- Use declared encoding ("ISO-8859-1") instead of default encoding to read the 
source files.
- Use declared line separator instead of "line.separator" property so that the 
file is generated the same on every plattform.

Modified:
    tomcat/trunk/java/org/apache/tomcat/buildutil/Txt2Html.java

Modified: tomcat/trunk/java/org/apache/tomcat/buildutil/Txt2Html.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/buildutil/Txt2Html.java?rev=1528211&r1=1528210&r2=1528211&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/buildutil/Txt2Html.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/buildutil/Txt2Html.java Tue Oct  1 
20:26:55 2013
@@ -19,9 +19,11 @@ package org.apache.tomcat.buildutil;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -53,6 +55,18 @@ public class Txt2Html
     private final List<FileSet> filesets = new LinkedList<>();
 
     /**
+     * The encoding of the source files (.java and .jsp).  Once they use
+     * UTF-8, this will need to be updated.
+     */
+    private static final String SOURCE_ENCODING = "ISO-8859-1";
+
+    /**
+     * Line terminator to be used for separating lines of the generated
+     * HTML page, to be independent from "line.separator" system property.
+     */
+    private static final String LINE_SEPARATOR = "\r\n";
+
+    /**
      * Sets the directory to contain the resulting files
      *
      * @param todir The directory
@@ -126,39 +140,41 @@ public class Txt2Html
         throws IOException
     {
         // Open files:
-        BufferedReader in = new BufferedReader( new FileReader( from ) );
-        PrintWriter out = new PrintWriter( new FileWriter( to ) );
+        try (BufferedReader in = new BufferedReader(new InputStreamReader(
+                new FileInputStream(from), SOURCE_ENCODING))) {
+            try (PrintWriter out = new PrintWriter(new OutputStreamWriter(
+                    new FileOutputStream(to), "UTF-8"))) {
+
+                // Output header:
+                out.print("<!DOCTYPE html><html><head><meta charset=\"UTF-8\" 
/>"
+                        + "<title>Source Code</title></head><body><pre>" );
+
+                // Convert, line-by-line:
+                String line;
+                while( (line = in.readLine()) != null ) {
+                    StringBuilder result = new StringBuilder();
+                    int len = line.length();
+                    for( int i = 0; i < len; i++ ) {
+                        char c = line.charAt( i );
+                        switch( c ) {
+                            case '&':
+                                result.append( "&amp;" );
+                                break;
+                            case '<':
+                                result.append( "&lt;" );
+                                break;
+                            default:
+                                result.append( c );
+                        }
+                    }
+                    out.print( result.toString() + LINE_SEPARATOR );
+                }
 
-        // Output header:
-        out.println( "<html><body><pre>" );
+                // Output footer:
+                out.print( "</pre></body></html>" );
 
-        // Convert, line-by-line:
-        String line;
-        while( (line = in.readLine()) != null ) {
-            StringBuilder result = new StringBuilder();
-            int len = line.length();
-            for( int i = 0; i < len; i++ ) {
-                char c = line.charAt( i );
-                switch( c ) {
-                    case '&':
-                        result.append( "&amp;" );
-                        break;
-                    case '<':
-                        result.append( "&lt;" );
-                        break;
-                    default:
-                        result.append( c );
-                }
             }
-            out.println( result.toString() );
         }
-
-        // Output footer:
-        out.println( "</pre></body></html>" );
-
-        // Close streams:
-        out.close();
-        in.close();
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to