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