    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/sos/configuration/ConfigurationFactory.java $
    002
    003    /*----------------------------------------------------------------------------
    004     This file is part of deegree, http://deegree.org/
    005     Copyright (C) 2001-2009 by:
    006       Department of Geography, University of Bonn
    007     and
    008       lat/lon GmbH
    009
    010     This library is free software; you can redistribute it and/or modify it under
    011     the terms of the GNU Lesser General Public License as published by the Free
    012     Software Foundation; either version 2.1 of the License, or (at your option)
    013     any later version.
    014     This library is distributed in the hope that it will be useful, but WITHOUT
    015     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    016     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    017     details.
    018     You should have received a copy of the GNU Lesser General Public License
    019     along with this library; if not, write to the Free Software Foundation, Inc.,
    020     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    021
    022     Contact information:
    023
    024     lat/lon GmbH
    025     Aennchenstr. 19, 53177 Bonn
    026     Germany
    027     http://lat-lon.de/
    028
    029     Department of Geography, University of Bonn
    030     Prof. Dr. Klaus Greve
    031     Postfach 1147, 53001 Bonn
    032     Germany
    033     http://www.geographie.uni-bonn.de/deegree/
    034
    035     e-mail: info@deegree.org
    036    ----------------------------------------------------------------------------*/
    037    package org.deegree.portal.standard.sos.configuration;
    038
    039    import java.io.FileReader;
    040    import java.io.IOException;
    041    import java.io.Reader;
    042    import java.net.MalformedURLException;
    043    import java.net.URI;
    044    import java.net.URL;
    045    import java.util.HashMap;
    046
    047    import org.deegree.framework.xml.ElementList;
    048    import org.deegree.framework.xml.XMLTools;
    049    import org.deegree.ogcbase.CommonNamespaces;
    050    import org.w3c.dom.Document;
    051    import org.w3c.dom.Element;
    052    import org.w3c.dom.Node;
    053    import org.xml.sax.SAXException;
    054
    055    /**
    056     * @author <a href="mailto:che@wupperverband.de.de">Christian Heier</a>
    057     * @author last edited by: $Author: mschneider $
    058     *
    059     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    060     */
    061    public class ConfigurationFactory {
    062        private static final URI SOSNS = CommonNamespaces.buildNSURI( "http://www.deegree.org/sosclient" );
    063
    064        /**
    065         *
    066         *
    067         * @param confFile
    068         *
    069         * @return Configuration
    070         *
    071         * @throws SAXException
    072         * @throws IOException
    073         * @throws Exception
    074         */
    075        public static SOSClientConfiguration createConfiguration( String confFile )
    076                                throws SAXException, IOException, Exception {
    077
    078            Reader reader = new FileReader( confFile );
    079            SOSClientConfiguration conf = createConfiguration( reader );
    080            reader.close();
    081
    082            return conf;
    083        }
    084
    085        /**
    086         *
    087         *
    088         * @param reader
    089         *
    090         * @return SOSClientConfiguration
    091         *
    092         * @throws SAXException
    093         * @throws IOException
    094         * @throws Exception
    095         */
    096        public static SOSClientConfiguration createConfiguration( Reader reader )
    097                                throws SAXException, IOException, Exception {
    098
    099            Document doc = XMLTools.parse( reader );
    100
    101            // sos descriptions
    102            Element element = doc.getDocumentElement();
    103            ElementList el = XMLTools.getChildElements( "sos", SOSNS, element );
    104            HashMap<String, URL> sosServers = createSOSDesc( el );
    105
    106            return new SOSClientConfiguration( sosServers );
    107        }
    108
    109        /**
    110         * creates a map of sos names and associated online resources
    111         */
    112        private static HashMap<String, URL> createSOSDesc( ElementList nl )
    113                                throws MalformedURLException {
    114
    115            HashMap<String, URL> sosServers = new HashMap<String, URL>();
    116
    117            for ( int i = 0; i < nl.getLength(); i++ ) {
    118                Element element = nl.item( i );
    119                Node node = element.getElementsByTagNameNS( SOSNS.toString(), "name" ).item( 0 );
    120                String name = node.getFirstChild().getNodeValue();
    121                node = element.getElementsByTagNameNS( SOSNS.toString(), "onlineResource" ).item( 0 );
    122
    123                String tmp = XMLTools.getStringValue( node );
    124                sosServers.put( name, new URL( tmp ) );
    125            }
    126
    127            return sosServers;
    128        }
    129
    130    }