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.BeneratorRootStatement;
30  import com.rapiddweller.benerator.engine.Statement;
31  import com.rapiddweller.benerator.engine.statement.DefineDOMTreeStatement;
32  import com.rapiddweller.benerator.engine.statement.IfStatement;
33  import com.rapiddweller.common.CollectionUtil;
34  import com.rapiddweller.common.ConfigurationError;
35  import com.rapiddweller.common.ConversionException;
36  import com.rapiddweller.script.Expression;
37  import org.w3c.dom.Element;
38  
39  import java.util.Set;
40  
41  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ID;
42  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_INPUT_URI;
43  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_NAMESPACE_AWARE;
44  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_OUTPUT_URI;
45  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_DOMTREE;
46  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseAttribute;
47  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseBooleanExpressionAttribute;
48  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseScriptableStringAttribute;
49  
50  /**
51   * Parses &lt;domtree&gt; elements in a Benerator descriptor file.<br/><br/>
52   * Created: 16.01.2014 15:59:48
53   *
54   * @author Volker Bergmann
55   * @since 0.9.0
56   */
57  public class DOMTreeParser extends AbstractBeneratorDescriptorParser {
58  
59    private static final Set<String> REQUIRED_ATTRIBUTES = CollectionUtil.toSet(ATT_ID, ATT_INPUT_URI);
60  
61    private static final Set<String> OPTIONAL_ATTRIBUTES = CollectionUtil.toSet(ATT_OUTPUT_URI, ATT_NAMESPACE_AWARE);
62  
63  
64    /**
65     * Instantiates a new Dom tree parser.
66     */
67    public DOMTreeParser() {
68      super(EL_DOMTREE, REQUIRED_ATTRIBUTES, OPTIONAL_ATTRIBUTES, BeneratorRootStatement.class, IfStatement.class);
69    }
70  
71    @Override
72    public DefineDOMTreeStatement doParse(Element element, Statement[] parentPath, BeneratorParseContext context) {
73      try {
74        Expression<String> id = parseAttribute(ATT_ID, element);
75        Expression<String> inputUri = parseScriptableStringAttribute(ATT_INPUT_URI, element);
76        Expression<String> outputUri = parseScriptableStringAttribute(ATT_OUTPUT_URI, element);
77        Expression<Boolean> namespaceAware = parseBooleanExpressionAttribute(ATT_NAMESPACE_AWARE, element);
78        return new DefineDOMTreeStatement(id, inputUri, outputUri, namespaceAware, context.getResourceManager());
79      } catch (ConversionException e) {
80        throw new ConfigurationError(e);
81      }
82    }
83  
84  }