myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mmarinsc...@apache.org
Subject svn commit: r294889 - in /myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html: HtmlResponseWriterImpl.java util/HTMLEncoder.java util/UnicodeEncoder.java
Date Tue, 04 Oct 2005 18:07:56 GMT
Author: mmarinschek
Date: Tue Oct  4 11:07:51 2005
New Revision: 294889

URL: http://svn.apache.org/viewcvs?rev=294889&view=rev
Log:
Thanks to Kalle Korhonen for this patch

Modified:
    myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
    myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/HTMLEncoder.java
    myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/UnicodeEncoder.java

Modified: myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
URL: http://svn.apache.org/viewcvs/myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java?rev=294889&r1=294888&r2=294889&view=diff
==============================================================================
--- myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
(original)
+++ myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
Tue Oct  4 11:07:51 2005
@@ -48,6 +48,7 @@
 
     private static final String DEFAULT_CONTENT_TYPE = "text/html";
     private static final String DEFAULT_CHARACTER_ENCODING = "ISO-8859-1";
+    private static final String UTF8 = "UTF-8";
 
     private boolean _writeDummyForm = false;
     private Set _dummyFormParams = null;
@@ -88,15 +89,14 @@
         _contentType = contentType;
         if (_contentType == null)
         {
-            if (log.isInfoEnabled()) log.debug("No content type given, using default content
type " + DEFAULT_CONTENT_TYPE);
+            if (log.isDebugEnabled()) log.debug("No content type given, using default content
type " + DEFAULT_CONTENT_TYPE);
             _contentType = DEFAULT_CONTENT_TYPE;
         }
-        _characterEncoding = characterEncoding;
-        if (_characterEncoding == null)
+        if (characterEncoding == null)
         {
-            if (log.isInfoEnabled()) log.debug("No character encoding given, using default
character encoding " + DEFAULT_CHARACTER_ENCODING);
+            if (log.isDebugEnabled()) log.debug("No character encoding given, using default
character encoding " + DEFAULT_CHARACTER_ENCODING);
             _characterEncoding = DEFAULT_CHARACTER_ENCODING;
-        } 
+        }
         else
         {
             // validates the encoding, it will throw an UnsupportedEncodingException if the
encoding is invalid
@@ -108,6 +108,9 @@
             {
                 throw new IllegalArgumentException("Unsupported encoding: "+characterEncoding);
             }
+            
+            // canonize to uppercase, that's the standard format
+            _characterEncoding = characterEncoding.toUpperCase();
         }
     }
 
@@ -343,7 +346,7 @@
             _writer.write(' ');
             _writer.write(name);
             _writer.write("=\"");
-            _writer.write(HTMLEncoder.encode(strValue, false, false));
+            _writer.write(HTMLEncoder.encode(strValue, false, false, !UTF8.equals(_characterEncoding)));
             _writer.write('"');
         }
     }
@@ -365,7 +368,7 @@
         _writer.write("=\"");
         if (strValue.toLowerCase().startsWith("javascript:"))
         {
-            _writer.write(HTMLEncoder.encode(strValue, false, false));
+            _writer.write(HTMLEncoder.encode(strValue, false, false, !UTF8.equals(_characterEncoding)));
         }
         else
         {
@@ -429,11 +432,13 @@
 
         if (isScriptOrStyle())
         {
-            _writer.write(UnicodeEncoder.encode(strValue, false, false));
+            // Don't bother encoding anything if chosen character encoding is UTF-8
+            if (UTF8.equals(_characterEncoding)) _writer.write(strValue);
+            else _writer.write(UnicodeEncoder.encode(strValue) );
         }
         else
         {
-            _writer.write(HTMLEncoder.encode(strValue, false, false));
+            _writer.write(HTMLEncoder.encode(strValue, false, false, !UTF8.equals(_characterEncoding)));
         }
     }
 
