001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/crs/configuration/resources/XMLFileResource.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 037 package org.deegree.crs.configuration.resources; 038 039 import java.io.BufferedReader; 040 import java.io.FileInputStream; 041 import java.io.IOException; 042 import java.io.InputStream; 043 import java.io.InputStreamReader; 044 import java.io.Reader; 045 import java.util.Properties; 046 047 import org.deegree.crs.configuration.AbstractCRSProvider; 048 import org.deegree.crs.exceptions.CRSConfigurationException; 049 import org.deegree.framework.log.ILogger; 050 import org.deegree.framework.log.LoggerFactory; 051 import org.deegree.framework.xml.XMLFragment; 052 import org.deegree.i18n.Messages; 053 import org.w3c.dom.Element; 054 import org.xml.sax.SAXException; 055 056 /** 057 * The <code>XMLFileResource</code> class TODO add class documentation here. 058 * 059 * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a> 060 * 061 * @author last edited by: $Author: mschneider $ 062 * 063 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 064 * 065 */ 066 public abstract class XMLFileResource extends XMLFragment implements XMLResource { 067 068 private static ILogger LOG = LoggerFactory.getLogger( XMLFileResource.class ); 069 070 private AbstractCRSProvider<Element> provider = null; 071 072 /** 073 * @param provider 074 * to use for the reverse lookup of coordinate systems, required 075 * @param properties 076 * to read the crs configuration file from, required, a property crs.configuration should be present, if 077 * not the crs.default.configuration property is checked, if this is missing as well, a 078 * {@link NullPointerException} will be thrown. 079 * @param requiredRootLocalName 080 * check for the root elements localname, may be <code>null</code> 081 * @param requiredNamespace 082 * check for the root elements namespace, may be <code>null</code> 083 */ 084 public XMLFileResource( AbstractCRSProvider<Element> provider, Properties properties, String requiredRootLocalName, 085 String requiredNamespace ) { 086 if ( properties == null ) { 087 throw new IllegalArgumentException( "The properties may not be null" ); 088 } 089 if ( provider == null ) { 090 throw new NullPointerException( "The provider is null, this may not be." ); 091 } 092 String fileName = properties.getProperty( "crs.configuration" ); 093 Reader read = null; 094 InputStream is = null; 095 try { 096 097 if ( fileName == null || "".equals( fileName ) ) { 098 LOG.logDebug( "No configuration file given, trying to load default file" ); 099 fileName = properties.getProperty( "crs.default.configuration" ); 100 if ( fileName == null || "".equals( fileName ) ) { 101 throw new NullPointerException( 102 "The CRS_FILE property was not set, this resolver can not function without a file. " ); 103 } 104 is = provider.getClass().getResourceAsStream( "/" + fileName ); 105 if ( is == null ) { 106 is = provider.getClass().getResourceAsStream( fileName ); 107 } else { 108 LOG.logDebug( "Using the configuration file loaded from root directory instead of org.deegree.crs.configuration" ); 109 } 110 if ( is == null ) { 111 throw new CRSConfigurationException( Messages.getMessage( "CRS_CONFIG_NO_DEFAULT_CONFIG_FOUND" ) ); 112 } 113 } else { 114 LOG.logDebug( "Trying to load configuration from file: " + fileName ); 115 is = new FileInputStream( fileName ); 116 } 117 read = new BufferedReader( new InputStreamReader( is ) ); 118 119 load( read, XMLFragment.DEFAULT_URL ); 120 if ( getRootElement() == null ) { 121 throw new NullPointerException( "The file: " + fileName + " does not contain a root element. " ); 122 } 123 if ( requiredRootLocalName != null && !"".equals( requiredRootLocalName ) ) { 124 if ( !requiredRootLocalName.equalsIgnoreCase( getRootElement().getLocalName() ) ) { 125 throw new IllegalArgumentException( "The local name of the root element of the given file is not: " 126 + requiredRootLocalName + " aborting." ); 127 } 128 } 129 if ( requiredNamespace != null ) { 130 if ( !requiredNamespace.equals( getRootElement().getNamespaceURI() ) ) { 131 throw new IllegalArgumentException( 132 "The root element of the given file is not in the required namespace: " 133 + requiredNamespace + " aborting." ); 134 } 135 } 136 137 } catch ( SAXException e ) { 138 LOG.logError( e.getLocalizedMessage(), e ); 139 throw new IllegalArgumentException( "File: " + fileName + " is an invalid xml file resource because: " 140 + e.getLocalizedMessage() ); 141 } catch ( IOException e ) { 142 LOG.logError( e.getLocalizedMessage(), e ); 143 throw new IllegalArgumentException( "File: " + fileName + " is an invalid xml file resource because: " 144 + e.getLocalizedMessage() ); 145 } finally { 146 if ( read != null ) { 147 try { 148 read.close(); 149 } catch ( IOException e ) { 150 LOG.logError( e ); 151 } 152 } 153 if ( is != null ) { 154 try { 155 is.close(); 156 } catch ( IOException e ) { 157 LOG.logError( e ); 158 } 159 } 160 } 161 this.provider = provider; 162 } 163 164 /** 165 * @param provider 166 * to be used for callback. 167 * @param rootElement 168 */ 169 public XMLFileResource( AbstractCRSProvider<Element> provider, Element rootElement ) { 170 super( rootElement ); 171 this.provider = provider; 172 } 173 174 /** 175 * @return the provider used for reversed look ups, will never be <code>null</code> 176 */ 177 public AbstractCRSProvider<Element> getProvider() { 178 return provider; 179 } 180 }