001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/model/filterencoding/Literal.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: Aennchenstr. 19
030: 53115 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 org.deegree.framework.xml.XMLTools;
047: import org.deegree.model.feature.Feature;
048: import org.w3c.dom.Element;
049:
050: /**
051: * Encapsulates the information of a <Literal>element as defined in the FeatureId DTD.
052: *
053: * @author Markus Schneider
054: * @version 07.08.2002
055: */
056: public class Literal extends Expression {
057:
058: /** The literal's value. */
059: private String value;
060:
061: /**
062: * Constructs a new Literal.
063: *
064: * @param value
065: */
066: public Literal(String value) {
067: id = ExpressionDefines.LITERAL;
068: this .value = value;
069: }
070:
071: /**
072: * Given a DOM-fragment, a corresponding Expression-object is built. This method recursively
073: * calls other buildFromDOM () - methods to validate the structure of the DOM-fragment.
074: *
075: * @param element
076: *
077: * @throws FilterConstructionException
078: * if the structure of the DOM-fragment is invalid
079: */
080: public static Expression buildFromDOM(Element element)
081: throws FilterConstructionException {
082:
083: // check if root element's name equals 'Literal'
084: if (!element.getLocalName().equals("Literal")) {
085: throw new FilterConstructionException(
086: "Name of element does not equal 'Literal'!");
087: }
088:
089: return new Literal(XMLTools.getStringValue(element));
090: }
091:
092: /**
093: * Returns the literal's value (as String).
094: *
095: * @return the literal's value (as String).
096: */
097: public String getValue() {
098: return value;
099: }
100:
101: /**
102: * @see org.deegree.model.filterencoding.Literal#getValue()
103: * @param value
104: */
105: public void setValue(String value) {
106: this .value = value;
107: }
108:
109: /**
110: * Produces an indented XML representation of this object.
111: *
112: * @return XML representation of this object.
113: */
114: public StringBuffer toXML() {
115: StringBuffer sb = new StringBuffer(200);
116: // use CDATA section because content may not allowed within
117: // simple XML elements
118: sb.append("<ogc:Literal><![CDATA[").append(value).append(
119: "]]></ogc:Literal>");
120: return sb;
121: }
122:
123: /**
124: * Returns the <code>Literal</code>'s value (to be used in the evaluation of a complexer
125: * <code>Expression</code>). If the value appears to be numerical, a <code>Double</code> is
126: * returned, else a <code>String</code>. TODO: Improve datatype handling.
127: *
128: * @param feature
129: * that determines the values of <code>PropertyNames</code> in the expression (no
130: * use here)
131: * @return the resulting value
132: */
133: public Object evaluate(Feature feature) {
134: try {
135: return new Double(value);
136: } catch (NumberFormatException e) {
137: }
138: return value;
139: }
140: }
|