001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/filterencoding/PropertyIsNullOperation.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.deegree.framework.xml.ElementList;
039    import org.deegree.framework.xml.XMLParsingException;
040    import org.deegree.framework.xml.XMLTools;
041    import org.deegree.model.feature.Feature;
042    import org.deegree.ogcbase.CommonNamespaces;
043    import org.deegree.ogcbase.OGCDocument;
044    import org.deegree.ogcbase.PropertyPath;
045    import org.w3c.dom.Element;
046    import org.w3c.dom.Text;
047    
048    /**
049     * Encapsulates the information of a <PropertyIsNull>-element (as defined in Filter DTD). The DTD defines the
050     * properties type to be tested as PropertyName or Literal.
051     *
052     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
053     * @author last edited by: $Author: mschneider $
054     *
055     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
056     */
057    public class PropertyIsNullOperation extends ComparisonOperation {
058    
059        private PropertyName propertyName;
060    
061        /**
062         * @param propertyName
063         *            to check for
064         */
065        public PropertyIsNullOperation( PropertyName propertyName ) {
066            super( OperationDefines.PROPERTYISNULL );
067            this.propertyName = propertyName;
068        }
069    
070        /**
071         * @return the property name
072         */
073        public PropertyName getPropertyName() {
074            return this.propertyName;
075        }
076    
077        /**
078         * Given a DOM-fragment, a corresponding Operation-object is built. This method recursively calls other buildFromDOM
079         * () - methods to validate the structure of the DOM-fragment.
080         *
081         * @param element
082         *            to build the bean from
083         * @return a Bean of the DOM
084         *
085         * @throws FilterConstructionException
086         *             if the structure of the DOM-fragment is invalid
087         * @deprecated use the 1.0.0 filter encoding aware method instead.
088         */
089        @Deprecated
090        public static Operation buildFromDOM( Element element )
091                                throws FilterConstructionException {
092            return buildFromDOM( element, false );
093        }
094    
095        /**
096         * Given a DOM-fragment, a corresponding Operation-object is built. This method recursively calls other buildFromDOM
097         * () - methods to validate the structure of the DOM-fragment.
098         *
099         * @param element
100         *            to build the bean from
101         * @param useVersion_1_0_0
102         *            if the filterencoding 1.0.0 should be used.
103         * @return a Bean of the DOM
104         *
105         * @throws FilterConstructionException
106         *             if the structure of the DOM-fragment is invalid
107         */
108        public static Operation buildFromDOM( Element element, boolean useVersion_1_0_0 )
109                                throws FilterConstructionException {
110    
111            // check if root element's name equals 'PropertyIsNull'
112            if ( !element.getLocalName().equals( "PropertyIsNull" ) )
113                throw new FilterConstructionException( "Name of element does not equal 'PropertyIsNull'!" );
114    
115            ElementList children = XMLTools.getChildElements( element );
116            if ( children.getLength() != 1 )
117                throw new FilterConstructionException( "'PropertyIsNull' requires exactly 1 element!" );
118    
119            Element child = children.item( 0 );
120            PropertyName propertyName = null;
121    
122            switch ( ExpressionDefines.getIdByName( child.getLocalName() ) ) {
123            case ExpressionDefines.PROPERTYNAME: {
124                propertyName = (PropertyName) PropertyName.buildFromDOM( child );
125                break;
126            }
127            case ExpressionDefines.LITERAL: {
128                if ( useVersion_1_0_0 ) {
129    
130                    try {
131                        Text node = (Text) XMLTools.getRequiredNode( child, "text()",
132                                                                     CommonNamespaces.getNamespaceContext() );
133                        PropertyPath propertyPath = OGCDocument.parsePropertyPath( node );
134                        propertyName = new PropertyName( propertyPath );
135                    } catch ( XMLParsingException e ) {
136                        throw new FilterConstructionException(
137                                                               "The literal "
138                                                                                       + child.getTextContent()
139                                                                                       + " in the PropertyIsNull operation Element could not be transformed to a 'PropertyName'!" );
140                    }
141                    break;
142                }
143                // slip through if useVersion_1_0_0 is false
144            }
145            default: {
146                throw new FilterConstructionException(
147                                                       "PropertyIsNull operation Element does not contain a 'PropertyName'!" );
148            }
149            }
150    
151            return new PropertyIsNullOperation( propertyName );
152        }
153    
154        public StringBuffer toXML() {
155            StringBuffer sb = new StringBuffer( 500 );
156            sb.append( "<ogc:" ).append( getOperatorName() ).append( ">" );
157            sb.append( propertyName.toXML() );
158            sb.append( "</ogc:" ).append( getOperatorName() ).append( ">" );
159            return sb;
160        }
161    
162        public StringBuffer to100XML() {
163            return toXML();
164        }
165    
166        public StringBuffer to110XML() {
167            return toXML();
168        }
169    
170        /**
171         * Calculates the <tt>PropertyIsNull</tt> -Operation's logical value based on the certain property values of the
172         * given <tt>Feature</tt>.
173         *
174         * @param feature
175         *            that determines the property values
176         * @return true, if the <tt>PropertyIsNull</tt> -Operation evaluates to true, else false
177         * @throws FilterEvaluationException
178         *             if the evaluation fails
179         */
180        public boolean evaluate( Feature feature )
181                                throws FilterEvaluationException {
182            Object value = propertyName.evaluate( feature );
183            if ( value == null )
184                return true;
185            return false;
186        }
187    }