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