1 package org
.argeo
.cms
.gcr
.xml
;
3 import java
.util
.HashSet
;
4 import java
.util
.Iterator
;
7 import org
.argeo
.api
.gcr
.AbstractContent
;
8 import org
.argeo
.api
.gcr
.Content
;
9 import org
.argeo
.api
.gcr
.ContentSession
;
10 import org
.w3c
.dom
.Attr
;
11 import org
.w3c
.dom
.Element
;
12 import org
.w3c
.dom
.NamedNodeMap
;
13 import org
.w3c
.dom
.Node
;
14 import org
.w3c
.dom
.NodeList
;
15 import org
.w3c
.dom
.Text
;
17 public class DomContent
extends AbstractContent
implements Content
{
19 private final DomContentSession contentSession
;
20 private final Element element
;
22 // private String text = null;
23 private Boolean hasText
= null;
25 public DomContent(DomContentSession contentSession
, Element element
) {
26 this.contentSession
= contentSession
;
27 this.element
= element
;
31 public Iterator
<Content
> iterator() {
32 NodeList nodeList
= element
.getChildNodes();
33 return new ElementIterator(contentSession
, nodeList
);
37 public String
getName() {
38 return element
.getNodeName();
42 public Iterable
<String
> keys() {
43 // TODO implement an iterator?
44 Set
<String
> result
= new HashSet
<>();
45 NamedNodeMap attributes
= element
.getAttributes();
46 for (int i
= 0; i
< attributes
.getLength(); i
++) {
47 Attr attr
= (Attr
) attributes
.item(i
);
48 String attrName
= attr
.getNodeName();
55 public <A
> A
get(String key
, Class
<A
> clss
) {
56 if (element
.hasAttribute(key
)) {
57 String value
= element
.getAttribute(key
);
58 if (clss
.isAssignableFrom(String
.class))
61 throw new IllegalArgumentException();
67 public ContentSession
getSession() {
68 return contentSession
;
72 public boolean hasText() {
73 // return element instanceof Text;
76 NodeList nodeList
= element
.getChildNodes();
77 if (nodeList
.getLength() > 1) {
81 nodes
: for (int i
= 0; i
< nodeList
.getLength(); i
++) {
82 Node node
= nodeList
.item(i
);
83 if (node
instanceof Text
) {
84 Text text
=(Text
) node
;
85 if (!text
.isElementContentWhitespace()) {
96 // text = element.getTextContent();
97 // return text != null;
101 public String
getText() {
103 return element
.getTextContent();