]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java
Remove unnecessary dependency to JTA.
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / JcrxApi.java
index ab34c730ee5d3382dba35b083ad4b8928caa359e..666b2593e5992dbb980f8e12e43c194fddb1f5c4 100644 (file)
@@ -30,30 +30,59 @@ public class JcrxApi {
         * XML
         */
        /**
-        * Set as a subnode which will be exported as an XML element.
+        * Get the XML text of this child node.
         */
        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();
+                               return null;
+                       Node child = node.getNode(name);
+                       return getXmlValue(child);
                } catch (RepositoryException e) {
                        throw new IllegalStateException("Cannot get " + name + " as XML text", e);
                }
        }
 
+       /**
+        * Get the XML text of this node.
+        */
+       public static String getXmlValue(Node node) {
+               try {
+                       if (!node.hasNode(Jcr.JCR_XMLTEXT))
+                               return null;
+                       Node xmlText = node.getNode(Jcr.JCR_XMLTEXT);
+                       if (!xmlText.hasProperty(Jcr.JCR_XMLCHARACTERS))
+                               throw new IllegalArgumentException(
+                                               "Node " + xmlText + " has no " + Jcr.JCR_XMLCHARACTERS + " property");
+                       return xmlText.getProperty(Jcr.JCR_XMLCHARACTERS).getString();
+               } catch (RepositoryException e) {
+                       throw new IllegalStateException("Cannot get " + node + " 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
+                       if (node.hasNode(name)) {
+                               Node child = node.getNode(name);
+                               setXmlValue(node, child, 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);
+                       throw new JcrException("Cannot set " + name + " as XML text", e);
+               }
+       }
+
+       public static void setXmlValue(Node node, Node child, String value) {
+               try {
+                       if (!child.hasNode(Jcr.JCR_XMLTEXT))
+                               child.addNode(Jcr.JCR_XMLTEXT, JcrxType.JCRX_XMLTEXT);
+                       child.getNode(Jcr.JCR_XMLTEXT).setProperty(Jcr.JCR_XMLCHARACTERS, value);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot set " + child + " as XML text", e);
                }
        }