KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > igfay > jfig > JFigParser


1 package org.igfay.jfig;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.TreeMap JavaDoc;
6 import java.util.regex.Matcher JavaDoc;
7 import java.util.regex.Pattern JavaDoc;
8
9 import org.apache.log4j.Logger;
10 import org.igfay.util.PropertyUtility;
11
12
13 /**
14  * Abstract class to parse a configuration file. Concrete classes parse either
15  * XML or INI file. This class contains common logic to resolve symbolic and
16  * property variables.
17  *
18  *@author bconrad
19  *@created April 3, 2001
20  */

21 public abstract class JFigParser {
22     private static Logger log = Logger.getLogger(JFigParser.class);
23     private static Logger logDetail = Logger.getLogger("ConfigDetail");
24     private JFigLocatorIF jfigLocator;
25
26     private static Pattern JavaDoc regexSubstitution;
27     private static Pattern JavaDoc regexGlobalSubstitution;
28     private static Pattern JavaDoc regexPropertyVariable;
29     private JFig config;
30
31     static {
32         PropertyUtility.setProperty(JFigConstants.PATH_SEPARATOR, PropertyUtility.getProperty("path.separator"));
33     }
34
35     /**
36      * Constructor to allow user to override configFileName default.
37      * Get the location from ConfigLocator
38      *
39      *@param configFileName Description of Parameter
40      */

41     public JFigParser(JFig config, JFigLocatorIF jfigLocator) {
42         setConfig(config);
43         setJFigLocator(jfigLocator);
44     }
45
46     /**
47      * Returns the allConfigFiles.
48      * @return List
49      */

50     public Map JavaDoc getAllConfigFiles() {
51         return getConfig().getAllConfigFiles();
52     }
53
54     /**
55      * processConfig
56      * @return Description of the Returned Value
57      *@exception JFigException Description of Exception
58      */

59     protected abstract boolean processConfig() throws JFigException;
60
61     /**
62      * Resolve symbolic values in the configuration. This method is
63      * called after all configuration files have been parsed.
64      *
65      * Iterate through the dictionary and resolve values for each section/key.
66      */

67     protected void resolveSymbolicValues() throws JFigException {
68         log.debug("");
69
70         Iterator JavaDoc sectionEnumeration = getConfigDictionary().getSectionIterator();
71
72         while (sectionEnumeration.hasNext()) {
73             String JavaDoc sectionName = (String JavaDoc) sectionEnumeration.next();
74             logDetail.debug("~! sectionName " + sectionName);
75
76             TreeMap JavaDoc sectionHashtable = getConfigDictionary().getSectionDictionary(sectionName);
77             Iterator JavaDoc keyEnumeration = sectionHashtable.keySet().iterator();
78
79             while (keyEnumeration.hasNext()) {
80                 String JavaDoc key = (String JavaDoc) keyEnumeration.next();
81                 String JavaDoc value = (String JavaDoc) sectionHashtable.get(key);
82                 value = resolvePropertyVariables(sectionName, key, value);
83                 resolveSymbolicValues(sectionName, key, value);
84             } // while keyEnumeration
85
} // while sectionEnumeration
86

87         log.debug("end resolveSymbolicValues()");
88     }
89
90     /**
91      * Replace all occurances of symbolic references with actual values. A
92      * symbolic reference looks like "&(section)key".
93      *
94      *@param section Description of Parameter
95      *@param key Description of Parameter
96      *@param value Description of Parameter
97      */

98     private void resolveSymbolicValues(String JavaDoc section, String JavaDoc key, String JavaDoc value) throws JFigException {
99         logDetail.debug("section " + section + " key " + key + " value " + value);
100         if (value ==null ) {
101             return;
102         }
103         
104         String JavaDoc replacementValue = null;
105         String JavaDoc replacementSection = null;
106         String JavaDoc replacementKey = null;
107         String JavaDoc start = null;
108         String JavaDoc rest = null;
109
110         // Save the value
111
String JavaDoc beforeValue = value;
112
113         // Loop through value while it has a symbolic reference
114
Matcher JavaDoc matcher = getRegexSubstitution().matcher(value);
115         while ( (value.indexOf(JFigConstants.SECTION_START_DELIMITER) >= 0)&& matcher.find()) {
116             
117             logDetail.debug("~ foundMatch ");
118             //matcher = getRegexSubstitution().matcher(value);
119
// Get the values that we matched in our regular expression
120
start = matcher.group(1);
121             replacementSection = matcher.group(3);
122
123             // If the replacement section is the special keyword "section" then use the current section
124
if (replacementSection.equalsIgnoreCase("section")) {
125                 replacementSection = section;
126             }
127
128             replacementKey = matcher.group(5);
129             rest = matcher.group(7);
130             logDetail.debug("~! rSection " + replacementSection + " rKey " + replacementKey);
131
132             // Get the value for the section/key that we are referencing
133
replacementValue = getConfigDictionary().getValue(replacementSection, replacementKey, "null");
134             logDetail.debug("~! rValue " + replacementValue);
135
136             // Substitute the reference with the new value.
137
value = start + replacementValue + rest;
138             logDetail.debug("~! value " + value);
139             matcher = getRegexSubstitution().matcher(value);
140         } // end while
141

142         // Set the new substituted value, if it has changed
143
if (!beforeValue.equals(value)) {
144             logDetail.debug("~!resolveSymbolicValues() replace " + beforeValue + " with " + value);
145             // put back the modified value.
146
getConfigDictionary().setConfigurationValue(section, key, value);
147         }
148     }
149
150     /**
151      * Replace all occurances of Property variables with properties.
152      * Property variable looks like "$PropertyVariable$".
153      *
154      *@param section Description of Parameter
155      *@param key Description of Parameter
156      *@param value Description of Parameter
157      */

158     private String JavaDoc resolvePropertyVariables(String JavaDoc section, String JavaDoc key, String JavaDoc value) {
159         if (value == null) {
160             return null;
161         }
162         String JavaDoc replacementValue = null;
163         String JavaDoc replacementKey = null;
164         String JavaDoc start = null;
165         String JavaDoc rest = null;
166
167         // Save the value
168
String JavaDoc beforeValue = value;
169
170         // Loop through value while it has a symbolic reference
171
Matcher JavaDoc matcher = getRegexPropertyVariable().matcher(value);
172         while ((value.indexOf(JFigConstants.PROPERTY_DELIMITER) >= 0) && matcher.find()) {
173             logDetail.debug("~!section " + section + " key " + key + " value " + value);
174
175             // Get the values that we matched in our regular expression
176
start = matcher.group(1);
177             replacementKey = matcher.group(3);
178             rest = matcher.group(5);
179             logDetail.debug("~! -- rKey " + replacementKey + " rest: " + rest);
180
181             // Get the value for the section/key that we are referencing
182
replacementValue = PropertyUtility.getProperty(replacementKey, "");
183             logDetail.debug("~!rValue " + replacementValue);
184
185             // Substitute the reference with the new value.
186
value = start + replacementValue + rest;
187             logDetail.debug("~!resolvePropertyVariables value " + value);
188             matcher = getRegexPropertyVariable().matcher(value);
189         }
190
191         // end while
192
// Set the new substituted value, if it has changed
193
if (!beforeValue.equals(value)) {
194             logDetail.debug("~!replace " + beforeValue + " with\n\t" + value);
195             getConfigDictionary().setConfigurationValue(section, key, value);
196
197             // put back the modified value.
198
}
199
200         return value;
201     }
202
203     protected void addPropertyValues() {
204         log.debug("");
205
206         TreeMap JavaDoc map = getConfigDictionary().getSectionNamed("properties", false);
207         if (map != null) {
208             Iterator JavaDoc iterator = map.keySet().iterator();
209             while (iterator.hasNext()) {
210                 String JavaDoc key = (String JavaDoc)iterator.next();
211                 String JavaDoc value = (String JavaDoc)map.get(key);
212                 PropertyUtility.setProperty(key,value);
213             }
214         }
215
216     }
217     protected JFigDictionary getConfigDictionary() {
218         return getConfig().getConfigDictionary();
219     }
220
221     protected String JavaDoc getConfigFileName() {
222         return getJFigLocator().getConfigFileName();
223     }
224
225     protected static Pattern JavaDoc getRegexPropertyVariable() {
226         if (regexPropertyVariable == null) {
227             regexPropertyVariable = Pattern.compile("(.*?)(\\"+JFigConstants.PROPERTY_DELIMITER+")(.*?)(\\"+JFigConstants.PROPERTY_DELIMITER+")(.*?)$", Pattern.MULTILINE);
228         }
229
230         return regexPropertyVariable;
231     }
232
233     protected static Pattern JavaDoc getRegexSubstitution() throws JFigException {
234         if (regexSubstitution == null) {
235             // ? sets to non-greedy. \\) says look for literal ")"
236
regexSubstitution = Pattern.compile("(.*)(\\" + JFigConstants.SECTION_START_DELIMITER + ")(.*?)(\\" +
237              JFigConstants.SECTION_END_DELIMITER+"?\\" + // 0 or 1 end section delimiter
238
JFigConstants.KEY_START_DELIMITER + ")(.*?)(\\" + JFigConstants.KEY_END_DELIMITER +
239              ")(.*?)$", Pattern.MULTILINE);
240         }
241         
242         return regexSubstitution;
243     }
244
245     // for convert
246
protected static Pattern JavaDoc getRegexGlobalSubstitution() throws JFigException {
247         if (regexGlobalSubstitution == null) {
248             // ? sets to non-greedy. \\) says look for literal ")"
249
regexGlobalSubstitution = Pattern.compile("(.*)(\\" + JFigConstants.SECTION_START_DELIMITER + ")(.*?)(\\" +
250                 JFigConstants.SECTION_END_DELIMITER+"?\\" + // 0 or 1 end section delimiter
251
JFigConstants.KEY_START_DELIMITER + ")(.*?)(\\" + JFigConstants.KEY_END_DELIMITER +
252                 ")([.|\n|\r]*?)", Pattern.MULTILINE);
253         }
254         
255         return regexGlobalSubstitution;
256     }
257
258 // protected void setConfigFileName(String configFileName) {
259
// this.configFileName = configFileName;
260
// log.info("configFileName " + configFileName);
261
// }
262

263     /**
264      * Returns the configLocation.
265      * @return String
266      */

267 // public String getConfigLocation() {
268
// return getJFigLocator().getConfigLocation();
269
// }
270

271     /**
272      * Sets the configLocation.
273      * @param configLocation The configLocation to set
274      */

275 // public void setConfigLocation(String configLocation) {
276
// this.configLocation = configLocation;
277
// }
278

279     /**
280      * @return
281      */

282     public JFig getConfig() {
283         return config;
284     }
285
286     /**
287      * @param config
288      */

289     public void setConfig(JFig config) {
290         this.config = config;
291     }
292     /**
293      * @return
294      */

295     public JFigLocatorIF getJFigLocator() {
296         return jfigLocator;
297     }
298
299     /**
300      * @param locatorIF
301      */

302     public void setJFigLocator(JFigLocatorIF locatorIF) {
303         jfigLocator = locatorIF;
304     }
305
306 }
307
Popular Tags