001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/sos/capabilities/CapabilitiesDocument.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     Aennchenstraße 19  
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     lat/lon GmbH
036     Aennchenstraße 19
037     53177 Bonn
038     Germany
039     E-Mail: greve@giub.uni-bonn.de
040    
041     ---------------------------------------------------------------------------*/
042    package org.deegree.ogcwebservices.sos.capabilities;
043    
044    import java.io.IOException;
045    import java.net.URI;
046    import java.net.URL;
047    import java.util.ArrayList;
048    import java.util.HashMap;
049    
050    import org.deegree.framework.log.ILogger;
051    import org.deegree.framework.log.LoggerFactory;
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.ogcbase.CommonNamespaces;
056    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
057    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
058    import org.deegree.ogcwebservices.getcapabilities.Operation;
059    import org.deegree.ogcwebservices.getcapabilities.OperationsMetadata;
060    import org.deegree.owscommon.OWSCommonCapabilitiesDocument;
061    import org.w3c.dom.Element;
062    import org.w3c.dom.Node;
063    import org.xml.sax.SAXException;
064    
065    /**
066     * Read the SOS Capabilities form a XML File
067     * 
068     * @author <a href="mailto:mkulbe@lat-lon.de">Matthias Kulbe </a>
069     * 
070     * @version 1.0
071     */
072    public class CapabilitiesDocument extends OWSCommonCapabilitiesDocument {
073    
074        private static final long serialVersionUID = 1L;
075    
076        private static final String XML_TEMPLATE = "SOSCapabilitiesTemplate.xml";
077    
078        protected static final URI SCSNS = CommonNamespaces.SOSNS;
079    
080        private static final ILogger LOG = LoggerFactory.getLogger( CapabilitiesDocument.class );
081    
082        /**
083         * creates an empty Document from template file
084         * 
085         */
086        public void createEmptyDocument() throws IOException, SAXException {
087    
088            URL url = CapabilitiesDocument.class.getResource( XML_TEMPLATE );
089            if ( url == null ) {
090                throw new IOException( "The resource '"
091                    + XML_TEMPLATE + " could not be found." );
092            }
093            this.load( url );
094        }
095    
096        public OGCCapabilities parseCapabilities() throws InvalidCapabilitiesException {
097            try {
098                return new SOSCapabilities( parseVersion(), parseUpdateSequence(),
099                    getServiceIdentification(), getServiceProvider(), getOperationsMetadata(), null,
100                    getPlatformList(), getSensorList() );
101    
102            } catch (Exception e) {
103                throw new InvalidCapabilitiesException( "Class representation of the SOS capabilities "
104                    + "document could not be generated: " + e.getMessage() );
105            }
106        }
107    
108        /**
109         * getOperationsMetadata
110         * 
111         * @return
112         * @throws XMLParsingException
113         */
114        public OperationsMetadata getOperationsMetadata() throws XMLParsingException {
115    
116            Node root = this.getRootElement();
117            Element child = XMLTools.getRequiredChildElement( "OperationsMetadata", OWSNS, root );
118            ElementList elementList = XMLTools.getChildElements( "Operation", OWSNS, child );
119    
120            // build HashMap of 'Operation'-elements for easier access
121            HashMap<String,Element> operations = new HashMap<String,Element>();
122            for (int i = 0; i < elementList.getLength(); i++) {
123                String attrValue = XMLTools.getRequiredAttrValue( "name", null, elementList.item( i ) );
124                operations.put( attrValue, elementList.item( i ) );
125            }
126    
127            // 'GetCapabilities'-operation
128            Operation getCapabilites = getOperation( OperationsMetadata.GET_CAPABILITIES_NAME, true,
129                operations );
130            // 'DescribePlatform'-operation
131            Operation describePlatform = getOperation( SOSOperationsMetadata.DESCRIBE_PLATFORM_NAME,
132                true, operations );
133            // 'DescribeSensor'-operation
134            Operation describeSensor = getOperation( SOSOperationsMetadata.DESCRIBE_SENSOR_NAME, false,
135                operations );
136            // 'GetObservation'-operation
137            Operation getObservation = getOperation( SOSOperationsMetadata.GET_OBSERVATION_NAME, true,
138                operations );
139    
140            return new SOSOperationsMetadata( getCapabilites, describePlatform, describeSensor,
141                getObservation );
142        }
143    
144        /**
145         * gets all platforms from the capabilities document
146         * 
147         * @return
148         * @throws XMLParsingException
149         */
150        protected ArrayList<Platform> getPlatformList() throws XMLParsingException {
151    
152            ArrayList<Platform> platformList = new ArrayList<Platform>( 100 );
153    
154            Element platformListElement = XMLTools.getRequiredChildElement( "PlatformList", SCSNS, getRootElement() );
155    
156            if ( platformListElement != null ) {
157                ElementList platformElements = XMLTools.getChildElements( "Platform", SCSNS, platformListElement );
158    
159                if ( ( platformElements != null )
160                    && ( platformElements.getLength() > 0 ) ) {
161    
162                    for (int i = 0; i < platformElements.getLength(); i++) {
163                        String platformId = XMLTools.getRequiredAttrValue( "Id", null, platformElements
164                            .item( i ) );
165                        String description = XMLTools.getAttrValue( platformElements.item( i ), null, "Description", null );
166                        Platform temp = new Platform( platformId, description );
167                        platformList.add( temp );
168                    }
169    
170                } else {
171                    LOG.logWarning( "no Platforms found in the capabilities Document" );
172                }
173            } else {
174                LOG.logWarning( "no Platforms found in the capabilities Document" );
175            }
176    
177            return platformList;
178        }
179    
180        /**
181         * gets all sensors from the capabilities document
182         * 
183         * 
184         */
185        protected ArrayList<Sensor> getSensorList() throws XMLParsingException {
186            ArrayList<Sensor> sensorList = new ArrayList<Sensor>();
187    
188            Element sensorListElement = XMLTools.getRequiredChildElement( "SensorList", SCSNS, getRootElement() );
189    
190            if ( sensorListElement != null ) {
191    
192                ElementList sensorElements = XMLTools.getChildElements( "Sensor", SCSNS, sensorListElement );
193    
194                if ( ( sensorElements != null )
195                    && ( sensorElements.getLength() > 0 ) ) {
196    
197                    for (int i = 0; i < sensorElements.getLength(); i++) {
198                        String sensorId = XMLTools.getRequiredAttrValue( "Id", null, sensorElements
199                            .item( i ) );
200                        String description = XMLTools.getAttrValue( sensorElements.item( i ), null, "Description", null );
201                        Sensor temp = new Sensor( sensorId, description );
202                        sensorList.add( temp );
203                    }
204                } else {
205                    LOG.logWarning( "no Sensors found in the capabilities Document" );
206                }
207    
208            } else {
209                LOG.logWarning( "no Sensors found in the capabilities Document" );
210            }
211    
212            return sensorList;
213        }
214    
215    }