001    // $HeadURL:
002    // /deegreerepository/deegree/src/org/deegree/model/filterencoding/capabilities/FilterCapabilities110Factory.java,v
003    // 1.1 2005/03/04 16:33:28 mschneider Exp $
004    /*----------------------------------------------------------------------------
005     This file is part of deegree, http://deegree.org/
006     Copyright (C) 2001-2009 by:
007       Department of Geography, University of Bonn
008     and
009       lat/lon GmbH
010    
011     This library is free software; you can redistribute it and/or modify it under
012     the terms of the GNU Lesser General Public License as published by the Free
013     Software Foundation; either version 2.1 of the License, or (at your option)
014     any later version.
015     This library is distributed in the hope that it will be useful, but WITHOUT
016     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
017     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
018     details.
019     You should have received a copy of the GNU Lesser General Public License
020     along with this library; if not, write to the Free Software Foundation, Inc.,
021     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022    
023     Contact information:
024    
025     lat/lon GmbH
026     Aennchenstr. 19, 53177 Bonn
027     Germany
028     http://lat-lon.de/
029    
030     Department of Geography, University of Bonn
031     Prof. Dr. Klaus Greve
032     Postfach 1147, 53001 Bonn
033     Germany
034     http://www.geographie.uni-bonn.de/deegree/
035    
036     e-mail: info@deegree.org
037    ----------------------------------------------------------------------------*/
038    package org.deegree.model.filterencoding.capabilities;
039    
040    import java.io.IOException;
041    import java.net.URL;
042    import java.util.List;
043    
044    import org.deegree.datatypes.QualifiedName;
045    import org.deegree.framework.xml.XMLFragment;
046    import org.deegree.framework.xml.XMLParsingException;
047    import org.deegree.framework.xml.XMLTools;
048    import org.deegree.ogcwebservices.getcapabilities.UnknownOperatorNameException;
049    import org.w3c.dom.Element;
050    import org.w3c.dom.Node;
051    import org.xml.sax.SAXException;
052    
053    /**
054     *
055     *
056     *
057     * @version $Revision: 18195 $
058     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
059     * @author last edited by: $Author: mschneider $
060     *
061     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
062     *
063     * @since 2.0
064     */
065    public class FilterCapabilities110Fragment extends XMLFragment {
066    
067        private static final long serialVersionUID = -5371910986551767458L;
068    
069        /**
070         * Creates a new <code>FilterCapabilities110Fragment</code> from the given parameters.
071         *
072         * @param url
073         * @throws SAXException
074         * @throws IOException
075         */
076        public FilterCapabilities110Fragment( URL url ) throws IOException, SAXException {
077            super( url );
078        }
079    
080        /**
081         * Creates a new <code>FilterCapabilities110Fragment</code> from the given parameters.
082         *
083         * @param element
084         * @param systemId
085         */
086        public FilterCapabilities110Fragment( Element element, URL systemId ) {
087            super( element );
088            setSystemId( systemId );
089        }
090    
091        /**
092         * Returns the object representation for the <code>ogc:Filter_Capabilities</code> root
093         * element.
094         *
095         * @return object representation for the given <code>ogc:Filter_Capabilities</code> element
096         * @throws XMLParsingException
097         */
098        public FilterCapabilities parseFilterCapabilities()
099                                throws XMLParsingException {
100            return new FilterCapabilities(
101                                           parseScalarCapabilities( (Element) XMLTools.getRequiredNode(
102                                                                                                        getRootElement(),
103                                                                                                        "ogc:Scalar_Capabilities",
104                                                                                                        nsContext ) ),
105                                           parseSpatialCapabilities( (Element) XMLTools.getRequiredNode(
106                                                                                                         getRootElement(),
107                                                                                                         "ogc:Spatial_Capabilities",
108                                                                                                         nsContext ) ),
109                                           parseIdCapabilities( (Element) XMLTools.getRequiredNode( getRootElement(),
110                                                                                                    "ogc:Id_Capabilities",
111                                                                                                    nsContext ) ) );
112        }
113    
114        /**
115         * Returns the object representation for an <code>ogc:Spatial_Capabilities</code> element that
116         * is compliant to the <code>OpenGIS Filter Encoding Specification 1.1.0</code>.
117         *
118         * @return object representation for the given <code>ogc:Spatial_Capabilities</code> element
119         * @throws XMLParsingException
120         */
121        private SpatialCapabilities parseSpatialCapabilities( Element spatialElement )
122                                throws XMLParsingException {
123    
124            List<Node> geometryOperandList = XMLTools.getNodes( spatialElement,
125                                                                "ogc:GeometryOperands/ogc:GeometryOperand/text()",
126                                                                nsContext );
127            QualifiedName[] geometryOperands = new QualifiedName[geometryOperandList.size()];
128            for ( int i = 0; i < geometryOperands.length; i++ ) {
129                geometryOperands[i] = XMLFragment.parseQualifiedName( geometryOperandList.get( i ) );
130            }
131            String[] operatorNames = XMLTools.getNodesAsStrings( spatialElement,
132                                                                 "ogc:SpatialOperators/ogc:SpatialOperator/@name",
133                                                                 nsContext );
134            SpatialOperator[] spatialOperators = new SpatialOperator[operatorNames.length];
135            for ( int i = 0; i < operatorNames.length; i++ ) {
136                try {
137                    spatialOperators[i] = OperatorFactory110.createSpatialOperator( operatorNames[i] );
138                } catch ( UnknownOperatorNameException e ) {
139                    throw new XMLParsingException( "Invalid operator name '" + operatorNames[i]
140                                                   + "' for spatial operator: " + e.getMessage(), e );
141                }
142            }
143            return new SpatialCapabilities( spatialOperators, geometryOperands );
144        }
145    
146        /**
147         * Returns the object representation for an <code>ogc:Scalar_Capabilities</code> element that
148         * is compliant to the <code>OpenGIS Filter Encoding Specification 1.1.0</code>.
149         *
150         * @return object representation for the given <code>ogc:Scalar_Capabilities</code> element
151         * @throws XMLParsingException
152         */
153        private ScalarCapabilities parseScalarCapabilities( Element element )
154                                throws XMLParsingException {
155    
156            boolean supportsLogicalOperators = XMLTools.getNode( element, "ogc:LogicalOperators", nsContext ) != null ? true
157                                                                                                                     : false;
158            Operator[] comparisonOperators = null;
159            Operator[] arithmeticOperators = null;
160    
161            Element comparisonOperatorsElement = (Element) XMLTools.getNode( element, "ogc:ComparisonOperators", nsContext );
162            if ( comparisonOperatorsElement != null ) {
163                String[] operatorNames = XMLTools.getNodesAsStrings( comparisonOperatorsElement,
164                                                                     "ogc:ComparisonOperator/text()", nsContext );
165                comparisonOperators = new Operator[operatorNames.length];
166                for ( int i = 0; i < operatorNames.length; i++ ) {
167                    try {
168                        comparisonOperators[i] = OperatorFactory110.createComparisonOperator( operatorNames[i] );
169                    } catch ( UnknownOperatorNameException e ) {
170                        throw new XMLParsingException( "Invalid operator name '" + operatorNames[i]
171                                                       + "' for comparison operator: " + e.getMessage(), e );
172                    }
173                }
174            }
175    
176            Element arithmeticOperatorsElement = (Element) XMLTools.getNode( element, "ogc:ArithmeticOperators", nsContext );
177            if ( arithmeticOperatorsElement != null ) {
178                XMLTools.getRequiredNode( arithmeticOperatorsElement, "ogc:SimpleArithmetic", nsContext );
179                List<Element> functionNameElementList = XMLTools.getElements(
180                                                                              arithmeticOperatorsElement,
181                                                                              "ogc:Functions/ogc:FunctionNames/ogc:FunctionName",
182                                                                              nsContext );
183                arithmeticOperators = new Function[functionNameElementList.size()];
184                for ( int i = 0; i < arithmeticOperators.length; i++ ) {
185                    arithmeticOperators[i] = parseFunction( functionNameElementList.get( i ) );
186                }
187            }
188            return new ScalarCapabilities( supportsLogicalOperators, comparisonOperators, arithmeticOperators );
189        }
190    
191        /**
192         * Returns the object representation for an <code>ogc:Id_Capabilities</code> element that is
193         * compliant to the <code>OpenGIS Filter Encoding Specification 1.1.0</code>.
194         *
195         * @return object representation for the given <code>ogc:Id_Capabilities</code> element
196         * @throws XMLParsingException
197         */
198        private IdCapabilities parseIdCapabilities( Element element )
199                                throws XMLParsingException {
200            List<Element> eidList = XMLTools.getElements( element, "ogc:EID", nsContext );
201            Element[] eidElements = new Element[eidList.size()];
202            for ( int i = 0; i < eidElements.length; i++ ) {
203                eidElements[i] = eidList.get( i );
204            }
205            List<Element> fidList = XMLTools.getElements( element, "ogc:FID", nsContext );
206            Element[] fidElements = new Element[fidList.size()];
207            for ( int i = 0; i < fidElements.length; i++ ) {
208                fidElements[i] = fidList.get( i );
209            }
210            return new IdCapabilities( eidElements, fidElements );
211        }
212    
213        private Function parseFunction( Element element )
214                                throws XMLParsingException {
215            String name = XMLTools.getRequiredNodeAsString( element, "text()", nsContext );
216            int nArgs = XMLTools.getRequiredNodeAsInt( element, "@nArgs", nsContext );
217            return new Function( name, nArgs );
218        }
219        /*
220         * private Operator parseSpatialOperator( Element element ) throws XMLParsingException { String
221         * name = XMLTools.getRequiredNodeAsString( element, "@name", nsContext ); List
222         * geometryOperandList = XMLTools.getNodes( element, "GeometryOperand/text()", nsContext );
223         * QualifiedName[] operands = new QualifiedName[geometryOperandList.size()]; for ( int i = 0; i <
224         * operands.length; i++ ) { operands[i] = XMLFragment.parseQualifiedName( (Node)
225         * geometryOperandList.get( i ) ); } return new SpatialOperator( name, operands ); }
226         */
227    }