View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.parser.xml;
28  
29  import com.rapiddweller.common.Context;
30  import com.rapiddweller.common.StringUtil;
31  import com.rapiddweller.common.converter.AnyConverter;
32  import com.rapiddweller.common.converter.ToStringConverter;
33  import com.rapiddweller.format.script.ScriptUtil;
34  import org.w3c.dom.Attr;
35  import org.w3c.dom.Element;
36  
37  /**
38   * Utility class for parsing benerator descriptors in XML format.<br/>
39   * <br/>
40   * Created at 02.01.2009 17:27:31
41   *
42   * @author Volker Bergmann
43   * @since 0.5.7
44   */
45  public class XmlDescriptorParser {
46  
47    /**
48     * Private constructor for preventing instantiation of this utility class
49     */
50    private XmlDescriptorParser() {
51    }
52  
53    // attribute parsing -----------------------------------------------------------------------------------------------
54  
55    /**
56     * Parse string attribute string.
57     *
58     * @param element the element
59     * @param name    the name
60     * @param context the context
61     * @return the string
62     */
63    public static String parseStringAttribute(Element element, String name, Context context) {
64      return parseStringAttribute(element, name, context, true);
65    }
66  
67    /**
68     * Parse string attribute string.
69     *
70     * @param element       the element
71     * @param name          the name
72     * @param context       the context
73     * @param resolveScript the resolve script
74     * @return the string
75     */
76    public static String parseStringAttribute(Element element, String name, Context context, boolean resolveScript) {
77      Object value = parseAttribute(element, name, context, resolveScript);
78      return ToStringConverter.convert(value, null);
79    }
80  
81    /**
82     * Parse string attribute string.
83     *
84     * @param attribute the attribute
85     * @param context   the context
86     * @return the string
87     */
88    public static String parseStringAttribute(Attr attribute, Context context) {
89      Object value = resolveScript(attribute.getName(), attribute.getValue(), context);
90      return StringUtil.unescape(ToStringConverter.convert(value, null));
91    }
92  
93    /**
94     * Parse int attribute int.
95     *
96     * @param element      the element
97     * @param name         the name
98     * @param context      the context
99     * @param defaultValue the default value
100    * @return the int
101    */
102   public static int parseIntAttribute(Element element, String name, Context context, int defaultValue) {
103     Object value = parseAttribute(element, name, context);
104     if (value instanceof Number) {
105       return ((Number) value).intValue();
106     } else if (value == null || (value instanceof String && StringUtil.isEmpty((String) value))) {
107       return defaultValue;
108     } else {
109       return AnyConverter.convert(value, Integer.class);
110     }
111   }
112 
113   /**
114    * Parse long attribute long.
115    *
116    * @param element      the element
117    * @param name         the name
118    * @param context      the context
119    * @param defaultValue the default value
120    * @return the long
121    */
122   public static long parseLongAttribute(Element element, String name, Context context, long defaultValue) {
123     Object value = parseAttribute(element, name, context);
124     if (value instanceof Number) {
125       return ((Number) value).longValue();
126     } else if (value == null || (value instanceof String && StringUtil.isEmpty((String) value))) {
127       return defaultValue;
128     } else {
129       return AnyConverter.convert(value, Long.class);
130     }
131   }
132 
133   /**
134    * Parse boolean attribute boolean.
135    *
136    * @param element      the element
137    * @param name         the name
138    * @param context      the context
139    * @param defaultValue the default value
140    * @return the boolean
141    */
142   public static boolean parseBooleanAttribute(Element element, String name,
143                                               Context context, boolean defaultValue) {
144     Object value = parseAttribute(element, name, context);
145     if (value instanceof Boolean) {
146       return (Boolean) value;
147     } else if (value == null || (value instanceof String && StringUtil.isEmpty((String) value))) {
148       return defaultValue;
149     } else {
150       return AnyConverter.convert(value, Boolean.class);
151     }
152   }
153 
154   /**
155    * Parse attribute object.
156    *
157    * @param element the element
158    * @param name    the name
159    * @param context the context
160    * @return the object
161    */
162   public static Object parseAttribute(Element element, String name, Context context) {
163     return parseAttribute(element, name, context, true);
164   }
165 
166   /**
167    * Parse attribute object.
168    *
169    * @param element       the element
170    * @param name          the name
171    * @param context       the context
172    * @param resolveScript the resolve script
173    * @return the object
174    */
175   public static Object parseAttribute(Element element, String name, Context context, boolean resolveScript) {
176     String value = element.getAttribute(name);
177     if (value != null && value.length() == 0) {
178       value = null;
179     }
180     return resolveScript(name, value, context);
181   }
182 
183   /**
184    * Parse attribute object.
185    *
186    * @param attribute the attribute
187    * @param context   the context
188    * @return the object
189    */
190   public static Object parseAttribute(Attr attribute, Context context) {
191     String name = attribute.getName();
192     String value = attribute.getValue();
193     return resolveScript(name, value, context);
194   }
195 
196   /**
197    * Resolve script object.
198    *
199    * @param name    the name
200    * @param value   the value
201    * @param context the context
202    * @return the object
203    */
204   public static Object resolveScript(String name, String value, Context context) {
205     if (value == null || "script".equals(name)) {
206       return value;
207     } else {
208       return ScriptUtil.evaluate(value, context);
209     }
210   }
211 
212 }