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