jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r510908 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml: SysViewImportHandler.java SysViewSAXEventGenerator.java
Date Fri, 23 Feb 2007 11:27:58 GMT
Author: jukka
Date: Fri Feb 23 03:27:57 2007
New Revision: 510908

URL: http://svn.apache.org/viewvc?view=rev&rev=510908
Log:
JCR-762: Removed the hardcoded sv: prefix in favor of properly namespaced system view name
constants.

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewImportHandler.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewSAXEventGenerator.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewImportHandler.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewImportHandler.java?view=diff&rev=510908&r1=510907&r2=510908
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewImportHandler.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/SysViewImportHandler.java
Fri Feb 23 03:27:57 2007
@@ -106,17 +106,13 @@
     public void startElement(String namespaceURI, String localName,
                              String qName, Attributes atts)
             throws SAXException {
-        // check namespace
-        if (!QName.NS_SV_URI.equals(namespaceURI)) {
-            throw new SAXException(new InvalidSerializedDataException("invalid namespace
for element in system view xml document: "
-                    + namespaceURI));
-        }
+        QName name = new QName(namespaceURI, localName);
         // check element name
-        if (SysViewSAXEventGenerator.NODE_ELEMENT.equals(localName)) {
+        if (name.equals(QName.SV_NODE)) {
             // sv:node element
 
             // node name (value of sv:name attribute)
-            String name = atts.getValue(SysViewSAXEventGenerator.PREFIXED_NAME_ATTRIBUTE);
+            String svName = getAttribute(atts, QName.SV_NAME);
             if (name == null) {
                 throw new SAXException(new InvalidSerializedDataException(
                         "missing mandatory sv:name attribute of element sv:node"));
@@ -135,43 +131,48 @@
             // push new ImportState instance onto the stack
             ImportState state = new ImportState();
             try {
-                state.nodeName = NameFormat.parse(name, nsContext);
+                state.nodeName = NameFormat.parse(svName, nsContext);
             } catch (NameException e) {
                 throw new SAXException(new InvalidSerializedDataException("illegal node name:
" + name, e));
             }
             stack.push(state);
-        } else if (SysViewSAXEventGenerator.PROPERTY_ELEMENT.equals(localName)) {
+        } else if (name.equals(QName.SV_PROPERTY)) {
             // sv:property element
 
             // reset temp fields
             currentPropValues.clear();
 
             // property name (value of sv:name attribute)
-            String name = atts.getValue(SysViewSAXEventGenerator.PREFIXED_NAME_ATTRIBUTE);
+            String svName = getAttribute(atts, QName.SV_NAME);
             if (name == null) {
                 throw new SAXException(new InvalidSerializedDataException(
                         "missing mandatory sv:name attribute of element sv:property"));
             }
             try {
-                currentPropName = NameFormat.parse(name, nsContext);
+                currentPropName = NameFormat.parse(svName, nsContext);
             } catch (NameException e) {
                 throw new SAXException(new InvalidSerializedDataException("illegal property
name: " + name, e));
             }
             // property type (sv:type attribute)
-            String type = atts.getValue(SysViewSAXEventGenerator.PREFIXED_TYPE_ATTRIBUTE);
+            String type = getAttribute(atts, QName.SV_TYPE);
             if (type == null) {
                 throw new SAXException(new InvalidSerializedDataException(
                         "missing mandatory sv:type attribute of element sv:property"));
             }
-            currentPropType = PropertyType.valueFromName(type);
-        } else if (SysViewSAXEventGenerator.VALUE_ELEMENT.equals(localName)) {
+            try {
+                currentPropType = PropertyType.valueFromName(type);
+            } catch (IllegalArgumentException e) {
+                throw new SAXException(new InvalidSerializedDataException(
+                        "Unknown property type: " + type, e));
+            }
+        } else if (name.equals(QName.SV_VALUE)) {
             // sv:value element
 
             // reset temp fields
             currentPropValue = new BufferedStringValue(nsContext);
         } else {
-            throw new SAXException(new InvalidSerializedDataException("unexpected element
found in system view xml document: "
-                    + localName));
+            throw new SAXException(new InvalidSerializedDataException(
+                    "Unexpected element in system view xml document: " + name));
         }
     }
 
@@ -215,9 +216,10 @@
      */
     public void endElement(String namespaceURI, String localName, String qName)
             throws SAXException {
+        QName name = new QName(namespaceURI, localName);
         // check element name
         ImportState state = (ImportState) stack.peek();
-        if (SysViewSAXEventGenerator.NODE_ELEMENT.equals(localName)) {
+        if (name.equals(QName.SV_NODE)) {
             // sv:node element
             if (!state.started) {
                 // need to start & end current node
@@ -229,7 +231,7 @@
             }
             // pop current state from stack
             stack.pop();
-        } else if (SysViewSAXEventGenerator.PROPERTY_ELEMENT.equals(localName)) {
+        } else if (name.equals(QName.SV_PROPERTY)) {
             // sv:property element
 
             // check if all system properties (jcr:primaryType, jcr:uuid etc.)
@@ -279,7 +281,7 @@
             }
             // reset temp fields
             currentPropValues.clear();
-        } else if (SysViewSAXEventGenerator.VALUE_ELEMENT.equals(localName)) {
+        } else if (name.equals(QName.SV_VALUE)) {
             // sv:value element
             currentPropValues.add(currentPropValue);
             // reset temp fields
@@ -318,4 +320,19 @@
          */
         boolean started = false;
     }
+
+    //-------------------------------------------------------------< private >
+
+    /**
+     * Returns the value of the named XML attribute.
+     *
+     * @param attributes set of XML attributes
+     * @param name attribute name
+     * @return attribute value,
+     *         or <code>null</code> if the named attribute is not found
+     */
+    private static String getAttribute(Attributes attributes, QName name) {
+        return attributes.getValue(name.getNamespaceURI(), name.getLocalName());
+    }
+
 }

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=510908&r1=510907&r2=510908
==============================================================================
--- 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 Feb 23 03:27:57 2007
@@ -16,12 +16,16 @@
  */
 package org.apache.jackrabbit.core.xml;
 
+import org.apache.jackrabbit.name.NameResolver;
+import org.apache.jackrabbit.name.ParsingNameResolver;
 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;
 
+import javax.jcr.NamespaceException;
 import javax.jcr.Node;
 import javax.jcr.Property;
 import javax.jcr.PropertyType;
@@ -36,33 +40,15 @@
  */
 public class SysViewSAXEventGenerator extends AbstractSAXEventGenerator {
 
-    /**
-     * The XML elements and attributes used in serialization
-     */
-    public static final String NODE_ELEMENT = "node";
-    public static final String PREFIXED_NODE_ELEMENT =
-        QName.NS_SV_PREFIX + ":" + NODE_ELEMENT;
-
-    public static final String PROPERTY_ELEMENT = "property";
-    public static final String PREFIXED_PROPERTY_ELEMENT =
-        QName.NS_SV_PREFIX + ":" + PROPERTY_ELEMENT;;
-
-    public static final String VALUE_ELEMENT = "value";
-    public static final String PREFIXED_VALUE_ELEMENT =
-        QName.NS_SV_PREFIX + ":" + VALUE_ELEMENT;;
-
-    public static final String NAME_ATTRIBUTE = "name";
-    public static final String PREFIXED_NAME_ATTRIBUTE =
-        QName.NS_SV_PREFIX + ":" + NAME_ATTRIBUTE;
-
-    public static final String TYPE_ATTRIBUTE = "type";
-    public static final String PREFIXED_TYPE_ATTRIBUTE =
-        QName.NS_SV_PREFIX + ":" + TYPE_ATTRIBUTE;
-
     public static final String CDATA_TYPE = "CDATA";
     public static final String ENUMERATION_TYPE = "ENUMERATION";
 
     /**
+     * Name resolver for producing qualified XML names.
+     */
+    private final NameResolver resolver;
+
+    /**
      * Constructor
      *
      * @param node           the node state which should be serialized
@@ -78,6 +64,7 @@
                                     ContentHandler contentHandler)
             throws RepositoryException {
         super(node, noRecurse, skipBinary, contentHandler);
+        resolver = new ParsingNameResolver(nsResolver);
     }
 
     /**
@@ -97,11 +84,9 @@
             nodeName = node.getName();
         }
 
-        attrs.addAttribute(QName.NS_SV_URI, NAME_ATTRIBUTE, PREFIXED_NAME_ATTRIBUTE,
-                CDATA_TYPE, nodeName);
+        addAttribute(attrs, QName.SV_NAME, CDATA_TYPE, nodeName);
         // start node element
-        contentHandler.startElement(QName.NS_SV_URI, NODE_ELEMENT,
-                PREFIXED_NODE_ELEMENT, attrs);
+        startElement(QName.SV_NODE, attrs);
     }
 
     /**
@@ -126,7 +111,7 @@
     protected void leaving(Node node, int level)
             throws RepositoryException, SAXException {
         // end node element
-        contentHandler.endElement(QName.NS_SV_URI, NODE_ELEMENT, PREFIXED_NODE_ELEMENT);
+        endElement(QName.SV_NODE);
     }
 
     /**
@@ -134,35 +119,27 @@
      */
     protected void entering(Property prop, int level)
             throws RepositoryException, SAXException {
-        String propName = prop.getName();
         AttributesImpl attrs = new AttributesImpl();
         // name attribute
-        attrs.addAttribute(QName.NS_SV_URI, NAME_ATTRIBUTE, PREFIXED_NAME_ATTRIBUTE,
-                CDATA_TYPE, propName);
+        addAttribute(attrs, QName.SV_NAME, CDATA_TYPE, prop.getName());
         // type attribute
-        int type = prop.getType();
-        String typeName;
         try {
-            typeName = PropertyType.nameFromValue(type);
-        } catch (IllegalArgumentException iae) {
+            String typeName = PropertyType.nameFromValue(prop.getType());
+            addAttribute(attrs, QName.SV_TYPE, ENUMERATION_TYPE, typeName);
+        } catch (IllegalArgumentException e) {
             // should never be getting here
-            throw new RepositoryException("unexpected property-type ordinal: "
-                    + type, iae);
+            throw new RepositoryException(
+                    "unexpected property-type ordinal: " + prop.getType(), e);
         }
-        attrs.addAttribute(QName.NS_SV_URI, TYPE_ATTRIBUTE, PREFIXED_TYPE_ATTRIBUTE,
-                ENUMERATION_TYPE, typeName);
 
         // start property element
-        contentHandler.startElement(QName.NS_SV_URI, PROPERTY_ELEMENT,
-                PREFIXED_PROPERTY_ELEMENT, attrs);
+        startElement(QName.SV_PROPERTY, attrs);
 
         // values
         if (prop.getType() == PropertyType.BINARY && skipBinary) {
             // empty value element
-            contentHandler.startElement(QName.NS_SV_URI, VALUE_ELEMENT,
-                    PREFIXED_VALUE_ELEMENT, new AttributesImpl());
-            contentHandler.endElement(QName.NS_SV_URI, VALUE_ELEMENT,
-                    PREFIXED_VALUE_ELEMENT);
+            startElement(QName.SV_VALUE, new AttributesImpl());
+            endElement(QName.SV_VALUE);
         } else {
             boolean multiValued = prop.getDefinition().isMultiple();
             Value[] vals;
@@ -175,8 +152,7 @@
                 Value val = vals[i];
 
                 // start value element
-                contentHandler.startElement(QName.NS_SV_URI, VALUE_ELEMENT,
-                        PREFIXED_VALUE_ELEMENT, new AttributesImpl());
+                startElement(QName.SV_VALUE, new AttributesImpl());
 
                 // characters
                 Writer writer = new Writer() {
@@ -210,8 +186,7 @@
                 }
 
                 // end value element
-                contentHandler.endElement(QName.NS_SV_URI, VALUE_ELEMENT,
-                        PREFIXED_VALUE_ELEMENT);
+                endElement(QName.SV_VALUE);
             }
         }
     }
@@ -221,7 +196,57 @@
      */
     protected void leaving(Property prop, int level)
             throws RepositoryException, SAXException {
-        contentHandler.endElement(QName.NS_SV_URI, PROPERTY_ELEMENT,
-                PREFIXED_PROPERTY_ELEMENT);
+        endElement(QName.SV_PROPERTY);
+    }
+
+    //-------------------------------------------------------------< private >
+
+    /**
+     * Adds an attribute to the given XML attribute set. The local part of
+     * the given {@link QName} is assumed to be a valid XML NCName, i.e. it
+     * won't be encoded.
+     *
+     * @param attributes the XML attribute set
+     * @param name name of the attribute
+     * @param type XML type of the attribute
+     * @param value value of the attribute
+     * @throws NamespaceException if the namespace of the attribute is not found
+     */
+    private void addAttribute(
+            AttributesImpl attributes, QName name, String type, String value)
+            throws NamespaceException {
+        attributes.addAttribute(
+                name.getNamespaceURI(), name.getLocalName(),
+                resolver.getJCRName(name), type, value);
     }
+
+    /**
+     * Starts an XML element. The local part of the given {@link QName} is
+     * assumed to be a valid XML NCName, i.e. it won't be encoded.
+     *
+     * @param name name of the element
+     * @param attributes XML attributes
+     * @throws NamespaceException if the namespace of the element is not found
+     */
+    private void startElement(QName name, Attributes attributes)
+            throws NamespaceException, SAXException {
+        contentHandler.startElement(
+                name.getNamespaceURI(), name.getLocalName(),
+                resolver.getJCRName(name), attributes);
+    }
+
+    /**
+     * Ends an XML element. The local part of the given {@link QName} is
+     * assumed to be a valid XML NCName, i.e. it won't be encoded.
+     *
+     * @param name name of the element
+     * @throws NamespaceException if the namespace of the element is not found
+     */
+    private void endElement(QName name)
+            throws NamespaceException, SAXException {
+        contentHandler.endElement(
+                name.getNamespaceURI(), name.getLocalName(),
+                resolver.getJCRName(name));
+    }
+
 }



Mime
View raw message