]>
git.argeo.org Git - gpl/argeo-jcr.git/blob - org.argeo.cms.jcr/src/org/argeo/jcr/xml/JcrXmlUtils.java
1 package org
.argeo
.jcr
.xml
;
3 import java
.io
.IOException
;
6 import java
.util
.TreeMap
;
9 import javax
.jcr
.NodeIterator
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.PropertyIterator
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.jcr
.Value
;
14 import javax
.jcr
.nodetype
.NodeType
;
16 import org
.argeo
.jcr
.Jcr
;
18 /** Utilities around JCR and XML. */
19 public class JcrXmlUtils
{
21 * Convenience method calling {@link #toXmlElements(Writer, Node, boolean)} with
24 public static void toXmlElements(Writer writer
, Node node
) throws RepositoryException
, IOException
{
25 toXmlElements(writer
, node
, null, false, false, false);
29 * Write JCR properties as XML elements in a tree structure whose elements are
30 * named by node primary type.
32 * @param writer the writer to use
33 * @param node the subtree
34 * @param depth maximal depth, or if <code>null</code> the whole
35 * subtree. It must be positive, with depth 0
36 * describing just the node without its children.
37 * @param withMetadata whether to write the primary type and mixins as
39 * @param withPrefix whether to keep the namespace prefixes
40 * @param propertiesAsElements whether single properties should be written as
41 * elements rather than attributes. If
42 * <code>false</code>, multiple properties will be
45 public static void toXmlElements(Writer writer
, Node node
, Integer depth
, boolean withMetadata
, boolean withPrefix
,
46 boolean propertiesAsElements
) throws RepositoryException
, IOException
{
47 if (depth
!= null && depth
< 0)
48 throw new IllegalArgumentException("Depth " + depth
+ " is negative.");
50 if (node
.getName().equals(Jcr
.JCR_XMLTEXT
)) {
51 writer
.write(node
.getProperty(Jcr
.JCR_XMLCHARACTERS
).getString());
55 if (!propertiesAsElements
) {
56 Map
<String
, String
> attrs
= new TreeMap
<>();
57 PropertyIterator pit
= node
.getProperties();
58 properties
: while (pit
.hasNext()) {
59 Property p
= pit
.nextProperty();
60 if (!p
.isMultiple()) {
61 String pName
= p
.getName();
62 if (!withMetadata
&& (pName
.equals(Jcr
.JCR_PRIMARY_TYPE
) || pName
.equals(Jcr
.JCR_UUID
)
63 || pName
.equals(Jcr
.JCR_CREATED
) || pName
.equals(Jcr
.JCR_CREATED_BY
)
64 || pName
.equals(Jcr
.JCR_LAST_MODIFIED
) || pName
.equals(Jcr
.JCR_LAST_MODIFIED_BY
)))
66 attrs
.put(withPrefix(p
.getName(), withPrefix
), p
.getString());
69 if (withMetadata
&& node
.hasProperty(Property
.JCR_UUID
))
70 attrs
.put("id", "urn:uuid:" + node
.getProperty(Property
.JCR_UUID
).getString());
71 attrs
.put(withPrefix ? Jcr
.JCR_NAME
: "name", node
.getName());
72 writeStart(writer
, withPrefix(node
.getPrimaryNodeType().getName(), withPrefix
), attrs
, node
.hasNodes());
74 if (withMetadata
&& node
.hasProperty(Property
.JCR_UUID
)) {
75 writeStart(writer
, withPrefix(node
.getPrimaryNodeType().getName(), withPrefix
), "id",
76 "urn:uuid:" + node
.getProperty(Property
.JCR_UUID
).getString());
78 writeStart(writer
, withPrefix(node
.getPrimaryNodeType().getName(), withPrefix
));
81 writeStart(writer
, withPrefix ? Jcr
.JCR_NAME
: "name");
82 writer
.append(node
.getName());
83 writeEnd(writer
, withPrefix ? Jcr
.JCR_NAME
: "name");
88 for (NodeType mixin
: node
.getMixinNodeTypes()) {
89 writeStart(writer
, withPrefix ? Jcr
.JCR_MIXIN_TYPES
: "mixinTypes");
90 writer
.append(mixin
.getName());
91 writeEnd(writer
, withPrefix ? Jcr
.JCR_MIXIN_TYPES
: "mixinTypes");
95 // properties as elements
96 if (propertiesAsElements
) {
97 PropertyIterator pit
= node
.getProperties();
98 properties
: while (pit
.hasNext()) {
99 Property p
= pit
.nextProperty();
100 if (p
.isMultiple()) {
101 for (Value value
: p
.getValues()) {
102 writeStart(writer
, withPrefix(p
.getName(), withPrefix
));
103 writer
.write(value
.getString());
104 writeEnd(writer
, withPrefix(p
.getName(), withPrefix
));
107 Value value
= p
.getValue();
108 String pName
= p
.getName();
109 if (!withMetadata
&& (pName
.equals(Jcr
.JCR_PRIMARY_TYPE
) || pName
.equals(Jcr
.JCR_UUID
)
110 || pName
.equals(Jcr
.JCR_CREATED
) || pName
.equals(Jcr
.JCR_CREATED_BY
)
111 || pName
.equals(Jcr
.JCR_LAST_MODIFIED
) || pName
.equals(Jcr
.JCR_LAST_MODIFIED_BY
)))
113 writeStart(writer
, withPrefix(p
.getName(), withPrefix
));
114 writer
.write(value
.getString());
115 writeEnd(writer
, withPrefix(p
.getName(), withPrefix
));
121 if (node
.hasNodes()) {
122 if (depth
== null || depth
> 0) {
123 NodeIterator nit
= node
.getNodes();
124 while (nit
.hasNext()) {
125 toXmlElements(writer
, nit
.nextNode(), depth
== null ?
null : depth
- 1, withMetadata
, withPrefix
,
126 propertiesAsElements
);
129 writeEnd(writer
, withPrefix(node
.getPrimaryNodeType().getName(), withPrefix
));
133 private static String
withPrefix(String str
, boolean withPrefix
) {
136 int index
= str
.indexOf(':');
139 return str
.substring(index
+ 1);
142 private static void writeStart(Writer writer
, String tagName
) throws IOException
{
144 writer
.append(tagName
);
148 private static void writeStart(Writer writer
, String tagName
, String attr
, String value
) throws IOException
{
150 writer
.append(tagName
);
153 writer
.append("=\"");
154 writer
.append(value
);
155 writer
.append("\">");
158 private static void writeStart(Writer writer
, String tagName
, Map
<String
, String
> attrs
, boolean hasChildren
)
161 writer
.append(tagName
);
162 for (String attr
: attrs
.keySet()) {
165 writer
.append("=\"");
166 writer
.append(attrs
.get(attr
));
175 private static void writeEnd(Writer writer
, String tagName
) throws IOException
{
177 writer
.append(tagName
);
182 private JcrXmlUtils() {