1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.common.xml;
17
18 import java.util.Properties;
19
20 import org.w3c.dom.NamedNodeMap;
21 import org.w3c.dom.Node;
22
23
24
25
26 public class NodeletUtils {
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public static boolean getBooleanAttribute(Properties attribs, String name, boolean def) {
41 String value = attribs.getProperty(name);
42 if (value == null) {
43 return def;
44 }
45 return "true".equals(value);
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public static int getIntAttribute(Properties attribs, String name, int def) {
61 String value = attribs.getProperty(name);
62 if (value == null) {
63 return def;
64 }
65 return Integer.parseInt(value);
66 }
67
68
69
70
71
72
73
74
75
76 public static Properties parseAttributes(Node n) {
77 return parseAttributes(n, null);
78 }
79
80
81
82
83
84
85
86
87
88
89
90 public static Properties parseAttributes(Node n, Properties variables) {
91 Properties attributes = new Properties();
92 NamedNodeMap attributeNodes = n.getAttributes();
93 for (int i = 0; i < attributeNodes.getLength(); i++) {
94 Node attribute = attributeNodes.item(i);
95 String value = parsePropertyTokens(attribute.getNodeValue(), variables);
96 attributes.put(attribute.getNodeName(), value);
97 }
98 return attributes;
99 }
100
101
102
103
104
105
106
107
108
109
110
111 public static String parsePropertyTokens(String string, Properties variables) {
112 final String OPEN = "${";
113 final String CLOSE = "}";
114
115 String newString = string;
116 if (newString != null && variables != null) {
117 int start = newString.indexOf(OPEN);
118 int end = newString.indexOf(CLOSE);
119
120 while (start > -1 && end > start) {
121 String prepend = newString.substring(0, start);
122 String append = newString.substring(end + CLOSE.length());
123 String propName = newString.substring(start + OPEN.length(), end);
124 String propValue = variables.getProperty(propName);
125 if (propValue == null) {
126 newString = prepend + propName + append;
127 } else {
128 newString = prepend + propValue + append;
129 }
130 start = newString.indexOf(OPEN);
131 end = newString.indexOf(CLOSE);
132 }
133 }
134 return newString;
135 }
136
137 }