jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ste...@apache.org
Subject svn commit: r170721 - in /incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core: state/xml/XMLPersistenceManager.java util/Text.java
Date Wed, 18 May 2005 08:10:24 GMT
Author: stefan
Date: Wed May 18 01:10:24 2005
New Revision: 170721

URL: http://svn.apache.org/viewcvs?rev=170721&view=rev
Log:
fixed bug in XML PM: '&' etc. in attribute values were not replaced by entity references

Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/state/xml/XMLPersistenceManager.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/Text.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/state/xml/XMLPersistenceManager.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/state/xml/XMLPersistenceManager.java?rev=170721&r1=170720&r2=170721&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/state/xml/XMLPersistenceManager.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/state/xml/XMLPersistenceManager.java
Wed May 18 01:10:24 2005
@@ -16,8 +16,6 @@
  */
 package org.apache.jackrabbit.core.state.xml;
 
-import org.apache.jackrabbit.core.value.BLOBFileValue;
-import org.apache.jackrabbit.core.value.InternalValue;
 import org.apache.jackrabbit.core.NodeId;
 import org.apache.jackrabbit.core.PropertyId;
 import org.apache.jackrabbit.core.QName;
@@ -38,6 +36,9 @@
 import org.apache.jackrabbit.core.state.PMContext;
 import org.apache.jackrabbit.core.state.PropertyState;
 import org.apache.jackrabbit.core.util.DOMWalker;
+import org.apache.jackrabbit.core.util.Text;
+import org.apache.jackrabbit.core.value.BLOBFileValue;
+import org.apache.jackrabbit.core.value.InternalValue;
 import org.apache.log4j.Logger;
 
 import javax.jcr.PropertyType;
@@ -510,7 +511,7 @@
                         + UUID_ATTRIBUTE + "=\"" + state.getUUID() + "\" "
                         + PARENTUUID_ATTRIBUTE + "=\"" + (state.getParentUUID() == null ?
"" : state.getParentUUID()) + "\" "
                         + DEFINITIONID_ATTRIBUTE + "=\"" + state.getDefinitionId().toString()
+ "\" "
-                        + NODETYPE_ATTRIBUTE + "=\"" + state.getNodeTypeName() + "\">\n");
+                        + NODETYPE_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(state.getNodeTypeName().toString())
+ "\">\n");
                 // parents
                 writer.write("\t<" + PARENTS_ELEMENT + ">\n");
                 Iterator iter = state.getParentUUIDs().iterator();
@@ -525,7 +526,7 @@
                 iter = state.getMixinTypeNames().iterator();
                 while (iter.hasNext()) {
                     writer.write("\t\t<" + MIXINTYPE_ELEMENT + " "
-                            + NAME_ATTRIBUTE + "=\"" + iter.next() + "\"/>\n");
+                            + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(iter.next().toString())
+ "\"/>\n");
                 }
                 writer.write("\t</" + MIXINTYPES_ELEMENT + ">\n");
 
@@ -535,7 +536,7 @@
                 while (iter.hasNext()) {
                     NodeState.PropertyEntry entry = (NodeState.PropertyEntry) iter.next();
                     writer.write("\t\t<" + PROPERTY_ELEMENT + " "
-                            + NAME_ATTRIBUTE + "=\"" + entry.getName() + "\">\n");
+                            + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(entry.getName().toString())
+ "\">\n");
                     // @todo serialize type, definition id and values
                     writer.write("\t\t</" + PROPERTY_ELEMENT + ">\n");
                 }
@@ -547,7 +548,7 @@
                 while (iter.hasNext()) {
                     NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) iter.next();
                     writer.write("\t\t<" + NODE_ELEMENT + " "
-                            + NAME_ATTRIBUTE + "=\"" + entry.getName() + "\" "
+                            + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(entry.getName().toString())
+ "\" "
                             + UUID_ATTRIBUTE + "=\"" + entry.getUUID() + "\">\n");
                     writer.write("\t\t</" + NODE_ELEMENT + ">\n");
                 }
@@ -601,7 +602,7 @@
 
                 writer.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
                 writer.write("<" + PROPERTY_ELEMENT + " "
-                        + NAME_ATTRIBUTE + "=\"" + state.getName() + "\" "
+                        + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(state.getName().toString())
+ "\" "
                         + PARENTUUID_ATTRIBUTE + "=\"" + state.getParentUUID() + "\" "
                         + MULTIVALUED_ATTRIBUTE + "=\"" + Boolean.toString(state.isMultiValued())
+ "\" "
                         + DEFINITIONID_ATTRIBUTE + "=\"" + state.getDefinitionId().toString()
+ "\" "
@@ -644,23 +645,7 @@
                                     blobVal = null; // gc hint
                                 }
                             } else {
-                                // escape '<' and '&'
-                                char[] chars = val.toString().toCharArray();
-                                int j = 0, last = 0;
-                                while (j < chars.length) {
-                                    char c = chars[j];
-                                    if (c == '<') {
-                                        writer.write(chars, last, j - last);
-                                        writer.write("&lt;");
-                                        last = j + 1;
-                                    } else if (c == '&') {
-                                        writer.write(chars, last, j - last);
-                                        writer.write("&amp;");
-                                        last = j + 1;
-                                    }
-                                    j++;
-                                }
-                                writer.write(chars, last, j - last);
+                                writer.write(Text.encodeIllegalXMLCharacters(val.toString()));
                             }
                         }
                         writer.write("</" + VALUE_ELEMENT + ">\n");

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/Text.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/Text.java?rev=170721&r1=170720&r2=170721&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/Text.java (original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/Text.java Wed May
18 01:10:24 2005
@@ -26,8 +26,11 @@
  */
 public class Text {
 
-    /** Hidden constructor. */
-    private Text() { }
+    /**
+     * Hidden constructor.
+     */
+    private Text() {
+    }
 
     /**
      * used for the md5
@@ -134,7 +137,7 @@
         }
 
         ArrayList strings = new ArrayList();
-        int pos = 0;
+        int pos;
         int lastpos = 0;
 
         // add snipples
@@ -186,4 +189,58 @@
         return sb.toString();
     }
 
+    /**
+     * Replaces illegal XML characters in the given string by their corresponding
+     * predefined entity references.
+     *
+     * @param text text to be escaped
+     * @return a string
+     */
+    public static String encodeIllegalXMLCharacters(String text) {
+        if (text == null) {
+            throw new IllegalArgumentException("null argument");
+        }
+        StringBuffer buf = null;
+        int length = text.length();
+        int pos = 0;
+        for (int i = 0; i < length; i++) {
+            int ch = text.charAt(i);
+            switch (ch) {
+                case '<':
+                case '>':
+                case '&':
+                case '"':
+                case '\'':
+                    if (buf == null) {
+                        buf = new StringBuffer();
+                    }
+                    if (i > 0) {
+                        buf.append(text.substring(pos, i));
+                    }
+                    pos = i + 1;
+                    break;
+                default:
+                    continue;
+            }
+            if (ch == '<') {
+                buf.append("&lt;");
+            } else if (ch == '>') {
+                buf.append("&gt;");
+            } else if (ch == '&') {
+                buf.append("&amp;");
+            } else if (ch == '"') {
+                buf.append("&quot;");
+            } else if (ch == '\'') {
+                buf.append("&apos;");
+            }
+        }
+        if (buf == null) {
+            return text;
+        } else {
+            if (pos < length) {
+                buf.append(text.substring(pos));
+            }
+            return buf.toString();
+        }
+    }
 }



Mime
View raw message