X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.entity.api%2Fsrc%2Forg%2Fargeo%2Fentity%2FEntityJcrUtils.java;fp=org.argeo.entity.api%2Fsrc%2Forg%2Fargeo%2Fentity%2FEntityJcrUtils.java;h=f32c1825659a3a960491d943734f0e37a8e72da9;hb=04cafe75836841a6c845b8dbc0cc80998442f78f;hp=0000000000000000000000000000000000000000;hpb=f1bad89ca61df0dc06b1304f147cf1c35d54803f;p=gpl%2Fargeo-suite.git diff --git a/org.argeo.entity.api/src/org/argeo/entity/EntityJcrUtils.java b/org.argeo.entity.api/src/org/argeo/entity/EntityJcrUtils.java new file mode 100644 index 0000000..f32c182 --- /dev/null +++ b/org.argeo.entity.api/src/org/argeo/entity/EntityJcrUtils.java @@ -0,0 +1,60 @@ +package org.argeo.entity; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; + +/** JCR utilities around the entity concepts. */ +public class EntityJcrUtils { + /** + * The name of a node which will be serialized as XML text, as per section 7.3.1 + * of the JCR 2.0 specifications. + */ + public final static String JCR_XMLTEXT = "jcr:xmltext"; + + /** + * The name of a property which will be serialized as XML text, as per section + * 7.3.1 of the JCR 2.0 specifications. + */ + public final static String JCR_XMLCHARACTERS = "jcr:xmlcharacters"; + + /* + * XML + */ + /** + * Set both as a property and as a subnode which will be exported as an XML + * element. + */ + +// public static void setAttrAndElem(Node node, String name, String value) { +// Jcr.set(node, name, value); +// setElem(node, name, value); +// } + /** + * 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_XMLTEXT).getProperty(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_XMLTEXT).setProperty(JCR_XMLCHARACTERS, value); + else + node.addNode(name, EntityType.xmlvalue.qualified()).addNode(JCR_XMLTEXT, EntityType.xmltext.qualified()) + .setProperty(JCR_XMLCHARACTERS, value); + } catch (RepositoryException e) { + throw new IllegalStateException("Cannot set " + name + " as XML text", e); + } + } + +}