[Yanel-commits] rev 28306 - public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization

josias at wyona.com josias at wyona.com
Thu Nov 1 09:44:23 CET 2007


Author: josias
Date: 2007-11-01 09:44:22 +0100 (Thu, 01 Nov 2007)
New Revision: 28306

Modified:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/HTMLSerializer.java
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/XMLSerializer.java
Log:
distinguish escaping of text nodes from attribute nodes, because a quote may occur unescaped in a text node, but not in an attribute

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/HTMLSerializer.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/HTMLSerializer.java	2007-11-01 08:42:54 UTC (rev 28305)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/HTMLSerializer.java	2007-11-01 08:44:22 UTC (rev 28306)
@@ -37,7 +37,7 @@
             String aName = ("".equals(aLocalName)) ? aQName : aLocalName;
             // don't copy namespace attributes
             if (!(aLocalName.startsWith("xmlns") || aQName.startsWith("xmlns"))) {
-                String aValue = replaceEntities(attrs.getValue(i));
+                String aValue = replaceAttrEntities(attrs.getValue(i));
                 element.append(" " + aName + "=\"" + aValue + "\"");
             }
         }

Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/XMLSerializer.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/XMLSerializer.java	2007-11-01 08:42:54 UTC (rev 28305)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/serialization/XMLSerializer.java	2007-11-01 08:44:22 UTC (rev 28306)
@@ -62,7 +62,7 @@
             String aLocalName = attrs.getLocalName(i);
             String aQName = attrs.getQName(i);
             String aName = ("".equals(aQName)) ? aLocalName : aQName;
-            String aValue = replaceEntities(attrs.getValue(i));
+            String aValue = replaceAttrEntities(attrs.getValue(i));
             element.append(" " + aName + "=\"" + aValue + "\"");
         }
         
@@ -124,6 +124,7 @@
     
     /**
      * Replaces some characters by their corresponding xml entities.
+     * This method escapes those characters which must not occur in an xml text node.
      * @param string
      * @return escaped string
      */
@@ -137,4 +138,21 @@
         return str;
     }
     
+    /**
+     * Replaces some characters by their corresponding xml entities.
+     * This method escapes those characters which must not occur in an xml attribute.
+     * @param string
+     * @return escaped string
+     */
+    public String replaceAttrEntities(String str) {
+        // there may be some & and some & mixed in the input, so first transform all
+        // & to & and then transform all & back to &
+        // this way we don't get double escaped &
+        str = str.replaceAll("&", "&");
+        str = str.replaceAll("&", "&");
+        str = str.replaceAll("<", "&lt;");
+        str = str.replaceAll("\"", "&quot;");
+        return str;
+    }
+    
 }



More information about the Yanel-commits mailing list