1 package org
.argeo
.cms
.jcr
.gcr
;
3 import java
.util
.Calendar
;
4 import java
.util
.Iterator
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.PropertyIterator
;
10 import javax
.jcr
.PropertyType
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Value
;
13 import javax
.jcr
.nodetype
.NodeType
;
14 import javax
.xml
.namespace
.QName
;
16 import org
.argeo
.api
.gcr
.Content
;
17 import org
.argeo
.api
.gcr
.spi
.AbstractContent
;
18 import org
.argeo
.api
.gcr
.spi
.ProvidedSession
;
19 import org
.argeo
.jcr
.Jcr
;
20 import org
.argeo
.jcr
.JcrException
;
22 public class JcrContent
extends AbstractContent
{
25 private JcrContentProvider provider
;
26 private ProvidedSession session
;
28 protected JcrContent(ProvidedSession session
, JcrContentProvider provider
, Node node
) {
29 this.session
= session
;
30 this.provider
= provider
;
35 public QName
getName() {
36 return session
.parsePrefixedName(Jcr
.getName(jcrNode
));
40 public <A
> A
get(QName key
, Class
<A
> clss
) {
41 if (isDefaultAttrTypeRequested(clss
)) {
42 return (A
) get(jcrNode
, key
.toString());
44 return (A
) Jcr
.get(jcrNode
, key
.toString());
48 public Iterator
<Content
> iterator() {
50 return new JcrContentIterator(jcrNode
.getNodes());
51 } catch (RepositoryException e
) {
52 throw new JcrException("Cannot list children of " + jcrNode
, e
);
57 protected Iterable
<QName
> keys() {
58 return new Iterable
<QName
>() {
61 public Iterator
<QName
> iterator() {
63 PropertyIterator propertyIterator
= jcrNode
.getProperties();
64 return new JcrKeyIterator(provider
, propertyIterator
);
65 } catch (RepositoryException e
) {
66 throw new JcrException("Cannot retrive properties from " + jcrNode
, e
);
72 public Node
getJcrNode() {
76 /** Cast to a standard Java object. */
77 static Object
get(Node node
, String property
) {
79 Value value
= node
.getProperty(property
).getValue();
80 switch (value
.getType()) {
81 case PropertyType
.STRING
:
82 return value
.getString();
83 case PropertyType
.DOUBLE
:
84 return (Double
) value
.getDouble();
85 case PropertyType
.LONG
:
86 return (Long
) value
.getLong();
87 case PropertyType
.BOOLEAN
:
88 return (Boolean
) value
.getBoolean();
89 case PropertyType
.DATE
:
90 Calendar calendar
= value
.getDate();
91 return calendar
.toInstant();
92 case PropertyType
.BINARY
:
93 throw new IllegalArgumentException("Binary is not supported as an attribute");
95 return value
.getString();
97 } catch (RepositoryException e
) {
98 throw new JcrException("Cannot cast value from " + property
+ " of node " + node
, e
);
102 class JcrContentIterator
implements Iterator
<Content
> {
103 private final NodeIterator nodeIterator
;
104 // we keep track in order to be able to delete it
105 private JcrContent current
= null;
107 protected JcrContentIterator(NodeIterator nodeIterator
) {
108 this.nodeIterator
= nodeIterator
;
112 public boolean hasNext() {
113 return nodeIterator
.hasNext();
117 public Content
next() {
118 current
= new JcrContent(session
, provider
, nodeIterator
.nextNode());
123 public void remove() {
124 if (current
!= null) {
125 Jcr
.remove(current
.getJcrNode());
132 public Content
getParent() {
133 return new JcrContent(session
, provider
, Jcr
.getParent(getJcrNode()));
137 public Content
add(QName name
, QName
... classes
) {
138 if (classes
.length
> 0) {
139 QName primaryType
= classes
[0];
140 Node child
= Jcr
.addNode(getJcrNode(), name
.toString(), primaryType
.toString());
141 for (int i
= 1; i
< classes
.length
; i
++) {
143 child
.addMixin(classes
[i
].toString());
144 } catch (RepositoryException e
) {
145 throw new JcrException("Cannot add child to " + getJcrNode(), e
);
150 Jcr
.addNode(getJcrNode(), name
.toString(), NodeType
.NT_UNSTRUCTURED
);
156 public void remove() {
157 Jcr
.remove(getJcrNode());
161 protected void removeAttr(QName key
) {
162 Property property
= Jcr
.getProperty(getJcrNode(), key
.toString());
163 if (property
!= null) {
166 } catch (RepositoryException e
) {
167 throw new JcrException("Cannot remove property " + key
+ " from " + getJcrNode(), e
);
173 class JcrKeyIterator
implements Iterator
<QName
> {
174 private final JcrContentProvider contentSession
;
175 private final PropertyIterator propertyIterator
;
177 protected JcrKeyIterator(JcrContentProvider contentSession
, PropertyIterator propertyIterator
) {
178 this.contentSession
= contentSession
;
179 this.propertyIterator
= propertyIterator
;
183 public boolean hasNext() {
184 return propertyIterator
.hasNext();
188 public QName
next() {
189 Property property
= null;
191 property
= propertyIterator
.nextProperty();
192 // TODO map standard property names
193 return session
.parsePrefixedName(property
.getName());
194 } catch (RepositoryException e
) {
195 throw new JcrException("Cannot retrieve property " + property
, null);