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