001    // $HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/getcapabilities/OGCCapabilitiesDocument.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.getcapabilities;
045    
046    import java.net.MalformedURLException;
047    import java.net.URI;
048    import java.net.URISyntaxException;
049    import java.net.URL;
050    
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.Phone;
057    import org.deegree.ogcbase.CommonNamespaces;
058    import org.deegree.ogcbase.OGCDocument;
059    import org.deegree.ogcwebservices.MetadataLink;
060    import org.deegree.ogcwebservices.MetadataType;
061    import org.w3c.dom.Element;
062    
063    /**
064     * Most basic capabilities document for any OGC service instance.
065     * 
066     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
067     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
068     * 
069     * @author last edited by: $Author: apoth $
070     * 
071     * @version 1.0. $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
072     * 
073     * @since 2.0
074     */
075    public abstract class OGCCapabilitiesDocument extends OGCDocument {
076    
077        protected static final URI OGCNS = CommonNamespaces.OGCNS;
078    
079        /**
080         * Returns the value of the version attribute of the capabilities document.
081         * 
082         * @return the value of the version attribute of the capabilities document.
083         */
084        public String parseVersion() {
085            return getRootElement().getAttribute( "version" );
086        }
087    
088        /**
089         * Returns the value of the updateSequence attribute of the capabilities document.
090         * 
091         * @return the value of the updateSequence attribute of the capabilities document.
092         */
093        public String parseUpdateSequence() {
094            return getRootElement().getAttribute( "updateSequence" );
095        }
096    
097        /**
098         * Creates a <tt>MetadataLink</tt> instance from the passed element.
099         * 
100         * @param element
101         * @return created <tt>MetadataLink</tt>
102         * @throws XMLParsingException
103         */
104        protected MetadataLink parseMetadataLink( Element element )
105                                throws XMLParsingException {
106            if ( element == null )
107                return null;
108    
109            try {
110                URL reference = new URL( XMLTools.getAttrValue( element, "xlink:href" ) );
111                String title = XMLTools.getAttrValue( element, "xlink:title" );
112                URI about = new URI( XMLTools.getAttrValue( element, null, "about", null ) );
113                String tmp = XMLTools.getAttrValue( element, null, "metadataType", null );
114                MetadataType metadataType = new MetadataType( tmp );
115                return new MetadataLink( reference, title, about, metadataType );
116            } catch ( MalformedURLException e ) {
117                throw new XMLParsingException( "Couldn't parse metadataLink reference\n"
118                                               + StringTools.stackTraceToString( e ) );
119            } catch ( URISyntaxException e ) {
120                throw new XMLParsingException( "Couldn't parse metadataLink about\n" + StringTools.stackTraceToString( e ) );
121            }
122        }
123    
124        /**
125         * Creates a class representation of the document.
126         * 
127         * @return class representation of the document
128         */
129        public abstract OGCCapabilities parseCapabilities()
130                                throws InvalidCapabilitiesException;
131    
132        /**
133         * Creates an <code>Address</code> instance from the passed element.
134         * 
135         * @param element
136         *            Address-element
137         * @param namespaceURI
138         *            namespace-prefix of all elements
139         * @return
140         * @throws XMLParsingException
141         */
142        protected Address parseAddress( Element element, URI namespaceURI ) {
143            ElementList el = XMLTools.getChildElements( "DeliveryPoint", namespaceURI, element );
144            String[] deliveryPoint = new String[el.getLength()];
145            for ( int i = 0; i < deliveryPoint.length; i++ ) {
146                deliveryPoint[i] = XMLTools.getStringValue( el.item( i ) );
147            }
148            String city = XMLTools.getStringValue( "City", namespaceURI, element, null );
149            String adminArea = XMLTools.getStringValue( "AdministrativeArea", namespaceURI, element, null );
150            String postalCode = XMLTools.getStringValue( "PostalCode", namespaceURI, element, null );
151            String country = XMLTools.getStringValue( "Country", namespaceURI, element, null );
152            el = XMLTools.getChildElements( "ElectronicMailAddress", namespaceURI, element );
153            String[] eMailAddresses = new String[el.getLength()];
154            for ( int i = 0; i < eMailAddresses.length; i++ ) {
155                eMailAddresses[i] = XMLTools.getStringValue( el.item( i ) );
156            }
157            return new Address( adminArea, city, country, deliveryPoint, eMailAddresses, postalCode );
158        }
159    
160        /**
161         * Creates a <tt>Phone</tt> instance from the passed element.
162         * 
163         * @param element
164         *            Phone-element
165         * @param namespaceURI
166         *            URI that all elements must have
167         * @return
168         * @throws XMLParsingException
169         */
170        protected Phone parsePhone( Element element, URI namespaceURI ) {
171    
172            // 'Voice'-elements (optional)
173            ElementList el = XMLTools.getChildElements( "Voice", namespaceURI, element );
174            String[] voiceNumbers = new String[el.getLength()];
175            for ( int i = 0; i < voiceNumbers.length; i++ ) {
176                voiceNumbers[i] = XMLTools.getStringValue( el.item( i ) );
177            }
178    
179            // 'Facsimile'-elements (optional)
180            el = XMLTools.getChildElements( "Facsimile", namespaceURI, element );
181            String[] facsimileNumbers = new String[el.getLength()];
182            for ( int i = 0; i < facsimileNumbers.length; i++ ) {
183                facsimileNumbers[i] = XMLTools.getStringValue( el.item( i ) );
184            }
185            return new Phone( facsimileNumbers, null, null, voiceNumbers );
186        }
187    
188    }