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.statement.WhileStatement;
31  import com.rapiddweller.common.CollectionUtil;
32  import com.rapiddweller.script.Expression;
33  import org.w3c.dom.Element;
34  
35  import java.util.List;
36  import java.util.Set;
37  
38  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_TEST;
39  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_IF;
40  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_SETUP;
41  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_WHILE;
42  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseBooleanExpressionAttribute;
43  
44  /**
45   * Parses a 'while' element.<br/><br/>
46   * Created: 19.02.2010 09:18:47
47   *
48   * @author Volker Bergmann
49   * @since 0.6.0
50   */
51  public class WhileParser extends AbstractBeneratorDescriptorParser {
52  
53    private static final Set<String> LEGAL_PARENTS = CollectionUtil.toSet(
54        EL_SETUP, EL_IF, EL_WHILE);
55  
56    /**
57     * Instantiates a new While parser.
58     */
59    public WhileParser() {
60      super(EL_WHILE, CollectionUtil.toSet(ATT_TEST), null);
61    }
62  
63    /**
64     * Supports boolean.
65     *
66     * @param elementName the element name
67     * @param parentName  the parent name
68     * @return the boolean
69     */
70    public boolean supports(String elementName, String parentName) {
71      return (EL_WHILE.equals(elementName) && LEGAL_PARENTS.contains(parentName));
72    }
73  
74    @Override
75    public Statementcom/rapiddweller/benerator/engine/Statement.html#Statement">Statement doParse(Element element, Statement[] parentPath, BeneratorParseContext context) {
76      Expression<Boolean> condition = parseBooleanExpressionAttribute(ATT_TEST, element);
77      WhileStatementement/WhileStatement.html#WhileStatement">WhileStatement whileStatement = new WhileStatement(condition);
78      List<Statement> subStatements = context.parseChildElementsOf(element, context.createSubPath(parentPath, whileStatement));
79      whileStatement.setSubStatements(subStatements);
80      return whileStatement;
81    }
82  
83  }