You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by ju...@apache.org on 2008/05/16 14:23:33 UTC

svn commit: r657028 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons: AbstractNode.java xml/DocumentViewExporter.java xml/Exporter.java xml/SystemViewExporter.java

Author: jukka
Date: Fri May 16 05:23:32 2008
New Revision: 657028

URL: http://svn.apache.org/viewvc?rev=657028&view=rev
Log:
JCR-1607: Add a NamespaceHelper in jcr-commons
    - Use NamespaceHelper in jcr-commons

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractNode.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DocumentViewExporter.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/Exporter.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SystemViewExporter.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractNode.java?rev=657028&r1=657027&r2=657028&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractNode.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/AbstractNode.java Fri May 16 05:23:32 2008
@@ -89,16 +89,6 @@
  */
 public abstract class AbstractNode extends AbstractItem implements Node {
 
-    /**
-     * The JCR namespace URI.
-     */
-    private static final String JCR = "http://www.jcp.org/jcr/1.0";
-
-    /**
-     * The JCR mix namespace URI.
-     */
-    private static final String MIX = "http://www.jcp.org/jcr/mix/1.0";
-
     //----------------------------------------------------------------< Item >
 
     /**
@@ -169,7 +159,7 @@
         try {
             NodeTypeManager manager =
                 getSession().getWorkspace().getNodeTypeManager();
-            Property property = getProperty(getName(JCR, "mixinTypes"));
+            Property property = getProperty(getName("jcr:mixinTypes"));
             Value[] values = property.getValues();
             NodeType[] types = new NodeType[values.length];
             for (int i = 0; i < values.length; i++) {
@@ -195,7 +185,7 @@
     public NodeType getPrimaryNodeType() throws RepositoryException {
         NodeTypeManager manager =
             getSession().getWorkspace().getNodeTypeManager();
-        Property property = getProperty(getName(JCR, "primaryType"));
+        Property property = getProperty(getName("jcr:primaryType"));
         return manager.getNodeType(property.getString());
     }
 
@@ -256,8 +246,8 @@
      */
     public String getUUID()
             throws UnsupportedRepositoryOperationException, RepositoryException {
-        if (isNodeType(getName(MIX, "referenceable"))) {
-            return getProperty(getName(JCR, "uuid")).getString();
+        if (isNodeType(getName("mix:referenceable"))) {
+            return getProperty(getName("jcr:uuid")).getString();
         } else {
             throw new UnsupportedRepositoryOperationException(
                     "This node is not referenceable: " + getPath());
@@ -383,9 +373,9 @@
      * @throws RepositoryException if an error occurs
      */
     public boolean isCheckedOut() throws RepositoryException {
-        if (isNodeType(getName(MIX, "versionable"))) {
+        if (isNodeType(getName("jcr:versionable"))) {
             // This node is versionable, check the jcr:isCheckedOut property
-            return getProperty(getName(JCR, "isCheckedOut")).getBoolean();
+            return getProperty(getName("jcr:isCheckedOut")).getBoolean();
         } else {
             try {
                 // This node is not versionable, is the parent checked out?
@@ -768,13 +758,8 @@
      * @return prefixed JCR name
      * @throws RepositoryException if an error occurs
      */
-    private String getName(String uri, String name) throws RepositoryException {
-        String prefix = getSession().getNamespacePrefix(uri);
-        if (prefix.length() > 0) {
-            return prefix + ":" + name;
-        } else {
-            return name;
-        }
+    private String getName(String name) throws RepositoryException {
+        return new NamespaceHelper(getSession()).getJcrName(name);
     }
 
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DocumentViewExporter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DocumentViewExporter.java?rev=657028&r1=657027&r2=657028&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DocumentViewExporter.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/DocumentViewExporter.java Fri May 16 05:23:32 2008
@@ -23,6 +23,7 @@
 import javax.jcr.Session;
 import javax.jcr.Value;
 
+import org.apache.jackrabbit.commons.NamespaceHelper;
 import org.apache.jackrabbit.util.ISO9075;
 import org.apache.jackrabbit.value.ValueHelper;
 import org.xml.sax.ContentHandler;
@@ -56,11 +57,11 @@
      */
     protected void exportNode(String uri, String local, Node node)
             throws RepositoryException, SAXException {
-        if (JCR.equals(uri) && "xmltext".equals(local)) {
+        if (NamespaceHelper.JCR.equals(uri) && "xmltext".equals(local)) {
             try {
                 // assume jcr:xmlcharacters is single-valued
                 Property property =
-                    node.getProperty(getJCRName(JCR, "xmlcharacters"));
+                    node.getProperty(helper.getJcrName("jcr:xmlcharacters"));
                 char[] ch = property.getString().toCharArray();
                 characters(ch, 0, ch.length);
             } catch (PathNotFoundException e) {

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/Exporter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/Exporter.java?rev=657028&r1=657027&r2=657028&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/Exporter.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/Exporter.java Fri May 16 05:23:32 2008
@@ -35,6 +35,7 @@
 import javax.jcr.Value;
 import javax.jcr.ValueFactory;
 
+import org.apache.jackrabbit.commons.NamespaceHelper;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
@@ -54,21 +55,6 @@
 public abstract class Exporter {
 
     /**
-     * The <code>jcr</code> namespace URI.
-     */
-    protected static final String JCR = "http://www.jcp.org/jcr/1.0";
-
-    /**
-     * The <code>nt</code> namespace URI.
-     */
-    private static final String NT = "http://www.jcp.org/jcr/nt/1.0";
-
-    /**
-     * The <code>mix</code> namespace URI.
-     */
-    private static final String MIX = "http://www.jcp.org/jcr/mix/1.0";
-
-    /**
      * Attributes of the next element. This single instance is reused for
      * all elements by simply clearing it after each element has been emitted.
      */
@@ -96,6 +82,11 @@
     private final Session session;
 
     /**
+     * Namespace helper.
+     */
+    protected final NamespaceHelper helper;
+
+    /**
      * SAX event handler to which the export events are sent.
      */
     private final ContentHandler handler;
@@ -122,6 +113,7 @@
             Session session, ContentHandler handler,
             boolean recurse, boolean binary) {
         this.session = session;
+        this.helper = new NamespaceHelper(session);
         this.handler = handler;
         this.recurse = recurse;
         this.binary = binary;
@@ -253,31 +245,28 @@
         // and jcr:uuid (mix:shareable is referenceable, so jcr:uuid exists)
         if (share) {
             ValueFactory factory = session.getValueFactory();
-            Value share =
-                factory.createValue(getJCRName(NT, "share"), PropertyType.NAME);
-            exportProperty(JCR, "primaryType", share);
-            exportProperty(JCR, "uuid", factory.createValue(node.getUUID()));
+            exportProperty(
+                    NamespaceHelper.JCR, "primaryType",
+                    factory.createValue(
+                            helper.getJcrName("nt:share"), PropertyType.NAME));
+            exportProperty(
+                    NamespaceHelper.JCR, "uuid",
+                    factory.createValue(node.getUUID()));
         } else {
             // Standard behaviour: return all properties (sorted, see JCR-1084)
             SortedMap properties = getProperties(node);
 
             // serialize jcr:primaryType, jcr:mixinTypes & jcr:uuid first:
-            exportProperty(properties, JCR, "primaryType");
-            exportProperty(properties, JCR, "mixinTypes");
-            exportProperty(properties, JCR, "uuid");
+            exportProperty(properties, helper.getJcrName("jcr:primaryType"));
+            exportProperty(properties, helper.getJcrName("jcr:mixinTypes"));
+            exportProperty(properties, helper.getJcrName("jcr:uuid"));
 
             // serialize remaining properties
             Iterator iterator = properties.entrySet().iterator();
             while (iterator.hasNext()) {
                 Map.Entry entry = (Map.Entry) iterator.next();
-                String uri = null;
                 String name = (String) entry.getKey();
-                int colon = name.indexOf(':');
-                if (colon != -1) {
-                    uri = session.getNamespaceURI(name.substring(0, colon));
-                    name = name.substring(colon + 1);
-                }
-                exportProperty(uri, name, (Property) entry.getValue());
+                exportProperty(name, (Property) entry.getValue());
             }
         }
     }
@@ -294,11 +283,11 @@
      */
     private void exportNode(Node node)
             throws RepositoryException, SAXException {
-        share = node.isNodeType(getJCRName(MIX, "shareable"))
+        share = node.isNodeType(helper.getJcrName("mix:shareable"))
             && !shareables.add(node.getUUID());
 
         if (node.getDepth() == 0) {
-            exportNode(JCR, "root", node);
+            exportNode(NamespaceHelper.JCR, "root", node);
         } else {
             String name = node.getName();
             int colon = name.indexOf(':');
@@ -335,16 +324,15 @@
      * The property is ignored if it does not exist.
      *
      * @param properties map of properties
-     * @param uri property namespace
-     * @param local property name
+     * @param name property name
      * @throws RepositoryException if a repository error occurs
      * @throws SAXException if a SAX error occurs
      */
-    private void exportProperty(Map properties, String uri, String local)
+    private void exportProperty(Map properties, String name)
             throws RepositoryException, SAXException {
-        Property property = (Property) properties.remove(getJCRName(uri, local));
+        Property property = (Property) properties.remove(name);
         if (property != null) {
-            exportProperty(uri, local, property);
+            exportProperty(name, property);
         }
     }
 
@@ -353,14 +341,21 @@
      * {@link #exportProperty(Value)} or {@link #exportProperty(int, Value[])}
      * depending on whether the the property is single- or multivalued.
      *
-     * @param uri property namespace
-     * @param local property name
+     * @param name property name
      * @param property property
      * @throws RepositoryException if a repository error occurs
      * @throws SAXException if a SAX error occurs
      */
-    private void exportProperty(String uri, String local, Property property)
+    private void exportProperty(String name, Property property)
             throws RepositoryException, SAXException {
+        String uri = null;
+        String local = name;
+        int colon = name.indexOf(':');
+        if (colon != -1) {
+            uri = session.getNamespaceURI(name.substring(0, colon));
+            local = name.substring(colon + 1);
+        }
+
         int type = property.getType();
         if (type != PropertyType.BINARY || binary) {
             if (property.getDefinition().isMultiple()) {
@@ -468,20 +463,6 @@
     }
 
     /**
-     * Returns the prefixed JCR name for the given namespace URI and local
-     * name.
-     *
-     * @param uri namespace URI (must not be the empty namespace)
-     * @param name local name
-     * @return prefixed JCR name
-     * @throws RepositoryException if a JCR namespace mapping is not available
-     */
-    protected String getJCRName(String uri, String name)
-            throws RepositoryException {
-        return session.getNamespacePrefix(uri) + ":" + name;
-    }
-
-    /**
      * Returns a prefixed XML name for the given namespace URI and local
      * name. If a prefix mapping for the namespace URI is not yet available,
      * it is created based on the namespace mappings of the current JCR

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SystemViewExporter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SystemViewExporter.java?rev=657028&r1=657027&r2=657028&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SystemViewExporter.java (original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/SystemViewExporter.java Fri May 16 05:23:32 2008
@@ -64,6 +64,7 @@
             Session session, ContentHandler handler,
             boolean recurse, boolean binary) {
         super(session, handler, recurse, binary);
+        addNamespace("sv", SV);
     }
 
     /**