001    // $HeadURL:
002    // /cvsroot/deegree/src/org/deegree/ogcwebservices/getcapabilities/CapabilitiesDocument.java,v
003    // 1.7 2004/06/22 13:25:14 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.getcapabilities;
039    
040    import java.net.MalformedURLException;
041    import java.net.URI;
042    import java.net.URISyntaxException;
043    import java.net.URL;
044    
045    import org.deegree.framework.util.StringTools;
046    import org.deegree.framework.xml.ElementList;
047    import org.deegree.framework.xml.XMLParsingException;
048    import org.deegree.framework.xml.XMLTools;
049    import org.deegree.ogcbase.CommonNamespaces;
050    import org.deegree.ogcwebservices.ExceptionFormat;
051    import org.deegree.ogcwebservices.MetadataLink;
052    import org.deegree.ogcwebservices.MetadataType;
053    import org.w3c.dom.Element;
054    import org.w3c.dom.Node;
055    
056    /**
057     *
058     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
059     *
060     * @author last edited by: $Author: mschneider $
061     *
062     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
063     *
064     * @since 2.0
065     */
066    public abstract class OGCStandardCapabilitiesDocument extends OGCCapabilitiesDocument {
067    
068        protected static final URI OGCNS = CommonNamespaces.OGCNS;
069    
070        protected static final URI GMLNS = CommonNamespaces.GMLNS;
071    
072        /**
073         * returns the value of the version attribute of the capabilities document
074         *
075         * @return the value of the version attribute of the capabilities document
076         */
077        @Override
078        public String parseVersion() {
079            return this.getRootElement().getAttribute( "version" );
080        }
081    
082        /**
083         * returns the service section of the configuration/capabilities. vendorspecific capabilities
084         * are not supported yet
085         *
086         * @param namespaceURI
087         *
088         * @return the service section of the configuration/capabilities. vendorspecific capabilities
089         *         are not supported yet
090         * @throws InvalidCapabilitiesException
091         */
092        public Capability getCapabilitySection( URI namespaceURI )
093                                throws InvalidCapabilitiesException {
094            try {
095                Node root = this.getRootElement();
096    
097                Element element = XMLTools.getRequiredChildElement( "Capability", namespaceURI, root );
098                Element elem = XMLTools.getRequiredChildElement( "Request", namespaceURI, element );
099                OperationsMetadata request = parseOperations( elem, namespaceURI );
100    
101                elem = XMLTools.getRequiredChildElement( "Exception", namespaceURI, element );
102                ExceptionFormat eFormat = getExceptionFormat( elem, namespaceURI );
103    
104                // vendorspecific capabilities are not supported yet
105                // elem = XMLTools.getRequiredChildByName(
106                // "VendorSpecificCapabilities", WCSNS, element);
107    
108                String version = element.getAttribute( "version" );
109                if ( version == null || version.equals( "" ) ) {
110                    version = this.parseVersion();
111                }
112                String updateSequence = element.getAttribute( "updateSequence" );
113                if ( updateSequence == null || updateSequence.equals( "" ) ) {
114                    updateSequence = this.getRootElement().getAttribute( "updateSequence" );
115                }
116    
117                return new Capability( version, updateSequence, request, eFormat, null );
118    
119            } catch ( XMLParsingException e ) {
120                String s = e.getMessage();
121                throw new InvalidCapabilitiesException( "Error while parsing the Capability "
122                                                        + "Section of the capabilities\n" + s
123                                                        + StringTools.stackTraceToString( e ) );
124            }
125        }
126    
127        /**
128         * creates a <tt>Request</tt> object (instance of WCSCapabilityRequest) from the passed
129         * element encapsulating the Request part of the WCS Capabiliy section
130         *
131         * @param element
132         * @return created <tt>Request</tt>
133         * @throws XMLParsingException
134         */
135        protected abstract OperationsMetadata parseOperations( Element element, URI namespaceURI )
136                                throws XMLParsingException;
137    
138        /**
139         * creates a <tt>MetadataLink</tt> object from the passed element.
140         *
141         * @param element
142         * @return created <tt>MetadataLink</tt>
143         * @throws XMLParsingException
144         */
145        @Override
146        protected MetadataLink parseMetadataLink( Element element )
147                                throws XMLParsingException {
148            if ( element == null )
149                return null;
150    
151            try {
152                URL reference = new URL( XMLTools.getAttrValue( element, "xlink:href" ) );
153                String title = XMLTools.getAttrValue( element, "xlink:title" );
154                URI about = new URI( XMLTools.getAttrValue( element, null, "about", null ) );
155                String tmp = XMLTools.getAttrValue( element, null, "metadataType", null );
156                MetadataType metadataType = new MetadataType( tmp );
157                return new MetadataLink( reference, title, about, metadataType );
158            } catch ( MalformedURLException e ) {
159                throw new XMLParsingException( "Couldn't parse metadataLink reference\n"
160                                               + StringTools.stackTraceToString( e ) );
161            } catch ( URISyntaxException e ) {
162                throw new XMLParsingException( "Couldn't parse metadataLink about\n" + StringTools.stackTraceToString( e ) );
163            }
164        }
165    
166        /**
167         * Creates an <tt>ExceptionFormat</tt> instance from the passed element.
168         *
169         * @param element
170         * @return the new instance
171         */
172        protected ExceptionFormat getExceptionFormat( Element element, URI namespaceURI ) {
173            ElementList el = XMLTools.getChildElements( "Format", namespaceURI, element );
174            String[] formats = new String[el.getLength()];
175            for ( int i = 0; i < formats.length; i++ ) {
176                formats[i] = XMLTools.getStringValue( el.item( i ) );
177            }
178            return new ExceptionFormat( formats );
179        }
180    
181        /**
182         * Creates a <code>DCPType</code> object from the passed <code>DCPType</code> element.
183         * <p>
184         * NOTE: Currently the <code>OnlineResources</code> included in the <code>DCPType</code> are
185         * just stored as simple <code>URLs</code> (not as <code>OnLineResource</code> instances)!
186         * <p>
187         * NOTE: In an <code>OGCStandardCapabilitiesDocument</code> the <code>XLinks</code> (the
188         * <code>URLs</code>) are stored in separate elements (<code>OnlineResource</code>), in
189         * an <code>OGCCommonCapabilitiesDocument</code> they are the
190         * <code>Get<code>/<code>Post</code> elements themselves.
191         *
192         * @param element
193         * @param namespaceURI
194         * @return created <code>DCPType</code>
195         * @throws XMLParsingException
196         * @see org.deegree.owscommon.OWSCommonCapabilities
197         */
198        protected DCPType getDCPType( Element element, URI namespaceURI )
199                                throws XMLParsingException {
200            try {
201                Element elem = XMLTools.getRequiredChildElement( "HTTP", namespaceURI, element );
202                ElementList el = XMLTools.getChildElements( "Get", namespaceURI, elem );
203    
204                URL[] get = new URL[el.getLength()];
205                for ( int i = 0; i < get.length; i++ ) {
206                    Element ell = XMLTools.getRequiredChildElement( "OnlineResource", namespaceURI, el.item( i ) );
207                    String s = XMLTools.getRequiredAttrValue( "href", XLNNS, ell );
208                    get[i] = new URL( s );
209                }
210                el = XMLTools.getChildElements( "Post", namespaceURI, elem );
211    
212                URL[] post = new URL[el.getLength()];
213                for ( int i = 0; i < post.length; i++ ) {
214                    Element ell = XMLTools.getRequiredChildElement( "OnlineResource", namespaceURI, el.item( i ) );
215                    String s = XMLTools.getRequiredAttrValue( "href", XLNNS, ell );
216                    post[i] = new URL( s );
217                }
218                Protocol protocol = new HTTP( get, post );
219                return new DCPType( protocol );
220            } catch ( MalformedURLException e ) {
221                throw new XMLParsingException( "Couldn't parse DCPType onlineresoure URL about\n"
222                                               + StringTools.stackTraceToString( e ) );
223            }
224        }
225    
226        /**
227         * Creates an array of <code>DCPType</code> objects from the passed element list.
228         *
229         * NOTE: Currently the <code>OnlineResources</code> included in the <code>DCPType</code> are
230         * just stored as simple <code>URLs</code> (not as <code>OnLineResource</code> instances)!
231         *
232         * @param el
233         * @param namespaceURI
234         * @return the dcp types
235         * @throws XMLParsingException
236         */
237        protected DCPType[] getDCPTypes( ElementList el, URI namespaceURI )
238                                throws XMLParsingException {
239            DCPType[] dcpTypes = new DCPType[el.getLength()];
240            for ( int i = 0; i < dcpTypes.length; i++ ) {
241                dcpTypes[i] = getDCPType( el.item( i ), namespaceURI );
242            }
243            return dcpTypes;
244        }
245    }