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.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
64
65
66
67
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
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
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
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 }