1 17 18 package org.apache.james.util; 19 20 import org.apache.oro.text.perl.MalformedPerl5PatternException; 21 import org.apache.oro.text.perl.Perl5Util; 22 import org.w3c.dom.*; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import java.io.File ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 70 public class XMLResources 71 { 72 75 private Map m_resource = new HashMap (); 76 77 80 static private Map stringTable = java.util.Collections.synchronizedMap(new HashMap ()); 81 82 85 private Perl5Util m_perl5Util = new Perl5Util(); 86 87 100 public void init(File xmlFile, String group, 101 String select, Map configParameters) 102 throws Exception 103 { 104 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 106 DocumentBuilder builder = factory.newDocumentBuilder(); 107 Document doc = builder.parse(xmlFile); 108 109 Element matcherElement = 111 (Element)(doc.getElementsByTagName("matchers").item(0)); 112 String selectTag = null; 113 if ( matcherElement != null ) { 114 selectTag = match(select, matcherElement); 115 m_perl5Util = null; } 117 118 NodeList sections = doc.getElementsByTagName("group"); 120 int sectionsCount = sections.getLength(); 121 Element sectionElement = null; 122 for (int i = 0; i < sectionsCount; i++ ) { 123 sectionElement = (Element)(sections.item(i)); 124 String sectionName = sectionElement.getAttribute("name"); 125 if ( sectionName != null && sectionName.equals(group) ) { 126 break; 127 } 128 129 } 130 if ( sectionElement == null ) { 131 StringBuffer exceptionBuffer = 132 new StringBuffer (64) 133 .append("Error loading string definition file. ") 134 .append("The element named \'") 135 .append(group) 136 .append("\' does not exist."); 137 throw new RuntimeException (exceptionBuffer.toString()); 138 } 139 140 Map parameters = new HashMap (); 143 Element parametersElement = 145 (Element)(sectionElement.getElementsByTagName("parameters").item(0)); 146 if ( parametersElement != null ) { 147 NamedNodeMap params = parametersElement.getAttributes(); 148 int paramCount = params.getLength(); 149 for (int i = 0; i < paramCount; i++ ) { 150 Attr param = (Attr)params.item(i); 151 String paramName = param.getName(); 152 String paramValue = param.getValue(); 153 parameters.put(paramName, paramValue); 154 } 155 } 156 parameters.putAll(configParameters); 158 159 Map defaultStrings = new HashMap (); 163 Map selectTagStrings = new HashMap (); 164 165 NodeList resDefs = sectionElement.getElementsByTagName("resource"); 168 int resCount = resDefs.getLength(); 169 for ( int i = 0; i < resCount; i++ ) { 170 Element resElement = (Element)(resDefs.item(i)); 172 String resSelect = resElement.getAttribute("for"); 173 Map resMap; 174 if ( resSelect.equals("")) { 175 resMap = defaultStrings; 177 } 178 else if (resSelect.equals(selectTag) ) { 179 resMap = selectTagStrings; 181 } 182 else { 183 continue; 185 } 186 187 String resKey = resElement.getAttribute("name"); 189 if ( resKey == null ) { 190 continue; 192 } 193 String resString = resElement.getFirstChild().getNodeValue(); 194 195 Iterator paramNames = parameters.keySet().iterator(); 197 while ( paramNames.hasNext() ) { 198 String paramName = (String )paramNames.next(); 199 String paramValue = (String )parameters.get(paramName); 200 201 StringBuffer replaceBuffer = 202 new StringBuffer (64) 203 .append("${") 204 .append(paramName) 205 .append("}"); 206 resString = substituteSubString(resString, replaceBuffer.toString(), paramValue); 207 } 208 209 String shared = (String ) stringTable.get(resString); 211 if (shared == null) { 213 stringTable.put(resString, resString); 214 } else { 215 resString = shared; 216 } 217 218 resMap.put(resKey, resString); 220 } 221 222 m_resource.putAll(defaultStrings); 224 m_resource.putAll(selectTagStrings); 225 } 226 227 239 private String match(String select, Element matchersElement) 240 throws MalformedPerl5PatternException 241 { 242 String selectTagName = select; 243 244 NodeList matchers = matchersElement.getElementsByTagName("matcher"); 245 for ( int i = 0; i < matchers.getLength(); i++ ) { 246 Element matcher = (Element)matchers.item(i); 248 String matchName = matcher.getAttribute("for"); 249 StringBuffer selectTagPatternBuffer = 250 new StringBuffer (64) 251 .append("/") 252 .append(matcher.getAttribute("match")) 253 .append("/i"); 254 255 if ( m_perl5Util.match(selectTagPatternBuffer.toString(), selectTagName) ) { 258 return matchName; 259 } 260 } 261 return null; 262 } 263 264 271 static private String substituteSubString( String input, 272 String find, 273 String replace ) 274 { 275 int find_length = find.length(); 276 int replace_length = replace.length(); 277 278 StringBuffer output = new StringBuffer (input); 279 int index = input.indexOf(find); 280 int outputOffset = 0; 281 282 while ( index > -1 ) { 283 output.replace(index + outputOffset, index + outputOffset + find_length, replace); 284 outputOffset = outputOffset + (replace_length - find_length); 285 286 index = input.indexOf(find, index + find_length); 287 } 288 289 String result = output.toString(); 290 return result; 291 } 292 293 300 public String getString(String name) 301 { 302 return (String )m_resource.get(name); 303 } 304 305 315 public String getString(String name, boolean required) 316 { 317 String str = getString(name); 318 319 if (str == null && required) { 320 StringBuffer exceptionBuffer = 321 new StringBuffer (64) 322 .append("Required String resource: '") 323 .append(name) 324 .append("' was not found."); 325 throw new RuntimeException (exceptionBuffer.toString()); 326 } 327 return str; 328 } 329 330 338 public String getString(String name, Map parameters) 339 { 340 return replaceParameters(getString(name), parameters); 341 } 342 343 351 static public String replaceParameters(String str, Map parameters) 352 { 353 Iterator paramNames = parameters.keySet().iterator(); 355 while ( paramNames.hasNext() ) { 356 String paramName = (String )paramNames.next(); 357 String paramValue = (String )parameters.get(paramName); 358 359 StringBuffer replaceBuffer = new StringBuffer (64).append("${").append(paramName).append("}"); 360 str = substituteSubString(str, replaceBuffer.toString(), paramValue); 361 } 362 363 return str; 364 } 365 } 366 | Popular Tags |