KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > xml > NodeletUtils


1 package com.ibatis.common.xml;
2
3 import org.w3c.dom.NamedNodeMap JavaDoc;
4 import org.w3c.dom.Node JavaDoc;
5
6 import java.util.Properties JavaDoc;
7
8 public class NodeletUtils {
9
10   public static boolean getBooleanAttribute(Properties JavaDoc attribs, String JavaDoc name, boolean def) {
11     String JavaDoc 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 JavaDoc attribs, String JavaDoc name, int def) {
20     String JavaDoc 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 JavaDoc parseAttributes(Node JavaDoc n) {
29     return parseAttributes(n, null);
30   }
31
32   public static Properties JavaDoc parseAttributes(Node JavaDoc n, Properties JavaDoc variables) {
33     Properties JavaDoc attributes = new Properties JavaDoc();
34     NamedNodeMap JavaDoc attributeNodes = n.getAttributes();
35     for (int i = 0; i < attributeNodes.getLength(); i++) {
36       Node JavaDoc attribute = attributeNodes.item(i);
37       String JavaDoc value = parsePropertyTokens(attribute.getNodeValue(), variables);
38       attributes.put(attribute.getNodeName(), value);
39     }
40     return attributes;
41   }
42
43   public static String JavaDoc parsePropertyTokens(String JavaDoc string, Properties JavaDoc variables) {
44     final String JavaDoc OPEN = "${";
45     final String JavaDoc CLOSE = "}";
46
47     String JavaDoc 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 JavaDoc prepend = newString.substring(0, start);
54         String JavaDoc append = newString.substring(end + CLOSE.length());
55         String JavaDoc propName = newString.substring(start + OPEN.length(), end);
56         String JavaDoc 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