jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From resc...@apache.org
Subject svn commit: r533070 - in /jackrabbit/trunk: contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/
Date Fri, 27 Apr 2007 12:00:26 GMT
Author: reschke
Date: Fri Apr 27 05:00:24 2007
New Revision: 533070

URL: http://svn.apache.org/viewvc?view=rev&rev=533070
Log:
JCR-674: add base64 encoding for non-binary properties that do non-XML characters (checking
for now: control characters other than TAB, NL, CR); note this only affects export, import
still needs to be done.

Modified:
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java?view=diff&rev=533070&r1=533069&r2=533070
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java
Fri Apr 27 05:00:24 2007
@@ -18,6 +18,7 @@
 
 import org.apache.jackrabbit.name.QName;
 import org.apache.jackrabbit.value.ValueHelper;
+import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
@@ -62,6 +63,21 @@
     public static final String CDATA_TYPE = "CDATA";
     public static final String ENUMERATION_TYPE = "ENUMERATION";
 
+    private static final String NS_XMLSCHEMA_INSTANCE_URI = "http://www.w3.org/2001/XMLSchema-instance";
+    private static final String NS_XMLSCHEMA_INSTANCE_PREFIX = "xsi";
+    private static final String NS_XMLSCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
+    private static final String NS_XMLSCHEMA_PREFIX = "xs";
+
+    private static final Attributes ATTRS_EMPTY = new AttributesImpl();
+    private static final Attributes ATTRS_BINARY_ENCODED_VALUE;
+    static {
+        AttributesImpl attrs = new AttributesImpl();
+        attrs.addAttribute(QName.NS_XMLNS_URI, NS_XMLSCHEMA_INSTANCE_PREFIX, "xmlns:" + NS_XMLSCHEMA_INSTANCE_PREFIX,
CDATA_TYPE, NS_XMLSCHEMA_INSTANCE_URI);
+        attrs.addAttribute(QName.NS_XMLNS_URI, NS_XMLSCHEMA_PREFIX, "xmlns:" + NS_XMLSCHEMA_PREFIX,
CDATA_TYPE, NS_XMLSCHEMA_URI);
+        attrs.addAttribute(NS_XMLSCHEMA_INSTANCE_URI, "type", NS_XMLSCHEMA_INSTANCE_PREFIX
+ ":type", "CDATA", NS_XMLSCHEMA_PREFIX + ":base64Binary");
+        ATTRS_BINARY_ENCODED_VALUE = attrs;
+    }
+
     /**
      * Constructor
      *
@@ -174,9 +190,28 @@
             for (int i = 0; i < vals.length; i++) {
                 Value val = vals[i];
 
+                Attributes attributes = ATTRS_EMPTY;
+                boolean mustSendBinary = false;
+
+                if (val.getType() != PropertyType.BINARY) {
+                    String ser = val.getString();
+                    for (int ci = 0; ci < ser.length() && mustSendBinary == false;
ci++) {
+                        char c = ser.charAt(ci);
+                        if (c >= 0 && c < 32 && c != '\r' &&
c != '\n' && c != '\t') {
+                            mustSendBinary = true;
+                        }
+                    }
+
+                    if (mustSendBinary) {
+                        contentHandler.startPrefixMapping(NS_XMLSCHEMA_INSTANCE_PREFIX, NS_XMLSCHEMA_INSTANCE_URI);
+                        contentHandler.startPrefixMapping(NS_XMLSCHEMA_PREFIX, NS_XMLSCHEMA_URI);
+                        attributes = ATTRS_BINARY_ENCODED_VALUE;
+                    }
+                }
+
                 // start value element
                 contentHandler.startElement(QName.NS_SV_URI, VALUE_ELEMENT,
-                        PREFIXED_VALUE_ELEMENT, new AttributesImpl());
+                        PREFIXED_VALUE_ELEMENT, attributes);
 
                 // characters
                 Writer writer = new Writer() {
@@ -195,7 +230,7 @@
                     }
                 };
                 try {
-                    ValueHelper.serialize(val, false, writer);
+                    ValueHelper.serialize(val, false, mustSendBinary, writer);
                     // no need to close our Writer implementation
                     //writer.close();
                 } catch (IOException ioe) {
@@ -212,6 +247,11 @@
                 // end value element
                 contentHandler.endElement(QName.NS_SV_URI, VALUE_ELEMENT,
                         PREFIXED_VALUE_ELEMENT);
+
+                if (mustSendBinary) {
+                    contentHandler.endPrefixMapping(NS_XMLSCHEMA_INSTANCE_PREFIX);
+                    contentHandler.endPrefixMapping(NS_XMLSCHEMA_PREFIX);
+                }
             }
         }
     }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java?view=diff&rev=533070&r1=533069&r2=533070
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java
Fri Apr 27 05:00:24 2007
@@ -43,6 +43,21 @@
     public static final String CDATA_TYPE = "CDATA";
     public static final String ENUMERATION_TYPE = "ENUMERATION";
 
+    private static final String NS_XMLSCHEMA_INSTANCE_URI = "http://www.w3.org/2001/XMLSchema-instance";
+    private static final String NS_XMLSCHEMA_INSTANCE_PREFIX = "xsi";
+    private static final String NS_XMLSCHEMA_URI = "http://www.w3.org/2001/XMLSchema";
+    private static final String NS_XMLSCHEMA_PREFIX = "xs";
+
+    private static final Attributes ATTRS_EMPTY = new AttributesImpl();
+    private static final Attributes ATTRS_BINARY_ENCODED_VALUE;
+    static {
+        AttributesImpl attrs = new AttributesImpl();
+        attrs.addAttribute(QName.NS_XMLNS_URI, NS_XMLSCHEMA_INSTANCE_PREFIX, "xmlns:" + NS_XMLSCHEMA_INSTANCE_PREFIX,
CDATA_TYPE, NS_XMLSCHEMA_INSTANCE_URI);
+        attrs.addAttribute(QName.NS_XMLNS_URI, NS_XMLSCHEMA_PREFIX, "xmlns:" + NS_XMLSCHEMA_PREFIX,
CDATA_TYPE, NS_XMLSCHEMA_URI);
+        attrs.addAttribute(NS_XMLSCHEMA_INSTANCE_URI, "type", NS_XMLSCHEMA_INSTANCE_PREFIX
+ ":type", "CDATA", NS_XMLSCHEMA_PREFIX + ":base64Binary");
+        ATTRS_BINARY_ENCODED_VALUE = attrs;
+    }
+
     /**
      * Name resolver for producing qualified XML names.
      */
