/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.kml.bindings;
import java.net.URI;
import javax.xml.namespace.QName;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.kml.KML;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.xml.AbstractComplexBinding;
import org.geotools.xml.ElementInstance;
import org.geotools.xml.Node;
/**
* Binding object for the type http://earth.google.com/kml/2.1:FeatureType.
*
* <p>
* <pre>
* <code>
* <complexType abstract="true" name="FeatureType">
* <complexContent>
* <extension base="kml:ObjectType">
* <sequence>
* <element minOccurs="0" name="name" type="string"/>
* <element default="1" minOccurs="0" name="visibility" type="boolean"/>
* <element default="1" minOccurs="0" name="open" type="boolean"/>
* <element minOccurs="0" name="address" type="string"/>
* <element minOccurs="0" name="phoneNumber" type="string"/>
* <element minOccurs="0" name="Snippet" type="kml:SnippetType"/>
* <element minOccurs="0" name="description" type="string"/>
* <element minOccurs="0" ref="kml:LookAt"/>
* <element minOccurs="0" ref="kml:TimePrimitive"/>
* <element minOccurs="0" ref="kml:styleUrl"/>
* <element maxOccurs="unbounded" minOccurs="0" ref="kml:StyleSelector"/>
* <element minOccurs="0" ref="kml:Region"/>
* <element minOccurs="0" name="Metadata" type="kml:MetadataType"/>
* </sequence>
* </extension>
* </complexContent>
* </complexType>
*
* </code>
* </pre>
* </p>
*
* @generated
*
*
* @source $URL$
*/
public class FeatureTypeBinding extends AbstractComplexBinding {
/**
* base feature type for kml features
*/
protected static final SimpleFeatureType featureType;
static {
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
tb.setNamespaceURI(KML.NAMESPACE);
tb.setName("feature");
//<element minOccurs="0" name="name" type="string"/>
tb.add("name", String.class);
//<element default="1" minOccurs="0" name="visibility" type="boolean"/>
tb.add("visibility", Boolean.class);
//<element default="1" minOccurs="0" name="open" type="boolean"/>
tb.add("open", Boolean.class);
//<element minOccurs="0" name="address" type="string"/>
tb.add("address", String.class);
//<element minOccurs="0" name="phoneNumber" type="string"/>
tb.add("phoneNumber", String.class);
//<element minOccurs="0" name="Snippet" type="kml:SnippetType"/>
//tb.add("Snippet",String.class):
//<element minOccurs="0" name="description" type="string"/>
tb.add("description", String.class);
//<element minOccurs="0" ref="kml:LookAt"/>
tb.add("LookAt", Coordinate.class);
//<element minOccurs="0" ref="kml:TimePrimitive"/>
//tb.add("TimePrimitive", ...);
//<element minOccurs="0" ref="kml:styleUrl"/>
tb.add("Style", FeatureTypeStyle.class);
//<element maxOccurs="unbounded" minOccurs="0" ref="kml:StyleSelector"/>
//<element minOccurs="0" ref="kml:Region"/>
tb.add("Region", Envelope.class);
featureType = tb.buildFeatureType();
}
StyleMap styleMap;
public FeatureTypeBinding(StyleMap styleMap) {
this.styleMap = styleMap;
}
/**
* @generated
*/
public QName getTarget() {
return KML.FeatureType;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated modifiable
*/
public Class getType() {
return SimpleFeature.class;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
*
* @generated modifiable
*/
public Object parse(ElementInstance instance, Node node, Object value)
throws Exception {
SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureType);
//<element minOccurs="0" name="name" type="string"/>
b.set("name", node.getChildValue("name"));
//<element default="1" minOccurs="0" name="visibility" type="boolean"/>
b.set("visibility", node.getChildValue("visibility", Boolean.TRUE));
//<element default="1" minOccurs="0" name="open" type="boolean"/>
b.set("open", node.getChildValue("open", Boolean.TRUE));
//<element minOccurs="0" name="address" type="string"/>
b.set("address", node.getChildValue("address"));
//<element minOccurs="0" name="phoneNumber" type="string"/>
b.set("phoneNumber", node.getChildValue("phoneNumber"));
//<element minOccurs="0" name="Snippet" type="kml:SnippetType"/>
//tb.add("Snippet",String.class):
//<element minOccurs="0" name="description" type="string"/>
b.set("description", node.getChildValue("description"));
//<element minOccurs="0" ref="kml:LookAt"/>
b.set("LookAt", node.getChildValue("LookAt"));
//<element minOccurs="0" ref="kml:TimePrimitive"/>
//tb.add("TimePrimitive", ...);
//<element minOccurs="0" ref="kml:styleUrl"/>
URI uri = (URI) node.getChildValue("styleUrl");
if (uri != null) {
//load the style from the style map
//TODO: use a proxy to do forward referencing
b.set("Style", styleMap.get(uri));
}
//<element maxOccurs="unbounded" minOccurs="0" ref="kml:StyleSelector"/>
//<element minOccurs="0" ref="kml:Region"/>
b.set("Region", node.getChildValue("Region"));
//<element minOccurs="0" name="Metadata" type="kml:MetadataType"/>
return b.buildFeature((String) node.getAttributeValue("id"));
}
public Object getProperty(Object object, QName name) throws Exception {
if( object instanceof FeatureCollection){
FeatureCollection features = (FeatureCollection) object;
if ( "id".equals( name.getLocalPart() ) ) {
return features.getID();
}
}
if( object instanceof SimpleFeature){
SimpleFeature feature = (SimpleFeature) object;
if ( "id".equals( name.getLocalPart() ) ) {
return feature.getID();
}
//<element minOccurs="0" name="name" type="string"/>
if ( "name".equals( name.getLocalPart() ) ) {
return feature.getAttribute( "name" );
}
//<element minOccurs="0" name="description" type="string"/>
if ( "description".equals( name.getLocalPart() ) ) {
return feature.getAttribute( "description" );
}
if ( KML.styleUrl.equals( name ) ) {
URI uri = (URI) feature.getAttribute( "Style" );
if ( uri != null ) {
return styleMap.get( uri );
}
}
//<element default="1" minOccurs="0" name="visibility" type="boolean"/>
//<element default="1" minOccurs="0" name="open" type="boolean"/>
//<element minOccurs="0" name="address" type="string"/>
//<element minOccurs="0" name="phoneNumber" type="string"/>
//<element minOccurs="0" name="Snippet" type="kml:SnippetType"/>
//<element minOccurs="0" name="description" type="string"/>
//<element minOccurs="0" ref="kml:LookAt"/>
//<element minOccurs="0" ref="kml:TimePrimitive"/>
//<element minOccurs="0" ref="kml:styleUrl"/>
//<element maxOccurs="unbounded" minOccurs="0" ref="kml:StyleSelector"/>
//<element minOccurs="0" ref="kml:Region"/>
//<element minOccurs="0" name="Metadata" type="kml:MetadataType"/>
}
return super.getProperty(object, name);
}
}