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.engine.parser.xml;
28  
29  import com.rapiddweller.benerator.engine.expression.ScriptExpression;
30  import com.rapiddweller.benerator.engine.expression.ScriptableExpression;
31  import com.rapiddweller.benerator.engine.expression.TypedScriptExpression;
32  import com.rapiddweller.common.StringUtil;
33  import com.rapiddweller.common.xml.XMLUtil;
34  import com.rapiddweller.format.text.SplitStringConverter;
35  import com.rapiddweller.script.Expression;
36  import com.rapiddweller.script.expression.ConstantExpression;
37  import com.rapiddweller.script.expression.ConvertingExpression;
38  import com.rapiddweller.script.expression.StringExpression;
39  import com.rapiddweller.script.expression.TypeConvertingExpression;
40  import com.rapiddweller.script.expression.UnescapeExpression;
41  import org.w3c.dom.Element;
42  
43  /**
44   * Provides utility methods for XML descriptor parsing.<br/><br/>
45   * Created: 19.02.2010 09:32:33
46   *
47   * @author Volker Bergmann
48   * @since 0.6.0
49   */
50  public class DescriptorParserUtil {
51  
52    // direct data retrieval -------------------------------------------------------------------------------------------
53  
54    /**
55     * Gets attribute.
56     *
57     * @param name    the name
58     * @param element the element
59     * @return the attribute
60     */
61    public static String getAttribute(String name, Element element) {
62      return (element.hasAttribute(name) ? element.getAttribute(name) : null);
63    }
64  
65    /**
66     * Gets element text.
67     *
68     * @param element the element
69     * @return the element text
70     */
71    public static String getElementText(Element element) {
72      return XMLUtil.getText(element);
73    }
74  
75    // creating expressions for data retrieval -------------------------------------------------------------------------
76  
77    /**
78     * Parse scriptable element text expression.
79     *
80     * @param element  the element
81     * @param unescape the unescape
82     * @return the expression
83     */
84    public static Expression<String> parseScriptableElementText(Element element, boolean unescape) {
85      Expression<String> result = new StringExpression(new ScriptableExpression(XMLUtil.getText(element), null));
86      if (unescape) {
87        result = new UnescapeExpression(result);
88      }
89      return result;
90    }
91  
92    /**
93     * Parse scriptable string attribute expression.
94     *
95     * @param name    the name
96     * @param element the element
97     * @return the expression
98     */
99    public static Expression<String> parseScriptableStringAttribute(String name, Element element) {
100     return parseScriptableStringAttribute(name, element, true);
101   }
102 
103   /**
104    * Parse scriptable string attribute expression.
105    *
106    * @param name     the name
107    * @param element  the element
108    * @param unescape the unescape
109    * @return the expression
110    */
111   public static Expression<String> parseScriptableStringAttribute(String name, Element element, boolean unescape) {
112     String attribute = getAttribute(name, element);
113     if (attribute == null) {
114       return null;
115     }
116     Expression<String> result = new StringExpression(new ScriptableExpression(attribute, null));
117     if (unescape) {
118       result = new UnescapeExpression(result);
119     }
120     return result;
121   }
122 
123   /**
124    * Parse scriptable string array attribute expression.
125    *
126    * @param name    the name
127    * @param element the element
128    * @return the expression
129    */
130   public static Expression<String[]> parseScriptableStringArrayAttribute(String name, Element element) {
131     String attribute = getAttribute(name, element);
132     if (attribute == null) {
133       return null;
134     }
135     Expression<String> rawEx = new TypeConvertingExpression<>(
136         new ScriptableExpression(attribute, null), String.class);
137     return new ConvertingExpression<>(rawEx, new SplitStringConverter(','));
138   }
139 
140   /**
141    * Parse int attribute expression.
142    *
143    * @param name    the name
144    * @param element the element
145    * @return the expression
146    */
147   public static Expression<Integer> parseIntAttribute(String name, Element element) {
148     return new TypedScriptExpression<>(getAttribute(name, element), Integer.class);
149   }
150 
151   /**
152    * Parse int attribute expression.
153    *
154    * @param name         the name
155    * @param element      the element
156    * @param defaultValue the default value
157    * @return the expression
158    */
159   public static Expression<Integer> parseIntAttribute(String name, Element element, int defaultValue) {
160     return parseIntAttribute(name, element, new ConstantExpression<>(defaultValue));
161   }
162 
163   /**
164    * Parse int attribute expression.
165    *
166    * @param name         the name
167    * @param element      the element
168    * @param defaultValue the default value
169    * @return the expression
170    */
171   public static Expression<Integer> parseIntAttribute(String name, Element element, Expression<Integer> defaultValue) {
172     String attribute = getAttribute(name, element);
173     if (StringUtil.isEmpty(attribute)) {
174       return defaultValue;
175     } else {
176       return new TypedScriptExpression<>(attribute, Integer.class);
177     }
178   }
179 
180   /**
181    * Parse long attribute expression.
182    *
183    * @param name         the name
184    * @param element      the element
185    * @param defaultValue the default value
186    * @return the expression
187    */
188   public static Expression<Long> parseLongAttribute(String name, Element element, long defaultValue) {
189     return parseLongAttribute(name, element, new ConstantExpression<>(defaultValue));
190   }
191 
192   /**
193    * Parse long attribute expression.
194    *
195    * @param name         the name
196    * @param element      the element
197    * @param defaultValue the default value
198    * @return the expression
199    */
200   public static Expression<Long> parseLongAttribute(String name, Element element, Expression<Long> defaultValue) {
201     String attribute = getAttribute(name, element);
202     if (StringUtil.isEmpty(attribute)) {
203       return defaultValue;
204     } else {
205       return new TypedScriptExpression<>(attribute, Long.class);
206     }
207   }
208 
209   /**
210    * Parse boolean expression attribute expression.
211    *
212    * @param name    the name
213    * @param element the element
214    * @return the expression
215    */
216   public static Expression<Boolean> parseBooleanExpressionAttribute(String name, Element element) {
217     return parseBooleanExpressionAttribute(name, element, null);
218   }
219 
220   /**
221    * Parse boolean expression attribute expression.
222    *
223    * @param name         the name
224    * @param element      the element
225    * @param defaultValue the default value
226    * @return the expression
227    */
228   public static Expression<Boolean> parseBooleanExpressionAttribute(String name, Element element, Boolean defaultValue) {
229     String attribute = getAttribute(name, element);
230     if (StringUtil.isEmpty(attribute)) {
231       return new ConstantExpression<>(defaultValue);
232     } else {
233       return new TypedScriptExpression<>(attribute, Boolean.class);
234     }
235   }
236 
237   /**
238    * Parse attribute constant expression.
239    *
240    * @param name    the name
241    * @param element the element
242    * @return the constant expression
243    */
244   public static ConstantExpression<String> parseAttribute(String name, Element element) {
245     String attribute = getAttribute(name, element);
246     return (attribute != null ? new ConstantExpression<>(attribute) : null);
247   }
248 
249   /**
250    * Parse script attribute expression.
251    *
252    * @param name    the name
253    * @param element the element
254    * @return the expression
255    */
256   public static Expression<?> parseScriptAttribute(String name, Element element) {
257     String rawAttribute = getAttribute(name, element);
258     if (StringUtil.isEmpty(rawAttribute)) {
259       return null;
260     } else {
261       return new ScriptExpression<>(rawAttribute);
262     }
263   }
264 
265 }