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