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.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
53
54
55
56
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
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
73 assertAtLeastOneAttributeIsSet(element, ATT_DEFAULTS, ATT_DOMAINS, ATT_PLATFORMS, ATT_CLASS);
74
75
76 ArrayBuilder<String> classImports = new ArrayBuilder<>(String.class);
77 ArrayBuilder<String> domainImports = new ArrayBuilder<>(String.class);
78
79
80 boolean defaults = ("true".equals(element.getAttribute("defaults")));
81
82
83 String attribute = element.getAttribute("class");
84 if (!StringUtil.isEmpty(attribute)) {
85 classImports.add(attribute);
86 }
87
88
89 attribute = element.getAttribute("domains");
90 if (!StringUtil.isEmpty(attribute)) {
91 domainImports.addAll(StringUtil.trimAll(StringUtil.tokenize(attribute, ',')));
92 }
93
94
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
125 return (PlatformDescriptor) BeanUtil.newInstance(descriptorClassName);
126 } catch (RuntimeException e) {
127 if (ExceptionUtil.getRootCause(e) instanceof ClassNotFoundException) {
128 return new DefaultPlatformDescriptor(platformPackage);
129 } else {
130 throw e;
131 }
132 }
133 }
134
135 }