KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.igfay.jfig;
2
3 import java.io.File JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.util.regex.Matcher JavaDoc;
6 import java.util.regex.Pattern JavaDoc;
7
8 import org.apache.log4j.Logger;
9 import org.igfay.util.FileUtility;
10 import org.igfay.util.PropertyUtility;
11
12 /**
13  * @author conrad4
14  *
15  * Perform JFig substitution on an InputStream.
16  *
17  */

18 public class JFigConverter {
19
20     private static Logger log = Logger.getLogger(JFigParser.class);
21     private static Logger logDetail = Logger.getLogger("ConverterDetail");
22     
23     public String JavaDoc convert(File JavaDoc file) throws JFigException {
24         String JavaDoc string = FileUtility.contentsOfFile(file);
25         return convert(string);
26     }
27
28
29     public String JavaDoc convert(File JavaDoc file, File JavaDoc outputFile) throws JFigException {
30         String JavaDoc string = FileUtility.contentsOfFile(file);
31         //log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
32
//log.info("~string \n"+string);
33
string = convert(string);
34         FileUtility.stringToFile(string, outputFile);
35         return string;
36     }
37
38     public String JavaDoc convert(InputStream JavaDoc inputStream) throws JFigException {
39         String JavaDoc string = FileUtility.streamToString(inputStream);
40         return convert(string);
41     }
42
43     public String JavaDoc convert(String JavaDoc value) throws JFigException {
44         value = resolveSymbolicValues(value);
45         value = resolvePropertyVariables(value);
46         return value;
47     }
48
49     public void convert(InputStream JavaDoc inputStream, File JavaDoc file ) {
50         return;
51     }
52
53     /**
54      * Replace all occurances of symbolic references with actual values. A
55      * symbolic reference looks like "&(section)key".
56      *
57      *@param section Description of Parameter
58      *@param key Description of Parameter
59      *@param value Description of Parameter
60      */

61     private String JavaDoc resolveSymbolicValues(String JavaDoc value) throws JFigException {
62         logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n value " + value);
63         if (value ==null ) {
64             return null;
65         }
66         
67         String JavaDoc replacementValue = null;
68         String JavaDoc replacementSection = null;
69         String JavaDoc replacementKey = null;
70         String JavaDoc start = null;
71         String JavaDoc rest = null;
72         logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~\n ");
73         
74         // Loop through value while it has a symbolic reference
75
Pattern JavaDoc reGlobal = JFigParser.getRegexSubstitution();
76         Matcher JavaDoc matcher = reGlobal.matcher(value);
77         while ( (value.indexOf(JFigConstants.SECTION_START_DELIMITER) >= 0) && matcher.find()) {
78             logDetail.debug("~!foundMatch ");
79
80             // Get the values that we matched in our regular expression
81
int index3 = matcher.start(3)-1;
82             start = value.substring(0,index3);
83             
84             replacementSection = matcher.group(3);
85             replacementKey = matcher.group(5);
86
87             int index6 = matcher.end(6);
88             rest = value.substring(index6);
89
90             // Get the value for the section/key that we are referencing
91
replacementValue = JFig.getInstance().getValue(replacementSection, replacementKey, "null");
92             logDetail.debug("~ rValue " + replacementValue);
93
94             // Substitute the reference with the new value.
95
value = start + replacementValue + rest;
96             logDetail.debug("~ value " + value);
97             matcher = reGlobal.matcher(value);
98         } // end while
99

100         return value;
101     }
102
103     /**
104      * Replace all occurances of Property variables with properties.
105      * Property variable looks like "$PropertyVariable$".
106      *
107      *@param section Description of Parameter
108      *@param key Description of Parameter
109      *@param value Description of Parameter
110      */

111     private String JavaDoc resolvePropertyVariables(String JavaDoc value) {
112         if (value == null) {
113             return null;
114         }
115         String JavaDoc replacementValue = null;
116         String JavaDoc replacementKey = null;
117         String JavaDoc start = null;
118         String JavaDoc rest = null;
119
120         // Loop through value while it has a symbolic reference
121
Matcher JavaDoc matcher = JFigParser.getRegexPropertyVariable().matcher(value);
122         while ((value.indexOf(JFigConstants.PROPERTY_DELIMITER) >= 0) && matcher.find()) {
123             logDetail.debug("~ value " + value);
124
125             // Get the values that we matched in our regular expression
126
int index3 = matcher.start(3)-1;
127             start = value.substring(0,index3);
128
129             // Get the value for the property that we are referencing
130
replacementKey = matcher.group(3);
131             replacementValue = PropertyUtility.getProperty(replacementKey, "");
132             
133             int indexRest = matcher.end(4);
134             rest = value.substring(indexRest);
135             logDetail.debug("~ -- rKey " + replacementKey + " rest: " + rest);
136
137             // Substitute the reference with the new value.
138
value = start + replacementValue + rest;
139             logDetail.debug("~ value " + value);
140             matcher = JFigParser.getRegexPropertyVariable().matcher(value);
141
142         }// end while
143

144         return value;
145     }
146
147
148
149 }
150
Popular Tags