001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/filterencoding/PropertyName.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstraße 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043:
044: package org.deegree.model.filterencoding;
045:
046: import java.net.URI;
047: import java.util.Iterator;
048: import java.util.Map;
049:
050: import org.deegree.datatypes.QualifiedName;
051: import org.deegree.framework.xml.XMLParsingException;
052: import org.deegree.framework.xml.XMLTools;
053: import org.deegree.io.datastore.PropertyPathResolvingException;
054: import org.deegree.model.feature.Feature;
055: import org.deegree.model.feature.FeatureProperty;
056: import org.deegree.model.feature.schema.FeatureType;
057: import org.deegree.model.spatialschema.Geometry;
058: import org.deegree.ogcbase.CommonNamespaces;
059: import org.deegree.ogcbase.OGCDocument;
060: import org.deegree.ogcbase.PropertyPath;
061: import org.deegree.ogcbase.PropertyPathFactory;
062: import org.w3c.dom.Element;
063: import org.w3c.dom.Text;
064:
065: /**
066: * Encapsulates the information of a PropertyName element.
067: *
068: * @author Markus Schneider
069: * @author last edited by: $Author: apoth $
070: *
071: * @version $Revision: 9343 $, $Date: 2007-12-27 05:30:32 -0800 (Thu, 27 Dec 2007) $
072: */
073: public class PropertyName extends Expression {
074:
075: /** the PropertyName's value (as an XPATH expression). */
076: private PropertyPath propertyPath;
077:
078: /**
079: * Creates a new instance of <code>PropertyName</code>.
080: *
081: * @param value
082: * @deprecated use #PropertyName(QualifiedName) instead
083: */
084: @Deprecated
085: public PropertyName(String value) {
086: this (new QualifiedName(value));
087: }
088:
089: /**
090: * Creates a new instance of <code>PropertyName</code>.
091: *
092: * @param elementName
093: */
094: public PropertyName(QualifiedName elementName) {
095: this (PropertyPathFactory.createPropertyPath(elementName));
096: }
097:
098: /**
099: * Creates a new instance of <code>PropertyName</code>.
100: *
101: * @param value
102: */
103: public PropertyName(PropertyPath value) {
104: id = ExpressionDefines.PROPERTYNAME;
105: setValue(value);
106: }
107:
108: /**
109: * Given a DOM-fragment, a corresponding Expression-object is built.
110: *
111: * @param element
112: * @return the Expression object for the passed element
113: * @throws FilterConstructionException
114: * if the structure of the DOM-fragment is invalid
115: */
116: public static Expression buildFromDOM(Element element)
117: throws FilterConstructionException {
118: // check if root element's name equals 'PropertyName'
119: if (!element.getLocalName().toLowerCase()
120: .equals("propertyname")) {
121: throw new FilterConstructionException(
122: "Name of element does not equal "
123: + "'PropertyName'!");
124: }
125: PropertyPath propertyPath;
126: try {
127: Text node = (Text) XMLTools.getRequiredNode(element,
128: "text()", CommonNamespaces.getNamespaceContext());
129: propertyPath = OGCDocument.parsePropertyPath(node);
130: } catch (XMLParsingException e) {
131: throw new FilterConstructionException(e.getMessage());
132: }
133: return new PropertyName(propertyPath);
134: }
135:
136: /**
137: * Returns the PropertyName's value.
138: *
139: * @return the PropertyName's value.
140: */
141: public PropertyPath getValue() {
142: return this .propertyPath;
143: }
144:
145: /**
146: * @param value
147: * @see org.deegree.model.filterencoding.PropertyName#getValue()
148: */
149: public void setValue(PropertyPath value) {
150: this .propertyPath = value;
151: }
152:
153: /**
154: * Produces an indented XML representation of this object.
155: */
156: @Override
157: public StringBuffer toXML() {
158: StringBuffer sb = new StringBuffer(200);
159: sb.append("<ogc:PropertyName");
160:
161: // TODO use methods from XMLTools
162: Map<String, URI> namespaceMap = this .propertyPath
163: .getNamespaceContext().getNamespaceMap();
164: Iterator<String> prefixIter = namespaceMap.keySet().iterator();
165: while (prefixIter.hasNext()) {
166: String prefix = prefixIter.next();
167: if (!CommonNamespaces.XMLNS_PREFIX.equals(prefix)) {
168: URI namespace = namespaceMap.get(prefix);
169: sb.append(" xmlns:");
170: sb.append(prefix);
171: sb.append("=");
172: sb.append("\"");
173: sb.append(namespace);
174: sb.append("\"");
175: }
176: }
177: sb.append(">").append(propertyPath).append(
178: "</ogc:PropertyName>");
179: return sb;
180: }
181:
182: /**
183: * Returns the <tt>PropertyName</tt>'s value (to be used in the evaluation of a complexer
184: * <tt>Expression</tt>). If the value is a geometry, an instance of <tt>Geometry</tt> is
185: * returned, if it appears to be numerical, a <tt>Double</tt>, else a <tt>String</tt>.
186: * <p>
187: * TODO: Improve datatype handling.
188: * <p>
189: *
190: * @param feature
191: * that determines the value of this <tt>PropertyName</tt>
192: * @return the resulting value
193: * @throws FilterEvaluationException
194: * if the <Feature>has no <tt>Property</tt> with a matching name
195: */
196: @Override
197: public Object evaluate(Feature feature)
198: throws FilterEvaluationException {
199:
200: if (feature == null) {
201: throw new FilterEvaluationException(
202: "Trying to evaluate an expression that depends "
203: + "on a property without a feature!");
204: }
205:
206: FeatureProperty property = null;
207: try {
208: property = feature.getDefaultProperty(this .propertyPath);
209: } catch (PropertyPathResolvingException e) {
210: e.printStackTrace();
211: throw new FilterEvaluationException(e.getMessage());
212: }
213: FeatureType ft = feature.getFeatureType();
214: if (property == null
215: && ft.getProperty(this .propertyPath.getStep(0)
216: .getPropertyName()) == null) {
217: throw new FilterEvaluationException("Feature '"
218: + feature.getFeatureType().getName()
219: + "' has no property identified by '"
220: + propertyPath + "'!");
221: }
222:
223: if (property == null || property.getValue() == null) {
224: return null;
225: }
226: Object object = property.getValue();
227: if (object instanceof Number || object instanceof Geometry) {
228: return object;
229: }
230: return object.toString();
231: }
232:
233: /**
234: * Indicates whether some other object is "equal to" this one.
235: *
236: * @return <code>true</code> if this object is the same as the obj argument;
237: * <code>false</code> otherwise
238: */
239: @Override
240: public boolean equals(Object other) {
241: if (other == null || !(other instanceof PropertyName)) {
242: return false;
243: }
244: return propertyPath.equals(((PropertyName) other).getValue());
245: }
246:
247: /**
248: * Returns a string representation of the object.
249: *
250: * @return a string representation of the object
251: */
252: @Override
253: public String toString() {
254: return this.propertyPath.getAsString();
255: }
256: }
|