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.Statement;
30  import com.rapiddweller.benerator.engine.expression.ErrorHandlerExpression;
31  import com.rapiddweller.benerator.engine.expression.context.DefaultPageSizeExpression;
32  import com.rapiddweller.benerator.engine.statement.GenerateOrIterateStatement;
33  import com.rapiddweller.benerator.engine.statement.RunTaskStatement;
34  import com.rapiddweller.benerator.engine.statement.WhileStatement;
35  import com.rapiddweller.common.ErrorHandler;
36  import com.rapiddweller.format.xml.AbstractXMLElementParser;
37  import com.rapiddweller.format.xml.ParseContext;
38  import com.rapiddweller.script.Expression;
39  import org.apache.logging.log4j.LogManager;
40  import org.apache.logging.log4j.Logger;
41  import org.w3c.dom.Element;
42  
43  import java.util.Set;
44  
45  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ON_ERROR;
46  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_PAGESIZE;
47  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseScriptableStringAttribute;
48  
49  /**
50   * Abstract parent class for Descriptor parsers.<br/><br/>
51   * Created: 25.10.2009 00:43:18
52   *
53   * @author Volker Bergmann
54   * @since 0.6.0
55   */
56  public abstract class AbstractBeneratorDescriptorParser extends AbstractXMLElementParser<Statement> {
57  
58    /**
59     * The Logger.
60     */
61    protected Logger logger = LogManager.getLogger(AbstractBeneratorDescriptorParser.class);
62  
63    /**
64     * Instantiates a new Abstract benerator descriptor parser.
65     *
66     * @param elementName          the element name
67     * @param requiredAttributes   the required attributes
68     * @param optionalAttributes   the optional attributes
69     * @param supportedParentTypes the supported parent types
70     */
71    public AbstractBeneratorDescriptorParser(String elementName,
72                                             Set<String> requiredAttributes, Set<String> optionalAttributes, Class<?>... supportedParentTypes) {
73      super(elementName, requiredAttributes, optionalAttributes, supportedParentTypes);
74    }
75  
76    /**
77     * Contains loop boolean.
78     *
79     * @param parentPath the parent path
80     * @return the boolean
81     */
82    public static boolean containsLoop(Statement[] parentPath) {
83      if (parentPath == null) {
84        return false;
85      }
86      for (Statement statement : parentPath) {
87        if (isLoop(statement)) {
88          return true;
89        }
90      }
91      return false;
92    }
93  
94    /**
95     * Is loop boolean.
96     *
97     * @param statement the statement
98     * @return the boolean
99     */
100   public static boolean isLoop(Statement statement) {
101     return (statement instanceof RunTaskStatement)
102         || (statement instanceof GenerateOrIterateStatement)
103         || (statement instanceof WhileStatement);
104   }
105 
106   /**
107    * Contains generator statement boolean.
108    *
109    * @param parentPath the parent path
110    * @return the boolean
111    */
112   public static boolean containsGeneratorStatement(Statement[] parentPath) {
113     if (parentPath == null) {
114       return false;
115     }
116     for (Statement statement : parentPath) {
117       if (statement instanceof GenerateOrIterateStatement) {
118         return true;
119       }
120     }
121     return false;
122   }
123 
124   @Override
125   public final Statementcom/rapiddweller/benerator/engine/Statement.html#Statement">Statement doParse(Element element, Statement[] parentPath, ParseContext<Statement> context) {
126     return doParse(element, parentPath, (BeneratorParseContext) context);
127   }
128 
129   /**
130    * Do parse statement.
131    *
132    * @param element    the element
133    * @param parentPath the parent path
134    * @param context    the context
135    * @return the statement
136    */
137   public abstract Statementcom/rapiddweller/benerator/engine/Statement.html#Statement">Statement doParse(Element element, Statement[] parentPath, BeneratorParseContext context);
138 
139   /**
140    * Parse on error attribute expression.
141    *
142    * @param element the element
143    * @param id      the id
144    * @return the expression
145    */
146   protected Expression<ErrorHandler> parseOnErrorAttribute(Element element, String id) {
147     return new ErrorHandlerExpression(id, parseScriptableStringAttribute(ATT_ON_ERROR, element));
148   }
149 
150   /**
151    * Parse page size expression.
152    *
153    * @param element the element
154    * @return the expression
155    */
156   protected Expression<Long> parsePageSize(Element element) {
157     return DescriptorParserUtil.parseLongAttribute(ATT_PAGESIZE, element, new DefaultPageSizeExpression());
158   }
159 
160 }