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.common.CollectionUtil;
32  import com.rapiddweller.common.ConfigurationError;
33  import com.rapiddweller.common.StringUtil;
34  import com.rapiddweller.common.xml.XMLUtil;
35  import com.rapiddweller.format.xml.XMLElementParser;
36  import org.w3c.dom.Element;
37  
38  import java.util.HashSet;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Map.Entry;
43  import java.util.Set;
44  
45  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_ACCEPT_UNKNOWN_SIMPLE_TYPES;
46  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_DATASET;
47  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_ENCODING;
48  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_ERR_HANDLER;
49  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_IMPORTS;
50  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_LINE_SEPARATOR;
51  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_LOCALE;
52  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_NULL;
53  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_ONE_TO_ONE;
54  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_PAGE_SIZE;
55  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_SCRIPT;
56  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_SEPARATOR;
57  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULT_TIME_ZONE;
58  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_GENERATOR_FACTORY;
59  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_MAX_COUNT;
60  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_SETUP;
61  
62  /**
63   * {@link XMLElementParser} implementation for parsing a Benerator descriptor file's root XML element.<br/><br/>
64   * Created: 14.12.2010 19:48:00
65   *
66   * @author Volker Bergmann
67   * @since 0.6.4
68   */
69  public class SetupParser extends AbstractBeneratorDescriptorParser {
70  
71    private static final Set<String> BENERATOR_PROPERTIES = CollectionUtil.toSet(
72        ATT_DEFAULT_SCRIPT,
73        ATT_DEFAULT_NULL,
74        ATT_DEFAULT_ENCODING,
75        ATT_DEFAULT_LINE_SEPARATOR,
76        ATT_DEFAULT_TIME_ZONE,
77        ATT_DEFAULT_LOCALE,
78        ATT_DEFAULT_DATASET,
79        ATT_DEFAULT_PAGE_SIZE,
80        ATT_DEFAULT_SEPARATOR,
81        ATT_DEFAULT_ONE_TO_ONE,
82        ATT_DEFAULT_ERR_HANDLER,
83        ATT_MAX_COUNT,
84        ATT_ACCEPT_UNKNOWN_SIMPLE_TYPES,
85        ATT_GENERATOR_FACTORY,
86        ATT_DEFAULT_IMPORTS
87    );
88  
89    private static final Set<String> XML_ATTRIBUTES = CollectionUtil.toSet(
90        "xmlns", "xmlns:xsi", "xsi:schemaLocation"
91    );
92  
93    private static final Set<String> OPTIONAL_ATTRIBUTES;
94  
95    static {
96      OPTIONAL_ATTRIBUTES = new HashSet<>(BENERATOR_PROPERTIES);
97      OPTIONAL_ATTRIBUTES.addAll(XML_ATTRIBUTES);
98    }
99  
100   /**
101    * Instantiates a new Setup parser.
102    */
103   public SetupParser() {
104     super(EL_SETUP, null, OPTIONAL_ATTRIBUTES);
105   }
106 
107   @Override
108   public Statementcom/rapiddweller/benerator/engine/Statement.html#Statement">Statement doParse(Element element, Statement[] parentPath, BeneratorParseContext context) {
109     Map<String, String> attributes = XMLUtil.getAttributes(element);
110     // remove standard XML root attributes and verify that the remaining ones are legal
111     Iterator<Entry<String, String>> iterator = attributes.entrySet().iterator();
112     while (iterator.hasNext()) {
113       Entry<String, String> attribute = iterator.next();
114       if (BENERATOR_PROPERTIES.contains(attribute.getKey())) {
115         attribute.setValue(StringUtil.unescape(attribute.getValue()));
116       } else if (isStandardXmlRootAttribute(attribute.getKey())) {
117         iterator.remove();
118       } else {
119         throw new ConfigurationError("Not a supported attribute in <" + EL_SETUP + ">: " + attribute.getKey());
120       }
121     }
122     // create root statement and configure its children
123     BeneratorRootStatementotStatement.html#BeneratorRootStatement">BeneratorRootStatement rootStatement = new BeneratorRootStatement(attributes);
124     Statement[] currentPath = context.createSubPath(parentPath, rootStatement);
125     List<Statement> subStatements = context.parseChildElementsOf(element, currentPath);
126     rootStatement.setSubStatements(subStatements);
127     return rootStatement;
128   }
129 
130   private static boolean isStandardXmlRootAttribute(String key) {
131     return XML_ATTRIBUTES.contains(key) || key.contains(":");
132   }
133 
134 }