1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package groovy.util.slurpersupport;
19
20 import groovy.lang.Closure;
21 import groovy.lang.GroovyObject;
22 import groovy.lang.GroovyRuntimeException;
23
24 import java.io.IOException;
25 import java.io.Writer;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
30
31 /***
32 * @author John Wilson
33 */
34
35 public class NodeChild extends GPathResult {
36 private final Node node;
37
38 public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map namespaceTagHints) {
39 super(parent, node.name(), namespacePrefix, namespaceTagHints);
40 this.node = node;
41 }
42
43 public NodeChild(final Node node, final GPathResult parent, final Map namespaceTagHints) {
44 this(node, parent, "*", namespaceTagHints);
45 }
46
47 public int size() {
48 return 1;
49 }
50
51 public String text() {
52 return this.node.text();
53 }
54
55 public GPathResult parents() {
56
57 throw new GroovyRuntimeException("parents() not implemented yet");
58 }
59
60 public Iterator iterator() {
61 return new Iterator() {
62 private boolean hasNext = true;
63
64 public boolean hasNext() {
65 return this.hasNext;
66 }
67
68 public Object next() {
69 try {
70 return (this.hasNext) ? NodeChild.this : null;
71 } finally {
72 this.hasNext = false;
73 }
74 }
75
76 public void remove() {
77 throw new UnsupportedOperationException();
78 }
79 };
80 }
81
82 public Iterator nodeIterator() {
83 return new Iterator() {
84 private boolean hasNext = true;
85
86 public boolean hasNext() {
87 return this.hasNext;
88 }
89
90 public Object next() {
91 try {
92 return (this.hasNext) ? NodeChild.this.node : null;
93 } finally {
94 this.hasNext = false;
95 }
96 }
97
98 public void remove() {
99 throw new UnsupportedOperationException();
100 }
101 };
102 }
103
104 public Object getAt(final int index) {
105 if (index == 0) {
106 return node;
107 } else {
108 throw new ArrayIndexOutOfBoundsException(index);
109 }
110 }
111
112 public Map attributes() {
113 return this.node.attributes();
114 }
115
116 public Iterator childNodes() {
117 return this.node.childNodes();
118 }
119
120 public GPathResult find(final Closure closure) {
121 if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[]{this.node}))) {
122 return this;
123 } else {
124 return new NoChildren(this, "", this.namespaceTagHints);
125 }
126 }
127
128 public GPathResult findAll(final Closure closure) {
129 return find(closure);
130 }
131
132 public void build(final GroovyObject builder) {
133 this.node.build(builder, this.namespaceMap, this.namespaceTagHints);
134 }
135
136 public Writer writeTo(final Writer out) throws IOException {
137 return this.node.writeTo(out);
138 }
139
140 protected void replaceNode(final Closure newValue) {
141 this.node.replaceNode(newValue, this);
142 }
143
144 protected void replaceBody(final Object newValue) {
145 this.node.replaceBody(newValue);
146 }
147
148 protected void appendNode(final Object newValue) {
149 this.node.appendNode(newValue, this);
150 }
151 }