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    /*----------------    FILE HEADER  ------------------------------------------
005    
006     This file is part of deegree.
007     Copyright (C) 2001-2008 by:
008     EXSE, Department of Geography, University of Bonn
009     http://www.giub.uni-bonn.de/deegree/
010     lat/lon GmbH
011     http://www.lat-lon.de
012    
013     This library is free software; you can redistribute it and/or
014     modify it under the terms of the GNU Lesser General Public
015     License as published by the Free Software Foundation; either
016     version 2.1 of the License, or (at your option) any later version.
017    
018     This library is distributed in the hope that it will be useful,
019     but WITHOUT ANY WARRANTY; without even the implied warranty of
020     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
021     Lesser General Public License for more details.
022    
023     You should have received a copy of the GNU Lesser General Public
024     License along with this library; if not, write to the Free Software
025     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026    
027     Contact:
028    
029     Andreas Poth
030     lat/lon GmbH
031     Aennchenstr. 19
032     53115 Bonn
033     Germany
034     E-Mail: poth@lat-lon.de
035    
036     Prof. Dr. Klaus Greve
037     Department of Geography
038     University of Bonn
039     Meckenheimer Allee 166
040     53115 Bonn
041     Germany
042     E-Mail: greve@giub.uni-bonn.de
043    
044     
045     ---------------------------------------------------------------------------*/
046    package org.deegree.ogcwebservices.getcapabilities;
047    
048    import java.net.MalformedURLException;
049    import java.net.URI;
050    import java.net.URISyntaxException;
051    import java.net.URL;
052    
053    import org.deegree.framework.util.StringTools;
054    import org.deegree.framework.xml.ElementList;
055    import org.deegree.framework.xml.XMLParsingException;
056    import org.deegree.framework.xml.XMLTools;
057    import org.deegree.ogcbase.CommonNamespaces;
058    import org.deegree.ogcwebservices.ExceptionFormat;
059    import org.deegree.ogcwebservices.MetadataLink;
060    import org.deegree.ogcwebservices.MetadataType;
061    import org.w3c.dom.Element;
062    import org.w3c.dom.Node;
063    
064    /**
065     * 
066     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
067     * 
068     * @author last edited by: $Author: apoth $
069     * 
070     * @version 1.0. $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
071     * 
072     * @since 2.0
073     */
074    public abstract class OGCStandardCapabilitiesDocument extends OGCCapabilitiesDocument {
075    
076        protected static final URI OGCNS = CommonNamespaces.OGCNS;
077    
078        protected static final URI GMLNS = CommonNamespaces.GMLNS;
079    
080        /**
081         * returns the value of the version attribute of the capabilities document
082         * 
083         * @return the value of the version attribute of the capabilities document
084         */
085        public String parseVersion() {
086            return this.getRootElement().getAttribute( "version" );
087        }
088    
089        /**
090         * returns the service section of the configuration/capabilities. vendorspecific capabilities
091         * are not supported yet
092         * 
093         * @return
094         * @throws InvalidCapabilitiesException
095         */
096        public Capability getCapabilitySection( URI namespaceURI )
097                                throws InvalidCapabilitiesException {
098            try {
099                Node root = this.getRootElement();
100    
101                Element element = XMLTools.getRequiredChildElement( "Capability", namespaceURI, root );
102                Element elem = XMLTools.getRequiredChildElement( "Request", namespaceURI, element );
103                OperationsMetadata request = parseOperations( elem, namespaceURI );
104    
105                elem = XMLTools.getRequiredChildElement( "Exception", namespaceURI, element );
106                ExceptionFormat eFormat = getExceptionFormat( elem, namespaceURI );
107    
108                // vendorspecific capabilities are not supported yet
109                // elem = XMLTools.getRequiredChildByName(
110                // "VendorSpecificCapabilities", WCSNS, element);
111    
112                String version = element.getAttribute( "version" );
113                if ( version == null || version.equals( "" ) ) {
114                    version = this.parseVersion();
115                }
116                String updateSequence = element.getAttribute( "updateSequence" );
117                if ( updateSequence == null || updateSequence.equals( "" ) ) {
118                    updateSequence = this.getRootElement().getAttribute( "updateSequence" );
119                }
120    
121                return new Capability( version, updateSequence, request, eFormat, null );
122    
123            } catch ( XMLParsingException e ) {
124                String s = e.getMessage();
125                throw new InvalidCapabilitiesException( "Error while parsing the Capability "
126                                                        + "Section of the capabilities\n" + s
127                                                        + StringTools.stackTraceToString( e ) );
128            }
129        }
130    
131        /**
132         * creates a <tt>Request</tt> object (instance of WCSCapabilityRequest) from the passed
133         * element encapsulating the Request part of the WCS Capabiliy section
134         * 
135         * @param element
136         * @return created <tt>Request</tt>
137         * @throws XMLParsingException
138         */
139        protected abstract OperationsMetadata parseOperations( Element element, URI namespaceURI )
140                                throws XMLParsingException;
141    
142        /**
143         * creates a <tt>MetadataLink</tt> object from the passed element.
144         * 
145         * @param element
146         * @return created <tt>MetadataLink</tt>
147         * @throws XMLParsingException
148         */
149        protected MetadataLink parseMetadataLink( Element element )
150                                throws XMLParsingException {
151            if ( element == null )
152                return null;
153    
154            try {
155                URL reference = new URL( XMLTools.getAttrValue( element, "xlink:href" ) );
156                String title = XMLTools.getAttrValue( element, "xlink:title" );
157                URI about = new URI( XMLTools.getAttrValue( element, null, "about", null ) );
158                String tmp = XMLTools.getAttrValue( element, null, "metadataType", null );
159                MetadataType metadataType = new MetadataType( tmp );
160                return new MetadataLink( reference, title, about, metadataType );
161            } catch ( MalformedURLException e ) {
162                throw new XMLParsingException( "Couldn't parse metadataLink reference\n"
163                                               + StringTools.stackTraceToString( e ) );
164            } catch ( URISyntaxException e ) {
165                throw new XMLParsingException( "Couldn't parse metadataLink about\n" + StringTools.stackTraceToString( e ) );
166            }
167        }
168    
169        /**
170         * Creates an <tt>ExceptionFormat</tt> instance from the passed element.
171         * 
172         * @param element
173         * @return
174         * @throws XMLParsingException
175         */
176        protected ExceptionFormat getExceptionFormat( Element element, URI namespaceURI ) {
177            ElementList el = XMLTools.getChildElements( "Format", namespaceURI, element );
178            String[] formats = new String[el.getLength()];
179            for ( int i = 0; i < formats.length; i++ ) {
180                formats[i] = XMLTools.getStringValue( el.item( i ) );
181            }
182            return new ExceptionFormat( formats );
183        }
184    
185        /**
186         * Creates a <code>DCPType</code> object from the passed <code>DCPType</code> element.
187         * <p>
188         * NOTE: Currently the <code>OnlineResources</code> included in the <code>DCPType</code> are
189         * just stored as simple <code>URLs</code> (not as <code>OnLineResource</code> instances)!
190         * <p>
191         * NOTE: In an <code>OGCStandardCapabilitiesDocument</code> the <code>XLinks</code> (the
192         * <code>URLs</code>) are stored in separate elements (<code>OnlineResource</code>), in
193         * an <code>OGCCommonCapabilitiesDocument</code> they are the
194         * <code>Get<code>/<code>Post</code> elements themselves.
195         * 
196         * @param element
197         * @param namespaceURI
198         * @return created <code>DCPType</code>
199         * @throws XMLParsingException
200         * @see org.deegree.owscommon.OWSCommonCapabilities
201         */
202        protected DCPType getDCPType( Element element, URI namespaceURI )
203                                throws XMLParsingException {
204            try {
205                Element elem = XMLTools.getRequiredChildElement( "HTTP", namespaceURI, element );
206                ElementList el = XMLTools.getChildElements( "Get", namespaceURI, elem );
207    
208                URL[] get = new URL[el.getLength()];
209                for ( int i = 0; i < get.length; i++ ) {
210                    Element ell = XMLTools.getRequiredChildElement( "OnlineResource", namespaceURI, el.item( i ) );
211                    String s = XMLTools.getRequiredAttrValue( "href", XLNNS, ell );
212                    get[i] = new URL( s );
213                }
214                el = XMLTools.getChildElements( "Post", namespaceURI, elem );
215    
216                URL[] post = new URL[el.getLength()];
217                for ( int i = 0; i < post.length; i++ ) {
218                    Element ell = XMLTools.getRequiredChildElement( "OnlineResource", namespaceURI, el.item( i ) );
219                    String s = XMLTools.getRequiredAttrValue( "href", XLNNS, ell );
220                    post[i] = new URL( s );
221                }
222                Protocol protocol = new HTTP( get, post );
223                return new DCPType( protocol );
224            } catch ( MalformedURLException e ) {
225                throw new XMLParsingException( "Couldn't parse DCPType onlineresoure URL about\n"
226                                               + StringTools.stackTraceToString( e ) );
227            }
228        }
229    
230        /**
231         * Creates an array of <code>DCPType</code> objects from the passed element list.
232         * 
233         * NOTE: Currently the <code>OnlineResources</code> included in the <code>DCPType</code> are
234         * just stored as simple <code>URLs</code> (not as <code>OnLineResource</code> instances)!
235         * 
236         * @param el
237         * @param namespaceURI
238         * @return
239         * @throws XMLParsingException
240         */
241        protected DCPType[] getDCPTypes( ElementList el, URI namespaceURI )
242                                throws XMLParsingException {
243            DCPType[] dcpTypes = new DCPType[el.getLength()];
244            for ( int i = 0; i < dcpTypes.length; i++ ) {
245                dcpTypes[i] = getDCPType( el.item( i ), namespaceURI );
246            }
247            return dcpTypes;
248        }
249    }