01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.gml2;
17:
18: import org.eclipse.xsd.XSDElementDeclaration;
19: import org.eclipse.xsd.XSDParticle;
20: import java.util.ArrayList;
21: import java.util.Iterator;
22: import java.util.List;
23: import javax.xml.namespace.QName;
24: import org.geotools.feature.Feature;
25: import org.geotools.feature.FeatureCollection;
26: import org.geotools.feature.FeatureType;
27: import org.geotools.gml2.bindings.GML;
28: import org.geotools.xml.PropertyExtractor;
29: import org.geotools.xml.SchemaIndex;
30: import org.geotools.xml.Schemas;
31:
32: /**
33: * Special property extractor for extracting attributes from features.
34: *
35: * @author Justin Deoliveira, The Open Planning Project
36: *
37: */
38: public class FeaturePropertyExtractor implements PropertyExtractor {
39: SchemaIndex schemaIndex;
40:
41: public FeaturePropertyExtractor(SchemaIndex schemaIndex) {
42: this .schemaIndex = schemaIndex;
43: }
44:
45: public boolean canHandle(Object object) {
46: return object instanceof Feature
47: && !(object instanceof FeatureCollection);
48: }
49:
50: public List properties(Object object, XSDElementDeclaration element) {
51: Feature feature = (Feature) object;
52: FeatureType featureType = feature.getFeatureType();
53:
54: String namespace = featureType.getNamespace().toString();
55: String typeName = featureType.getTypeName();
56:
57: //find the type in the schema
58: element = schemaIndex.getElementDeclaration(new QName(
59: namespace, typeName));
60:
61: List particles = Schemas.getChildElementParticles(element
62: .getType(), true);
63: List properties = new ArrayList();
64:
65: for (Iterator p = particles.iterator(); p.hasNext();) {
66: XSDParticle particle = (XSDParticle) p.next();
67: XSDElementDeclaration attribute = (XSDElementDeclaration) particle
68: .getContent();
69:
70: if (attribute.isElementDeclarationReference()) {
71: attribute = attribute.getResolvedElementDeclaration();
72: }
73:
74: //ignore gml attributes
75: if (GML.NAMESPACE.equals(attribute.getTargetNamespace())) {
76: continue;
77: }
78:
79: //make sure the feature type has an element
80: if (featureType.getAttributeType(attribute.getName()) == null) {
81: continue;
82: }
83:
84: Object attributeValue = feature.getAttribute(attribute
85: .getName());
86: properties.add(new Object[] { particle, attributeValue });
87: }
88:
89: return properties;
90: }
91: }
|