001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/filterencoding/PropertyIsBetweenOperation.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.log.ILogger; 039 import org.deegree.framework.log.LoggerFactory; 040 import org.deegree.framework.xml.ElementList; 041 import org.deegree.framework.xml.XMLTools; 042 import org.deegree.model.feature.Feature; 043 import org.w3c.dom.Element; 044 045 /** 046 * Encapsulates the information of a <PropertyIsBetween>-element (as defined in Filter DTD). 047 * 048 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a> 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 class PropertyIsBetweenOperation extends ComparisonOperation { 054 055 private static ILogger LOG = LoggerFactory.getLogger( PropertyIsBetweenOperation.class ); 056 057 private PropertyName propertyName; 058 059 private Expression lowerBoundary; 060 061 private Expression upperBoundary; 062 063 /** 064 * @param propertyName 065 * to check against 066 * @param lowerBoundary 067 * of the property 068 * @param upperBoundary 069 * of the property 070 */ 071 public PropertyIsBetweenOperation( PropertyName propertyName, Expression lowerBoundary, Expression upperBoundary ) { 072 super( OperationDefines.PROPERTYISBETWEEN ); 073 this.propertyName = propertyName; 074 this.lowerBoundary = lowerBoundary; 075 this.upperBoundary = upperBoundary; 076 } 077 078 /** 079 * Given a DOM-fragment, a corresponding Operation-object is built. This method recursively calls other buildFromDOM 080 * () - methods to validate the structure of the DOM-fragment. 081 * 082 * @param element 083 * @return the propertyIsBetween operation 084 * 085 * @throws FilterConstructionException 086 * if the structure of the DOM-fragment is invalid 087 */ 088 public static Operation buildFromDOM( Element element ) 089 throws FilterConstructionException { 090 091 // check if root element's name equals 'PropertyIsBetween' 092 if ( !element.getLocalName().equals( "PropertyIsBetween" ) ) 093 throw new FilterConstructionException( "Name of element does not equal 'PropertyIsBetween'!" ); 094 095 ElementList children = XMLTools.getChildElements( element ); 096 if ( children.getLength() != 3 ) 097 throw new FilterConstructionException( "'PropertyIsBetween' requires exactly 3 elements!" ); 098 099 PropertyName propertyName = (PropertyName) PropertyName.buildFromDOM( children.item( 0 ) ); 100 Expression lowerBoundary = buildLowerBoundaryFromDOM( children.item( 1 ) ); 101 Expression upperBoundary = buildUpperBoundaryFromDOM( children.item( 2 ) ); 102 103 return new PropertyIsBetweenOperation( propertyName, lowerBoundary, upperBoundary ); 104 } 105 106 /** 107 * Given a DOM-fragment, a corresponding Expression-object (for the LowerBoundary-element) is built. This method 108 * recursively calls other buildFromDOM () - methods to validate the structure of the DOM-fragment. 109 * 110 * @throws FilterConstructionException 111 * if the structure of the DOM-fragment is invalid 112 */ 113 private static Expression buildLowerBoundaryFromDOM( Element element ) 114 throws FilterConstructionException { 115 116 // check if root element's name equals 'LowerBoundary' 117 if ( !element.getLocalName().equals( "LowerBoundary" ) ) 118 throw new FilterConstructionException( "Name of element does not equal 'LowerBoundary'!" ); 119 120 ElementList children = XMLTools.getChildElements( element ); 121 if ( children.getLength() != 1 ) 122 throw new FilterConstructionException( "'LowerBoundary' requires exactly 1 element!" ); 123 124 return Expression.buildFromDOM( children.item( 0 ) ); 125 } 126 127 /** 128 * Given a DOM-fragment, a corresponding Expression-object (for the UpperBoundary-element) is built. This method 129 * recursively calls other buildFromDOM () - methods to validate the structure of the DOM-fragment. 130 * 131 * @throws FilterConstructionException 132 * if the structure of the DOM-fragment is invalid 133 */ 134 private static Expression buildUpperBoundaryFromDOM( Element element ) 135 throws FilterConstructionException { 136 137 // check if root element's name equals 'UpperBoundary' 138 if ( !element.getLocalName().equals( "UpperBoundary" ) ) 139 throw new FilterConstructionException( "Name of element does not equal 'UpperBoundary'!" ); 140 141 ElementList children = XMLTools.getChildElements( element ); 142 if ( children.getLength() != 1 ) 143 throw new FilterConstructionException( "'UpperBoundary' requires exactly 1 element!" ); 144 145 return Expression.buildFromDOM( children.item( 0 ) ); 146 } 147 148 /** 149 * @return the name of the property that shall be compared to the boundaries 150 * 151 */ 152 public PropertyName getPropertyName() { 153 return propertyName; 154 } 155 156 /** 157 * @return the lower boundary of the operation as an <tt>Expression</tt> 158 * 159 */ 160 public Expression getLowerBoundary() { 161 return lowerBoundary; 162 } 163 164 /** 165 * @return the upper boundary of the operation as an <tt>Expression</tt> 166 * 167 */ 168 public Expression getUpperBoundary() { 169 return upperBoundary; 170 } 171 172 public StringBuffer toXML() { 173 StringBuffer sb = new StringBuffer( 500 ); 174 sb.append( "<ogc:" ).append( getOperatorName() ).append( ">" ); 175 sb.append( propertyName.toXML() ); 176 sb.append( "<ogc:LowerBoundary>" ); 177 sb.append( lowerBoundary.toXML() ); 178 sb.append( "</ogc:LowerBoundary>" ); 179 sb.append( "<ogc:UpperBoundary>" ); 180 sb.append( upperBoundary.toXML() ); 181 sb.append( "</ogc:UpperBoundary>" ); 182 sb.append( "</ogc:" ).append( getOperatorName() ).append( ">" ); 183 return sb; 184 } 185 186 public StringBuffer to100XML() { 187 return toXML(); 188 } 189 190 public StringBuffer to110XML() { 191 return toXML(); 192 } 193 194 /** 195 * Calculates the <tt>PropertyIsBetween</tt> -Operation's logical value based on the certain property values of the 196 * given <tt>Feature</tt>. TODO: Improve datatype handling. 197 * 198 * @param feature 199 * that determines the property values 200 * @return true, if the <tt>Operation</tt> evaluates to true, else false 201 * @throws FilterEvaluationException 202 * if the evaluation fails 203 */ 204 public boolean evaluate( Feature feature ) 205 throws FilterEvaluationException { 206 207 Object lowerValue = lowerBoundary.evaluate( feature ); 208 Object upperValue = upperBoundary.evaluate( feature ); 209 210 try { 211 if ( lowerValue instanceof String ) { 212 lowerValue = new Double( (String) lowerValue ); 213 } 214 if ( upperValue instanceof String ) { 215 upperValue = new Double( (String) lowerValue ); 216 } 217 218 if ( lowerValue == null || upperValue == null ) { 219 // this is because datasource may contain null values for properties 220 // that shall be applied to a 'is between' operation. This shall not 221 // be treated as an exception. 222 return false; 223 } 224 225 Object thisValue = propertyName.evaluate( feature ); 226 if ( thisValue instanceof String ) { 227 thisValue = new Double( (String) lowerValue ); 228 } 229 230 if ( !( lowerValue instanceof Number && upperValue instanceof Number && thisValue instanceof Number ) ) { 231 if ( thisValue == null ) { 232 LOG.logInfo( "thisValue == null" ); 233 } else { 234 LOG.logInfo( "thisValue > " + thisValue.getClass().getName() ); 235 } 236 throw new FilterEvaluationException( "PropertyIsBetweenOperation can only be " 237 + "applied to numerical expressions!" ); 238 } 239 240 double d1 = ( (Number) lowerValue ).doubleValue(); 241 double d2 = ( (Number) upperValue ).doubleValue(); 242 double d3 = ( (Number) thisValue ).doubleValue(); 243 return d1 <= d3 && d3 <= d2; 244 } catch ( NumberFormatException nfe ) { 245 // we're comparing two strings 246 String v = propertyName.evaluate( feature ).toString(); 247 return lowerValue.toString().compareTo( v ) <= 0 && v.toString().compareTo( upperValue.toString() ) <= 0; 248 } 249 } 250 }