X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.jcr%2Fsrc%2Forg%2Fargeo%2Fjcr%2FJcrxApi.java;h=8f1ee9fa9a4f67752dd1661c460f08cb662b72d3;hb=623a35d1a39522cf8b6a1d6c860ac60a2646f03d;hp=ab34c730ee5d3382dba35b083ad4b8928caa359e;hpb=a2590cf3e2ad039f004f13ef6c97a9f702841e5b;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java b/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java index ab34c730e..8f1ee9fa9 100644 --- a/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java +++ b/org.argeo.jcr/src/org/argeo/jcr/JcrxApi.java @@ -35,8 +35,15 @@ public class JcrxApi { 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); + if (!child.hasNode(Jcr.JCR_XMLTEXT)) + return null; + Node xmlText = child.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 " + name + " as XML text", e); } @@ -47,13 +54,24 @@ public class JcrxApi { */ 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); } }