/* * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org * * (C) 2004-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.feature; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import org.geotools.factory.CommonFactoryFinder; import org.geotools.factory.Hints; import org.geotools.feature.type.AttributeDescriptorImpl; import org.geotools.feature.type.AttributeTypeImpl; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.feature.type.AttributeDescriptor; import org.opengis.feature.type.FeatureType; import org.opengis.filter.Filter; import org.opengis.filter.FilterFactory; import org.opengis.filter.PropertyIsNull; /** * This class contains utility methods focused on the schema represented by * the FeatureType data model. *

* These methods are often used for implementation the convience methods * such as FeatureType.getAttributeCount(), although they may be used directly * with any FeatureType. *

*

* These schema methods are based on the *complete* picture indicated by a FeatureType * and its ancestors. Many of these methods are focused on the derivation of AttribtueTypes * during an override. *

* @see FeatureTypes * @see FeatureType * @author Jody Garnett * @since 2.1.0 * @deprecated This helper class was for the old feature model; please use FeatureTypes * * * @source $URL$ */ public class Schema { private static Schema DEFAULT = new Schema(); private FilterFactory ff; public Schema(){ this( (Hints) null ); } public Schema( Hints hints ){ this( CommonFactoryFinder.getFilterFactory( hints )); } public Schema( FilterFactory filterFactory ){ ff = filterFactory; } /** * Walk the provided FeatureType and produce a count of distinct attribtues. *

* used to detect duplicate attributes names (ie override) *

* * @param featureType */ public int getAttributeCount( SimpleFeatureType featureType ) { return getNames( featureType ).size(); } /** * Does a quick walk to detect only a list of attribute names. *

* This method does not produce the complete schema (ie derrived restrictions based * on attribute facets). It is only used to get a list of the unique attribtues in * the resulting schema. *

* @param featureType * * @return Set of unique attribute names */ public List getNames( SimpleFeatureType featureType ) { return getNames( featureType, new ArrayList() ); } /** * This order is to be respected, based on Ancestors and so on. *

* This method is "faster" then actually constructing the merged * AttribtueTypes. *

*/ public List getNames( SimpleFeatureType featureType, List names ){ if( featureType == null || featureType.getAttributeDescriptors() == null ){ return names; } List ancestors = FeatureTypes.getAncestors(featureType); if( ancestors != null && !ancestors.isEmpty() ){ for( int i=0, length = ancestors.size(); i * This method is "faster" then actually constructing the merged * AttribtueTypes. *

*/ public List getAttributes( SimpleFeatureType featureType, List list ){ if( featureType == null || featureType.getAttributeDescriptors() == null ) { return list; } List ancestors = FeatureTypes.getAncestors(featureType); if( ancestors != null && !ancestors.isEmpty()){ for( int i=0, length = ancestors.size(); i * AttributeType needs a xpath based access *

* @param type * @param xpath * */ public AttributeDescriptor getXPath( SimpleFeatureType type, String xpath) { return getAttribute( type, xpath ); // for now, use JXPath later } // Utility Methods // private int getIndexOf( List attributes, String name ){ int index = 0; for( Iterator i=attributes.iterator(); i.hasNext(); index++){ AttributeDescriptor type = (AttributeDescriptor) i.next(); if( name.equals( type.getLocalName() )) return index; } return -1; } private AttributeDescriptor override(AttributeDescriptor type, AttributeDescriptor override ){ int max = override.getMaxOccurs(); if( max < 0 ) max = type.getMinOccurs(); int min = override.getMinOccurs(); if( min < 0 ) min = type.getMinOccurs(); String name = override.getLocalName(); if( name == null ) name = type.getLocalName(); List restrictions = override( type.getType().getRestrictions(), override.getType().getRestrictions() ); Class javaType = override.getType().getBinding(); if( javaType == null ) javaType = type.getType().getBinding(); boolean isNilable = override.isNillable(); Object defaultValue = override.getDefaultValue(); if( defaultValue == null ) defaultValue = type.getDefaultValue(); // WARNING cannot copy metadata! return new AttributeDescriptorImpl( new AttributeTypeImpl( new NameImpl( name ), javaType, false, false, restrictions, null, null ), new NameImpl( name ), min, max, isNilable, defaultValue ); } private List restriction( SimpleFeatureType featureType, String name, List filters ){ List ancestors = FeatureTypes.getAncestors(featureType); if( ancestors != null && !ancestors.isEmpty()){ for( int i=0, length = ancestors.size(); i * used to detect duplicate attributes names (ie override) *

* * @param featureType */ public static int attributeCount( SimpleFeatureType featureType ){ return DEFAULT.getAttributeCount(featureType); } /** * @deprecated use getAttribute( type, index ) */ public static AttributeDescriptor attribute( SimpleFeatureType type, int index ) { return DEFAULT.getAttribute(type, index); } /** @deprecated use getAttribute( type, name ) */ public static AttributeDescriptor attribute( SimpleFeatureType type, String name ){ return DEFAULT.getAttribute(type, name ); } /** @deprecated use getAttributes( featureType ) */ public static List attributes( SimpleFeatureType featureType ){ return DEFAULT.getAttributes(featureType); } /** @deprecated use getAttributes( featureType, list ) */ public static List attributes( SimpleFeatureType featureType, List list ){ return DEFAULT.getAttributes(featureType, list); } /** * @deprecated please use getIndexOf( type, name ) */ public static int find( SimpleFeatureType type, String name ) { return DEFAULT.getIndexOf(type, name); } /** * @deprecated use getNames( featureType ) */ public static List names( SimpleFeatureType featureType ) { return DEFAULT.getNames(featureType); } /** * @deprecated use getNames( featureType, List ) */ public static List names( SimpleFeatureType featureType, List names ){ return DEFAULT.getNames( featureType, names ); } /** * @deprecated please use getRestriction( featureType, name ) */ public static Filter restriction( SimpleFeatureType featureType, String name ){ return DEFAULT.getRestrictions(featureType, name); } /** * @deprecated use getXPath( type, xpath ); */ public static AttributeDescriptor xpath( SimpleFeatureType type, String xpath) { return DEFAULT.getAttribute( type, xpath ); // for now, use JXPath later } }