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.DescriptorConstants;
30  import com.rapiddweller.benerator.engine.Statement;
31  import com.rapiddweller.benerator.engine.expression.ScriptExpression;
32  import com.rapiddweller.benerator.engine.statement.EvaluateStatement;
33  import com.rapiddweller.common.CollectionUtil;
34  import com.rapiddweller.common.converter.String2CharConverter;
35  import com.rapiddweller.script.Expression;
36  import com.rapiddweller.script.expression.ConvertingExpression;
37  import com.rapiddweller.script.expression.FeatureAccessExpression;
38  import com.rapiddweller.script.expression.StringExpression;
39  import org.w3c.dom.Element;
40  
41  import java.util.Set;
42  
43  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ASSERT;
44  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ENCODING;
45  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ID;
46  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_INVALIDATE;
47  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ON_ERROR;
48  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_OPTIMIZE;
49  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_SEPARATOR;
50  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_TARGET;
51  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_TYPE;
52  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_URI;
53  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseAttribute;
54  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseBooleanExpressionAttribute;
55  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseScriptableElementText;
56  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseScriptableStringAttribute;
57  
58  /**
59   * Parses an &lt;evaluate&gt; element in a Benerator descriptor file.<br/><br/>
60   * Created: 25.10.2009 01:01:02
61   *
62   * @author Volker Bergmann
63   * @since 0.6.0
64   */
65  public class EvaluateParser extends AbstractBeneratorDescriptorParser {
66  
67    private static final Set<String> OPTIONAL_ATTRIBUTES = CollectionUtil.toSet(
68        ATT_ID, ATT_URI, ATT_TYPE, ATT_TARGET, ATT_SEPARATOR, ATT_ON_ERROR, ATT_ENCODING,
69        ATT_OPTIMIZE, ATT_INVALIDATE, ATT_ASSERT);
70  
71    /**
72     * Instantiates a new Evaluate parser.
73     */
74    public EvaluateParser() {
75      super(null, null, OPTIONAL_ATTRIBUTES);
76    }
77  
78    @Override
79    public boolean supports(Element element, Statement[] parentPath) {
80      String name = element.getNodeName();
81      return DescriptorConstants.EL_EVALUATE.equals(name)
82          || DescriptorConstants.EL_EXECUTE.equals(name);
83    }
84  
85    @Override
86    public EvaluateStatement doParse(Element element, Statement[] parentPath, BeneratorParseContext context) {
87      boolean evaluate = DescriptorConstants.EL_EVALUATE.equals(element.getNodeName());
88      if (evaluate) {
89        assertAtLeastOneAttributeIsSet(element, ATT_ID, ATT_ASSERT);
90      } else {
91        assertAttributeIsNotSet(element, ATT_ID);
92        assertAttributeIsNotSet(element, ATT_ASSERT);
93      }
94      Expression<String> id = parseAttribute(ATT_ID, element);
95      Expression<String> text = new StringExpression(parseScriptableElementText(element, false));
96      Expression<String> uri = parseScriptableStringAttribute(ATT_URI, element);
97      Expression<String> type = parseAttribute(ATT_TYPE, element);
98      Expression<?> targetObject = new FeatureAccessExpression<>(element.getAttribute(ATT_TARGET));
99      Expression<Character> separator = new ConvertingExpression<>(parseScriptableStringAttribute(ATT_SEPARATOR, element), new String2CharConverter());
100     Expression<String> onError = parseScriptableStringAttribute(ATT_ON_ERROR, element);
101     Expression<String> encoding = parseScriptableStringAttribute(ATT_ENCODING, element);
102     Expression<Boolean> optimize = parseBooleanExpressionAttribute(ATT_OPTIMIZE, element, false);
103     Expression<Boolean> invalidate = parseBooleanExpressionAttribute(ATT_INVALIDATE, element, null);
104     Expression<?> assertion = new ScriptExpression<>(element.getAttribute(ATT_ASSERT));
105     return new EvaluateStatement(evaluate, id, text, uri, type, targetObject, separator, onError, encoding, optimize, invalidate, assertion);
106   }
107 
108 }