1 package org.igfay.jfig; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 import java.util.TreeMap ; 6 import java.util.regex.Matcher ; 7 import java.util.regex.Pattern ; 8 9 import org.apache.log4j.Logger; 10 import org.igfay.util.PropertyUtility; 11 12 13 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 regexSubstitution; 27 private static Pattern regexGlobalSubstitution; 28 private static Pattern regexPropertyVariable; 29 private JFig config; 30 31 static { 32 PropertyUtility.setProperty(JFigConstants.PATH_SEPARATOR, PropertyUtility.getProperty("path.separator")); 33 } 34 35 41 public JFigParser(JFig config, JFigLocatorIF jfigLocator) { 42 setConfig(config); 43 setJFigLocator(jfigLocator); 44 } 45 46 50 public Map getAllConfigFiles() { 51 return getConfig().getAllConfigFiles(); 52 } 53 54 59 protected abstract boolean processConfig() throws JFigException; 60 61 67 protected void resolveSymbolicValues() throws JFigException { 68 log.debug(""); 69 70 Iterator sectionEnumeration = getConfigDictionary().getSectionIterator(); 71 72 while (sectionEnumeration.hasNext()) { 73 String sectionName = (String ) sectionEnumeration.next(); 74 logDetail.debug("~! sectionName " + sectionName); 75 76 TreeMap sectionHashtable = getConfigDictionary().getSectionDictionary(sectionName); 77 Iterator keyEnumeration = sectionHashtable.keySet().iterator(); 78 79 while (keyEnumeration.hasNext()) { 80 String key = (String ) keyEnumeration.next(); 81 String value = (String ) sectionHashtable.get(key); 82 value = resolvePropertyVariables(sectionName, key, value); 83 resolveSymbolicValues(sectionName, key, value); 84 } } 87 log.debug("end resolveSymbolicValues()"); 88 } 89 90 98 private void resolveSymbolicValues(String section, String key, String value) throws JFigException { 99 logDetail.debug("section " + section + " key " + key + " value " + value); 100 if (value ==null ) { 101 return; 102 } 103 104 String replacementValue = null; 105 String replacementSection = null; 106 String replacementKey = null; 107 String start = null; 108 String rest = null; 109 110 String beforeValue = value; 112 113 Matcher matcher = getRegexSubstitution().matcher(value); 115 while ( (value.indexOf(JFigConstants.SECTION_START_DELIMITER) >= 0)&& matcher.find()) { 116 117 logDetail.debug("~ foundMatch "); 118 start = matcher.group(1); 121 replacementSection = matcher.group(3); 122 123 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 replacementValue = getConfigDictionary().getValue(replacementSection, replacementKey, "null"); 134 logDetail.debug("~! rValue " + replacementValue); 135 136 value = start + replacementValue + rest; 138 logDetail.debug("~! value " + value); 139 matcher = getRegexSubstitution().matcher(value); 140 } 142 if (!beforeValue.equals(value)) { 144 logDetail.debug("~!resolveSymbolicValues() replace " + beforeValue + " with " + value); 145 getConfigDictionary().setConfigurationValue(section, key, value); 147 } 148 } 149 150 158 private String resolvePropertyVariables(String section, String key, String value) { 159 if (value == null) { 160 return null; 161 } 162 String replacementValue = null; 163 String replacementKey = null; 164 String start = null; 165 String rest = null; 166 167 String beforeValue = value; 169 170 Matcher 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 start = matcher.group(1); 177 replacementKey = matcher.group(3); 178 rest = matcher.group(5); 179 logDetail.debug("~! -- rKey " + replacementKey + " rest: " + rest); 180 181 replacementValue = PropertyUtility.getProperty(replacementKey, ""); 183 logDetail.debug("~!rValue " + replacementValue); 184 185 value = start + replacementValue + rest; 187 logDetail.debug("~!resolvePropertyVariables value " + value); 188 matcher = getRegexPropertyVariable().matcher(value); 189 } 190 191 if (!beforeValue.equals(value)) { 194 logDetail.debug("~!replace " + beforeValue + " with\n\t" + value); 195 getConfigDictionary().setConfigurationValue(section, key, value); 196 197 } 199 200 return value; 201 } 202 203 protected void addPropertyValues() { 204 log.debug(""); 205 206 TreeMap map = getConfigDictionary().getSectionNamed("properties", false); 207 if (map != null) { 208 Iterator iterator = map.keySet().iterator(); 209 while (iterator.hasNext()) { 210 String key = (String )iterator.next(); 211 String value = (String )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 getConfigFileName() { 222 return getJFigLocator().getConfigFileName(); 223 } 224 225 protected static Pattern 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 getRegexSubstitution() throws JFigException { 234 if (regexSubstitution == null) { 235 regexSubstitution = Pattern.compile("(.*)(\\" + JFigConstants.SECTION_START_DELIMITER + ")(.*?)(\\" + 237 JFigConstants.SECTION_END_DELIMITER+"?\\" + JFigConstants.KEY_START_DELIMITER + ")(.*?)(\\" + JFigConstants.KEY_END_DELIMITER + 239 ")(.*?)$", Pattern.MULTILINE); 240 } 241 242 return regexSubstitution; 243 } 244 245 protected static Pattern getRegexGlobalSubstitution() throws JFigException { 247 if (regexGlobalSubstitution == null) { 248 regexGlobalSubstitution = Pattern.compile("(.*)(\\" + JFigConstants.SECTION_START_DELIMITER + ")(.*?)(\\" + 250 JFigConstants.SECTION_END_DELIMITER+"?\\" + JFigConstants.KEY_START_DELIMITER + ")(.*?)(\\" + JFigConstants.KEY_END_DELIMITER + 252 ")([.|\n|\r]*?)", Pattern.MULTILINE); 253 } 254 255 return regexGlobalSubstitution; 256 } 257 258 263 267 271 275 279 282 public JFig getConfig() { 283 return config; 284 } 285 286 289 public void setConfig(JFig config) { 290 this.config = config; 291 } 292 295 public JFigLocatorIF getJFigLocator() { 296 return jfigLocator; 297 } 298 299 302 public void setJFigLocator(JFigLocatorIF locatorIF) { 303 jfigLocator = locatorIF; 304 } 305 306 } 307 | Popular Tags |