1 25 26 package org.jrobin.core; 27 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.Element ; 30 import org.xml.sax.InputSource ; 31 32 import java.io.IOException ; 33 import java.io.File ; 34 import java.util.*; 35 import java.util.regex.Pattern ; 36 import java.util.regex.Matcher ; 37 import java.awt.*; 38 39 47 public abstract class XmlTemplate { 48 private static final String PATTERN_STRING = "\\$\\{(\\w+)\\}"; 49 private static final Pattern PATTERN = Pattern.compile(PATTERN_STRING); 50 51 protected Element root; 52 private HashMap valueMap = new HashMap(); 53 private HashSet validatedNodes = new HashSet(); 54 55 protected XmlTemplate(InputSource xmlSource) throws IOException , RrdException { 56 root = Util.Xml.getRootElement(xmlSource); 57 } 58 59 protected XmlTemplate(String xmlString) throws IOException , RrdException { 60 root = Util.Xml.getRootElement(xmlString); 61 } 62 63 protected XmlTemplate(File xmlFile) throws IOException , RrdException { 64 root = Util.Xml.getRootElement(xmlFile); 65 } 66 67 70 public void clearValues() { 71 valueMap.clear(); 72 } 73 74 81 public void setVariable(String name, String value) { 82 valueMap.put(name, value); 83 } 84 85 92 public void setVariable(String name, int value) { 93 valueMap.put(name, new Integer (value)); 94 } 95 96 103 public void setVariable(String name, long value) { 104 valueMap.put(name, new Long (value)); 105 } 106 107 114 public void setVariable(String name, double value) { 115 valueMap.put(name, new Double (value)); 116 } 117 118 125 public void setVariable(String name, Color value) { 126 valueMap.put(name, "#" + Integer.toHexString(value.getRGB() & 0xFFFFFF)); 127 } 128 129 136 public void setVariable(String name, Date value) { 137 setVariable(name, Util.getTimestamp(value)); 138 } 139 140 147 public void setVariable(String name, GregorianCalendar value) { 148 setVariable(name, Util.getTimestamp(value)); 149 } 150 151 158 public void setVariable(String name, boolean value) { 159 valueMap.put(name, "" + value); 160 } 161 162 168 public boolean hasVariables() { 169 return PATTERN.matcher( root.toString() ).find(); 170 } 171 172 177 public String [] getVariables() 178 { 179 ArrayList list = new ArrayList(); 180 Matcher m = PATTERN.matcher( root.toString() ); 181 182 while ( m.find() ) 183 { 184 String var = m.group(1); 185 if ( !list.contains( var ) ) 186 list.add( var ); 187 } 188 189 return (String []) list.toArray( new String [list.size()] ); 190 } 191 192 protected static Node [] getChildNodes(Node parentNode, String childName) { 193 return Util.Xml.getChildNodes(parentNode, childName); 194 } 195 196 protected static Node [] getChildNodes(Node parentNode) { 197 return Util.Xml.getChildNodes(parentNode, null); 198 } 199 200 protected static Node getFirstChildNode(Node parentNode, String childName) throws RrdException { 201 return Util.Xml.getFirstChildNode(parentNode, childName); 202 } 203 204 protected boolean hasChildNode(Node parentNode, String childName) { 205 return Util.Xml.hasChildNode(parentNode, childName); 206 } 207 208 protected String getChildValue( Node parentNode, String childName ) throws RrdException { 209 return getChildValue( parentNode, childName, true ); 210 } 211 212 protected String getChildValue(Node parentNode, String childName, boolean trim) throws RrdException { 213 String value = Util.Xml.getChildValue(parentNode, childName, trim); 214 return resolveMappings(value); 215 } 216 217 protected String getValue( Node parentNode ) { 218 return getValue( parentNode, true ); 219 } 220 221 protected String getValue(Node parentNode, boolean trim ) { 222 String value = Util.Xml.getValue(parentNode, trim); 223 return resolveMappings(value); 224 } 225 226 private String resolveMappings(String templateValue) { 227 if(templateValue == null) { 228 return null; 229 } 230 Matcher matcher = PATTERN.matcher(templateValue); 231 StringBuffer result = new StringBuffer (); 232 int lastMatchEnd = 0; 233 while(matcher.find()) { 234 String var = matcher.group(1); 235 if(valueMap.containsKey(var)) { 236 result.append(templateValue.substring(lastMatchEnd, matcher.start())); 238 result.append(valueMap.get(var).toString()); 239 lastMatchEnd = matcher.end(); 240 } 241 else { 242 throw new IllegalArgumentException ( 245 "No mapping found for template variable ${" + var + "}"); 246 } 247 } 248 result.append(templateValue.substring(lastMatchEnd)); 249 return result.toString(); 250 } 251 252 protected int getChildValueAsInt(Node parentNode, String childName) throws RrdException { 253 String valueStr = getChildValue(parentNode, childName); 254 return Integer.parseInt(valueStr); 255 } 256 257 protected int getValueAsInt(Node parentNode) { 258 String valueStr = getValue(parentNode); 259 return Integer.parseInt(valueStr); 260 } 261 262 protected long getChildValueAsLong(Node parentNode, String childName) throws RrdException { 263 String valueStr = getChildValue(parentNode, childName); 264 return Long.parseLong(valueStr); 265 } 266 267 protected long getValueAsLong(Node parentNode) { 268 String valueStr = getValue(parentNode); 269 return Long.parseLong(valueStr); 270 } 271 272 protected double getChildValueAsDouble(Node parentNode, String childName) throws RrdException { 273 String valueStr = getChildValue(parentNode, childName); 274 return Util.parseDouble(valueStr); 275 } 276 277 protected double getValueAsDouble(Node parentNode) { 278 String valueStr = getValue(parentNode); 279 return Util.parseDouble(valueStr); 280 } 281 282 protected boolean getChildValueAsBoolean(Node parentNode, String childName) throws RrdException { 283 String valueStr = getChildValue(parentNode, childName); 284 return Util.parseBoolean(valueStr); 285 } 286 287 protected boolean getValueAsBoolean(Node parentNode) { 288 String valueStr = getValue(parentNode); 289 return Util.parseBoolean(valueStr); 290 } 291 292 protected boolean isEmptyNode(Node node) { 293 return node.getNodeName().equals("#comment") || 295 (node.getNodeName().equals("#text") && node.getNodeValue().trim().length() == 0); 296 } 297 298 protected void validateTagsOnlyOnce(Node parentNode, String [] allowedChildNames) throws RrdException { 299 if(validatedNodes.contains(parentNode)) { 301 return; 302 } 303 Node [] childs = getChildNodes(parentNode); 304 main: 305 for(int i = 0; i < childs.length; i++) { 306 String childName = childs[i].getNodeName(); 307 for(int j = 0; j < allowedChildNames.length; j++) { 308 if(allowedChildNames[j].equals(childName)) { 309 allowedChildNames[j] = "<--removed-->"; 311 continue main; 312 } 313 else if(allowedChildNames[j].equals(childName + "*")) { 314 continue main; 316 } 317 } 318 if(!isEmptyNode(childs[i])) { 319 throw new RrdException("Unexpected tag encountered: <" + childName + ">"); 320 } 321 } 322 validatedNodes.add(parentNode); 324 } 325 } 326 | Popular Tags |