@@ -453,21 +458,23 @@
         if (isScriptOrStyle())
         {
             String strValue = new String(cbuf, off, len);
-            _writer.write(UnicodeEncoder.encode(strValue, false, false));
+            // Don't bother encoding anything if chosen character encoding is UTF-8
+            if (UTF8.equals(_characterEncoding)) _writer.write(strValue);
+            else _writer.write(UnicodeEncoder.encode(strValue) );
         }
         else if (isTextarea())
         {
             // For textareas we must *not* map successive spaces to &nbsp or Newlines
to <br/>
             // TODO: Make HTMLEncoder support char arrays directly
             String strValue = new String(cbuf, off, len);
-            _writer.write(HTMLEncoder.encode(strValue, false, false));
+            _writer.write(HTMLEncoder.encode(strValue, false, false, !UTF8.equals(_characterEncoding)));
         }
         else
         {
             // We map successive spaces to &nbsp; and Newlines to <br/>
             // TODO: Make HTMLEncoder support char arrays directly
             String strValue = new String(cbuf, off, len);
-            _writer.write(HTMLEncoder.encode(strValue, true, true));
+            _writer.write(HTMLEncoder.encode(strValue, true, true, !UTF8.equals(_characterEncoding)));
         }
     }
 
@@ -559,7 +566,9 @@
     {
         closeStartTagIfNecessary();
         String strValue = new String(cbuf, off, len);
-        _writer.write(UnicodeEncoder.encode(strValue, false, false));
+        // Don't bother encoding anything if chosen character encoding is UTF-8
+        if (UTF8.equals(_characterEncoding)) _writer.write(strValue);
+        else _writer.write(UnicodeEncoder.encode(strValue) );
     }
 
     public void write(int c) throws IOException
@@ -572,7 +581,9 @@
     {
         closeStartTagIfNecessary();
         String strValue = new String(cbuf);
-        _writer.write(UnicodeEncoder.encode(strValue, false, false));
+        // Don't bother encoding anything if chosen character encoding is UTF-8
+        if (UTF8.equals(_characterEncoding)) _writer.write(strValue);
+        else _writer.write(UnicodeEncoder.encode(strValue) );
     }
 
     public void write(String str) throws IOException
@@ -582,7 +593,9 @@
         // in such case, do not call down the writer chain
         if (str.length() > 0)
         {
-            _writer.write(UnicodeEncoder.encode(str, false, false));
+            // Don't bother encoding anything if chosen character encoding is UTF-8
+            if (UTF8.equals(_characterEncoding)) _writer.write(str);
+            else _writer.write(UnicodeEncoder.encode(str) );
         }
     }
 
@@ -590,7 +603,9 @@
     {
         closeStartTagIfNecessary();
         String strValue = str.substring(off, off+len);
-        _writer.write(UnicodeEncoder.encode(strValue, false, false));
+        // Don't bother encoding anything if chosen character encoding is UTF-8
+        if (UTF8.equals(_characterEncoding)) _writer.write(strValue);
+        else _writer.write(UnicodeEncoder.encode(strValue) );
     }
 
     // DummyFormResponseWriter support

