001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xml.impl;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.geotools.xml.InstanceComponent;
024: import org.geotools.xml.Node;
025:
026: public class NodeImpl implements Node {
027: private InstanceComponent component;
028: private Object value;
029: List children;
030: List attributes;
031:
032: public NodeImpl(InstanceComponent component) {
033: this .component = component;
034: children = new ArrayList();
035: attributes = new ArrayList();
036: }
037:
038: public NodeImpl(InstanceComponent component, Object value) {
039: this (component);
040: this .value = value;
041: }
042:
043: public InstanceComponent getComponent() {
044: return component;
045: }
046:
047: public Object getValue() {
048: return value;
049: }
050:
051: public void setValue(Object value) {
052: this .value = value;
053: }
054:
055: public boolean hasChild(String name) {
056: if (name == null) {
057: return false;
058: }
059:
060: for (int i = 0; i < children.size(); i++) {
061: Node child = (Node) children.get(i);
062:
063: if (name.equals(child.getComponent().getName())) {
064: return true;
065: }
066: }
067:
068: return false;
069: }
070:
071: public boolean hasChild(Class clazz) {
072: if (clazz == null) {
073: return false;
074: }
075:
076: for (int i = 0; i < children.size(); i++) {
077: Node child = (Node) children.get(i);
078:
079: if (child.getValue() == null) {
080: continue;
081: }
082:
083: if (clazz.isAssignableFrom(child.getValue().getClass())) {
084: return true;
085: }
086: }
087:
088: return false;
089: }
090:
091: /**
092: * Contents of this node.
093: * <p>
094: * XXX: either return unmodifeable Collection, or return the collection directly.
095: * Client code should make the copy iff they need it. Going to try changing it and see
096: * what breaks.
097: * </p>
098: * @see org.geotools.xml.Node#getChildren()
099: */
100: public List getChildren() {
101: return Collections.unmodifiableList(children);
102: }
103:
104: public int getNChildren() {
105: return children.size();
106: }
107:
108: public List getChildren(String name) {
109: ArrayList matches = new ArrayList();
110:
111: if (name == null) {
112: return matches;
113: }
114:
115: for (Iterator itr = children.iterator(); itr.hasNext();) {
116: Node child = (Node) itr.next();
117:
118: if (name.equals(child.getComponent().getName())) {
119: matches.add(child);
120: }
121: }
122:
123: return matches;
124: }
125:
126: public List getChildren(Class clazz) {
127: ArrayList matches = new ArrayList();
128:
129: if (clazz == null) {
130: return matches;
131: }
132:
133: for (Iterator itr = children.iterator(); itr.hasNext();) {
134: Node child = (Node) itr.next();
135:
136: if (child.getValue() == null) {
137: continue;
138: }
139:
140: if (clazz.isAssignableFrom(child.getValue().getClass())) {
141: matches.add(child);
142: }
143: }
144:
145: return matches;
146: }
147:
148: public Node getChild(String name) {
149: if (name == null) {
150: return null;
151: }
152:
153: for (Iterator itr = children.iterator(); itr.hasNext();) {
154: Node child = (Node) itr.next();
155:
156: if (name.equals(child.getComponent().getName())) {
157: return child;
158: }
159: }
160:
161: return null;
162: }
163:
164: public Node getChild(Class clazz) {
165: if (clazz == null) {
166: return null;
167: }
168:
169: for (Iterator itr = children.iterator(); itr.hasNext();) {
170: Node child = (Node) itr.next();
171:
172: if (child.getValue() == null) {
173: continue;
174: }
175:
176: if (clazz.isAssignableFrom(child.getValue().getClass())) {
177: return child;
178: }
179: }
180:
181: return null;
182: }
183:
184: public boolean hasAttribute(Class clazz) {
185: if (clazz == null) {
186: return false;
187: }
188:
189: for (Iterator itr = attributes.iterator(); itr.hasNext();) {
190: Node att = (Node) itr.next();
191:
192: if (att.getValue() == null) {
193: continue;
194: }
195:
196: if (clazz.isAssignableFrom(att.getValue().getClass())) {
197: return true;
198: }
199: }
200:
201: return false;
202: }
203:
204: public boolean hasAttribute(String name) {
205: if (name == null) {
206: return false;
207: }
208:
209: for (Iterator itr = attributes.iterator(); itr.hasNext();) {
210: Node att = (Node) itr.next();
211:
212: if (name.equals(att.getComponent().getName())) {
213: return true;
214: }
215: }
216:
217: return false;
218: }
219:
220: public List getAttributes() {
221: return new ArrayList(attributes);
222: }
223:
224: public List getAttributes(Class clazz) {
225: ArrayList matches = new ArrayList();
226:
227: if (clazz == null) {
228: return matches;
229: }
230:
231: for (Iterator a = attributes.iterator(); a.hasNext();) {
232: Node att = (Node) a.next();
233:
234: if (att.getValue() == null) {
235: continue;
236: }
237:
238: if (clazz.isAssignableFrom(att.getValue().getClass())) {
239: matches.add(att);
240: }
241: }
242:
243: return matches;
244: }
245:
246: public int getNAttributes() {
247: return attributes.size();
248: }
249:
250: public Node getAttribute(String name) {
251: if (name == null) {
252: return null;
253: }
254:
255: for (Iterator itr = attributes.iterator(); itr.hasNext();) {
256: Node att = (Node) itr.next();
257:
258: if (name.equals(att.getComponent().getName())) {
259: return att;
260: }
261: }
262:
263: return null;
264: }
265:
266: public Node getAttribute(Class clazz) {
267: if (clazz == null) {
268: return null;
269: }
270:
271: for (Iterator itr = attributes.iterator(); itr.hasNext();) {
272: Node att = (Node) itr.next();
273:
274: if (att.getValue() == null) {
275: continue;
276: }
277:
278: if (clazz.isAssignableFrom(att.getValue().getClass())) {
279: return att;
280: }
281: }
282:
283: return null;
284: }
285:
286: public Object getAttributeValue(String name) {
287: Node node = getAttribute(name);
288:
289: if (node != null) {
290: return node.getValue();
291: }
292:
293: return null;
294: }
295:
296: public Object getAttributeValue(Class clazz) {
297: if (clazz == null) {
298: return null;
299: }
300:
301: for (Iterator a = attributes.iterator(); a.hasNext();) {
302: Node att = (Node) a.next();
303:
304: if (att.getValue() == null) {
305: continue;
306: }
307:
308: if (clazz.isAssignableFrom(att.getValue().getClass())) {
309: return att.getValue();
310: }
311: }
312:
313: return null;
314: }
315:
316: public List getAttributeValues(Class clazz) {
317: ArrayList matches = new ArrayList();
318:
319: if (clazz == null) {
320: return matches;
321: }
322:
323: for (Iterator a = attributes.iterator(); a.hasNext();) {
324: Node att = (Node) a.next();
325:
326: if (att.getValue() == null) {
327: continue;
328: }
329:
330: if (clazz.isAssignableFrom(att.getValue().getClass())) {
331: matches.add(att.getValue());
332: }
333: }
334:
335: return matches;
336: }
337:
338: public String toString() {
339: return getComponent().getName() + "=" + getValue();
340: }
341:
342: public Object getChildValue(int index) {
343: return ((Node) children.get(index)).getValue();
344: }
345:
346: public Object getChildValue(String name) {
347: Node node = getChild(name);
348:
349: if (node != null) {
350: return node.getValue();
351: }
352:
353: return null;
354: }
355:
356: public Object getChildValue(Class clazz) {
357: Node node = getChild(clazz);
358:
359: if (node != null) {
360: return node.getValue();
361: }
362:
363: return null;
364: }
365:
366: public List getChildValues(String name) {
367: ArrayList matches = new ArrayList();
368:
369: if (name == null) {
370: return matches;
371: }
372:
373: for (Iterator itr = children.iterator(); itr.hasNext();) {
374: Node child = (Node) itr.next();
375:
376: if (name.equals(child.getComponent().getName())) {
377: matches.add(child.getValue());
378: }
379: }
380:
381: return matches;
382: }
383:
384: public List getChildValues(Class clazz) {
385: ArrayList matches = new ArrayList();
386:
387: if (clazz == null) {
388: return matches;
389: }
390:
391: for (Iterator itr = children.iterator(); itr.hasNext();) {
392: Node child = (Node) itr.next();
393: Object parsed = child.getValue();
394:
395: if (parsed == null) {
396: continue;
397: }
398:
399: if (clazz.isAssignableFrom(parsed.getClass())) {
400: matches.add(parsed);
401: }
402: }
403:
404: return matches;
405: }
406:
407: //additional methods, not part of public api
408: public void addChild(Node child) {
409: children.add(child);
410: }
411:
412: public Node removeChild(String name) {
413: Node child = getChild(name);
414: if (child != null) {
415: children.remove(child);
416: }
417:
418: return child;
419: }
420:
421: public void removeChild(Node child) {
422: children.remove(child);
423: }
424:
425: public void addAttribute(Node attribute) {
426: attributes.add(attribute);
427: }
428:
429: public Node removeAttribute(String name) {
430: Node attribute = getAttribute(name);
431: if (attribute != null) {
432: attributes.remove(attribute);
433: }
434:
435: return attribute;
436: }
437:
438: }
|