@@ -151,8 +166,27 @@
             for (int i = 0; i < vals.length; i++) {
                 Value val = vals[i];
 
+                Attributes attributes = ATTRS_EMPTY;
+                boolean mustSendBinary = false;
+
+                if (val.getType() != PropertyType.BINARY) {
+                    String ser = val.getString();
+                    for (int ci = 0; ci < ser.length() && mustSendBinary == false;
ci++) {
+                        char c = ser.charAt(ci);
+                        if (c >= 0 && c < 32 && c != '\r' &&
c != '\n' && c != '\t') {
+                            mustSendBinary = true;
+                        }
+                    }
+
+                    if (mustSendBinary) {
+                        contentHandler.startPrefixMapping(NS_XMLSCHEMA_INSTANCE_PREFIX, NS_XMLSCHEMA_INSTANCE_URI);
+                        contentHandler.startPrefixMapping(NS_XMLSCHEMA_PREFIX, NS_XMLSCHEMA_URI);
+                        attributes = ATTRS_BINARY_ENCODED_VALUE;
+                    }
+                }
+
                 // start value element
-                startElement(QName.SV_VALUE, new AttributesImpl());
+                startElement(QName.SV_VALUE, attributes);
 
                 // characters
                 Writer writer = new Writer() {
@@ -171,7 +205,7 @@
                     }
                 };
                 try {
-                    ValueHelper.serialize(val, false, writer);
+                    ValueHelper.serialize(val, false, mustSendBinary, writer);
                     // no need to close our Writer implementation
                     //writer.close();
                 } catch (IOException ioe) {
@@ -187,6 +221,11 @@
 
                 // end value element
                 endElement(QName.SV_VALUE);
+
+                if (mustSendBinary) {
+                    contentHandler.endPrefixMapping(NS_XMLSCHEMA_INSTANCE_PREFIX);
+                    contentHandler.endPrefixMapping(NS_XMLSCHEMA_PREFIX);
+                }
             }
         }
     }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java?view=diff&rev=533070&r1=533069&r2=533070
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java
Fri Apr 27 05:00:24 2007
@@ -562,7 +562,7 @@
             throws IllegalStateException, RepositoryException {
         StringWriter writer = new StringWriter();
         try {
-            serialize(value, encodeBlanks, writer);
+            serialize(value, encodeBlanks, false, writer);
         } catch (IOException ioe) {
             throw new RepositoryException("failed to serialize value",
                     ioe);
@@ -571,6 +571,15 @@
     }
 
     /**
+     * @deprecated use {@link #serialize(Value, boolean, boolean, Writer)} instead
+     */
+    public static void serialize(Value value, boolean encodeBlanks,
+        Writer writer)
+        throws IllegalStateException, IOException, RepositoryException {
+        serialize(value, encodeBlanks, false, writer);
+    }
+    
+      /**
      * Outputs the serialized value to a <code>Writer</code>. The serialization
      * format is the same as used by Document & System View XML, i.e.
      * binary values will be Base64-encoded whereas for all others
@@ -579,13 +588,14 @@
      * @param value        the value to be serialized
      * @param encodeBlanks if <code>true</code> space characters will be encoded
      *                     as <code>"_x0020_"</code> within he output string.
+     * @param enforceBase64 if <code>true</code>, base64 encoding will always
be used
      * @param writer       writer to output the encoded data
      * @throws IllegalStateException if the given value is in an illegal state
      * @throws IOException           if an i/o error occured during the
      *                               serialization
      * @throws RepositoryException   if an error occured during the serialization.
      */
-    public static void serialize(Value value, boolean encodeBlanks,
+    public static void serialize(Value value, boolean encodeBlanks, boolean enforceBase64,
                                  Writer writer)
             throws IllegalStateException, IOException, RepositoryException {
         if (value.getType() == PropertyType.BINARY) {
@@ -606,11 +616,17 @@
             }
         } else {
             String textVal = value.getString();
-            if (encodeBlanks) {
-                // enocde blanks in string
-                textVal = Text.replace(textVal, " ", "_x0020_");
+            if (enforceBase64) {
+                byte bytes[] = textVal.getBytes("UTF-8");
+                Base64.encode(bytes, 0, bytes.length, writer);
+            }
+            else {
+                if (encodeBlanks) {
+                    // enocde blanks in string
+                    textVal = Text.replace(textVal, " ", "_x0020_");
+                }
+                writer.write(textVal);
             }
-            writer.write(textVal);
         }
     }
 



Mime
View raw message