You are viewing a plain text version of this content. The canonical link for it is here.
Posted to doxia-commits@maven.apache.org by lt...@apache.org on 2007/11/14 14:45:32 UTC

svn commit: r594878 - /maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/HtmlTools.java

Author: ltheussl
Date: Wed Nov 14 05:45:31 2007
New Revision: 594878

URL: http://svn.apache.org/viewvc?rev=594878&view=rev
Log:
Generalize escapeHTML

Modified:
    maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/HtmlTools.java

Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/HtmlTools.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/HtmlTools.java?rev=594878&r1=594877&r2=594878&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/HtmlTools.java (original)
+++ maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/util/HtmlTools.java Wed Nov 14 05:45:31 2007
@@ -33,19 +33,35 @@
 public class HtmlTools
 {
     /**
-     * Escape special characters in a text in HTML.
+     * Escape special HTML characters in a String in <code>xml</code> mode.
+     *
+     * @param text the String to escape, may be null.
+     * @return The escaped text or the empty string if text == null.
+     * @see #escapeHTML(String,boolean).
+     */
+    public static String escapeHTML( String text )
+    {
+        return escapeHTML( text, true );
+    }
+
+    /**
+     * Escape special HTML characters in a String.
      *
      * <pre>
-     * < becomes <code>&</code>lt;
-     * > becomes <code>&</code>gt;
-     * & becomes <code>&</code>amp;
-     * " becomes <code>&</code>quot;
+     * < becomes <code>&lt;</code>
+     * > becomes <code>&gt;</code>
+     * & becomes <code>&amp;</code>
+     * " becomes <code>&quot;</code>
      * </pre>
      *
-     * @param text the String to escape, may be null
-     * @return the text escaped, "" if null String input
+     * If <code>xmlMode</code> is true, every other character than the above remains unchanged,
+     * if <code>xmlMode</code> is false, non-ascii characters get replaced by their hex code.
+     *
+     * @param text The String to escape, may be null.
+     * @param xmlMode set to <code>false</code> to replace non-ascii characters.
+     * @return The escaped text or the empty string if text == null.
      */
-    public static String escapeHTML( String text )
+    public static final String escapeHTML( String text, boolean xmlMode )
     {
         if ( text == null )
         {
@@ -73,7 +89,24 @@
                     buffer.append( "&quot;" );
                     break;
                 default:
-                    buffer.append( c );
+                    if ( xmlMode )
+                    {
+                        buffer.append( c );
+                    }
+                    else
+                    {
+                        if ( c <= 0x7E )
+                        {
+                            // ASCII.
+                            buffer.append( c );
+                        }
+                        else
+                        {
+                            buffer.append( "&#" );
+                            buffer.append( (int) c );
+                            buffer.append( ';' );
+                        }
+                    }
             }
         }