001: /*
002: * Copyright 2005 John G. Wilson
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package groovy.util.slurpersupport;
018:
019: import groovy.lang.Closure;
020: import groovy.lang.GroovyObject;
021: import groovy.lang.GroovyRuntimeException;
022:
023: import java.io.IOException;
024: import java.io.Writer;
025: import java.util.Map;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.ArrayList;
029:
030: /**
031: * @author John Wilson
032: */
033:
034: class Attributes extends NodeChildren {
035: final String attributeName;
036:
037: public Attributes(final GPathResult parent, final String name,
038: final String namespacePrefix, final Map namespaceTagHints) {
039: super (parent, name, namespacePrefix, namespaceTagHints);
040: this .attributeName = this .name.substring(1);
041: }
042:
043: public Attributes(final GPathResult parent, final String name,
044: final Map namespaceTagHints) {
045: this (parent, name, "*", namespaceTagHints);
046: }
047:
048: public String name() {
049: // this name contains @name we need to return name
050: return this .name.substring(1);
051: }
052:
053: public Iterator childNodes() {
054: throw new GroovyRuntimeException(
055: "Can't get the child nodes on a a GPath expression selecting attributes: ...."
056: + this .parent.name() + "." + name()
057: + ".childNodes()");
058: }
059:
060: public Iterator iterator() {
061: return new NodeIterator(nodeIterator()) {
062: protected Object getNextNode(final Iterator iter) {
063: while (iter.hasNext()) {
064: final Object next = iter.next();
065: if (next instanceof Attribute) {
066: return next;
067: } else {
068: final String value = (String) ((Node) next)
069: .attributes().get(
070: Attributes.this .attributeName);
071: if (value != null) {
072: return new Attribute(
073: Attributes.this .attributeName,
074: value,
075: new NodeChild(
076: (Node) next,
077: Attributes.this .parent.parent,
078: "",
079: Attributes.this .namespaceTagHints),
080: "",
081: Attributes.this .namespaceTagHints);
082: }
083: }
084: }
085: return null;
086: }
087: };
088: }
089:
090: public Iterator nodeIterator() {
091: return this .parent.nodeIterator();
092: }
093:
094: public GPathResult parents() {
095: return super .parents();
096: }
097:
098: public String text() {
099: final StringBuffer buf = new StringBuffer();
100: final Iterator iter = iterator();
101: while (iter.hasNext()) {
102: buf.append(iter.next());
103: }
104: return buf.toString();
105: }
106:
107: public List list() {
108: final Iterator iter = iterator();
109: final List result = new ArrayList();
110: while (iter.hasNext()) {
111: result.add(iter.next());
112: }
113: return result;
114: }
115:
116: public GPathResult findAll(final Closure closure) {
117: return new FilteredAttributes(this , closure,
118: this .namespaceTagHints);
119: }
120:
121: public Writer writeTo(final Writer out) throws IOException {
122: out.write(text());
123: return out;
124: }
125:
126: public void build(final GroovyObject builder) {
127: builder.getProperty("mkp");
128: builder.invokeMethod("yield", new Object[] { text() });
129: }
130: }
|