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