mschachter 01/06/21 18:30:51 Modified: resources build.xml resources/src/java/org/apache/commons/resources ConfigurationReader.java FileResource.java LocalStrings.properties MessageResourcesFactory.java ResourceManager.java Added: resources/src/java/org/apache/commons/resources FileResourceFactory.java Log: - add FileResourceFactory - Made ConfigurationReader use Digester - Updated build.xml to use digester package - and whatnot Revision Changes Path 1.2 +9 -2 jakarta-commons-sandbox/resources/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/resources/build.xml,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- build.xml 2001/05/30 16:45:37 1.1 +++ build.xml 2001/06/22 01:30:47 1.2 @@ -3,7 +3,7 @@ <!-- "Resource Facilities" component of the Jakarta Commons Subproject - $Id: build.xml,v 1.1 2001/05/30 16:45:37 craigmcc Exp $ + $Id: build.xml,v 1.2 2001/06/22 01:30:47 mschachter Exp $ --> @@ -20,12 +20,15 @@ <!-- The home directory for the Commons collection classes distribution --> <property name="commons-collections.home" value="../collections/dist"/> + + <!-- The home directory for the Digester package --> + <property name="commons-digester.home" value="../digester/dist" /> <!-- The directory containing your binary distribution of JUnit, version 3.2 or later --> <property name="junit.home" value="/usr/local/junit3.5"/> + - <!-- ========== Derived Values ============================================ --> @@ -34,6 +37,9 @@ <!-- The pathname of the "junit.jar" JAR file --> <property name="junit.jar" value="${junit.home}/junit.jar"/> + + <!-- The pathname of the "digester.jar" JAR file --> + <property name="commons-digester.jar" value="${commons-digester.home}/commons-digester.jar" /> <!-- ========== Component Declarations ==================================== --> @@ -83,6 +89,7 @@ <path id="compile.classpath"> <pathelement location="${build.home}/classes"/> <pathelement location="${commons-collections.jar}"/> + <pathelement location="${commons-digester.jar}"/> </path> 1.2 +89 -2 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ConfigurationReader.java Index: ConfigurationReader.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ConfigurationReader.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ConfigurationReader.java 2001/06/20 21:16:17 1.1 +++ ConfigurationReader.java 2001/06/22 01:30:48 1.2 @@ -6,6 +6,12 @@ import java.util.Map; import java.util.Collection; +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import org.apache.commons.digester.Rule; +import org.apache.commons.digester.Digester; + /** * This class is responsible for reading configuration data for the resouces * and exposing methods that the ResourceManager uses to get data from @@ -14,6 +20,13 @@ public class ConfigurationReader { /** + * The message resources for this class + */ + protected static MessageResources messageResources = + MessageResourcesFactory.createFactory().createResources( + "org.apache.commons.resources.LocalStrings"); + + /** * The Map that represents the resources, where the key is the name of the * resource and the value is the uninitialized Resource object */ @@ -37,7 +50,81 @@ /** * Read the URL "config" and populate the internal resource Map with data */ - public void read(InputStream inputStream) throws IOException { - ; + public void read(InputStream inputStream) throws ResourceException, IOException { + Digester digester = new Digester(); + digester.addRule("resources-config/resources/resource", + new AddResourceRule(digester, this)); + digester.addSetProperty("resources-config/resources/resource/set-property", + "name", "value"); + try { + digester.parse(inputStream); + } + catch (SAXException sax) { + throw new ResourceException( + messageResources.getMessage("resources.config.sax"), sax); + } + } +} + +/** + * This subclass of Rule creates a resource from a resource tag and + * pushes it to the top of the stack, then after the resource is populated + * with properties, pops the resource back out and puts it into the + * ConfigurationReader's resource Map + */ +class AddResourceRule extends Rule { + + protected ConfigurationReader reader; + + public AddResourceRule(Digester digester, ConfigurationReader reader) { + super(digester); + this.reader = reader; + } + + public void begin(Attributes attributes) throws ResourceException { + + //create instance of resource from factory instance + String factoryClass = attributes.getValue("", "factory"); + String config = attributes.getValue("", "config"); + Resource resource = null; + try { + ResourceFactory factory; + Class cFactory = Class.forName(factoryClass); + if (cFactory.isAssignableFrom(ResourceFactory.class)) { + factory = (ResourceFactory) cFactory.newInstance(); + resource = factory.createResource(config); + } + else { + throw new ResourceException( + ConfigurationReader.messageResources.getMessage( + "resource.config.invalidfactory", + cFactory.getClass().getName())); + } + } + catch (ClassNotFoundException cnfe) { + throw new ResourceException( + ConfigurationReader.messageResources.getMessage( + "resources.config.classnotfound", + factoryClass), cnfe); + } + catch (IllegalAccessException iae) { + throw new ResourceException( + ConfigurationReader.messageResources.getMessage( + "resources.config.illegalaccess", + factoryClass), iae); + } + catch (InstantiationException ie) { + throw new ResourceException( + ConfigurationReader.messageResources.getMessage( + "resources.config.instantiation", + factoryClass), ie); + } + //push to top of digester stack for property population + digester.push(resource); + } + + public void end() { + Resource resource = (Resource) digester.pop(); + reader.getResourceMap().put(resource.getName(), resource); } } 1.3 +4 -20 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/FileResource.java Index: FileResource.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/FileResource.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- FileResource.java 2001/06/20 21:16:17 1.2 +++ FileResource.java 2001/06/22 01:30:48 1.3 @@ -40,22 +40,6 @@ */ protected String baseDir = ""; - /** - * Get the name of this resource - * @return A logical String representation of the name of this resource - */ - public String getName() { - return name; - } - - /** - * Set the name of this resource - * @param name The name of this resource - */ - public void setName(String name) { - this.name = name; - } - /** * Sets the base directory of this resource. */ @@ -95,7 +79,7 @@ } catch (IOException ioe) { throw new ResourceException( - messageResources.getMessage("resource.file.ioexception", + messageResources.getMessage("resources.file.ioexception", file.getAbsolutePath()), ioe); } return data; @@ -123,18 +107,18 @@ } catch (FileNotFoundException fnfe) { throw new ResourceException( - messageResources.getMessage("resource.file.filenotfound", + messageResources.getMessage("resources.file.filenotfound", file.getAbsolutePath()), fnfe); } catch (IOException ioe) { throw new ResourceException( - messageResources.getMessage("resource.file.ioexception", + messageResources.getMessage("resources.file.ioexception", file.getAbsolutePath()), ioe); } } else { throw new ResourceException( - messageResources.getMessage("resource.file.filenotfound", + messageResources.getMessage("resources.file.filenotfound", file.getAbsolutePath())); } return stream; 1.2 +8 -3 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/LocalStrings.properties Index: LocalStrings.properties =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/LocalStrings.properties,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- LocalStrings.properties 2001/06/20 21:16:18 1.1 +++ LocalStrings.properties 2001/06/22 01:30:48 1.2 @@ -1,3 +1,8 @@ -resource.file.ioexception=[FileResource] problem opening file {0} -resource.file.filenotfound=[FileResource] file {0} not found -resource.manager.ioexception=[ResourceManager] IOException +resources.file.ioexception=[FileResource] problem opening file {0} +resources.file.filenotfound=[FileResource] file {0} not found +resources.manager.ioexception=[ResourceManager] IOException +resources.config.invalidfactory=[ConfigurationReader] factory class is not of type org.apache.commons.resources.ResourceFactory, but type {1} +resources.config.classnotfound=[ConfigurationReader] class {0} not found +resources.config.instantiation=InstantiationException for class {0} +resources.config.sax=[ConfigurationReader]SAXException while parsing configuration +resources.config.illegalaccess=[ConfigurationReader] IllegalAccessException 1.2 +14 -5 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResourcesFactory.java Index: MessageResourcesFactory.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResourcesFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MessageResourcesFactory.java 2001/05/30 16:45:49 1.1 +++ MessageResourcesFactory.java 2001/06/22 01:30:49 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResourcesFactory.java,v 1.1 2001/05/30 16:45:49 craigmcc Exp $ - * $Revision: 1.1 $ - * $Date: 2001/05/30 16:45:49 $ + * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/MessageResourcesFactory.java,v 1.2 2001/06/22 01:30:49 mschachter Exp $ + * $Revision: 1.2 $ + * $Date: 2001/06/22 01:30:49 $ * * ==================================================================== * @@ -81,10 +81,12 @@ * </ul> * * @author Craig R. McClanahan - * @version $Revision: 1.1 $ $Date: 2001/05/30 16:45:49 $ + * @version $Revision: 1.2 $ $Date: 2001/06/22 01:30:49 $ */ -public abstract class MessageResourcesFactory implements Serializable { +public abstract class MessageResourcesFactory + extends ResourceFactory + implements Serializable { // ---------------------------------------------------- Instance Properties @@ -115,6 +117,13 @@ * @param config Configuration parameter(s) for the requested bundle */ public abstract MessageResources createResources(String config); + + /** + * Implementation of ResourceFactory method + */ + public Resource createResource(String config) { + return createResources(config); + } // ------------------------------------------------------ Static Properties 1.2 +1 -1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourceManager.java Index: ResourceManager.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourceManager.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ResourceManager.java 2001/06/20 21:16:18 1.1 +++ ResourceManager.java 2001/06/22 01:30:49 1.2 @@ -46,7 +46,7 @@ } catch (IOException ioe) { throw new ResourceException( - messageResources.getMessage("resource.manager.ioexception", ioe)); + messageResources.getMessage("resources.manager.ioexception", ioe)); } resources = configReader.getResourceMap(); initResources(); 1.1 jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/FileResourceFactory.java Index: FileResourceFactory.java =================================================================== package org.apache.commons.resources; /** * This is a Subclass of ResourceFactory that defines a method for * creating FileResource objects */ public class FileResourceFactory extends ResourceFactory { public Resource createResource(String config) { return null; } }