001    // $HeadURL:
002    // /cvsroot/deegree/src/org/deegree/ogcwebservices/csw/capabilities/CatalogCapabilitiesDocument.java,v
003    // 1.22 2004/08/05 15:40:08 ap 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.ogcwebservices.csw.capabilities;
039    
040    import java.io.IOException;
041    import java.net.URI;
042    import java.net.URL;
043    import java.util.Arrays;
044    import java.util.HashMap;
045    import java.util.List;
046    import java.util.Map;
047    
048    import org.deegree.datatypes.xlink.SimpleLink;
049    import org.deegree.framework.log.ILogger;
050    import org.deegree.framework.log.LoggerFactory;
051    import org.deegree.framework.xml.ElementList;
052    import org.deegree.framework.xml.XMLParsingException;
053    import org.deegree.framework.xml.XMLTools;
054    import org.deegree.i18n.Messages;
055    import org.deegree.model.filterencoding.capabilities.FilterCapabilities;
056    import org.deegree.model.filterencoding.capabilities.FilterCapabilities100Fragment;
057    import org.deegree.ogcbase.CommonNamespaces;
058    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
059    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
060    import org.deegree.ogcwebservices.getcapabilities.Operation;
061    import org.deegree.ogcwebservices.getcapabilities.OperationsMetadata;
062    import org.deegree.owscommon.OWSDomainType;
063    import org.w3c.dom.Element;
064    import org.w3c.dom.Node;
065    import org.xml.sax.SAXException;
066    
067    /**
068     * Represents an XML capabilities document for an OGC CSW 2.0 compliant service.
069     *
070     * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </a>
071     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
072     *
073     * @author last edited by: $Author: apoth $
074     *
075     * @version 2.0, $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
076     *
077     * @since 2.0
078     *
079     */
080    public class CatalogueCapabilitiesDocument_2_0_2 extends CatalogueCapabilitiesDocument {
081    
082        private static final long serialVersionUID = -5403408250387290444L;
083    
084        private static final ILogger LOG = LoggerFactory.getLogger( CatalogueCapabilitiesDocument_2_0_2.class );
085    
086        protected static final URI OGCNS = CommonNamespaces.OGCNS;
087    
088        private static final String XML_TEMPLATE = "CatalogueCapabilities2.0.2Template.xml";
089    
090        /**
091         * Creates a skeleton capabilities document that contains the mandatory elements only.
092         *
093         * @throws IOException
094         * @throws SAXException
095         */
096        @Override
097        public void createEmptyDocument()
098                                throws IOException, SAXException {
099            URL url = CatalogueCapabilitiesDocument_2_0_2.class.getResource( XML_TEMPLATE );
100            if ( url == null ) {
101                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
102            }
103            load( url );
104        }
105    
106        /**
107         * Creates a class representation of the document.
108         *
109         * @return class representation of the configuration document
110         */
111        @Override
112        public OGCCapabilities parseCapabilities()
113                                throws InvalidCapabilitiesException {
114            try {
115                FilterCapabilities filterCapabilities = null;
116                Element filterCapabilitiesElement = (Element) XMLTools.getNode( getRootElement(),
117                                                                                "ogc:Filter_Capabilities", nsContext );
118                if ( filterCapabilitiesElement != null ) {
119                    filterCapabilities = new FilterCapabilities100Fragment( filterCapabilitiesElement, getSystemId() ).parseFilterCapabilities();
120                }
121                return new CatalogueCapabilities( parseVersion(), parseUpdateSequence(), getServiceIdentification(),
122                                                  getServiceProvider(), getOperationsMetadata(), null, filterCapabilities );
123            } catch ( Exception e ) {
124                LOG.logError( e.getMessage(), e );
125                throw new InvalidCapabilitiesException( e.getMessage() );
126            }
127        }
128    
129        /**
130         * Creates a class representation of the <code>OperationsMetadata</code>- section.
131         *
132         * @return opertaions metadata
133         */
134        @Override
135        public OperationsMetadata getOperationsMetadata()
136                                throws XMLParsingException {
137    
138            Node root = this.getRootElement();
139    
140            Node omNode = XMLTools.getRequiredChildElement( "OperationsMetadata", OWSNS, root );
141            ElementList elementList = XMLTools.getChildElements( "Operation", OWSNS, omNode );
142    
143            ElementList parameterElements = XMLTools.getChildElements( "Parameter", OWSNS, omNode );
144            OWSDomainType[] parameters = new OWSDomainType[parameterElements.getLength()];
145    
146            for ( int i = 0; i < parameters.length; i++ ) {
147                parameters[i] = getOWSDomainType( null, parameterElements.item( i ) );
148            }
149    
150            OWSDomainType[] constraints = getContraints( (Element) omNode );
151    
152            // build HashMap of 'Operation'-elements for easier access
153            HashMap<String, Node> operations = new HashMap<String, Node>();
154            for ( int i = 0; i < elementList.getLength(); i++ ) {
155                operations.put( XMLTools.getRequiredAttrValue( "name", null, elementList.item( i ) ), elementList.item( i ) );
156            }
157    
158            // 'GetCapabilities'-operation
159            Operation getCapabilites = getOperation( OperationsMetadata.GET_CAPABILITIES_NAME, true, operations );
160            // 'DescribeRecord'-operation
161            Operation describeRecord = getOperation( CatalogueOperationsMetadata.DESCRIBE_RECORD_NAME, true, operations );
162            // 'GetDomain'-operation
163            Operation getDomain = getOperation( CatalogueOperationsMetadata.GET_DOMAIN_NAME, false, operations );
164            // 'GetRecords'-operation
165            Operation getRecords = getOperation( CatalogueOperationsMetadata.GET_RECORDS_NAME, true, operations );
166            // 'GetRecordById'-operation
167            Operation getRecordById = getOperation( CatalogueOperationsMetadata.GET_RECORD_BY_ID_NAME, true, operations );
168            // 'Transaction'-operation
169            Operation transaction = getOperation( CatalogueOperationsMetadata.TRANSACTION_NAME, false, operations );
170            // 'Harvest'-operation
171            Operation harvest = getOperation( CatalogueOperationsMetadata.HARVEST_NAME, false, operations );
172    
173            return new CatalogueOperationsMetadata( getCapabilites, describeRecord, getDomain, getRecords, getRecordById,
174                                                    transaction, harvest, parameters, constraints );
175        }
176    
177        /**
178         * @return a {@link EBRIMCapabilities} element (specified in the ogc-ebrim extension)
179         * @throws XMLParsingException
180         *             if a required node isn't found
181         */
182        @Override
183        protected EBRIMCapabilities parseEBRIMCapabilities()
184                                throws XMLParsingException {
185            Element rootElement = getRootElement();
186    
187            String prefix = rootElement.getOwnerDocument().lookupPrefix( CommonNamespaces.WRS_EBRIMNS.toString() );
188    
189            if ( prefix == null || "".equals( prefix.trim() ) ) {
190                return null;
191            }
192    
193            // SeviceFeatures
194            Element serviceFeature = (Element) XMLTools.getRequiredNode( rootElement, "wrs:ServiceFeatures", nsContext );
195            List<Node> nl = XMLTools.getNodes( serviceFeature, "wrs:feature", nsContext );
196            Map<URI, CSWFeature> features = new HashMap<URI, CSWFeature>();
197            for ( Object n : nl ) {
198                Node featureElement = (Node) n;
199                URI featureName = XMLTools.getRequiredNodeAsURI( featureElement, "@name", nsContext );
200                LOG.logDebug( "found featurename: " + featureName );
201                if ( features.containsKey( featureName ) ) {
202                    throw new XMLParsingException(
203                                                   Messages.getMessage( "WRS_UNAMBIGUOUS_FEAT_PROP", featureName.toString() ) );
204                }
205                CSWFeature feature = new CSWFeature( parsePropties( featureElement ) );
206                features.put( featureName, feature );
207            }
208    
209            Element serviceProps = (Element) XMLTools.getRequiredNode( rootElement, "wrs:ServiceProperties", nsContext );
210            Map<URI, List<String>> serviceProperties = parsePropties( serviceProps );
211            SimpleLink wsdl_SimpleLink = parseSimpleLink( (Element) XMLTools.getRequiredNode( rootElement,
212                                                                                              "wrs:WSDL-services",
213                                                                                              nsContext ) );
214    
215            return new EBRIMCapabilities( features, serviceProperties, wsdl_SimpleLink );
216        }
217    
218        private Map<URI, List<String>> parsePropties( Node xmlNode )
219                                throws XMLParsingException {
220            List<Node> pnl = XMLTools.getNodes( xmlNode, "wrs:property", nsContext );
221            Map<URI, List<String>> properties = new HashMap<URI, List<String>>();
222    
223            for ( Object pn : pnl ) {
224                Node property = (Node) pn;
225                URI propName = XMLTools.getRequiredNodeAsURI( property, "@name", nsContext );
226                List<String> propValues = Arrays.asList( XMLTools.getRequiredNodesAsStrings( property, "wrs:value",
227                                                                                             nsContext ) );
228                properties.put( propName, propValues );
229            }
230            return properties;
231        }
232    }