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 class Attributes extends NodeChildren {
35 final String attributeName;
36
37 public Attributes(final GPathResult parent, final String name, final String namespacePrefix, final Map namespaceTagHints) {
38 super(parent, name, namespacePrefix, namespaceTagHints);
39
40 this.attributeName = this.name.substring(1);
41 }
42
43 public Attributes(final GPathResult parent, final String name, final Map namespaceTagHints) {
44 this(parent, name, "*", namespaceTagHints);
45 }
46
47
48
49
50 public Iterator childNodes() {
51 throw new GroovyRuntimeException("Can't get the child nodes on a a GPath expression selecting attributes: ...." + this.parent.name() + "." + name() + ".childNodes()");
52 }
53
54
55
56
57 public Iterator iterator() {
58 return new NodeIterator(this.parent.nodeIterator()) {
59
60
61
62 protected Object getNextNode(final Iterator iter) {
63 while (iter.hasNext()) {
64 final Object value = ((Node)iter.next()).attributes().get(Attributes.this.attributeName);
65
66 if (value != null) {
67 return value;
68 }
69 }
70
71 return null;
72 }
73 };
74 }
75
76
77
78
79 public GPathResult parents() {
80 return super.parents();
81 }
82
83
84
85
86 public String text() {
87 final StringBuffer buf = new StringBuffer();
88 final Iterator iter = iterator();
89
90 while (iter.hasNext()) {
91 buf.append(iter.next());
92 }
93
94 return buf.toString();
95 }
96
97
98
99
100 public GPathResult find(final Closure closure) {
101 return new FilteredAttributes(this, closure, this.namespaceTagHints);
102 }
103
104
105
106
107 public Writer writeTo(final Writer out) throws IOException {
108 out.write(text());
109
110 return out;
111 }
112
113
114
115
116 public void build(final GroovyObject builder) {
117 builder.getProperty("mkp");
118 builder.invokeMethod("yield", new Object[]{text()});
119 }
120 }