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.gml3.bindings;
017:
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import javax.xml.namespace.QName;
021: import com.vividsolutions.jts.geom.Envelope;
022: import org.opengis.referencing.crs.CoordinateReferenceSystem;
023: import org.geotools.feature.Feature;
024: import org.geotools.feature.FeatureType;
025: import org.geotools.geometry.jts.ReferencedEnvelope;
026: import org.geotools.gml2.FeatureTypeCache;
027: import org.geotools.xml.AbstractComplexBinding;
028: import org.geotools.xml.BindingWalkerFactory;
029: import org.geotools.xml.ElementInstance;
030: import org.geotools.xml.Node;
031:
032: /**
033: * Binding object for the type http://www.opengis.net/gml:AbstractFeatureType.
034: *
035: * <p>
036: * <pre>
037: * <code>
038: * <complexType abstract="true" name="AbstractFeatureType">
039: * <annotation>
040: * <documentation>An abstract feature provides a set of common properties, including id, metaDataProperty, name and description inherited from AbstractGMLType, plus boundedBy. A concrete feature type must derive from this type and specify additional properties in an application schema. A feature must possess an identifying attribute ('id' - 'fid' has been deprecated).</documentation>
041: * </annotation>
042: * <complexContent>
043: * <extension base="gml:AbstractGMLType">
044: * <sequence>
045: * <element minOccurs="0" ref="gml:boundedBy"/>
046: * <element minOccurs="0" ref="gml:location">
047: * <annotation>
048: * <appinfo>deprecated</appinfo>
049: * <documentation>deprecated in GML version 3.1</documentation>
050: * </annotation>
051: * </element>
052: * <!-- additional properties must be specified in an application schema -->
053: * </sequence>
054: * </extension>
055: * </complexContent>
056: * </complexType>
057: *
058: * </code>
059: * </pre>
060: * </p>
061: *
062: * @generated
063: */
064: public class AbstractFeatureTypeBinding extends AbstractComplexBinding {
065: FeatureTypeCache ftCache;
066: BindingWalkerFactory bwFactory;
067:
068: public AbstractFeatureTypeBinding(FeatureTypeCache ftCache,
069: BindingWalkerFactory bwFactory) {
070: this .ftCache = ftCache;
071: this .bwFactory = bwFactory;
072: }
073:
074: /**
075: * @generated
076: */
077: public QName getTarget() {
078: return GML.AbstractFeatureType;
079: }
080:
081: /**
082: * <!-- begin-user-doc -->
083: * <!-- end-user-doc -->
084: *
085: * @generated modifiable
086: */
087: public Class getType() {
088: return Feature.class;
089: }
090:
091: /**
092: * <!-- begin-user-doc -->
093: * <!-- end-user-doc -->
094: *
095: * @generated modifiable
096: */
097: public Object parse(ElementInstance instance, Node node,
098: Object value) throws Exception {
099: return GML3ParsingUtils.parseFeature(instance, node, value,
100: ftCache, bwFactory);
101:
102: // //get the definition of the element
103: // XSDElementDeclaration decl = instance.getElementDeclaration();
104: //
105: // //look for a feature type in the cache
106: // FeatureType fType = ftCache.get(decl.getName());
107: //
108: // if (fType == null) {
109: // fType = GML3ParsingUtils.featureType(decl, bwFactory);
110: // ftCache.put(fType);
111: // }
112: //
113: // //TODO: this could pick up wrong thing, node api needs to be
114: // // namespace aware
115: // String fid = (String) node.getAttributeValue("id");
116: //
117: // return GML3ParsingUtils.feature(fType, fid, node);
118: }
119:
120: public Element encode(Object object, Document document,
121: Element value) throws Exception {
122: Feature feature = (Feature) object;
123: FeatureType featureType = feature.getFeatureType();
124:
125: String namespace = featureType.getNamespace().toString();
126: String typeName = featureType.getTypeName();
127:
128: Element encoding = document
129: .createElementNS(namespace, typeName);
130: encoding.setAttributeNS(GML.NAMESPACE, "id", feature.getID());
131:
132: return encoding;
133: }
134:
135: public Object getProperty(Object object, QName name)
136: throws Exception {
137: //JD: here we only handle the "GML" attributes, all the application
138: // schema attributes are handled by FeaturePropertyExtractor
139:
140: //JD: TODO: handle all properties here and kill FeautrePropertyExtractor
141: Feature feature = (Feature) object;
142:
143: if (GML.name.equals(name)) {
144: return feature.getAttribute("name");
145: }
146:
147: if (GML.description.equals(name)) {
148: return feature.getAttribute("description");
149: }
150:
151: if (GML.location.equals(name)) {
152: return feature.getAttribute("location");
153: }
154:
155: if (GML.boundedBy.equals(name)) {
156: Envelope bounds = feature.getBounds();
157:
158: if (bounds instanceof ReferencedEnvelope) {
159: return bounds;
160: }
161:
162: CoordinateReferenceSystem crs = (feature.getFeatureType()
163: .getDefaultGeometry() != null) ? feature
164: .getFeatureType().getDefaultGeometry()
165: .getCoordinateSystem() : null;
166:
167: return new ReferencedEnvelope(bounds, crs);
168: }
169:
170: return null;
171: }
172: }
|