001    // $HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wcs/getcapabilities/WCSCapabilitiesDocument.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     
043     ---------------------------------------------------------------------------*/
044    package org.deegree.ogcwebservices.wcs.getcapabilities;
045    
046    import java.io.IOException;
047    import java.net.URI;
048    import java.net.URL;
049    
050    import org.deegree.datatypes.CodeList;
051    import org.deegree.framework.util.StringTools;
052    import org.deegree.framework.xml.ElementList;
053    import org.deegree.framework.xml.XMLParsingException;
054    import org.deegree.framework.xml.XMLTools;
055    import org.deegree.model.metadata.iso19115.Address;
056    import org.deegree.model.metadata.iso19115.CitedResponsibleParty;
057    import org.deegree.model.metadata.iso19115.ContactInfo;
058    import org.deegree.model.metadata.iso19115.Keywords;
059    import org.deegree.model.metadata.iso19115.OnlineResource;
060    import org.deegree.model.metadata.iso19115.Phone;
061    import org.deegree.ogcbase.CommonNamespaces;
062    import org.deegree.ogcbase.OGCException;
063    import org.deegree.ogcwebservices.LonLatEnvelope;
064    import org.deegree.ogcwebservices.MetadataLink;
065    import org.deegree.ogcwebservices.OGCWebServiceException;
066    import org.deegree.ogcwebservices.getcapabilities.Capability;
067    import org.deegree.ogcwebservices.getcapabilities.DCPType;
068    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
069    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
070    import org.deegree.ogcwebservices.getcapabilities.OGCStandardCapabilitiesDocument;
071    import org.deegree.ogcwebservices.getcapabilities.Operation;
072    import org.deegree.ogcwebservices.getcapabilities.OperationsMetadata;
073    import org.deegree.ogcwebservices.getcapabilities.Service;
074    import org.deegree.ogcwebservices.wcs.CoverageOfferingBrief;
075    import org.w3c.dom.DOMException;
076    import org.w3c.dom.Element;
077    import org.xml.sax.SAXException;
078    
079    /**
080     * 
081     * 
082     * @version $Revision: 12803 $
083     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
084     * @author last edited by: $Author: rbezema $
085     * 
086     * @version 1.0. $Revision: 12803 $, $Date: 2008-07-10 13:16:27 +0200 (Do, 10 Jul 2008) $
087     * 
088     * @since 2.0
089     */
090    public class WCSCapabilitiesDocument extends OGCStandardCapabilitiesDocument {
091    
092        public static final String XML_TEMPLATE = "WCSCapabilitiesTemplate.xml";
093    
094        protected static URI WCSNS = CommonNamespaces.WCSNS;
095    
096        protected static URI DGRNS = CommonNamespaces.DEEGREEWCS;
097    
098        public void createEmptyDocument()
099                                throws IOException, SAXException {
100            URL url = WCSCapabilitiesDocument.class.getResource( XML_TEMPLATE );
101            if ( url == null ) {
102                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
103            }
104            load( url );
105        }
106    
107        /**
108         * @see org.deegree.ogcwebservices.getcapabilities.OGCCapabilitiesDocument#parseCapabilities()
109         */
110        public OGCCapabilities parseCapabilities()
111                                throws InvalidCapabilitiesException {
112            String version = parseVersion();
113            String updateSeq = parseUpdateSequence();
114            Service service = parseServiceSection();
115            Capability capabilitiy = getCapabilitySection( WCSNS );
116            ContentMetadata contentMetadata = parseContentMetadataSection();
117            return new WCSCapabilities( version, updateSeq, service, capabilitiy, contentMetadata );
118        }
119    
120        /**
121         * returns the service section of the WCS configuration/capabilities
122         * 
123         * @return created <tt>CapabilitiesService</tt>
124         * @throws InvalidCapabilitiesException
125         */
126        public Service parseServiceSection()
127                                throws InvalidCapabilitiesException {
128            try {
129                Element element = XMLTools.getRequiredChildElement( "Service", WCSNS, getRootElement() );
130                Element elem = XMLTools.getChildElement( "metadataLink", WCSNS, element );
131                MetadataLink mLink = parseMetadataLink( elem );
132                String desc = XMLTools.getStringValue( "description", WCSNS, element, null );
133                String name = XMLTools.getRequiredStringValue( "name", WCSNS, element );
134                String label = XMLTools.getRequiredStringValue( "label", WCSNS, element );
135                ElementList el = XMLTools.getChildElements( "keywords", WCSNS, element );
136                Keywords[] keywords = parseKeywords( el, WCSNS );
137                elem = XMLTools.getChildElement( "responsibleParty", WCSNS, element );
138                CitedResponsibleParty crp = parseResponsibleParty( elem );
139                elem = XMLTools.getChildElement( "fees", WCSNS, element );
140                CodeList fees = parseCodeList( elem );
141                el = XMLTools.getChildElements( "accessConstraints", WCSNS, element );
142                CodeList[] accessConstraints = parseCodeListArray( el );
143    
144                String version = element.getAttribute( "version" );
145                if ( version == null || version.equals( "" ) ) {
146                    version = this.parseVersion();
147                }
148                String updateSequence = element.getAttribute( "updateSequence" );
149                if ( updateSequence == null || updateSequence.equals( "" ) ) {
150                    updateSequence = this.getRootElement().getAttribute( "updateSequence" );
151                }
152    
153                Service service = new Service( desc, name, mLink, label, keywords, crp, fees, accessConstraints, version,
154                                               updateSequence );
155                return service;
156            } catch ( XMLParsingException e ) {
157                String s = e.getMessage();
158                throw new InvalidCapabilitiesException( "Error while parsing the Service Section "
159                                                        + "of the WCS capabilities\n" + s
160                                                        + StringTools.stackTraceToString( e ) );
161            } catch ( DOMException e ) {
162                String s = e.getMessage();
163                throw new InvalidCapabilitiesException( "Error handling the DOM object of the "
164                                                        + "Service Section of the WCS capabilities\n" + s
165                                                        + StringTools.stackTraceToString( e ) );
166            } catch ( OGCException e ) {
167                String s = e.getMessage();
168                throw new InvalidCapabilitiesException( "Error initializing the Service object from "
169                                                        + "the Service Section of the WCS capabilities\n" + s
170                                                        + StringTools.stackTraceToString( e ) );
171            }
172        }
173    
174        /**
175         * returns the contentMetadata section of the WCS configuration/capabilities
176         * 
177         * @return
178         * @throws InvalidCapabilitiesException
179         */
180        public ContentMetadata parseContentMetadataSection()
181                                throws InvalidCapabilitiesException {
182            try {
183                Element element = XMLTools.getRequiredChildElement( "ContentMetadata", WCSNS, getRootElement() );
184                ElementList el = XMLTools.getChildElements( "CoverageOfferingBrief", WCSNS, element );
185                CoverageOfferingBrief[] cob = parseCoverageOfferingBrief( el );
186    
187                String version = element.getAttribute( "version" );
188                if ( version == null || version.equals( "" ) ) {
189                    version = this.parseVersion();
190                }
191                String updateSequence = element.getAttribute( "updateSequence" );
192                if ( updateSequence == null || updateSequence.equals( "" ) ) {
193                    updateSequence = this.getRootElement().getAttribute( "updateSequence" );
194                }
195    
196                return new ContentMetadata( version, updateSequence, cob );
197            } catch ( XMLParsingException e ) {
198                String s = e.getMessage();
199                throw new InvalidCapabilitiesException( "Error while parsing the ContentMetadata "
200                                                        + "Section of the WCS capabilities\n" + s
201                                                        + StringTools.stackTraceToString( e ) );
202            } catch ( OGCException e ) {
203                String s = e.getMessage();
204                throw new InvalidCapabilitiesException( "Error while parsing the ContentMetadata "
205                                                        + "Section of the WCS capabilities\n" + s
206                                                        + StringTools.stackTraceToString( e ) );
207            }
208        }
209    
210        /**
211         * creates a <tt>CitedResponsibleParty</tt> object from the passed element
212         * 
213         * @param element
214         * @return
215         * @throws XMLParsingException
216         */
217        private CitedResponsibleParty parseResponsibleParty( Element element )
218                                throws XMLParsingException {
219            if ( element == null )
220                return null;
221            String indName = XMLTools.getStringValue( "individualName", WCSNS, element, null );
222            String orgName = XMLTools.getStringValue( "organisationName", WCSNS, element, null );
223            String posName = XMLTools.getStringValue( "positionName", WCSNS, element, null );
224            Element elem = XMLTools.getChildElement( "contactInfo", WCSNS, element );
225            ContactInfo contactInfo = parseContactInfo( elem );
226            return new CitedResponsibleParty( new ContactInfo[] { contactInfo }, new String[] { indName },
227                                              new String[] { orgName }, new String[] { posName }, null );
228        }
229    
230        /**
231         * creates a <tt>ContactInfo</tt> object from the passed element
232         * 
233         * @param element
234         * @return
235         * @throws XMLParsingException
236         */
237        private ContactInfo parseContactInfo( Element element )
238                                throws XMLParsingException {
239            if ( element == null )
240                return null;
241            Element elem = XMLTools.getChildElement( "phone", WCSNS, element );
242            Phone phone = parsePhone( elem );
243            elem = XMLTools.getChildElement( "address", WCSNS, element );
244            Address addr = parseAddress( elem, WCSNS );
245            elem = XMLTools.getChildElement( "onlineResource", WCSNS, element );
246            OnlineResource olr = parseOnLineResource( elem );
247            return new ContactInfo( addr, null, null, olr, phone );
248        }
249    
250        /**
251         * Creates an <code>Address</code> instance from the passed element.
252         * 
253         * @param element
254         *            Address-element
255         * @param namespaceURI
256         *            namespace-prefix of all elements
257         * @return the parsed Address
258         */
259        @Override
260        protected Address parseAddress( Element element, URI namespaceURI ) {
261            ElementList el = XMLTools.getChildElements( "deliveryPoint", namespaceURI, element );
262            String[] deliveryPoint = new String[el.getLength()];
263            for ( int i = 0; i < deliveryPoint.length; i++ ) {
264                deliveryPoint[i] = XMLTools.getStringValue( el.item( i ) );
265            }
266            String city = XMLTools.getStringValue( "city", namespaceURI, element, null );
267            String adminArea = XMLTools.getStringValue( "administrativeArea", namespaceURI, element, null );
268            String postalCode = XMLTools.getStringValue( "postalCode", namespaceURI, element, null );
269            String country = XMLTools.getStringValue( "country", namespaceURI, element, null );
270            el = XMLTools.getChildElements( "electronicMailAddress", namespaceURI, element );
271            String[] eMailAddresses = new String[el.getLength()];
272            for ( int i = 0; i < eMailAddresses.length; i++ ) {
273                eMailAddresses[i] = XMLTools.getStringValue( el.item( i ) );
274            }
275            return new Address( adminArea, city, country, deliveryPoint, eMailAddresses, postalCode );
276        }
277    
278        /**
279         * creates a <tt>Phone</tt> object from the passed element
280         * 
281         * @param element
282         * @return
283         * @throws XMLParsingException
284         */
285        private Phone parsePhone( Element element ) {
286            if ( element == null )
287                return null;
288            ElementList el = XMLTools.getChildElements( "voice", WCSNS, element );
289            String[] voice = new String[el.getLength()];
290            for ( int i = 0; i < voice.length; i++ ) {
291                voice[i] = XMLTools.getStringValue( el.item( i ) );
292            }
293            el = XMLTools.getChildElements( "facsimile", WCSNS, element );
294            String[] facsimile = new String[el.getLength()];
295            for ( int i = 0; i < facsimile.length; i++ ) {
296                facsimile[i] = XMLTools.getStringValue( el.item( i ) );
297            }
298            return new Phone( facsimile, null, null, voice );
299        }
300    
301        /**
302         * creates a <tt>Request</tt> object (instance of WCSCapabilityRequest) from the passed
303         * element encapsulating the Request part of the WCS Capabiliy section
304         * 
305         * @param element
306         * @return created <tt>Request</tt>
307         * @throws XMLParsingException
308         */
309        protected OperationsMetadata parseOperations( Element element, URI namespaceURI )
310                                throws XMLParsingException {
311    
312            Element gCapa = XMLTools.getRequiredChildElement( "GetCapabilities", WCSNS, element );
313            ElementList el = XMLTools.getChildElements( "DCPType", WCSNS, gCapa );
314            DCPType[] dcp = getDCPTypes( el, WCSNS );
315            Operation getCapaOperation = new Operation( "GetCapabilities", dcp );
316    
317            Element dCover = XMLTools.getRequiredChildElement( "DescribeCoverage", WCSNS, element );
318            el = XMLTools.getChildElements( "DCPType", WCSNS, dCover );
319            dcp = getDCPTypes( el, WCSNS );
320            Operation descCoverOperation = new Operation( "DescribeCoverage", dcp );
321    
322            Element gCover = XMLTools.getRequiredChildElement( "GetCoverage", WCSNS, element );
323            el = XMLTools.getChildElements( "DCPType", WCSNS, gCover );
324            dcp = getDCPTypes( el, WCSNS );
325            Operation getCoverOperation = new Operation( "GetCoverage", dcp );
326    
327            return new WCSCapabilityOperations( getCapaOperation, descCoverOperation, getCoverOperation );
328    
329        }
330    
331        /**
332         * creates an array of <tt>CoverageOfferingBrief</tt> objects from the passed element list
333         * encapsulating all CoverageOfferingBrief parts of the WCS ContentMetadata section
334         * 
335         * @param el
336         * @return creates array of <tt>CoverageOfferingBrief</tt>
337         * @throws XMLParsingException
338         * @throws OGCWebServiceException
339         * @throws OGCException
340         */
341        private CoverageOfferingBrief[] parseCoverageOfferingBrief( ElementList el )
342                                throws XMLParsingException, OGCWebServiceException, OGCException {
343            if ( el == null )
344                return null;
345            CoverageOfferingBrief[] cob = new CoverageOfferingBrief[el.getLength()];
346            for ( int i = 0; i < cob.length; i++ ) {
347                cob[i] = parseCoverageOfferingBrief( el.item( i ) );
348            }
349            return cob;
350        }
351    
352        /**
353         * creates a <tt>CoverageOfferingBrief</tt> object from the passed element encapsulating one
354         * CoverageOfferingBrief part of the WCS ContentMetadata section
355         * 
356         * @param element
357         * @return created <tt>CoverageOfferingBrief</tt>
358         * @throws XMLParsingException
359         * @throws OGCWebServiceException
360         * @throws OGCException
361         */
362        protected CoverageOfferingBrief parseCoverageOfferingBrief( Element element )
363                                throws XMLParsingException, OGCWebServiceException, OGCException {
364            Element elem = XMLTools.getChildElement( "metadataLink", WCSNS, element );
365            MetadataLink mLink = parseMetadataLink( elem );
366            String desc = XMLTools.getStringValue( "description", WCSNS, element, null );
367            String name = XMLTools.getRequiredStringValue( "name", WCSNS, element );
368            String label = XMLTools.getRequiredStringValue( "label", WCSNS, element );
369            elem = XMLTools.getChildElement( "lonLatEnvelope", WCSNS, element );
370            LonLatEnvelope llEnv = parseLonLatEnvelope( elem );
371            ElementList el = XMLTools.getChildElements( "keywords", WCSNS, element );
372            Keywords[] keywords = parseKeywords( el, WCSNS );
373    
374            return new CoverageOfferingBrief( name, label, desc, mLink, llEnv, keywords, null );
375        }
376    
377    }