001: package org.igfay.jfig;
002:
003: import java.io.File;
004: import java.io.InputStream;
005: import java.util.regex.Matcher;
006: import java.util.regex.Pattern;
007:
008: import org.apache.log4j.Logger;
009: import org.igfay.util.FileUtility;
010: import org.igfay.util.PropertyUtility;
011:
012: /**
013: * @author conrad4
014: *
015: * Perform JFig substitution on an InputStream.
016: *
017: */
018: public class JFigConverter {
019:
020: private static Logger log = Logger.getLogger(JFigParser.class);
021: private static Logger logDetail = Logger
022: .getLogger("ConverterDetail");
023:
024: public String convert(File file) throws JFigException {
025: String string = FileUtility.contentsOfFile(file);
026: return convert(string);
027: }
028:
029: public String convert(File file, File outputFile)
030: throws JFigException {
031: String string = FileUtility.contentsOfFile(file);
032: //log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
033: //log.info("~string \n"+string);
034: string = convert(string);
035: FileUtility.stringToFile(string, outputFile);
036: return string;
037: }
038:
039: public String convert(InputStream inputStream) throws JFigException {
040: String string = FileUtility.streamToString(inputStream);
041: return convert(string);
042: }
043:
044: public String convert(String value) throws JFigException {
045: value = resolveSymbolicValues(value);
046: value = resolvePropertyVariables(value);
047: return value;
048: }
049:
050: public void convert(InputStream inputStream, File file) {
051: return;
052: }
053:
054: /**
055: * Replace all occurances of symbolic references with actual values. A
056: * symbolic reference looks like "&(section)key".
057: *
058: *@param section Description of Parameter
059: *@param key Description of Parameter
060: *@param value Description of Parameter
061: */
062: private String resolveSymbolicValues(String value)
063: throws JFigException {
064: logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n value "
065: + value);
066: if (value == null) {
067: return null;
068: }
069:
070: String replacementValue = null;
071: String replacementSection = null;
072: String replacementKey = null;
073: String start = null;
074: String rest = null;
075: logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~\n ");
076:
077: // Loop through value while it has a symbolic reference
078: Pattern reGlobal = JFigParser.getRegexSubstitution();
079: Matcher matcher = reGlobal.matcher(value);
080: while ((value.indexOf(JFigConstants.SECTION_START_DELIMITER) >= 0)
081: && matcher.find()) {
082: logDetail.debug("~!foundMatch ");
083:
084: // Get the values that we matched in our regular expression
085: int index3 = matcher.start(3) - 1;
086: start = value.substring(0, index3);
087:
088: replacementSection = matcher.group(3);
089: replacementKey = matcher.group(5);
090:
091: int index6 = matcher.end(6);
092: rest = value.substring(index6);
093:
094: // Get the value for the section/key that we are referencing
095: replacementValue = JFig.getInstance().getValue(
096: replacementSection, replacementKey, "null");
097: logDetail.debug("~ rValue " + replacementValue);
098:
099: // Substitute the reference with the new value.
100: value = start + replacementValue + rest;
101: logDetail.debug("~ value " + value);
102: matcher = reGlobal.matcher(value);
103: } // end while
104:
105: return value;
106: }
107:
108: /**
109: * Replace all occurances of Property variables with properties.
110: * Property variable looks like "$PropertyVariable$".
111: *
112: *@param section Description of Parameter
113: *@param key Description of Parameter
114: *@param value Description of Parameter
115: */
116: private String resolvePropertyVariables(String value) {
117: if (value == null) {
118: return null;
119: }
120: String replacementValue = null;
121: String replacementKey = null;
122: String start = null;
123: String rest = null;
124:
125: // Loop through value while it has a symbolic reference
126: Matcher matcher = JFigParser.getRegexPropertyVariable()
127: .matcher(value);
128: while ((value.indexOf(JFigConstants.PROPERTY_DELIMITER) >= 0)
129: && matcher.find()) {
130: logDetail.debug("~ value " + value);
131:
132: // Get the values that we matched in our regular expression
133: int index3 = matcher.start(3) - 1;
134: start = value.substring(0, index3);
135:
136: // Get the value for the property that we are referencing
137: replacementKey = matcher.group(3);
138: replacementValue = PropertyUtility.getProperty(
139: replacementKey, "");
140:
141: int indexRest = matcher.end(4);
142: rest = value.substring(indexRest);
143: logDetail.debug("~ -- rKey " + replacementKey
144: + " rest: " + rest);
145:
146: // Substitute the reference with the new value.
147: value = start + replacementValue + rest;
148: logDetail.debug("~ value " + value);
149: matcher = JFigParser.getRegexPropertyVariable().matcher(
150: value);
151:
152: }// end while
153:
154: return value;
155: }
156:
157: }
|