View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * The Class NodeletUtils.
25   */
26  public class NodeletUtils {
27  
28    /**
29     * Gets the boolean attribute.
30     *
31     * @param attribs
32     *          the attribs
33     * @param name
34     *          the name
35     * @param def
36     *          the def
37     *
38     * @return the boolean attribute
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     * Gets the int attribute.
50     *
51     * @param attribs
52     *          the attribs
53     * @param name
54     *          the name
55     * @param def
56     *          the def
57     *
58     * @return the int attribute
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     * Parses the attributes.
70     *
71     * @param n
72     *          the n
73     *
74     * @return the properties
75     */
76    public static Properties parseAttributes(Node n) {
77      return parseAttributes(n, null);
78    }
79  
80    /**
81     * Parses the attributes.
82     *
83     * @param n
84     *          the n
85     * @param variables
86     *          the variables
87     *
88     * @return the properties
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    * Parses the property tokens.
103    *
104    * @param string
105    *          the string
106    * @param variables
107    *          the variables
108    *
109    * @return the string
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 }