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