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.statement;
28  
29  import com.rapiddweller.benerator.BeneratorUtil;
30  import com.rapiddweller.benerator.engine.BeneratorContext;
31  import com.rapiddweller.benerator.engine.DescriptorRunner;
32  import com.rapiddweller.benerator.engine.Statement;
33  import com.rapiddweller.benerator.parser.DefaultEntryConverter;
34  import com.rapiddweller.common.ConfigurationError;
35  import com.rapiddweller.common.IOUtil;
36  import com.rapiddweller.format.script.ScriptConverterForStrings;
37  import com.rapiddweller.platform.xml.XMLSchemaDescriptorProvider;
38  import com.rapiddweller.script.Expression;
39  import org.apache.logging.log4j.LogManager;
40  import org.apache.logging.log4j.Logger;
41  
42  import java.io.IOException;
43  
44  /**
45   * Executes an &lt;include/&gt; from an XML descriptor file.<br/>
46   * <br/>
47   * Created at 23.07.2009 07:18:54
48   *
49   * @author Volker Bergmann
50   * @since 0.6.0
51   */
52  public class IncludeStatement implements Statement {
53  
54    private static final Logger logger = LogManager.getLogger(IncludeStatement.class);
55  
56    private Expression<String> uriEx;
57  
58    /**
59     * Instantiates a new Include statement.
60     *
61     * @param uri the uri
62     */
63    public IncludeStatement(Expression<String> uri) {
64      this.uriEx = uri;
65    }
66  
67    /**
68     * Gets uri.
69     *
70     * @return the uri
71     */
72    public Expression<String> getUri() {
73      return uriEx;
74    }
75  
76    /**
77     * Sets uri.
78     *
79     * @param uri the uri
80     */
81    public void setUri(Expression<String> uri) {
82      this.uriEx = uri;
83    }
84  
85    @Override
86    public boolean execute(BeneratorContext context) {
87      String uri = context.resolveRelativeUri(uriEx.evaluate(context));
88      String lcUri = uri.toLowerCase();
89      try {
90        if (lcUri.endsWith(".properties")) {
91          includeProperties(uri, context);
92        } else if (BeneratorUtil.isDescriptorFilePath(uri)) {
93          includeDescriptor(uri, context);
94        } else if (lcUri.endsWith(".xsd")) {
95          includeXmlSchema(uri, context);
96        } else {
97          throw new ConfigurationError("Not a supported import file type: " + uri);
98        }
99        return true;
100     } catch (IOException e) {
101       throw new ConfigurationError("Error processing " + uri, e);
102     }
103   }
104 
105   /**
106    * Include properties.
107    *
108    * @param uri     the uri
109    * @param context the context
110    * @throws IOException the io exception
111    */
112   public static void includeProperties(String uri, BeneratorContext context) throws IOException {
113     logger.debug("Including properties file: " + uri);
114     ScriptConverterForStrings preprocessor = new ScriptConverterForStrings(context);
115     DefaultEntryConverter converter = new DefaultEntryConverter(preprocessor, context, true);
116     IOUtil.readProperties(uri, converter);
117   }
118 
119   /**
120    * Include xml schema.
121    *
122    * @param uri     the uri
123    * @param context the context
124    */
125   public static void includeXmlSchema(String uri, BeneratorContext context) {
126     logger.debug("Including XML Schema: " + uri);
127     new XMLSchemaDescriptorProvider(uri, context).close();
128   }
129 
130   private static void includeDescriptor(String uri, BeneratorContext context) throws IOException {
131     logger.debug("Including Benerator descriptor file: " + uri);
132     DescriptorRunnerescriptorRunner.html#DescriptorRunner">DescriptorRunner runner = new DescriptorRunner(uri, context);
133     runner.runWithoutShutdownHook();
134     runner.close();
135   }
136 
137 }