Hello, Torque uses Texen's PropertiesUtil object and it was giving us problems in how it guessed where to load files from (e.g. relying on just templatePath being null to insinuate that useClasspath=false).
So, instead of relying on ad hoc mechanisms for guessing where to load a properties file from, I updated PropertiesUtil to instead call RuntimeSingleton.getContent and put the resulting text into a Properties object. The patched PropertiesUtil passes all of the unit tests (test-texen and test-texen-classpath) along with correctly handling for the deprecated $generator.templatePath being in the propertiesFile parameter. And it also gets rid of the messy logic on guessing where to load the file from and instead relies on Velocity's existing resource management infrastructure. Does this look okay to commit? I think it works great, and it passes the tests, but if there are any objections or changes I should make, just let me know. Thanks, Stephen
Index: PropertiesUtil.java =================================================================== RCS file: /home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/texen/util/PropertiesUtil.java,v retrieving revision 1.9 diff -u -r1.9 PropertiesUtil.java --- PropertiesUtil.java 22 Oct 2001 03:45:15 -0000 1.9 +++ PropertiesUtil.java 13 Jul 2002 20:59:22 -0000 @@ -54,11 +54,13 @@ * <http://www.apache.org/>. */ -import java.io.FileInputStream; -import java.io.InputStream; -import java.io.IOException; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; import java.util.Properties; -import java.util.StringTokenizer; + +import org.apache.velocity.runtime.RuntimeSingleton; +import org.apache.velocity.runtime.resource.Resource; import org.apache.velocity.texen.Generator; /** @@ -80,126 +82,44 @@ * @return a properties instance filled with the properties found * in the file or an empty instance if no file was found. */ - public Properties load(String propertiesFile) + public Properties load(String propertiesFile) throws Exception { - Properties properties = new Properties(); + // Chop off the $generator.templatePath/ String templatePath = Generator.getInstance().getTemplatePath(); - if (templatePath != null) + if (templatePath == null) { - properties = loadFromTemplatePath(propertiesFile); + // There might be the literal $generator.templatePath + if (propertiesFile.startsWith("$generator")) + { + propertiesFile = +propertiesFile.substring("$generator.templatePath/".length()); + } } - else + else if (propertiesFile.startsWith(templatePath)) { - properties = loadFromClassPath(propertiesFile); + // $generator.templatePath got interpretted to it's actual value + propertiesFile = propertiesFile.substring(templatePath.length()); } - - return properties; + + // Get the raw string of the file from the Velocity resources + Resource resource = RuntimeSingleton.getContent(propertiesFile); + String propertiesAsString = (String) resource.getData(); - } - - /** - * Load a properties file from the templatePath defined in the - * generator. As the templatePath can contains multiple paths, - * it will cycle through them to find the file. The first file - * that can be successfully loaded is considered. (kind of - * like the java classpath), it is done to clone the Velocity - * process of loading templates. - * - * @param propertiesFile the properties file to load. It must be - * a relative pathname. - * @return a properties instance loaded with the properties from - * the file. If no file can be found it returns an empty instance. - */ - protected Properties loadFromTemplatePath(String propertiesFile) - { - Properties properties = new Properties(); - String templatePath = Generator.getInstance().getTemplatePath(); + // For some reason, we need to prepend a new line to the string + // otherwise the first properties gets ignored when it's loaded into the +properties + propertiesAsString = "\n" + propertiesAsString; - // We might have something like the following: - // - // #set ($dbprops = $properties.load("$generator.templatePath/path/props") - // - // as we have in Torque but we want people to start using - // - // #set ($dbprops = $properties.load("path/props") - // - // so that everything works from the filesystem or from - // a JAR. So the actual Generator.getTemplatePath() - // is not deprecated but it's use in templates - // should be. - StringTokenizer st = new StringTokenizer(templatePath, ","); - while (st.hasMoreTokens()) - { - String templateDir = st.nextToken(); - try - { - // If the properties file is being pulled from the - // file system and someone is using the method whereby - // the properties file is assumed to be in the template - // path and they are simply using: - // - // #set ($dbprops = $properties.load("props") (1) - // - // than we have to tack on the templatePath in order - // for the properties file to be found. We want (1) - // to work whether the generation is being run from - // the file system or from a JAR file. - String fullPath = propertiesFile; - - // FIXME probably not that clever since there could be - // a mix of file separators and the test will fail :-( - if (!fullPath.startsWith(templateDir)) - { - fullPath = templateDir + "/" + propertiesFile; - } - - properties.load(new FileInputStream(fullPath)); - // first pick wins, we don't need to go further since - // we found a valid file. - break; - } - catch (Exception e) - { - // do nothing - } - } - return properties; - } - - /** - * Load a properties file from the classpath - * - * @param propertiesFile the properties file to load. - * @return a properties instance loaded with the properties from - * the file. If no file can be found it returns an empty instance. - */ - protected Properties loadFromClassPath(String propertiesFile) - { + // Write the properties in to a byte array in memory + ByteArrayOutputStream byteOutputStream = new +ByteArrayOutputStream(propertiesAsString.length()); + DataOutputStream dataStream = new DataOutputStream(byteOutputStream); + dataStream.writeUTF(propertiesAsString); + byte[] propertiesInMemory = byteOutputStream.toByteArray(); + + // Now we can get an InputStream and put it into a Properties object + ByteArrayInputStream byteInputStream = new +ByteArrayInputStream(propertiesInMemory); Properties properties = new Properties(); - ClassLoader classLoader = this.getClass().getClassLoader(); + properties.load(byteInputStream); - try - { - // This is a hack for now to make sure that properties - // files referenced in the filesystem work in - // a JAR file. We have to deprecate the use - // of $generator.templatePath in templates first - // and this hack will allow those same templates - // that use $generator.templatePath to work in - // JAR files. - if (propertiesFile.startsWith("$generator")) - { - propertiesFile = propertiesFile.substring( - "$generator.templatePath/".length()); - } - - InputStream inputStream = classLoader.getResourceAsStream(propertiesFile); - properties.load(inputStream); - } - catch (IOException ioe) - { - // do nothing - } return properties; } + }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
