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.InvokerHelper;
30
31 /***
32 * @author John Wilson
33 *
34 */
35
36 public class NodeChild extends GPathResult {
37 private final Node node;
38
39 public NodeChild(final Node node, final GPathResult parent, final String namespacePrefix, final Map namespaceTagHints) {
40 super(parent, node.name(), namespacePrefix, namespaceTagHints);
41 this.node = node;
42 }
43
44 public NodeChild(final Node node, final GPathResult parent, final Map namespaceTagHints) {
45 this(node, parent, "*", namespaceTagHints);
46 }
47
48
49
50
51 public int size() {
52 return 1;
53 }
54
55
56
57
58 public String text() {
59 return this.node.text();
60 }
61
62
63
64
65 public GPathResult parents() {
66
67 throw new GroovyRuntimeException("parents() not implemented yet");
68 }
69
70
71
72
73 public Iterator iterator() {
74 return new Iterator() {
75 private boolean hasNext = true;
76
77 public boolean hasNext() {
78 return this.hasNext;
79 }
80
81 public Object next() {
82 try {
83 return (this.hasNext) ? NodeChild.this : null;
84 } finally {
85 this.hasNext = false;
86 }
87 }
88
89 public void remove() {
90 throw new UnsupportedOperationException();
91 }
92 };
93 }
94
95
96
97
98 public Iterator nodeIterator() {
99 return new Iterator() {
100 private boolean hasNext = true;
101
102 public boolean hasNext() {
103 return this.hasNext;
104 }
105
106 public Object next() {
107 try {
108 return (this.hasNext) ? NodeChild.this.node : null;
109 } finally {
110 this.hasNext = false;
111 }
112 }
113
114 public void remove() {
115 throw new UnsupportedOperationException();
116 }
117 };
118 }
119
120
121
122
123 public Object getAt(final int index) {
124 if (index == 0) {
125 return node;
126 } else {
127 throw new ArrayIndexOutOfBoundsException(index);
128 }
129 }
130
131 public Map attributes() {
132 return this.node.attributes();
133 }
134
135 public Iterator childNodes() {
136 return this.node.childNodes();
137 }
138
139
140
141 public GPathResult find(final Closure closure) {
142 if (InvokerHelper.asBool(closure.call(new Object[]{this}))) {
143 return this;
144 } else {
145 return new NoChildren(this, "", this.namespaceTagHints);
146 }
147 }
148
149
150
151
152 public GPathResult findAll(final Closure closure) {
153 return find(closure);
154 }
155
156
157
158
159 public void build(final GroovyObject builder) {
160 this.node.build(builder, this.namespaceMap, this.namespaceTagHints);
161 }
162
163
164
165
166 public Writer writeTo(final Writer out) throws IOException {
167 return this.node.writeTo(out);
168 }
169 }