001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/IODocument.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.io;
037    
038    import java.net.MalformedURLException;
039    
040    import org.deegree.framework.log.ILogger;
041    import org.deegree.framework.log.LoggerFactory;
042    import org.deegree.framework.util.CharsetUtils;
043    import org.deegree.framework.xml.XMLFragment;
044    import org.deegree.framework.xml.XMLParsingException;
045    import org.deegree.framework.xml.XMLTools;
046    import org.w3c.dom.Element;
047    
048    /**
049     * This class provides method for reading IO configuration elements that are common to several
050     * services/applications.
051     *
052     * @version $Revision: 18195 $
053     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
054     * @author last edited by: $Author: mschneider $
055     *
056     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
057     *
058     * @since 2.0
059     */
060    public class IODocument extends XMLFragment {
061    
062        private static ILogger LOG = LoggerFactory.getLogger( IODocument.class );
063    
064        private static final long serialVersionUID = -8166238044553562735L;
065    
066        /**
067         * @param element
068         */
069        public IODocument( Element element ) {
070            super( element );
071        }
072    
073        /**
074         * parses a JDBCConnection element and returns the object representation
075         *
076         * @return a jdbc connection object
077         * @throws XMLParsingException
078         */
079        public JDBCConnection parseJDBCConnection()
080                                throws XMLParsingException {
081            JDBCConnection connection = null;
082    
083            Element connectionElement = this.getRootElement();
084            if ( connectionElement != null ) {
085                String driver = XMLTools.getRequiredNodeAsString( connectionElement, "dgjdbc:Driver/text()", nsContext );
086                String logon = XMLTools.getRequiredNodeAsString( connectionElement, "dgjdbc:Url/text()", nsContext );
087                // following code handles the relative paths in the configuration of a hsql datatore,
088                // which was configured with the semi variable ${docRoot}, which actually is a
089                // stringconstant :-)
090                if ( logon.contains( "hsqldb" ) && logon.contains( "${docRoot}" ) ) {
091                    String filename = logon.substring( logon.lastIndexOf( "${docRoot}" ) + "${docRoot}".length() );
092                    int firstSlash = filename.indexOf( '/' );
093                    if ( firstSlash != -1 && firstSlash == 0 ) {
094                        filename = filename.substring( 1 );
095                    }
096                    LOG.logDebug( "found filename: " + filename );
097                    String extension = ".script";
098                    try {
099                        filename = resolve( filename + extension ).toExternalForm();
100                    } catch ( MalformedURLException e ) {
101                        e.printStackTrace();
102                    }
103                    filename = filename.substring( 0, filename.indexOf( extension ) );
104                    LOG.logDebug( "resolved filename: " + filename );
105                    logon = "jdbc:hsqldb:" + filename;
106                }
107    
108                String user = XMLTools.getNodeAsString( connectionElement, "dgjdbc:User/text()", nsContext, null );
109                String password = XMLTools.getNodeAsString( connectionElement, "dgjdbc:Password/text()", nsContext, null );
110                String securityConstraints = XMLTools.getNodeAsString( connectionElement,
111                                                                       "dgjdbc:SecurityConstraints/text()", nsContext, null );
112                String encoding = XMLTools.getNodeAsString( connectionElement, "dgjdbc:Encoding/text()", nsContext,
113                                                            CharsetUtils.getSystemCharset() );
114                String aliasPrefix = XMLTools.getNodeAsString( connectionElement, "dgjdbc:AliasPrefix/text()", nsContext,
115                                                               null );
116                String sdeDatabase = XMLTools.getNodeAsString( connectionElement, "dgjdbc:SDEDatabase/text()", nsContext,
117                                                               null );
118                String sdeVersion = XMLTools.getNodeAsString( connectionElement, "dgjdbc:SDEVersion/text()", nsContext,
119                                                              null );
120                connection = new JDBCConnection( driver, logon, user, password, securityConstraints, encoding, aliasPrefix,
121                                                 sdeDatabase, sdeVersion );
122            }
123            return connection;
124        }
125    
126    }