1 package com.ibatis.common.xml; 2 3 import org.w3c.dom.NamedNodeMap ; 4 import org.w3c.dom.Node ; 5 6 import java.util.Properties ; 7 8 public class NodeletUtils { 9 10 public static boolean getBooleanAttribute(Properties attribs, String name, boolean def) { 11 String value = attribs.getProperty(name); 12 if (value == null) { 13 return def; 14 } else { 15 return "true".equals(value); 16 } 17 } 18 19 public static int getIntAttribute(Properties attribs, String name, int def) { 20 String value = attribs.getProperty(name); 21 if (value == null) { 22 return def; 23 } else { 24 return Integer.parseInt(value); 25 } 26 } 27 28 public static Properties parseAttributes(Node n) { 29 return parseAttributes(n, null); 30 } 31 32 public static Properties parseAttributes(Node n, Properties variables) { 33 Properties attributes = new Properties (); 34 NamedNodeMap attributeNodes = n.getAttributes(); 35 for (int i = 0; i < attributeNodes.getLength(); i++) { 36 Node attribute = attributeNodes.item(i); 37 String value = parsePropertyTokens(attribute.getNodeValue(), variables); 38 attributes.put(attribute.getNodeName(), value); 39 } 40 return attributes; 41 } 42 43 public static String parsePropertyTokens(String string, Properties variables) { 44 final String OPEN = "${"; 45 final String CLOSE = "}"; 46 47 String newString = string; 48 if (newString != null && variables != null) { 49 int start = newString.indexOf(OPEN); 50 int end = newString.indexOf(CLOSE); 51 52 while (start > -1 && end > start) { 53 String prepend = newString.substring(0, start); 54 String append = newString.substring(end + CLOSE.length()); 55 String propName = newString.substring(start + OPEN.length(), end); 56 String propValue = variables.getProperty(propName); 57 if (propValue == null) { 58 newString = prepend + propName + append; 59 } else { 60 newString = prepend + propValue + append; 61 } 62 start = newString.indexOf(OPEN); 63 end = newString.indexOf(CLOSE); 64 } 65 } 66 return newString; 67 } 68 69 70 } 71 | Popular Tags |