01: package org.geotools.feature.iso.xpath;
02:
03: import java.util.ArrayList;
04: import java.util.Collection;
05: import java.util.Collections;
06: import java.util.List;
07:
08: import org.apache.commons.jxpath.ri.QName;
09: import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
10: import org.apache.commons.jxpath.ri.model.NodeIterator;
11: import org.apache.commons.jxpath.ri.model.NodePointer;
12: import org.geotools.feature.iso.Types;
13: import org.geotools.resources.Utilities;
14: import org.opengis.feature.Attribute;
15: import org.opengis.feature.ComplexAttribute;
16: import org.opengis.feature.type.AttributeDescriptor;
17: import org.opengis.feature.type.Name;
18:
19: /**
20: * Special node iterator for {@link Attribute}.
21: *
22: * @author Justin Deoliveira, The Open Planning Project
23: * @author Gabriel Roldan, Axios Engineering
24: *
25: */
26: public class AttributeNodeIterator implements NodeIterator {
27:
28: /**
29: * The feature node pointer
30: */
31: AttributeNodePointer pointer;
32: /**
33: * The feature.
34: */
35: ComplexAttribute feature;
36:
37: List children;
38:
39: /**
40: * current position
41: */
42: int position;
43:
44: public AttributeNodeIterator(AttributeNodePointer pointer) {
45: this .pointer = pointer;
46: feature = (ComplexAttribute) pointer.getImmediateNode();
47: children = new ArrayList((Collection) feature.getValue());
48: position = 1;
49: }
50:
51: public AttributeNodeIterator(AttributeNodePointer pointer,
52: NodeNameTest filter) {
53: this .pointer = pointer;
54: feature = (ComplexAttribute) pointer.getImmediateNode();
55:
56: String localName = filter.getNodeName().getName();
57: String nameSpace = filter.getNamespaceURI();
58:
59: AttributeDescriptor descriptor = feature.getDescriptor();
60: Name attName = descriptor == null ? feature.getType().getName()
61: : descriptor.getName();
62: if (Utilities.equals(nameSpace, attName.getNamespaceURI())
63: && Utilities.equals(localName, attName.getLocalPart())) {
64: children = Collections.singletonList(feature);
65: } else {
66: Name name = Types.typeName(nameSpace, localName);
67: children = feature.get(name);
68: }
69:
70: position = children.size() > 0 ? 1 : 0;
71: }
72:
73: public int getPosition() {
74: return position;
75: }
76:
77: public boolean setPosition(int position) {
78: this .position = position;
79: return position <= children.size();
80: }
81:
82: public NodePointer getNodePointer() {
83: Attribute attribute = (Attribute) children.get(position - 1);
84: Name name = attribute.getDescriptor().getName();
85: QName qname = new QName(name.getNamespaceURI(), name
86: .getLocalPart());
87: return new AttributeNodePointer(pointer, attribute, qname);
88: }
89:
90: }
|