001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/filterencoding/AbstractFilter.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.model.filterencoding;
037    
038    import org.w3c.dom.Element;
039    import org.w3c.dom.Node;
040    import org.w3c.dom.NodeList;
041    
042    /**
043     * Abstract superclass representing <code>Filter</code> elements (as defined in the Filter DTD). A
044     * <code>Filter</code> element either consists of (one or more) FeatureId-elements or one
045     * operation-element. This is reflected in the two implementations FeatureFilter and ComplexFilter.
046     *
047     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
048     *
049     * @author last edited by: $Author: mschneider $
050     *
051     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
052     */
053    public abstract class AbstractFilter implements Filter {
054    
055        /**
056         * Given a DOM-fragment, a corresponding Filter-object is built. This method recursively calls
057         * other buildFromDOM () - methods to validate the structure of the DOM-fragment.
058         *
059         * @param element
060         * @return corresponding Filter-object
061         * @throws FilterConstructionException
062         *             if the structure of the DOM-fragment is invalid
063         * @deprecated use the 1.0.0 filter encoding aware method instead.
064         */
065        @Deprecated
066        public static Filter buildFromDOM( Element element )
067                                throws FilterConstructionException {
068            return buildFromDOM( element, false );
069        }
070    
071        /**
072         * Given a DOM-fragment, a corresponding Filter-object is built. This method recursively calls
073         * other buildFromDOM () - methods to validate the structure of the DOM-fragment.
074         *
075         * @param element
076         * @param useVersion_1_0_0
077         *            if the filter encoding 1_0_0 should be used.
078         * @return corresponding Filter-object
079         * @throws FilterConstructionException
080         *             if the structure of the DOM-fragment is invalid
081         */
082        public static Filter buildFromDOM( Element element, boolean useVersion_1_0_0 )
083                                throws FilterConstructionException {
084            Filter filter = null;
085    
086            // check if root element's name equals 'filter'
087            if ( !element.getLocalName().equals( "Filter" ) ) {
088                throw new FilterConstructionException( "Name of element does not equal 'Filter'!" );
089            }
090    
091            // determine type of Filter (FeatureFilter / ComplexFilter)
092            Element firstElement = null;
093            NodeList children = element.getChildNodes();
094            for ( int i = 0; i < children.getLength(); i++ ) {
095                if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
096                    firstElement = (Element) children.item( i );
097                    break;
098                }
099            }
100            if ( firstElement == null ) {
101                throw new FilterConstructionException( "Filter node is empty!" );
102            }
103    
104            if ( firstElement.getLocalName().equals( "FeatureId" ) ) {
105                // must be a FeatureFilter
106                FeatureFilter fFilter = new FeatureFilter();
107                children = element.getChildNodes();
108                for ( int i = 0; i < children.getLength(); i++ ) {
109                    if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
110                        Element fid = (Element) children.item( i );
111                        if ( !fid.getLocalName().equals( "FeatureId" ) )
112                            throw new FilterConstructionException( "Unexpected element encountered: " + fid.getLocalName() );
113                        fFilter.addFeatureId( FeatureId.buildFromDOM( fid ) );
114                    }
115                }
116                filter = fFilter;
117            } else if ( firstElement.getLocalName().equals( "GmlObjectId" ) ) {
118                // must be a FeatureFilter
119                FeatureFilter fFilter = new FeatureFilter();
120                children = element.getChildNodes();
121                for ( int i = 0; i < children.getLength(); i++ ) {
122                    if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
123                        Element fid = (Element) children.item( i );
124                        if ( !fid.getLocalName().equals( "GmlObjectId" ) )
125                            throw new FilterConstructionException( "Unexpected element encountered: " + fid.getLocalName() );
126                        fFilter.addFeatureId( FeatureId.buildGMLIdFromDOM( fid ) );
127                    }
128                }
129                filter = fFilter;
130            } else {
131                // must be a ComplexFilter
132                children = element.getChildNodes();
133                boolean justOne = false;
134                for ( int i = 0; i < children.getLength(); i++ ) {
135                    if ( children.item( i ).getNodeType() == Node.ELEMENT_NODE ) {
136                        Element operator = (Element) children.item( i );
137                        if ( justOne )
138                            throw new FilterConstructionException( "Unexpected element encountered: "
139                                                                   + operator.getLocalName() );
140                        ComplexFilter cFilter = new ComplexFilter( AbstractOperation.buildFromDOM( operator,
141                                                                                                   useVersion_1_0_0 ) );
142                        filter = cFilter;
143                        justOne = true;
144                    }
145                }
146            }
147            return filter;
148        }
149    }