1 package org.igfay.jfig; 2 3 import java.io.File ; 4 import java.io.InputStream ; 5 import java.util.regex.Matcher ; 6 import java.util.regex.Pattern ; 7 8 import org.apache.log4j.Logger; 9 import org.igfay.util.FileUtility; 10 import org.igfay.util.PropertyUtility; 11 12 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 convert(File file) throws JFigException { 24 String string = FileUtility.contentsOfFile(file); 25 return convert(string); 26 } 27 28 29 public String convert(File file, File outputFile) throws JFigException { 30 String string = FileUtility.contentsOfFile(file); 31 string = convert(string); 34 FileUtility.stringToFile(string, outputFile); 35 return string; 36 } 37 38 public String convert(InputStream inputStream) throws JFigException { 39 String string = FileUtility.streamToString(inputStream); 40 return convert(string); 41 } 42 43 public String convert(String value) throws JFigException { 44 value = resolveSymbolicValues(value); 45 value = resolvePropertyVariables(value); 46 return value; 47 } 48 49 public void convert(InputStream inputStream, File file ) { 50 return; 51 } 52 53 61 private String resolveSymbolicValues(String value) throws JFigException { 62 logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n value " + value); 63 if (value ==null ) { 64 return null; 65 } 66 67 String replacementValue = null; 68 String replacementSection = null; 69 String replacementKey = null; 70 String start = null; 71 String rest = null; 72 logDetail.debug("~~~~~~~~~~~~~~~~~~~~~~~~~\n "); 73 74 Pattern reGlobal = JFigParser.getRegexSubstitution(); 76 Matcher matcher = reGlobal.matcher(value); 77 while ( (value.indexOf(JFigConstants.SECTION_START_DELIMITER) >= 0) && matcher.find()) { 78 logDetail.debug("~!foundMatch "); 79 80 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 replacementValue = JFig.getInstance().getValue(replacementSection, replacementKey, "null"); 92 logDetail.debug("~ rValue " + replacementValue); 93 94 value = start + replacementValue + rest; 96 logDetail.debug("~ value " + value); 97 matcher = reGlobal.matcher(value); 98 } 100 return value; 101 } 102 103 111 private String resolvePropertyVariables(String value) { 112 if (value == null) { 113 return null; 114 } 115 String replacementValue = null; 116 String replacementKey = null; 117 String start = null; 118 String rest = null; 119 120 Matcher matcher = JFigParser.getRegexPropertyVariable().matcher(value); 122 while ((value.indexOf(JFigConstants.PROPERTY_DELIMITER) >= 0) && matcher.find()) { 123 logDetail.debug("~ value " + value); 124 125 int index3 = matcher.start(3)-1; 127 start = value.substring(0,index3); 128 129 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 value = start + replacementValue + rest; 139 logDetail.debug("~ value " + value); 140 matcher = JFigParser.getRegexPropertyVariable().matcher(value); 141 142 } 144 return value; 145 } 146 147 148 149 } 150 | Popular Tags |