Modified: myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/HTMLEncoder.java
URL: http://svn.apache.org/viewcvs/myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/HTMLEncoder.java?rev=294889&r1=294888&r2=294889&view=diff
==============================================================================
--- myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/HTMLEncoder.java (original)
+++ myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/HTMLEncoder.java Tue
Oct  4 11:07:51 2005
@@ -40,14 +40,24 @@
 	}
 
 	/**
+	 * Variant of {@link #encode} where encodeNbsp and encodeNonLatin are true 
+	 */
+	public static String encode (String string, boolean encodeNewline, boolean encodeSubsequentBlanksToNbsp)
+	{
+		return encode(string, encodeNewline, true, true);
+	}
+
+	/**
 	 * Encodes the given string, so that it can be used within a html page.
 	 * @param string the string to convert
 	 * @param encodeNewline if true newline characters are converted to &lt;br&gt;'s
 	 * @param encodeSubsequentBlanksToNbsp if true subsequent blanks are converted to &amp;nbsp;'s
+	 * @param encodeNonLatin if true encode non-latin characters as numeric character references
 	 */
 	public static String encode (String string,
 								 boolean encodeNewline,
-								 boolean encodeSubsequentBlanksToNbsp)
+								 boolean encodeSubsequentBlanksToNbsp,
+								 boolean encodeNonLatin)
 	{
 		if (string == null)
 		{
@@ -82,29 +92,34 @@
                     }
                     break;
 
-                //german umlauts
-			    case '\u00E4' : app = "&auml;";  break;
-			    case '\u00C4' : app = "&Auml;";  break;
-			    case '\u00F6' : app = "&ouml;";  break;
-			    case '\u00D6' : app = "&Ouml;";  break;
-			    case '\u00FC' : app = "&uuml;";  break;
-			    case '\u00DC' : app = "&Uuml;";  break;
-			    case '\u00DF' : app = "&szlig;"; break;
-
-                //misc
-                //case 0x80: app = "&euro;"; break;  sometimes euro symbol is ascii 128,
should we suport it?
-                case '\u20AC': app = "&euro;";  break;
-                case '\u00AB': app = "&laquo;"; break;
-                case '\u00BB': app = "&raquo;"; break;
-                case '\u00A0': app = "&#160;"; break;
 
                 default:
-                    if (((int)c) >= 0x80)
-                    {
-                        //encode all non basic latin characters
-                        app = "&#" + ((int)c) + ";";
-                    }
-                    break;
+                	if (encodeNonLatin) switch(c) {
+	                	//german umlauts
+					    case '\u00E4' : app = "&auml;";  break;
+					    case '\u00C4' : app = "&Auml;";  break;
+					    case '\u00F6' : app = "&ouml;";  break;
+					    case '\u00D6' : app = "&Ouml;";  break;
+					    case '\u00FC' : app = "&uuml;";  break;
+					    case '\u00DC' : app = "&Uuml;";  break;
+					    case '\u00DF' : app = "&szlig;"; break;
+		
+		                //misc
+		                //case 0x80: app = "&euro;"; break;  sometimes euro symbol is ascii
128, should we suport it?
+		                case '\u20AC': app = "&euro;";  break;
+		                case '\u00AB': app = "&laquo;"; break;
+		                case '\u00BB': app = "&raquo;"; break;
+		                case '\u00A0': app = "&#160;"; break;
+                	
+		                default :
+	                    if (((int)c) >= 0x80)
+	                    {
+	                        //encode all non basic latin characters
+	                        app = "&#" + ((int)c) + ";";
+	                    }
+	                    break;
+                	}
+                	break;
 			}
 			if (app != null)
 			{

Modified: myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/UnicodeEncoder.java
URL: http://svn.apache.org/viewcvs/myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/UnicodeEncoder.java?rev=294889&r1=294888&r2=294889&view=diff
==============================================================================
--- myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/UnicodeEncoder.java
(original)
+++ myfaces/share/trunk/src/java/org/apache/myfaces/renderkit/html/util/UnicodeEncoder.java
Tue Oct  4 11:07:51 2005
@@ -19,35 +19,16 @@
 
 
 /**
- * Converts Strings so that they can be used within HTML-Code.
+ * Converts characters outside of latin-1 set in a string to numeric character references.
+ * 
  */
 public abstract class UnicodeEncoder
 {
 	/**
-	 * Variant of {@link #encode} where encodeNewline is false and encodeNbsp is true.
-	 */
-	public static String encode (String string)
-	{
-		return encode(string, false, true);
-	}
-
-	/**
-	 * Variant of {@link #encode} where encodeNbsp is true.
-	 */
-	public static String encode (String string, boolean encodeNewline)
-	{
-		return encode(string, encodeNewline, true);
-	}
-
-	/**
 	 * Encodes the given string, so that it can be used within a html page.
 	 * @param string the string to convert
-	 * @param encodeNewline if true newline characters are converted to &lt;br&gt;'s
-	 * @param encodeSubsequentBlanksToNbsp if true subsequent blanks are converted to &amp;nbsp;'s
 	 */
-	public static String encode (String string,
-								 boolean encodeNewline,
-								 boolean encodeSubsequentBlanksToNbsp)
+	public static String encode (String string)
 	{
 		if (string == null)
 		{



Mime
View raw message