1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package groovy.util.slurpersupport;
20
21 import groovy.lang.Closure;
22 import groovy.lang.GroovyObject;
23 import groovy.lang.GroovyRuntimeException;
24
25 import java.io.IOException;
26 import java.io.Writer;
27 import java.util.Iterator;
28 import java.util.Map;
29
30 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
31
32 public class Attribute extends GPathResult {
33 private final String value;
34
35 public Attribute(final String name, final String value, final GPathResult parent, final String namespacePrefix, final Map namespaceTagHints) {
36 super(parent, name, namespacePrefix, namespaceTagHints);
37 this.value = value;
38 }
39
40 public String name() {
41
42 return this.name.substring(1);
43 }
44
45 public int size() {
46 return 1;
47 }
48
49 public String text() {
50 return this.value;
51 }
52
53 public GPathResult parents() {
54
55 throw new GroovyRuntimeException("parents() not implemented yet");
56 }
57
58 public Iterator childNodes() {
59 throw new GroovyRuntimeException("can't call childNodes() in the attribute " + this.name);
60 }
61
62 public Iterator iterator() {
63 return nodeIterator();
64 }
65
66 public GPathResult find(final Closure closure) {
67 if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[]{this}))) {
68 return this;
69 } else {
70 return new NoChildren(this, "", this.namespaceTagHints);
71 }
72 }
73
74 public GPathResult findAll(final Closure closure) {
75 return find(closure);
76 }
77
78 public Iterator nodeIterator() {
79 return new Iterator() {
80 private boolean hasNext = true;
81
82 public boolean hasNext() {
83 return this.hasNext;
84 }
85
86 public Object next() {
87 try {
88 return (this.hasNext) ? Attribute.this : null;
89 } finally {
90 this.hasNext = false;
91 }
92 }
93
94 public void remove() {
95 throw new UnsupportedOperationException();
96 }
97 };
98 }
99
100 public Writer writeTo(final Writer out) throws IOException {
101 out.write(this.value);
102 return out;
103 }
104
105 public void build(final GroovyObject builder) {
106 builder.getProperty("mkp");
107 builder.invokeMethod("yield", new Object[]{this.value});
108 }
109
110 protected void replaceNode(final Closure newValue) {
111 }
112
113 protected void replaceBody(final Object newValue) {
114 }
115
116 protected void appendNode(final Object newValue) {
117 }
118 }