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-2007 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: 6845 $, $Date: 2007-05-07 11:28:14 +0200 (Mo, 07 Mai 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"
166                                               + StringTools.stackTraceToString( e ) );
167            }
168        }
169    
170        /**
171         * Creates an <tt>ExceptionFormat</tt> instance from the passed element.
172         * 
173         * @param element
174         * @return
175         * @throws XMLParsingException
176         */
177        protected ExceptionFormat getExceptionFormat( Element element, URI namespaceURI ) {
178            ElementList el = XMLTools.getChildElements( "Format", namespaceURI, element );
179            String[] formats = new String[el.getLength()];
180            for ( int i = 0; i < formats.length; i++ ) {
181                formats[i] = XMLTools.getStringValue( el.item( i ) );
182            }
183            return new ExceptionFormat( formats );
184        }
185    
186        /**
187         * Creates a <code>DCPType</code> object from the passed <code>DCPType</code> element.
188         * <p>
189         * NOTE: Currently the <code>OnlineResources</code> included in the <code>DCPType</code> are
190         * just stored as simple <code>URLs</code> (not as <code>OnLineResource</code> instances)!
191         * <p>
192         * NOTE: In an <code>OGCStandardCapabilitiesDocument</code> the <code>XLinks</code> (the
193         * <code>URLs</code>) are stored in separate elements (<code>OnlineResource</code>), in
194         * an <code>OGCCommonCapabilitiesDocument</code> they are the
195         * <code>Get<code>/<code>Post</code> elements themselves.
196         * 
197         * @param element
198         * @param namespaceURI
199         * @return created <code>DCPType</code>
200         * @throws XMLParsingException
201         * @see org.deegree.owscommon.OWSCommonCapabilities
202         */
203        protected DCPType getDCPType( Element element, URI namespaceURI )
204                                throws XMLParsingException {
205            try {
206                Element elem = XMLTools.getRequiredChildElement( "HTTP", namespaceURI, element );
207                ElementList el = XMLTools.getChildElements( "Get", namespaceURI, elem );
208    
209                URL[] get = new URL[el.getLength()];
210                for ( int i = 0; i < get.length; i++ ) {
211                    Element ell = XMLTools.getRequiredChildElement( "OnlineResource", namespaceURI,
212                                                                    el.item( i ) );
213                    String s = XMLTools.getRequiredAttrValue( "href", XLNNS, ell );
214                    get[i] = new URL( s );
215                }
216                el = XMLTools.getChildElements( "Post", namespaceURI, elem );
217    
218                URL[] post = new URL[el.getLength()];
219                for ( int i = 0; i < post.length; i++ ) {
220                    Element ell = XMLTools.getRequiredChildElement( "OnlineResource", namespaceURI,
221                                                                    el.item( i ) );
222                    String s = XMLTools.getRequiredAttrValue( "href", XLNNS, ell );
223                    post[i] = new URL( s );
224                }
225                Protocol protocol = new HTTP( get, post );
226                return new DCPType( protocol );
227            } catch ( MalformedURLException e ) {
228                throw new XMLParsingException( "Couldn't parse DCPType onlineresoure URL about\n"
229                                               + StringTools.stackTraceToString( e ) );
230            }
231        }
232    
233        /**
234         * Creates an array of <code>DCPType</code> objects from the passed element list.
235         * 
236         * NOTE: Currently the <code>OnlineResources</code> included in the <code>DCPType</code> are
237         * just stored as simple <code>URLs</code> (not as <code>OnLineResource</code> instances)!
238         * 
239         * @param el
240         * @param namespaceURI
241         * @return
242         * @throws XMLParsingException
243         */
244        protected DCPType[] getDCPTypes( ElementList el, URI namespaceURI )
245                                throws XMLParsingException {
246            DCPType[] dcpTypes = new DCPType[el.getLength()];
247            for ( int i = 0; i < dcpTypes.length; i++ ) {
248                dcpTypes[i] = getDCPType( el.item( i ), namespaceURI );
249            }
250            return dcpTypes;
251        }
252    }