Introduce JCRX, JCR canonical extensions.
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 1 Nov 2020 10:30:50 +0000 (11:30 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 1 Nov 2020 10:30:50 +0000 (11:30 +0100)
org.argeo.jcr/bnd.bnd
org.argeo.jcr/src/org/argeo/jcr/JcrUtils.java
org.argeo.jcr/src/org/argeo/jcr/JcrxType.java [new file with mode: 0644]
org.argeo.jcr/src/org/argeo/jcr/JcrxUtils.java [new file with mode: 0644]
org.argeo.jcr/src/org/argeo/jcr/jcrx.cnd [new file with mode: 0644]

index 457dc0300f0162ea4fe6907045ea483546ea8df4..551686bd0c340307843e4b59d113fdcf500ffa96 100644 (file)
@@ -1,3 +1,6 @@
+Provide-Capability:\
+cms.datamodel; name=jcrx; cnd=/org/argeo/jcr/jcrx.cnd; abstract=true
+
 Import-Package: junit.framework;resolution:=optional,\
 org.xml.sax;version="0.0.0",\
 org.apache.jackrabbit.*;resolution:=optional,\
index e304649e23ac386c718895b532b0b276e52e92ca..9dfbf305a89024df372d1c08ce81307f867e20e6 100644 (file)
@@ -305,20 +305,46 @@ public class JcrUtils {
                }
        }
 
+//     /**
+//      * Routine that get the child with this name, adding it if it does not already
+//      * exist
+//      */
+//     public static Node getOrAdd(Node parent, String name, String primaryNodeType) throws RepositoryException {
+//             return parent.hasNode(name) ? parent.getNode(name) : parent.addNode(name, primaryNodeType);
+//     }
+
        /**
-        * Routine that get the child with this name, adding id it does not already
+        * Routine that get the child with this name, adding it if it does not already
         * exist
         */
-       public static Node getOrAdd(Node parent, String childName, String childPrimaryNodeType) throws RepositoryException {
-               return parent.hasNode(childName) ? parent.getNode(childName) : parent.addNode(childName, childPrimaryNodeType);
+       public static Node getOrAdd(Node parent, String name, String primaryNodeType, String... mixinNodeTypes)
+                       throws RepositoryException {
+               Node node;
+               if (parent.hasNode(name)) {
+                       node = parent.getNode(name);
+                       if (primaryNodeType != null && !node.isNodeType(primaryNodeType))
+                               throw new IllegalArgumentException("Node " + node + " exists but is of primary node type "
+                                               + node.getPrimaryNodeType().getName() + ", not " + primaryNodeType);
+                       for (String mixin : mixinNodeTypes) {
+                               if (!node.isNodeType(mixin))
+                                       node.addMixin(mixin);
+                       }
+                       return node;
+               } else {
+                       node = primaryNodeType != null ? parent.addNode(name, primaryNodeType) : parent.addNode(name);
+                       for (String mixin : mixinNodeTypes) {
+                               node.addMixin(mixin);
+                       }
+                       return node;
+               }
        }
 
        /**
-        * Routine that get the child with this name, adding id it does not already
+        * Routine that get the child with this name, adding it if it does not already
         * exist
         */
-       public static Node getOrAdd(Node parent, String childName) throws RepositoryException {
-               return parent.hasNode(childName) ? parent.getNode(childName) : parent.addNode(childName);
+       public static Node getOrAdd(Node parent, String name) throws RepositoryException {
+               return parent.hasNode(name) ? parent.getNode(name) : parent.addNode(name);
        }
 
        /** Convert a {@link NodeIterator} to a list of {@link Node} */
diff --git a/org.argeo.jcr/src/org/argeo/jcr/JcrxType.java b/org.argeo.jcr/src/org/argeo/jcr/JcrxType.java
new file mode 100644 (file)
index 0000000..984a739
--- /dev/null
@@ -0,0 +1,14 @@
+package org.argeo.jcr;
+
+/** Node types declared by the JCR extensions. */
+public interface JcrxType {
+       /**
+        * Node type for an XML value, which will be serialized in XML as an element
+        * containing text.
+        */
+       public final static String JCRX_XMLVALUE = "{http://www.argeo.org/ns/jcrx}xmlvalue";
+
+       /** Node type for the node containing the text. */
+       public final static String JCRX_XMLTEXT = "{http://www.argeo.org/ns/jcrx}xmltext";
+
+}
diff --git a/org.argeo.jcr/src/org/argeo/jcr/JcrxUtils.java b/org.argeo.jcr/src/org/argeo/jcr/JcrxUtils.java
new file mode 100644 (file)
index 0000000..d5f8f18
--- /dev/null
@@ -0,0 +1,40 @@
+package org.argeo.jcr;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+
+/** Uilities around the JCR extensions. */
+public class JcrxUtils {
+
+       /*
+        * XML
+        */
+       /**
+        * Set as a subnode which will be exported as an XML element.
+        */
+       public static String getXmlValue(Node node, String name) {
+               try {
+                       if (!node.hasNode(name))
+                               throw new IllegalArgumentException("No XML text named " + name);
+                       return node.getNode(name).getNode(Jcr.JCR_XMLTEXT).getProperty(Jcr.JCR_XMLCHARACTERS).getString();
+               } catch (RepositoryException e) {
+                       throw new IllegalStateException("Cannot get " + name + " as XML text", e);
+               }
+       }
+
+       /**
+        * Set as a subnode which will be exported as an XML element.
+        */
+       public static void setXmlValue(Node node, String name, String value) {
+               try {
+                       if (node.hasNode(name))
+                               node.getNode(name).getNode(Jcr.JCR_XMLTEXT).setProperty(Jcr.JCR_XMLCHARACTERS, value);
+                       else
+                               node.addNode(name, JcrxType.JCRX_XMLVALUE).addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT)
+                                               .setProperty(Jcr.JCR_XMLCHARACTERS, value);
+               } catch (RepositoryException e) {
+                       throw new IllegalStateException("Cannot set " + name + " as XML text", e);
+               }
+       }
+
+}
diff --git a/org.argeo.jcr/src/org/argeo/jcr/jcrx.cnd b/org.argeo.jcr/src/org/argeo/jcr/jcrx.cnd
new file mode 100644 (file)
index 0000000..c8c44ad
--- /dev/null
@@ -0,0 +1,11 @@
+//
+// JCR EXTENSIONS
+//
+<jcrx = "http://www.argeo.org/ns/jcrx">
+
+[jcrx:xmlvalue]
+- *
++ jcr:xmltext (jcrx:xmltext) = jcrx:xmltext
+
+[jcrx:xmltext]
+ - jcr:xmlcharacters (STRING) mandatory