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.CascadeParent;
31  import com.rapiddweller.benerator.engine.statement.CascadeStatement;
32  import com.rapiddweller.benerator.engine.statement.MutatingTypeExpression;
33  import com.rapiddweller.benerator.engine.statement.TranscodeStatement;
34  import com.rapiddweller.common.ArrayUtil;
35  import com.rapiddweller.common.CollectionUtil;
36  import com.rapiddweller.common.xml.XMLUtil;
37  import org.w3c.dom.Element;
38  
39  import java.util.Set;
40  
41  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_REF;
42  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_ATTRIBUTE;
43  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_CASCADE;
44  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_ID;
45  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_REFERENCE;
46  
47  /**
48   * Parses &lt;cascade ref="..."&gt; descriptors.<br/><br/>
49   * Created: 18.04.2011 08:27:48
50   *
51   * @author Volker Bergmann
52   * @since 0.6.6
53   */
54  public class CascadeParser extends AbstractBeneratorDescriptorParser {
55  
56    private static final Set<String> MEMBER_ELEMENTS = CollectionUtil.toSet(
57        EL_ID, EL_ATTRIBUTE, EL_REFERENCE);
58  
59    /**
60     * Instantiates a new Cascade parser.
61     */
62    public CascadeParser() {
63      super(EL_CASCADE, CollectionUtil.toSet(ATT_REF), null,
64          TranscodeStatement.class, CascadeStatement.class);
65    }
66  
67    @Override
68    public Statementcom/rapiddweller/benerator/engine/Statement.html#Statement">Statement doParse(Element element, Statement[] parentPath,
69                             BeneratorParseContext context) {
70      CascadeParent./../../../com/rapiddweller/benerator/engine/statement/CascadeParent.html#CascadeParent">CascadeParent parent = (CascadeParent) ArrayUtil.lastElementOf(parentPath);
71      String ref = getRequiredAttribute("ref", element);
72      CascadeStatemente/statement/CascadeStatement.html#CascadeStatement">CascadeStatement result = new CascadeStatement(ref, new MutatingTypeExpression(element, null), parent);
73      Statement[] currentPath = context.createSubPath(parentPath, result);
74      for (Element child : XMLUtil.getChildElements(element)) {
75        String childName = child.getNodeName();
76        if (!MEMBER_ELEMENTS.contains(childName)) {
77          result.addSubStatement(context.parseChildElement(child, currentPath));
78        }
79        // The 'component' child elements (id, attribute, reference) are handled by the MutatingTypeExpression
80      }
81      return result;
82    }
83  
84  }