001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/model/filterencoding/PropertyName.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 037 package org.deegree.model.filterencoding; 038 039 import java.net.URI; 040 import java.util.Iterator; 041 import java.util.Map; 042 043 import org.deegree.datatypes.QualifiedName; 044 import org.deegree.framework.xml.XMLParsingException; 045 import org.deegree.framework.xml.XMLTools; 046 import org.deegree.io.datastore.PropertyPathResolvingException; 047 import org.deegree.model.feature.Feature; 048 import org.deegree.model.feature.FeatureProperty; 049 import org.deegree.model.feature.schema.FeatureType; 050 import org.deegree.model.spatialschema.Geometry; 051 import org.deegree.ogcbase.CommonNamespaces; 052 import org.deegree.ogcbase.OGCDocument; 053 import org.deegree.ogcbase.PropertyPath; 054 import org.deegree.ogcbase.PropertyPathFactory; 055 import org.w3c.dom.Element; 056 import org.w3c.dom.Text; 057 058 /** 059 * Encapsulates the information of a PropertyName element. 060 * 061 * @author Markus Schneider 062 * @author last edited by: $Author: mschneider $ 063 * 064 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $ 065 */ 066 public class PropertyName extends Expression { 067 068 /** the PropertyName's value (as an XPATH expression). */ 069 private PropertyPath propertyPath; 070 071 /** 072 * Creates a new instance of <code>PropertyName</code>. 073 * 074 * @param value 075 * @deprecated use #PropertyName(QualifiedName) instead 076 */ 077 @Deprecated 078 public PropertyName( String value ) { 079 this( new QualifiedName( value ) ); 080 } 081 082 /** 083 * Creates a new instance of <code>PropertyName</code>. 084 * 085 * @param elementName 086 */ 087 public PropertyName( QualifiedName elementName ) { 088 this( PropertyPathFactory.createPropertyPath( elementName ) ); 089 } 090 091 /** 092 * Creates a new instance of <code>PropertyName</code>. 093 * 094 * @param value 095 */ 096 public PropertyName( PropertyPath value ) { 097 id = ExpressionDefines.PROPERTYNAME; 098 setValue( value ); 099 } 100 101 /** 102 * Given a DOM-fragment, a corresponding Expression-object is built. 103 * 104 * @param element 105 * @return the Expression object for the passed element 106 * @throws FilterConstructionException 107 * if the structure of the DOM-fragment is invalid 108 */ 109 public static Expression buildFromDOM( Element element ) 110 throws FilterConstructionException { 111 // check if root element's name equals 'PropertyName' 112 if ( !element.getLocalName().toLowerCase().equals( "propertyname" ) ) { 113 throw new FilterConstructionException( "Name of element does not equal " + "'PropertyName'!" ); 114 } 115 PropertyPath propertyPath; 116 try { 117 Text node = (Text) XMLTools.getRequiredNode( element, "text()", CommonNamespaces.getNamespaceContext() ); 118 propertyPath = OGCDocument.parsePropertyPath( node ); 119 } catch ( XMLParsingException e ) { 120 throw new FilterConstructionException( e.getMessage() ); 121 } 122 return new PropertyName( propertyPath ); 123 } 124 125 /** 126 * Returns the PropertyName's value. 127 * 128 * @return the PropertyName's value. 129 */ 130 public PropertyPath getValue() { 131 return this.propertyPath; 132 } 133 134 /** 135 * @param value 136 * @see org.deegree.model.filterencoding.PropertyName#getValue() 137 */ 138 public void setValue( PropertyPath value ) { 139 this.propertyPath = value; 140 } 141 142 /** 143 * Produces an indented XML representation of this object. 144 */ 145 @Override 146 public StringBuffer toXML() { 147 StringBuffer sb = new StringBuffer( 200 ); 148 sb.append( "<ogc:PropertyName" ); 149 150 // TODO use methods from XMLTools 151 Map<String, URI> namespaceMap = this.propertyPath.getNamespaceContext().getNamespaceMap(); 152 Iterator<String> prefixIter = namespaceMap.keySet().iterator(); 153 while ( prefixIter.hasNext() ) { 154 String prefix = prefixIter.next(); 155 if ( !CommonNamespaces.XMLNS_PREFIX.equals( prefix ) ) { 156 URI namespace = namespaceMap.get( prefix ); 157 sb.append( " xmlns:" ); 158 sb.append( prefix ); 159 sb.append( "=" ); 160 sb.append( "\"" ); 161 sb.append( namespace ); 162 sb.append( "\"" ); 163 } 164 } 165 sb.append( ">" ).append( propertyPath ).append( "</ogc:PropertyName>" ); 166 return sb; 167 } 168 169 /** 170 * Returns the <tt>PropertyName</tt>'s value (to be used in the evaluation of a complexer 171 * <tt>Expression</tt>). If the value is a geometry, an instance of <tt>Geometry</tt> is 172 * returned, if it appears to be numerical, a <tt>Double</tt>, else a <tt>String</tt>. 173 * <p> 174 * TODO: Improve datatype handling. 175 * <p> 176 * 177 * @param feature 178 * that determines the value of this <tt>PropertyName</tt> 179 * @return the resulting value 180 * @throws FilterEvaluationException 181 * if the <Feature>has no <tt>Property</tt> with a matching name 182 */ 183 @Override 184 public Object evaluate( Feature feature ) 185 throws FilterEvaluationException { 186 187 if ( feature == null ) { 188 throw new FilterEvaluationException( "Trying to evaluate an expression that depends " 189 + "on a property without a feature!" ); 190 } 191 192 FeatureProperty property = null; 193 try { 194 property = feature.getDefaultProperty( this.propertyPath ); 195 } catch ( PropertyPathResolvingException e ) { 196 e.printStackTrace(); 197 throw new FilterEvaluationException( e.getMessage() ); 198 } 199 FeatureType ft = feature.getFeatureType(); 200 if ( property == null && ft.getProperty( this.propertyPath.getStep( 0 ).getPropertyName() ) == null ) { 201 throw new FilterEvaluationException( "Feature '" + feature.getFeatureType().getName() 202 + "' has no property identified by '" + propertyPath + "'!" ); 203 } 204 205 if ( property == null || property.getValue() == null ) { 206 return null; 207 } 208 Object object = property.getValue(); 209 if ( object instanceof Number || object instanceof Geometry ) { 210 return object; 211 } 212 return object.toString(); 213 } 214 215 /** 216 * Indicates whether some other object is "equal to" this one. 217 * 218 * @return <code>true</code> if this object is the same as the obj argument; 219 * <code>false</code> otherwise 220 */ 221 @Override 222 public boolean equals( Object other ) { 223 if ( other == null || !( other instanceof PropertyName ) ) { 224 return false; 225 } 226 return propertyPath.equals( ( (PropertyName) other ).getValue() ); 227 } 228 229 /** 230 * Returns a string representation of the object. 231 * 232 * @return a string representation of the object 233 */ 234 @Override 235 public String toString() { 236 return this.propertyPath.getAsString(); 237 } 238 }