001    // $HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/wcs/getcapabilities/WCSCapabilitiesDocument.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2007 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: 6940 $
083     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
084     * @author last edited by: $Author: apoth $
085     * 
086     * @version 1.0. $Revision: 6940 $, $Date: 2007-05-08 20:32:51 +0200 (Di, 08 Mai 2007) $
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 a <tt>Phone</tt> object from the passed element
252         * 
253         * @param element
254         * @return
255         * @throws XMLParsingException
256         */
257        private Phone parsePhone( Element element ) {
258            if ( element == null )
259                return null;
260            ElementList el = XMLTools.getChildElements( "voice", WCSNS, element );
261            String[] voice = new String[el.getLength()];
262            for ( int i = 0; i < voice.length; i++ ) {
263                voice[i] = XMLTools.getStringValue( el.item( i ) );
264            }
265            el = XMLTools.getChildElements( "facsimile", WCSNS, element );
266            String[] facsimile = new String[el.getLength()];
267            for ( int i = 0; i < facsimile.length; i++ ) {
268                facsimile[i] = XMLTools.getStringValue( el.item( i ) );
269            }
270            return new Phone( facsimile, null, null, voice );
271        }
272    
273        /**
274         * creates a <tt>Request</tt> object (instance of WCSCapabilityRequest) from the passed
275         * element encapsulating the Request part of the WCS Capabiliy section
276         * 
277         * @param element
278         * @return created <tt>Request</tt>
279         * @throws XMLParsingException
280         */
281        protected OperationsMetadata parseOperations( Element element, URI namespaceURI )
282                                throws XMLParsingException {
283    
284            Element gCapa = XMLTools.getRequiredChildElement( "GetCapabilities", WCSNS, element );
285            ElementList el = XMLTools.getChildElements( "DCPType", WCSNS, gCapa );
286            DCPType[] dcp = getDCPTypes( el, WCSNS );
287            Operation getCapaOperation = new Operation( "GetCapabilities", dcp );
288    
289            Element dCover = XMLTools.getRequiredChildElement( "DescribeCoverage", WCSNS, element );
290            el = XMLTools.getChildElements( "DCPType", WCSNS, dCover );
291            dcp = getDCPTypes( el, WCSNS );
292            Operation descCoverOperation = new Operation( "DescribeCoverage", dcp );
293    
294            Element gCover = XMLTools.getRequiredChildElement( "GetCoverage", WCSNS, element );
295            el = XMLTools.getChildElements( "DCPType", WCSNS, gCover );
296            dcp = getDCPTypes( el, WCSNS );
297            Operation getCoverOperation = new Operation( "GetCoverage", dcp );
298    
299            return new WCSCapabilityOperations( getCapaOperation, descCoverOperation, getCoverOperation );
300    
301        }
302    
303        /**
304         * creates an array of <tt>CoverageOfferingBrief</tt> objects from the passed element list
305         * encapsulating all CoverageOfferingBrief parts of the WCS ContentMetadata section
306         * 
307         * @param el
308         * @return creates array of <tt>CoverageOfferingBrief</tt>
309         * @throws XMLParsingException
310         * @throws OGCWebServiceException
311         * @throws OGCException
312         */
313        private CoverageOfferingBrief[] parseCoverageOfferingBrief( ElementList el )
314                                throws XMLParsingException, OGCWebServiceException, OGCException {
315            if ( el == null )
316                return null;
317            CoverageOfferingBrief[] cob = new CoverageOfferingBrief[el.getLength()];
318            for ( int i = 0; i < cob.length; i++ ) {
319                cob[i] = parseCoverageOfferingBrief( el.item( i ) );
320            }
321            return cob;
322        }
323    
324        /**
325         * creates a <tt>CoverageOfferingBrief</tt> object from the passed element encapsulating one
326         * CoverageOfferingBrief part of the WCS ContentMetadata section
327         * 
328         * @param element
329         * @return created <tt>CoverageOfferingBrief</tt>
330         * @throws XMLParsingException
331         * @throws OGCWebServiceException
332         * @throws OGCException
333         */
334        protected CoverageOfferingBrief parseCoverageOfferingBrief( Element element )
335                                throws XMLParsingException, OGCWebServiceException, OGCException {
336            Element elem = XMLTools.getChildElement( "metadataLink", WCSNS, element );
337            MetadataLink mLink = parseMetadataLink( elem );
338            String desc = XMLTools.getStringValue( "description", WCSNS, element, null );
339            String name = XMLTools.getRequiredStringValue( "name", WCSNS, element );
340            String label = XMLTools.getRequiredStringValue( "label", WCSNS, element );
341            elem = XMLTools.getChildElement( "lonLatEnvelope", WCSNS, element );
342            LonLatEnvelope llEnv = parseLonLatEnvelope( elem );
343            ElementList el = XMLTools.getChildElements( "keywords", WCSNS, element );
344            Keywords[] keywords = parseKeywords( el, WCSNS );
345    
346            return new CoverageOfferingBrief( name, label, desc, mLink, llEnv, keywords, null );
347        }
348    
349    }