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:
018: package groovy.util.slurpersupport;
019:
020: import groovy.lang.Closure;
021: import groovy.lang.GroovyObject;
022: import groovy.lang.GroovyRuntimeException;
023:
024: import java.io.IOException;
025: import java.io.Writer;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
030:
031: /**
032: * @author John Wilson
033: */
034:
035: public class NodeChild extends GPathResult {
036: private final Node node;
037:
038: public NodeChild(final Node node, final GPathResult parent,
039: final String namespacePrefix, final Map namespaceTagHints) {
040: super (parent, node.name(), namespacePrefix, namespaceTagHints);
041: this .node = node;
042: }
043:
044: public NodeChild(final Node node, final GPathResult parent,
045: final Map namespaceTagHints) {
046: this (node, parent, "*", namespaceTagHints);
047: }
048:
049: public int size() {
050: return 1;
051: }
052:
053: public String text() {
054: return this .node.text();
055: }
056:
057: public GPathResult parents() {
058: // TODO Auto-generated method stub
059: throw new GroovyRuntimeException(
060: "parents() not implemented yet");
061: }
062:
063: public Iterator iterator() {
064: return new Iterator() {
065: private boolean hasNext = true;
066:
067: public boolean hasNext() {
068: return this .hasNext;
069: }
070:
071: public Object next() {
072: try {
073: return (this .hasNext) ? NodeChild.this : null;
074: } finally {
075: this .hasNext = false;
076: }
077: }
078:
079: public void remove() {
080: throw new UnsupportedOperationException();
081: }
082: };
083: }
084:
085: public Iterator nodeIterator() {
086: return new Iterator() {
087: private boolean hasNext = true;
088:
089: public boolean hasNext() {
090: return this .hasNext;
091: }
092:
093: public Object next() {
094: try {
095: return (this .hasNext) ? NodeChild.this .node : null;
096: } finally {
097: this .hasNext = false;
098: }
099: }
100:
101: public void remove() {
102: throw new UnsupportedOperationException();
103: }
104: };
105: }
106:
107: public Object getAt(final int index) {
108: if (index == 0) {
109: return node;
110: } else {
111: throw new ArrayIndexOutOfBoundsException(index);
112: }
113: }
114:
115: public Map attributes() {
116: return this .node.attributes();
117: }
118:
119: public Iterator childNodes() {
120: return this .node.childNodes();
121: }
122:
123: public GPathResult find(final Closure closure) {
124: if (DefaultTypeTransformation.castToBoolean(closure
125: .call(new Object[] { this .node }))) {
126: return this ;
127: } else {
128: return new NoChildren(this , "", this .namespaceTagHints);
129: }
130: }
131:
132: public GPathResult findAll(final Closure closure) {
133: return find(closure);
134: }
135:
136: public void build(final GroovyObject builder) {
137: this .node.build(builder, this .namespaceMap,
138: this .namespaceTagHints);
139: }
140:
141: public Writer writeTo(final Writer out) throws IOException {
142: return this .node.writeTo(out);
143: }
144:
145: protected void replaceNode(final Closure newValue) {
146: this .node.replaceNode(newValue, this );
147: }
148:
149: protected void replaceBody(final Object newValue) {
150: this .node.replaceBody(newValue);
151: }
152:
153: protected void appendNode(final Object newValue) {
154: this.node.appendNode(newValue, this);
155: }
156: }
|