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.MutatingTypeExpression;
31  import com.rapiddweller.benerator.engine.statement.TranscodeStatement;
32  import com.rapiddweller.benerator.engine.statement.TranscodingTaskStatement;
33  import com.rapiddweller.common.ArrayUtil;
34  import com.rapiddweller.common.CollectionUtil;
35  import com.rapiddweller.common.ErrorHandler;
36  import com.rapiddweller.common.xml.XMLUtil;
37  import com.rapiddweller.platform.db.DBSystem;
38  import com.rapiddweller.script.Expression;
39  import com.rapiddweller.script.expression.FallbackExpression;
40  import org.w3c.dom.Element;
41  
42  import java.util.Set;
43  
44  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ON_ERROR;
45  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_PAGESIZE;
46  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_SELECTOR;
47  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_SOURCE;
48  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_TABLE;
49  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_TARGET;
50  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_ATTRIBUTE;
51  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_ID;
52  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_REFERENCE;
53  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_TRANSCODE;
54  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.getAttribute;
55  import static com.rapiddweller.benerator.engine.parser.xml.DescriptorParserUtil.parseScriptableStringAttribute;
56  
57  /**
58   * Parses a &lt;transcode&gt; element.<br/><br/>
59   * Created: 08.09.2010 16:13:13
60   *
61   * @author Volker Bergmann
62   * @since 0.6.4
63   */
64  public class TranscodeParser extends AbstractTranscodeParser {
65  
66    private static final Set<String> MEMBER_ELEMENTS = CollectionUtil.toSet(
67        EL_ID, EL_ATTRIBUTE, EL_REFERENCE);
68  
69    /**
70     * Instantiates a new Transcode parser.
71     */
72    public TranscodeParser() {
73      super(EL_TRANSCODE,
74          CollectionUtil.toSet(ATT_TABLE),
75          CollectionUtil.toSet(ATT_SOURCE, ATT_SELECTOR, ATT_TARGET, ATT_PAGESIZE, ATT_ON_ERROR),
76          TranscodingTaskStatement.class);
77    }
78  
79    @Override
80    public Statementcom/rapiddweller/benerator/engine/Statement.html#Statement">Statement doParse(Element element, Statement[] parentPath, BeneratorParseContext context) {
81      String table = getAttribute(ATT_TABLE, element);
82      TranscodingTaskStatementcom/rapiddweller/benerator/engine/statement/TranscodingTaskStatement.html#TranscodingTaskStatement">TranscodingTaskStatement parent = (TranscodingTaskStatement) ArrayUtil.lastElementOf(parentPath);
83      Expression<DBSystem> sourceEx = parseSource(element, parent);
84      Expression<String> selectorEx = parseSelector(element, parent);
85      Expression<DBSystem> targetEx = parseTarget(element, parent);
86      Expression<Long> pageSizeEx = parsePageSize(element, parent);
87      Expression<ErrorHandler> errorHandlerEx = parseOnErrorAttribute(element, table);
88      TranscodeStatementstatement/TranscodeStatement.html#TranscodeStatement">TranscodeStatement result = new TranscodeStatement(new MutatingTypeExpression(element, getRequiredAttribute("table", element)),
89          parent, sourceEx, selectorEx, targetEx, pageSizeEx, errorHandlerEx);
90      Statement[] currentPath = context.createSubPath(parentPath, result);
91      for (Element child : XMLUtil.getChildElements(element)) {
92        String childName = child.getNodeName();
93        if (!MEMBER_ELEMENTS.contains(childName)) {
94          result.addSubStatement(context.parseChildElement(child, currentPath));
95        }
96        // The 'component' child elements (id, attribute, reference) are handled by the MutatingTypeExpression
97      }
98      return result;
99    }
100 
101   private static Expression<String> parseSelector(Element element, TranscodingTaskStatement parent) {
102     return parseScriptableStringAttribute("selector", element);
103   }
104 
105   private Expression<Long> parsePageSize(Element element, Statement parent) {
106     Expression<Long> result = super.parsePageSize(element);
107     if (parent instanceof TranscodingTaskStatement) {
108       result = new FallbackExpression<>(result, ((TranscodingTaskStatement) parent).getPageSizeEx());
109     }
110     return result;
111   }
112 
113   private Expression<DBSystem> parseSource(Element element, Statement parent) {
114     Expression<DBSystem> result = super.parseSource(element);
115     if (parent instanceof TranscodingTaskStatement) {
116       result = new FallbackExpression<>(result, ((TranscodingTaskStatement) parent).getSourceEx());
117     }
118     return result;
119   }
120 
121   private Expression<DBSystem> parseTarget(Element element, Statement parent) {
122     Expression<DBSystem> result = super.parseTarget(element);
123     if (parent instanceof TranscodingTaskStatement) {
124       result = new FallbackExpression<>(result, ((TranscodingTaskStatement) parent).getTargetEx());
125     }
126     return result;
127   }
128 
129 }