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.DefaultPlatformDescriptor;
30  import com.rapiddweller.benerator.PlatformDescriptor;
31  import com.rapiddweller.benerator.engine.Statement;
32  import com.rapiddweller.benerator.engine.statement.ImportStatement;
33  import com.rapiddweller.common.ArrayBuilder;
34  import com.rapiddweller.common.BeanUtil;
35  import com.rapiddweller.common.CollectionUtil;
36  import com.rapiddweller.common.ExceptionUtil;
37  import com.rapiddweller.common.StringUtil;
38  import com.rapiddweller.format.xml.XMLElementParser;
39  import org.w3c.dom.Element;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  import java.util.Set;
44  
45  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_CLASS;
46  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DEFAULTS;
47  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_DOMAINS;
48  import static com.rapiddweller.benerator.engine.DescriptorConstants.ATT_PLATFORMS;
49  import static com.rapiddweller.benerator.engine.DescriptorConstants.EL_IMPORT;
50  
51  /**
52   * Parses an &lt;import&gt; element in a Benerator descriptor file.<br/><br/>
53   * Created: 25.10.2009 00:53:06
54   *
55   * @author Volker Bergmann
56   * @since 0.6.0
57   */
58  public class ImportParser extends AbstractBeneratorDescriptorParser {
59  
60    private static final Set<String> OPTIONAL_ATTRIBUTES = CollectionUtil.toSet(
61        ATT_CLASS, ATT_DEFAULTS, ATT_DOMAINS, ATT_PLATFORMS);
62  
63    /**
64     * Instantiates a new Import parser.
65     */
66    public ImportParser() {
67      super(EL_IMPORT, null, OPTIONAL_ATTRIBUTES);
68    }
69  
70    @Override
71    public ImportStatement doParse(Element element, Statement[] parentPath, BeneratorParseContext context) {
72      // check syntax
73      assertAtLeastOneAttributeIsSet(element, ATT_DEFAULTS, ATT_DOMAINS, ATT_PLATFORMS, ATT_CLASS);
74  
75      // prepare parsing
76      ArrayBuilder<String> classImports = new ArrayBuilder<>(String.class);
77      ArrayBuilder<String> domainImports = new ArrayBuilder<>(String.class);
78  
79      // defaults import
80      boolean defaults = ("true".equals(element.getAttribute("defaults")));
81  
82      // check class import
83      String attribute = element.getAttribute("class");
84      if (!StringUtil.isEmpty(attribute)) {
85        classImports.add(attribute);
86      }
87  
88      // (multiple) domain import
89      attribute = element.getAttribute("domains");
90      if (!StringUtil.isEmpty(attribute)) {
91        domainImports.addAll(StringUtil.trimAll(StringUtil.tokenize(attribute, ',')));
92      }
93  
94      // (multiple) platform import
95      attribute = element.getAttribute("platforms");
96  
97      List<PlatformDescriptor> platformImports = null;
98      if (!StringUtil.isEmpty(attribute)) {
99        platformImports = importPlatforms(StringUtil.trimAll(attribute.split(",")), context);
100     }
101 
102     return new ImportStatement(defaults, classImports.toArray(), domainImports.toArray(), platformImports);
103   }
104 
105   private static List<PlatformDescriptor> importPlatforms(String[] platformNames, BeneratorParseContext context) {
106     List<PlatformDescriptor> platforms = new ArrayList<>(platformNames.length);
107     for (String platformName : platformNames) {
108       PlatformDescriptor platformDescriptor = createPlatformDescriptor(platformName);
109       List<XMLElementParser<Statement>> parsers = platformDescriptor.getParsers();
110       if (parsers != null) {
111         for (XMLElementParser<Statement> parser : parsers) {
112           context.addParser(parser);
113         }
114       }
115       platforms.add(platformDescriptor);
116     }
117     return platforms;
118   }
119 
120   private static PlatformDescriptor createPlatformDescriptor(String platformName) {
121     String platformPackage = (platformName.indexOf('.') < 0 ? "com.rapiddweller.platform." + platformName : platformName);
122     String descriptorClassName = platformPackage + ".PlatformDescriptor";
123     try {
124       // if there is a platform descriptor, then use it
125       return (PlatformDescriptor) BeanUtil.newInstance(descriptorClassName);
126     } catch (RuntimeException e) {
127       if (ExceptionUtil.getRootCause(e) instanceof ClassNotFoundException) { // TODO test
128         return new DefaultPlatformDescriptor(platformPackage);
129       } else {
130         throw e;
131       }
132     }
133   }
134 
135 }