1 package org
.argeo
.cms
.acr
.xml
;
3 import java
.nio
.CharBuffer
;
4 import java
.util
.HashSet
;
5 import java
.util
.Iterator
;
6 import java
.util
.Optional
;
8 import java
.util
.concurrent
.CompletableFuture
;
10 import javax
.xml
.XMLConstants
;
11 import javax
.xml
.namespace
.NamespaceContext
;
12 import javax
.xml
.namespace
.QName
;
14 import org
.argeo
.api
.acr
.Content
;
15 import org
.argeo
.api
.acr
.ContentName
;
16 import org
.argeo
.api
.acr
.spi
.ProvidedContent
;
17 import org
.argeo
.api
.acr
.spi
.ProvidedSession
;
18 import org
.argeo
.cms
.acr
.AbstractContent
;
19 import org
.argeo
.cms
.acr
.ContentUtils
;
20 import org
.w3c
.dom
.Attr
;
21 import org
.w3c
.dom
.Document
;
22 import org
.w3c
.dom
.Element
;
23 import org
.w3c
.dom
.NamedNodeMap
;
24 import org
.w3c
.dom
.Node
;
25 import org
.w3c
.dom
.NodeList
;
26 import org
.w3c
.dom
.Text
;
28 /** Content persisted as a DOM element. */
29 public class DomContent
extends AbstractContent
implements ProvidedContent
{
31 private final ProvidedSession session
;
32 private final DomContentProvider provider
;
33 private final Element element
;
35 // private String text = null;
36 private Boolean hasText
= null;
38 public DomContent(ProvidedSession session
, DomContentProvider contentProvider
, Element element
) {
39 this.session
= session
;
40 this.provider
= contentProvider
;
41 this.element
= element
;
44 public DomContent(DomContent context
, Element element
) {
45 this(context
.getSession(), context
.getProvider(), element
);
49 public QName
getName() {
50 if (element
.getParentNode() == null) {// root
51 String mountPath
= provider
.getMountPath();
52 if (mountPath
!= null) {
53 Content mountPoint
= session
.getMountPoint(mountPath
);
54 return mountPoint
.getName();
57 return toQName(this.element
);
60 protected QName
toQName(Node node
) {
61 String prefix
= node
.getPrefix();
63 String namespaceURI
= node
.getNamespaceURI();
64 if (namespaceURI
== null)
65 namespaceURI
= node
.getOwnerDocument().lookupNamespaceURI(null);
66 if (namespaceURI
== null) {
67 return toQName(node
, node
.getLocalName());
69 String contextPrefix
= provider
.getPrefix(namespaceURI
);
70 if (contextPrefix
== null)
71 throw new IllegalStateException("Namespace " + namespaceURI
+ " is unbound");
72 return toQName(node
, namespaceURI
, node
.getLocalName(), provider
);
75 String namespaceURI
= node
.getNamespaceURI();
76 if (namespaceURI
== null)
77 namespaceURI
= node
.getOwnerDocument().lookupNamespaceURI(prefix
);
78 if (namespaceURI
== null) {
79 namespaceURI
= provider
.getNamespaceURI(prefix
);
80 if (XMLConstants
.NULL_NS_URI
.equals(namespaceURI
))
81 throw new IllegalStateException("Prefix " + prefix
+ " is unbound");
82 // TODO bind the prefix in the document?
84 return toQName(node
, namespaceURI
, node
.getLocalName(), provider
);
88 protected QName
toQName(Node source
, String namespaceURI
, String localName
, NamespaceContext namespaceContext
) {
89 return new ContentName(namespaceURI
, localName
, namespaceContext
);
92 protected QName
toQName(Node source
, String localName
) {
93 return new ContentName(localName
);
96 * ATTRIBUTES OPERATIONS
100 public Iterable
<QName
> keys() {
101 // TODO implement an iterator?
102 Set
<QName
> result
= new HashSet
<>();
103 NamedNodeMap attributes
= element
.getAttributes();
104 for (int i
= 0; i
< attributes
.getLength(); i
++) {
105 Attr attr
= (Attr
) attributes
.item(i
);
106 QName key
= toQName(attr
);
112 @SuppressWarnings("unchecked")
114 public <A
> Optional
<A
> get(QName key
, Class
<A
> clss
) {
115 String namespaceUriOrNull
= XMLConstants
.NULL_NS_URI
.equals(key
.getNamespaceURI()) ?
null
116 : key
.getNamespaceURI();
117 if (element
.hasAttributeNS(namespaceUriOrNull
, key
.getLocalPart())) {
118 String value
= element
.getAttributeNS(namespaceUriOrNull
, key
.getLocalPart());
119 if (clss
.isAssignableFrom(String
.class))
120 return Optional
.of((A
) value
);
122 return Optional
.empty();
128 public Object
put(QName key
, Object value
) {
129 Object previous
= get(key
);
130 String namespaceUriOrNull
= XMLConstants
.NULL_NS_URI
.equals(key
.getNamespaceURI()) ?
null
131 : key
.getNamespaceURI();
132 String prefixToUse
= registerPrefixIfNeeded(key
);
133 element
.setAttributeNS(namespaceUriOrNull
,
134 namespaceUriOrNull
== null ? key
.getLocalPart() : prefixToUse
+ ":" + key
.getLocalPart(),
139 protected String
registerPrefixIfNeeded(QName name
) {
140 String namespaceUriOrNull
= XMLConstants
.NULL_NS_URI
.equals(name
.getNamespaceURI()) ?
null
141 : name
.getNamespaceURI();
143 if (namespaceUriOrNull
!= null) {
144 String registeredPrefix
= provider
.getPrefix(namespaceUriOrNull
);
145 if (registeredPrefix
!= null) {
146 prefixToUse
= registeredPrefix
;
148 provider
.registerPrefix(name
.getPrefix(), namespaceUriOrNull
);
149 prefixToUse
= name
.getPrefix();
158 public boolean hasText() {
159 // return element instanceof Text;
162 NodeList nodeList
= element
.getChildNodes();
163 if (nodeList
.getLength() > 1) {
167 nodes
: for (int i
= 0; i
< nodeList
.getLength(); i
++) {
168 Node node
= nodeList
.item(i
);
169 if (node
instanceof Text
) {
170 Text text
= (Text
) node
;
171 if (!text
.isElementContentWhitespace()) {
182 // text = element.getTextContent();
183 // return text != null;
187 public String
getText() {
189 return element
.getTextContent();
199 public Iterator
<Content
> iterator() {
200 NodeList nodeList
= element
.getChildNodes();
201 return new ElementIterator(this, session
, provider
, nodeList
);
205 public Content
getParent() {
206 Node parentNode
= element
.getParentNode();
207 if (parentNode
== null) {
208 String mountPath
= provider
.getMountPath();
209 if (mountPath
== null)
211 String
[] parent
= ContentUtils
.getParentPath(mountPath
);
212 return session
.get(parent
[0]);
214 if (parentNode
instanceof Document
)
216 if (!(parentNode
instanceof Element
))
217 throw new IllegalStateException("Parent is not an element");
218 return new DomContent(this, (Element
) parentNode
);
222 public Content
add(QName name
, QName
... classes
) {
223 // TODO consider classes
224 Document document
= this.element
.getOwnerDocument();
225 String namespaceUriOrNull
= XMLConstants
.NULL_NS_URI
.equals(name
.getNamespaceURI()) ?
null
226 : name
.getNamespaceURI();
227 String prefixToUse
= registerPrefixIfNeeded(name
);
228 Element child
= document
.createElementNS(namespaceUriOrNull
,
229 namespaceUriOrNull
== null ? name
.getLocalPart() : prefixToUse
+ ":" + name
.getLocalPart());
230 element
.appendChild(child
);
231 return new DomContent(this, child
);
235 public void remove() {
236 // TODO make it more robust
237 element
.getParentNode().removeChild(element
);
242 protected void removeAttr(QName key
) {
243 String namespaceUriOrNull
= XMLConstants
.NULL_NS_URI
.equals(key
.getNamespaceURI()) ?
null
244 : key
.getNamespaceURI();
245 element
.removeAttributeNS(namespaceUriOrNull
,
246 namespaceUriOrNull
== null ? key
.getLocalPart() : key
.getPrefix() + ":" + key
.getLocalPart());
250 @SuppressWarnings("unchecked")
252 public <A
> A
adapt(Class
<A
> clss
) throws IllegalArgumentException
{
253 if (CharBuffer
.class.isAssignableFrom(clss
)) {
254 String textContent
= element
.getTextContent();
255 CharBuffer buf
= CharBuffer
.wrap(textContent
);
258 return super.adapt(clss
);
261 @SuppressWarnings("unchecked")
262 public <A
> CompletableFuture
<A
> write(Class
<A
> clss
) {
263 if (String
.class.isAssignableFrom(clss
)) {
264 CompletableFuture
<String
> res
= new CompletableFuture
<>();
265 res
.thenAccept((s
) -> {
266 session
.notifyModification(this);
267 element
.setTextContent(s
);
269 return (CompletableFuture
<A
>) res
;
271 return super.write(clss
);
278 public ProvidedContent
getMountPoint(String relativePath
) {
279 // FIXME use qualified names
280 Element childElement
= (Element
) element
.getElementsByTagName(relativePath
).item(0);
281 // TODO check that it is a mount
282 return new DomContent(this, childElement
);
285 public ProvidedSession
getSession() {
289 public DomContentProvider
getProvider() {