KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > texen > util > PropertiesUtil


1 package org.apache.velocity.texen.util;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.FileInputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import org.apache.velocity.texen.Generator;
25
26 /**
27  * A property utility class for the texen text/code generator
28  * Usually this class is only used from a Velocity context.
29  *
30  * @author <a HREF="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
31  * @author <a HREF="mailto:sbailliez@apache.org">Stephane Bailliez</a>
32  * @version $Id: PropertiesUtil.java,v 1.9.8.1 2004/03/03 23:23:07 geirm Exp $
33  */

34 public class PropertiesUtil
35 {
36     /**
37      * Load properties from either a file in the templatePath if there
38      * is one or the classPath.
39      *
40      * @param propertiesFile the properties file to load through
41      * either the templatePath or the classpath.
42      * @return a properties instance filled with the properties found
43      * in the file or an empty instance if no file was found.
44      */

45     public Properties JavaDoc load(String JavaDoc propertiesFile)
46     {
47         Properties JavaDoc properties = new Properties JavaDoc();
48         String JavaDoc templatePath = Generator.getInstance().getTemplatePath();
49         if (templatePath != null)
50         {
51             properties = loadFromTemplatePath(propertiesFile);
52         }
53         else
54         {
55             properties = loadFromClassPath(propertiesFile);
56         }
57     
58         return properties;
59         
60     }
61     
62     /**
63      * Load a properties file from the templatePath defined in the
64      * generator. As the templatePath can contains multiple paths,
65      * it will cycle through them to find the file. The first file
66      * that can be successfully loaded is considered. (kind of
67      * like the java classpath), it is done to clone the Velocity
68      * process of loading templates.
69      *
70      * @param propertiesFile the properties file to load. It must be
71      * a relative pathname.
72      * @return a properties instance loaded with the properties from
73      * the file. If no file can be found it returns an empty instance.
74      */

75     protected Properties JavaDoc loadFromTemplatePath(String JavaDoc propertiesFile)
76     {
77         Properties JavaDoc properties = new Properties JavaDoc();
78         String JavaDoc templatePath = Generator.getInstance().getTemplatePath();
79         
80         // We might have something like the following:
81
//
82
// #set ($dbprops = $properties.load("$generator.templatePath/path/props")
83
//
84
// as we have in Torque but we want people to start using
85
//
86
// #set ($dbprops = $properties.load("path/props")
87
//
88
// so that everything works from the filesystem or from
89
// a JAR. So the actual Generator.getTemplatePath()
90
// is not deprecated but it's use in templates
91
// should be.
92
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(templatePath, ",");
93         while (st.hasMoreTokens())
94         {
95             String JavaDoc templateDir = st.nextToken();
96             try
97             {
98                 // If the properties file is being pulled from the
99
// file system and someone is using the method whereby
100
// the properties file is assumed to be in the template
101
// path and they are simply using:
102
//
103
// #set ($dbprops = $properties.load("props") (1)
104
//
105
// than we have to tack on the templatePath in order
106
// for the properties file to be found. We want (1)
107
// to work whether the generation is being run from
108
// the file system or from a JAR file.
109
String JavaDoc fullPath = propertiesFile;
110                 
111                 // FIXME probably not that clever since there could be
112
// a mix of file separators and the test will fail :-(
113
if (!fullPath.startsWith(templateDir))
114                 {
115                     fullPath = templateDir + "/" + propertiesFile;
116                 }
117
118                 properties.load(new FileInputStream JavaDoc(fullPath));
119                 // first pick wins, we don't need to go further since
120
// we found a valid file.
121
break;
122             }
123             catch (Exception JavaDoc e)
124             {
125                 // do nothing
126
}
127         }
128         return properties;
129     }
130
131     /**
132      * Load a properties file from the classpath
133      *
134      * @param propertiesFile the properties file to load.
135      * @return a properties instance loaded with the properties from
136      * the file. If no file can be found it returns an empty instance.
137      */

138     protected Properties JavaDoc loadFromClassPath(String JavaDoc propertiesFile)
139     {
140         Properties JavaDoc properties = new Properties JavaDoc();
141         ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
142         
143         try
144         {
145             // This is a hack for now to make sure that properties
146
// files referenced in the filesystem work in
147
// a JAR file. We have to deprecate the use
148
// of $generator.templatePath in templates first
149
// and this hack will allow those same templates
150
// that use $generator.templatePath to work in
151
// JAR files.
152
if (propertiesFile.startsWith("$generator"))
153             {
154                 propertiesFile = propertiesFile.substring(
155                     "$generator.templatePath/".length());
156             }
157             
158             InputStream JavaDoc inputStream = classLoader.getResourceAsStream(propertiesFile);
159             properties.load(inputStream);
160         }
161         catch (IOException JavaDoc ioe)
162         {
163             // do nothing
164
}
165         return properties;
166     }
167 }
168
Popular Tags