001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/graphics/sld/FeatureTypeConstraint.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.graphics.sld;
037    
038    import static org.deegree.framework.xml.XMLTools.escape;
039    
040    import java.util.ArrayList;
041    import java.util.List;
042    
043    import org.deegree.datatypes.QualifiedName;
044    import org.deegree.framework.xml.Marshallable;
045    import org.deegree.model.filterencoding.Filter;
046    
047    /**
048     * A FeatureTypeConstraint element is used to identify a feature type by well-known name, using the FeatureTypeName
049     * element.
050     *
051     * @author <a href="mailto:k.lupp@web.de">Katharina Lupp </a>
052     * @version $Revision: 18195 $ $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
053     */
054    
055    public class FeatureTypeConstraint implements Marshallable {
056    
057        private List<Extent> extents = null;
058    
059        private Filter filter = null;
060    
061        private QualifiedName featureTypeName = null;
062    
063        /**
064         * constructor initializing the class with the <FeatureTypeConstraint>
065         *
066         * @param featureTypeName
067         * @param filter
068         * @param extents
069         */
070        public FeatureTypeConstraint( QualifiedName featureTypeName, Filter filter, Extent[] extents ) {
071            if ( extents != null ) {
072                this.extents = new ArrayList<Extent>( extents.length );
073                setExtents( extents );
074            }
075            setFeatureTypeName( featureTypeName );
076            setFilter( filter );
077    
078        }
079    
080        /**
081         * returns the name of the feature type
082         *
083         * @return the name of the feature type
084         */
085        public QualifiedName getFeatureTypeName() {
086            return featureTypeName;
087        }
088    
089        /**
090         * sets the name of the feature type
091         *
092         * @param featureTypeName
093         *            the name of the feature type
094         */
095        public void setFeatureTypeName( QualifiedName featureTypeName ) {
096            this.featureTypeName = featureTypeName;
097        }
098    
099        /**
100         * returns a feature-filter as defined in WFS specifications.
101         *
102         * @return the filter of the FeatureTypeConstraints
103         */
104        public Filter getFilter() {
105            return filter;
106        }
107    
108        /**
109         * sets a feature-filter as defined in WFS specifications.
110         *
111         * @param filter
112         *            the filter of the FeatureTypeConstraints
113         */
114        public void setFilter( Filter filter ) {
115            this.filter = filter;
116        }
117    
118        /**
119         * returns the extent for filtering the feature type
120         *
121         * @return the extent for filtering the feature type
122         */
123        public Extent[] getExtents() {
124            if ( this.extents != null ) {
125                return extents.toArray( new Extent[extents.size()] );
126            }
127            return new Extent[0];
128        }
129    
130        /**
131         * sets the extent for filtering the feature type
132         *
133         * @param extents
134         *            extents for filtering the feature type
135         */
136        public void setExtents( Extent[] extents ) {
137            if ( this.extents != null ) {
138                this.extents.clear();
139            }
140    
141            if ( extents != null ) {
142                for ( int i = 0; i < extents.length; i++ ) {
143                    addExtent( extents[i] );
144                }
145            }
146        }
147    
148        /**
149         * Adds an Extent to the Extent-List of a FeatureTypeConstraint
150         *
151         * @param extent
152         *            an extent to add
153         */
154        public void addExtent( Extent extent ) {
155            if ( this.extents != null ) {
156                extents = new ArrayList<Extent>();
157            }
158            extents.add( extent );
159        }
160    
161        /**
162         * Removes an Extent from the Extent-List of a FeatureTypeConstraint
163         *
164         * @param extent
165         *            an extent to remove
166         */
167        public void removeExtent( Extent extent ) {
168            extents.remove( extents.indexOf( extent ) );
169        }
170    
171        /**
172         * @return the FeatureTypeConstraint as String
173         */
174        @Override
175        public String toString() {
176            String ret = getClass().getName() + "\n";
177            ret = "featureTypeName = " + featureTypeName + "\n";
178            ret += ( "filter = " + filter + "\n" );
179            ret += ( "extents = " + extents + "\n" );
180    
181            return ret;
182        }
183    
184        /**
185         * exports the content of the FeatureTypeConstraint as XML formated String
186         *
187         * @return xml representation of the FeatureTypeConstraint
188         */
189        public String exportAsXML() {
190    
191            StringBuffer sb = new StringBuffer( 1000 );
192            sb.append( "<FeatureTypeConstraint>" );
193            if ( featureTypeName != null ) {
194                if ( featureTypeName.getNamespace() == null ) {
195                    sb.append( "<FeatureTypeName>" );
196                    sb.append( escape( featureTypeName.getLocalName() ) );
197                } else {
198                    sb.append( "<FeatureTypeName xmlns:" ).append( featureTypeName.getPrefix() );
199                    sb.append( "='" ).append( featureTypeName.getNamespace().toASCIIString() ).append( "'>" );
200                    sb.append( featureTypeName.getPrefixedName() );
201                }
202                sb.append( "</FeatureTypeName>" );
203            }
204            if ( filter != null ) {
205                sb.append( filter.to110XML() );
206            }
207            if ( extents != null ) {
208                for ( int i = 0; i < extents.size(); i++ ) {
209                    sb.append( ( (Marshallable) extents.get( i ) ).exportAsXML() );
210                }
211            }
212            sb.append( "</FeatureTypeConstraint>" );
213    
214            return sb.toString();
215        }
216    
217    }