1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
46
47
48
49
50
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
60
61
62
63 public IncludeStatement(Expression<String> uri) {
64 this.uriEx = uri;
65 }
66
67
68
69
70
71
72 public Expression<String> getUri() {
73 return uriEx;
74 }
75
76
77
78
79
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
107
108
109
110
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
121
122
123
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 }