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