    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/model/filterencoding/Function.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 java.io.IOException;
    039    import java.io.InputStream;
    040    import java.util.ArrayList;
    041    import java.util.HashMap;
    042    import java.util.Iterator;
    043    import java.util.List;
    044    import java.util.Map;
    045    import java.util.Properties;
    046
    047    import org.deegree.framework.log.ILogger;
    048    import org.deegree.framework.log.LoggerFactory;
    049    import org.deegree.framework.xml.ElementList;
    050    import org.deegree.framework.xml.XMLTools;
    051    import org.deegree.i18n.Messages;
    052    import org.deegree.model.feature.Feature;
    053    import org.w3c.dom.Element;
    054
    055    /**
    056     * Encapsulates the information of a <code>Function</code>element as defined in the Expression
    057     * DTD.
    058     *
    059     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
    060     * @author last edited by: $Author: mschneider $
    061     *
    062     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    063     */
    064    public abstract class Function extends Expression {
    065
    066        private static final ILogger LOG = LoggerFactory.getLogger( Function.class );
    067
    068        /** The Function's name (as specified in it's name attribute). */
    069        protected String name;
    070
    071        /** The Function's arguments. */
    072        protected List<Expression> args;
    073
    074        private static Map<String, Class<?>> functions;
    075
    076        static {
    077            if ( Function.functions == null ) {
    078                Function.initialize();
    079            }
    080        }
    081
    082        private static void initialize() {
    083
    084            functions = new HashMap<String, Class<?>>();
    085
    086            try {
    087                // initialize mappings with mappings from "functions.properties" file in this package
    088                InputStream is = Function.class.getResourceAsStream( "function.properties" );
    089                Properties props = new Properties();
    090                try {
    091                    props.load( is );
    092                } catch ( IOException e ) {
    093                    LOG.logError( e.getMessage(), e );
    094                }
    095                Iterator<?> iter = props.keySet().iterator();
    096                while ( iter.hasNext() ) {
    097                    String key = (String) iter.next();
    098                    String className = props.getProperty( key );
    099                    functions.put( key, Class.forName( className ) );
    100                }
    101
    102                // override mappings with mappings from "functions.properties" file in root package
    103                is = Function.class.getResourceAsStream( "/function.properties" );
    104                if ( is != null ) {
    105                    props = new Properties();
    106                    props.load( is );
    107                    iter = props.keySet().iterator();
    108                    while ( iter.hasNext() ) {
    109                        String key = (String) iter.next();
    110                        String className = props.getProperty( key );
    111                        functions.put( key, Class.forName( className ) );
    112                    }
    113                }
    114
    115                for ( String functionName : functions.keySet() ) {
    116                    LOG.logDebug( "Filter function '" + functionName + "' is handled by class '"
    117                                  + functions.get( functionName ).getName() + "'." );
    118                }
    119            } catch ( Exception e ) {
    120                LOG.logError( e.getMessage(), e );
    121            }
    122        }
    123
    124        protected Function() {
    125            // protected constructor?
    126        }
    127
    128        /**
    129         * Constructs a new Function.
    130         *
    131         * @param name
    132         * @param args
    133         */
    134        public Function( String name, List<Expression> args ) {
    135            this();
    136            this.id = ExpressionDefines.FUNCTION;
    137            this.name = name;
    138            this.args = args;
    139        }
    140
    141        /**
    142         *
    143         * @param args
    144         */
    145        public void setArguments( List<Expression> args ) {
    146            this.args = args;
    147        }
    148
    149        /**
    150         * Given a DOM-fragment, a corresponding Expression-object is built. This method recursively
    151         * calls other buildFromDOM () - methods to validate the structure of the DOM-fragment.
    152         *
    153         * @throws FilterConstructionException
    154         *             if the structure of the DOM-fragment is invalid
    155         */
    156        public static Expression buildFromDOM( Element element )
    157                                throws FilterConstructionException {
    158
    159            // check if root element's name equals 'Function'
    160            if ( !element.getLocalName().toLowerCase().equals( "function" ) ) {
    161                throw new FilterConstructionException( Messages.getMessage( "FILTER_WRONG_ROOTELEMENT" ) );
    162            }
    163
    164            // determine the name of the Function
    165            String name = element.getAttribute( "name" );
    166            if ( name == null ) {
    167                throw new FilterConstructionException( Messages.getMessage( "FILTER_MISSING_NAME" ) );
    168            }
    169
    170            // determine the arguments of the Function
    171            ElementList children = XMLTools.getChildElements( element );
    172            if ( children.getLength() < 1 ) {
    173                throw new FilterConstructionException( Messages.getMessage( "FILTER_MISSING_ELEMENT", name ) );
    174            }
    175
    176            ArrayList<Expression> args = new ArrayList<Expression>( children.getLength() );
    177            for ( int i = 0; i < children.getLength(); i++ ) {
    178                args.add( Expression.buildFromDOM( children.item( i ) ) );
    179            }
    180
    181            Class<?> function = Function.functions.get( name );
    182            if ( function == null ) {
    183                throw new FilterConstructionException( Messages.getMessage( "FILTER_UNKNOWN_FUNCTION", name ) );
    184            }
    185            Function func;
    186            try {
    187                func = (Function) function.newInstance();
    188            } catch ( InstantiationException e ) {
    189                LOG.logError( e.getMessage(), e );
    190                throw new FilterConstructionException( e.getMessage() );
    191            } catch ( IllegalAccessException e ) {
    192                LOG.logError( e.getMessage(), e );
    193                throw new FilterConstructionException( e.getMessage() );
    194            }
    195            func.setName( name );
    196            func.setArguments( args );
    197
    198            return func;
    199        }
    200
    201        /**
    202         * Returns the Function's name.
    203         *
    204         * @return functions name
    205         */
    206        public String getName() {
    207            return this.name;
    208        }
    209
    210        /**
    211         * @see org.deegree.model.filterencoding.Function#getName()
    212         *
    213         * @param name
    214         */
    215        public void setName( String name ) {
    216            this.name = name;
    217        }
    218
    219        /**
    220         * returns the arguments of the function
    221         *
    222         * @return arguments of the function
    223         */
    224        public List<Expression> getArguments() {
    225            return this.args;
    226        }
    227
    228        /*
    229         * (non-Javadoc)
    230         *
    231         * @see org.deegree.model.filterencoding.Expression#getExpressionId()
    232         */
    233        @Override
    234        public int getExpressionId() {
    235            return ExpressionDefines.FUNCTION;
    236        }
    237
    238        /**
    239         * Returns the <tt>Function</tt>'s value (to be used in the evaluation of a complexer
    240         * <tt>Expression</tt>).
    241         *
    242         * @param feature
    243         *            that determines the concrete values of <tt>PropertyNames</tt> found in the
    244         *            expression
    245         * @return the resulting value
    246         */
    247        @Override
    248        public abstract Object evaluate( Feature feature )
    249                                throws FilterEvaluationException;
    250
    251        /**
    252         * Produces an indented XML representation of this object.
    253         *
    254         * @return xml representation
    255         */
    256        @Override
    257        public StringBuffer toXML() {
    258            StringBuffer sb = new StringBuffer( 1000 );
    259            sb.append( "<ogc:Function name=\"" ).append( this.name ).append( "\">" );
    260            for ( int i = 0; i < this.args.size(); i++ ) {
    261                sb.append( this.args.get( i ).toXML() );
    262            }
    263            sb.append( "</ogc:Function>" );
    264            return sb;
    265        }
    266    }