Author: rgoers Date: Thu Apr 23 23:32:24 2009 New Revision: 768096 URL: http://svn.apache.org/viewvc?rev=768096&view=rev Log: Allow catalog resolver to use FileSystem. Have System Properties configuration use DefaultConfigurationBuilder's base path
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/resolver/CatalogResolver.java commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testValidation.xml Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java?rev=768096&r1=768095&r2=768096&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/DefaultConfigurationBuilder.java Thu Apr 23 23:32:24 2009 @@ -737,7 +737,7 @@ { try { - SystemConfiguration.setSystemProperties(fileName); + SystemConfiguration.setSystemProperties(getConfigurationBasePath(), fileName); } catch (Exception ex) { @@ -754,6 +754,7 @@ XMLBeanDeclaration decl = new XMLBeanDeclaration(this, KEY_ENTITY_RESOLVER, true); EntityResolver resolver = (EntityResolver) BeanHelper.createBean(decl, CatalogResolver.class); BeanHelper.setProperty(resolver, "fileSystem", getFileSystem()); + BeanHelper.setProperty(resolver, "baseDir", getBasePath()); setEntityResolver(resolver); } } Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java?rev=768096&r1=768095&r2=768096&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/SystemConfiguration.java Thu Apr 23 23:32:24 2009 @@ -38,7 +38,6 @@ super(System.getProperties()); } - /** * The method allows system properties to be set from a property file. * @param fileName The name of the property file. @@ -46,8 +45,25 @@ */ public static void setSystemProperties(String fileName) throws Exception { + setSystemProperties(null, fileName); + } + + /** + * The method allows system properties to be set from a property file. + * @param basePath The base path to look for the property file. + * @param fileName The name of the property file. + * @throws Exception if an error occurs. + */ + public static void setSystemProperties(String basePath, String fileName) throws Exception + { PropertiesConfiguration config = fileName.endsWith(".xml") - ? new XMLPropertiesConfiguration(fileName) : new PropertiesConfiguration(fileName); + ? new XMLPropertiesConfiguration() : new PropertiesConfiguration(); + if (basePath != null) + { + config.setBasePath(basePath); + } + config.setFileName(fileName); + config.load(); setSystemProperties(config); } Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/resolver/CatalogResolver.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/resolver/CatalogResolver.java?rev=768096&r1=768095&r2=768096&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/resolver/CatalogResolver.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/resolver/CatalogResolver.java Thu Apr 23 23:32:24 2009 @@ -19,13 +19,20 @@ import org.xml.sax.EntityResolver; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -import org.apache.xml.resolver.CatalogManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.impl.NoOpLog; import org.apache.commons.configuration2.FileSystem; +import org.apache.commons.configuration2.ConfigurationException; +import org.apache.commons.configuration2.ConfigurationUtils; +import org.apache.xml.resolver.readers.CatalogReader; +import org.apache.xml.resolver.CatalogException; +import java.net.FileNameMap; +import java.net.URLConnection; import java.net.URL; import java.io.InputStream; +import java.io.IOException; +import java.util.Vector; /** * Thin wrapper around xml commons CatalogResolver to allow list of catalogs @@ -44,26 +51,31 @@ private static final int DEBUG_ALL = 9; /** - * Debug nothing. + * Normal debug setting. */ - private static final int DEBUG_NONE = 0; + private static final int DEBUG_NORMAL = 4; /** - * The CatalogResolver + * Debug nothing. */ - protected org.apache.xml.resolver.tools.CatalogResolver resolver; + private static final int DEBUG_NONE = 0; /** * The CatalogManager */ protected CatalogManager manager = new CatalogManager(); - /** + /** * The FileSystem in use. */ protected FileSystem fs = FileSystem.getDefaultFileSystem(); /** + * The CatalogResolver + */ + private org.apache.xml.resolver.tools.CatalogResolver resolver; + + /** * Stores the logger. */ private Log log; @@ -75,6 +87,8 @@ { manager.setIgnoreMissingProperties(true); manager.setUseStaticCatalog(false); + manager.setCatalogClassName(Catalog.class.getName()); + manager.setFileSystem(fs); setLogger(null); } @@ -86,7 +100,6 @@ public void setCatalogFiles(String catalogs) { manager.setCatalogFiles(catalogs); - resolver = new org.apache.xml.resolver.tools.CatalogResolver(manager); } /** @@ -96,6 +109,16 @@ public void setFileSystem(FileSystem fileSystem) { this.fs = fileSystem; + manager.setFileSystem(fileSystem); + } + + /** + * Set the base path. + * @param baseDir The base path String. + */ + public void setBaseDir(String baseDir) + { + manager.setBaseDir(baseDir); } /** @@ -143,7 +166,7 @@ public InputSource resolveEntity(String publicId, String systemId) throws SAXException { - String resolved = resolver.getResolvedEntity(publicId, systemId); + String resolved = getResolver().getResolvedEntity(publicId, systemId); if (resolved != null) { @@ -199,4 +222,208 @@ this.log = (log != null) ? log : new NoOpLog(); } + private synchronized org.apache.xml.resolver.tools.CatalogResolver getResolver() + { + if (resolver == null) + { + resolver = new org.apache.xml.resolver.tools.CatalogResolver(manager); + } + return resolver; + } + + /** + * Extend the CatalogManager to make the FileSystem and base directory accessible. + */ + public static class CatalogManager extends org.apache.xml.resolver.CatalogManager + { + /** The FileSystem */ + private FileSystem fs; + + /** The base directory */ + private String baseDir = System.getProperty("user.dir"); + + /** + * Set the FileSystem + * @param fileSystem The FileSystem in use. + */ + public void setFileSystem(FileSystem fileSystem) + { + this.fs = fileSystem; + } + + /** + * Retrieve the FileSystem. + * @return The FileSystem. + */ + public FileSystem getFileSystem() + { + return this.fs; + } + + /** + * Set the base directory. + * @param baseDir The base directory. + */ + public void setBaseDir(String baseDir) + { + if (baseDir != null) + { + this.baseDir = baseDir; + } + } + + /** + * Return the base directory. + * @return The base directory. + */ + public String getBaseDir() + { + return this.baseDir; + } + } + + /** + * Overrides the Catalog implementation to use the underlying FileSystem. + */ + public static class Catalog extends org.apache.xml.resolver.Catalog + { + /** The FileSystem */ + private FileSystem fs; + + /** FileNameMap to determine the mime type */ + private FileNameMap fileNameMap = URLConnection.getFileNameMap(); + + /** + * Load the catalogs. + * @throws IOException if an error occurs. + */ + public void loadSystemCatalogs() throws IOException + { + fs = ((CatalogManager) catalogManager).getFileSystem(); + String base = ((CatalogManager) catalogManager).getBaseDir(); + + Vector catalogs = catalogManager.getCatalogFiles(); + if (catalogs != null) + { + for (int count = 0; count < catalogs.size(); count++) + { + String fileName = (String) catalogs.elementAt(count); + + URL url = null; + InputStream is = null; + + try + { + url = ConfigurationUtils.locate(fs, base, fileName); + if (url != null) + { + is = fs.getInputStream(url); + } + } + catch (ConfigurationException ce) + { + String name = (url == null) ? fileName : url.toString(); + // Ignore the exception. + catalogManager.debug.message(DEBUG_ALL, + "Unable to get input stream for " + name + ". " + ce.getMessage()); + } + if (is != null) + { + String mimeType = fileNameMap.getContentTypeFor(fileName); + try + { + if (mimeType != null) + { + parseCatalog(mimeType, is); + continue; + } + } + catch (Exception ex) + { + // Ignore the exception. + catalogManager.debug.message(DEBUG_ALL, + "Exception caught parsing input stream for " + fileName + ". " + + ex.getMessage()); + } + finally + { + is.close(); + } + } + parseCatalog(base, fileName); + } + } + + } + + /** + * Parse the specified catalog file. + * @param baseDir The base directory, if not included in the file name. + * @param fileName The catalog file. May be a full URI String. + * @throws IOException If an error occurs. + */ + public void parseCatalog(String baseDir, String fileName) throws IOException + { + base = ConfigurationUtils.locate(fs, baseDir, fileName); + catalogCwd = base; + default_override = catalogManager.getPreferPublic(); + catalogManager.debug.message(DEBUG_NORMAL, "Parse catalog: " + fileName); + + boolean parsed = false; + + for (int count = 0; !parsed && count < readerArr.size(); count++) + { + CatalogReader reader = (CatalogReader) readerArr.get(count); + InputStream inStream; + + try + { + inStream = fs.getInputStream(base); + } + catch (Exception ex) + { + catalogManager.debug.message(DEBUG_NORMAL, "Unable to access " + base + + ex.getMessage()); + break; + } + + try + { + reader.readCatalog(this, inStream); + parsed = true; + } + catch (CatalogException ce) + { + catalogManager.debug.message(DEBUG_NORMAL, "Parse failed for " + fileName + + ce.getMessage()); + if (ce.getExceptionType() == CatalogException.PARSE_FAILED) + { + break; + } + else + { + // try again! + continue; + } + } + finally + { + try + { + inStream.close(); + } + catch (IOException ioe) + { + // Ignore the exception. + inStream = null; + } + } + } + + if (parsed) + { + parsePendingCatalogs(); + } + } + } } Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testValidation.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testValidation.xml?rev=768096&r1=768095&r2=768096&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testValidation.xml (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/resources/testValidation.xml Thu Apr 23 23:32:24 2009 @@ -6,7 +6,7 @@ <nodeCombiner config-class="org.apache.commons.configuration2.tree.OverrideCombiner"/> <expressionEngine config-class="org.apache.commons.configuration2.tree.xpath.XPathExpressionEngine"/> </result> - <entity-resolver catalogFiles="target/test-classes/catalog.xml"/> + <entity-resolver catalogFiles="catalog.xml"/> </header> <system/> <properties fileName="test.properties.xml" throwExceptionOnMissing="true"