Skip to content

Commit 54cf693

Browse files
committed
#69: Merge branch 'dev_xmlplugin'
Conflicts: pom.xml
2 parents f2bbd3e + 72f51b1 commit 54cf693

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1180
-154
lines changed

cobigen/cobigen-xmlplugin/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<groupId>com.capgemini</groupId>
99
<artifactId>cobigen-xmlplugin</artifactId>
1010
<name>CobiGen - XML Plug-In</name>
11-
<version>1.0.1</version>
11+
<version>2.0.0-SNAPSHOT</version>
1212
<packaging>jar</packaging>
1313

1414
<parent>
@@ -21,7 +21,7 @@
2121
<dependency>
2222
<groupId>com.capgemini</groupId>
2323
<artifactId>cobigen-core</artifactId>
24-
<version>1.0.0</version>
24+
<version>1.1.0</version>
2525
</dependency>
2626

2727
<!-- XML Merge for structural merge-->

cobigen/cobigen-xmlplugin/src/main/java/com/capgemini/cobigen/xmlplugin/XmlPluginActivator.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import com.capgemini.cobigen.extension.IGeneratorPluginActivator;
66
import com.capgemini.cobigen.extension.IMerger;
77
import com.capgemini.cobigen.extension.ITriggerInterpreter;
8-
import com.capgemini.cobigen.xmlplugin.action.CompleteMergeAction;
9-
import com.capgemini.cobigen.xmlplugin.action.OverrideMergeAction;
8+
import com.capgemini.cobigen.xmlplugin.merger.XmlMerger;
9+
import com.capgemini.cobigen.xmlplugin.merger.action.CompleteMergeAction;
10+
import com.capgemini.cobigen.xmlplugin.merger.action.OverrideMergeAction;
1011
import com.google.common.collect.Lists;
1112

1213
/**
@@ -33,7 +34,7 @@ public List<IMerger> bindMerger() {
3334
*/
3435
@Override
3536
public List<ITriggerInterpreter> bindTriggerInterpreter() {
36-
return null;
37+
return Lists.<ITriggerInterpreter> newArrayList(new XmlTriggerInterpreter("xml"));
3738
}
3839

3940
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.capgemini.cobigen.xmlplugin;
2+
3+
import com.capgemini.cobigen.extension.IInputReader;
4+
import com.capgemini.cobigen.extension.IMatcher;
5+
import com.capgemini.cobigen.extension.ITriggerInterpreter;
6+
import com.capgemini.cobigen.xmlplugin.inputreader.XmlInputReader;
7+
import com.capgemini.cobigen.xmlplugin.matcher.XmlMatcher;
8+
9+
/**
10+
* {@link ITriggerInterpreter} implementation of a Xml Interpreter
11+
*
12+
* @author fkreis (18.11.2014)
13+
*/
14+
public class XmlTriggerInterpreter implements ITriggerInterpreter {
15+
16+
/**
17+
* {@link ITriggerInterpreter} type to be registered
18+
*/
19+
public String type;
20+
21+
/**
22+
* creates a new {@link XmlTriggerInterpreter}
23+
*
24+
* @param type
25+
* to be registered
26+
* @author fkreis (18.11.2014)
27+
*/
28+
public XmlTriggerInterpreter(String type) {
29+
super();
30+
this.type = type;
31+
}
32+
33+
/**
34+
* {@inheritDoc}
35+
* @author fkreis (18.11.2014)
36+
*/
37+
@Override
38+
public String getType() {
39+
return type;
40+
}
41+
42+
/**
43+
* {@inheritDoc}
44+
* @author fkreis (18.11.2014)
45+
*/
46+
@Override
47+
public IInputReader getInputReader() {
48+
return new XmlInputReader();
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
* @author fkreis (18.11.2014)
54+
*/
55+
@Override
56+
public IMatcher getMatcher() {
57+
return new XmlMatcher();
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.capgemini.cobigen.xmlplugin.inputreader;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* String constants of the xml object model for generation.
8+
*
9+
* @author fkreis (10.11.2014)
10+
*/
11+
public class ModelConstant {
12+
13+
/**
14+
* Root element for xml model ({@link Map}&lt;{@link String}, {@link Object}&gt;).
15+
*
16+
* @Deprecated use the xml document's root name instead.
17+
*/
18+
@Deprecated
19+
public static final String ROOT = "root";
20+
21+
/**
22+
* The node's name represented as {@link String}.
23+
*/
24+
public static final String NODE_NAME = "_nodeName_";
25+
26+
/**
27+
* Node for the concatenated text content of a xml node (PCDATA)
28+
*/
29+
public static final String TEXT_CONTENT = "_text_";
30+
31+
/**
32+
* A List of all children of type {@code TEXT_NODE}.
33+
*/
34+
public static final String TEXT_NODES = "TextNodes";
35+
36+
/**
37+
* prefix for a single attribute. An attribute will be represented by mapping from the attribute's name to
38+
* its value ({@link Map}&lt;{@link String}, {@link Object}&gt;)
39+
*/
40+
public static final String SINGLE_ATTRIBUTE = "_at_";
41+
42+
/**
43+
* A list of all attributes. Each of the SINGLE_ATTRIBUTEs will be provided here as a reference (
44+
* {@link List}&lt; {@link Map}&lt;{@link String}, {@link Object} &gt;&gt;).
45+
*/
46+
public static final String ATTRIBUTES = "Attributes";
47+
48+
/**
49+
* The attribute's name represented as {@link String}.
50+
*/
51+
public static final String ATTRIBUTE_NAME = "_attName_";
52+
53+
/**
54+
* The attribute's value represented as {@link String}.
55+
*/
56+
public static final String ATTRIBUTE_VALUE = "_attValue_";
57+
58+
/**
59+
* prefix for a single child node. An child node will be represented by mapping from the child's name to
60+
* its model ({@link Map}&lt;{@link String}, {@link Object}&gt;). If two ore more children have the same
61+
* name they will not provided here as SINGLE_CHILD, but in the list CHILDREN, like all die other
62+
* children.
63+
*/
64+
public static final String SINGLE_CHILD = "";
65+
66+
/**
67+
* A list of all children. In addition to the SINGLE_CHILDren also the children which do not have an
68+
* unique name will be provided here ( {@link List}&lt; {@link Map}&lt;{@link String}, {@link Object}
69+
* &gt;&gt;).
70+
*/
71+
public static final String CHILDREN = "Children";
72+
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.capgemini.cobigen.xmlplugin.inputreader;
2+
3+
import java.nio.charset.Charset;
4+
import java.util.HashMap;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import org.w3c.dom.Document;
10+
import org.w3c.dom.Element;
11+
import org.w3c.dom.NamedNodeMap;
12+
import org.w3c.dom.Node;
13+
import org.w3c.dom.NodeList;
14+
15+
import com.capgemini.cobigen.extension.IInputReader;
16+
17+
/**
18+
*
19+
* @author fkreis (10.11.2014)
20+
*/
21+
public class XmlInputReader implements IInputReader {
22+
23+
/**
24+
* {@inheritDoc}
25+
* @author fkreis (10.11.2014)
26+
*/
27+
@Override
28+
public boolean isValidInput(Object input) {
29+
if (input instanceof Document) {
30+
return true;
31+
} else {
32+
return false;
33+
}
34+
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
* @author fkreis (10.11.2014)
40+
*/
41+
@Override
42+
public Map<String, Object> createModel(Object input) {
43+
if (isValidInput(input)) {
44+
Document doc = (Document) input;
45+
Element rootElement = doc.getDocumentElement();
46+
Map<String, Object> model = new HashMap<>();
47+
model.put(rootElement.getNodeName(), deriveSubModel(rootElement));
48+
return new HashMap<>(model);
49+
} else {
50+
return null;
51+
}
52+
53+
}
54+
55+
/**
56+
* {@inheritDoc}<br>
57+
* <br>
58+
* Since the {@link XmlInputReader} does not support multiple input objects it always returns
59+
* <code>false</code>.
60+
* @author fkreis (10.11.2014)
61+
*/
62+
@Override
63+
public boolean combinesMultipleInputObjects(Object input) {
64+
return false;
65+
}
66+
67+
/**
68+
* {@inheritDoc}<br>
69+
* <br>
70+
* Since the {@link XmlInputReader} does not support multiple input objects it always returns an empty
71+
* {@link List}.
72+
* @author fkreis (10.11.2014)
73+
*/
74+
@Override
75+
public List<Object> getInputObjects(Object input, Charset inputCharset) {
76+
List<Object> emptyList = new LinkedList<>();
77+
return emptyList;
78+
}
79+
80+
/**
81+
* {@inheritDoc} <br>
82+
* <br>
83+
* Since the {@link XmlInputReader} does not provide any template methods it always returns an empty
84+
* {@link Map}.
85+
* @author fkreis (10.11.2014)
86+
*/
87+
@Override
88+
public Map<String, Object> getTemplateMethods(Object input) {
89+
Map<String, Object> emptyMap = new HashMap<>();
90+
return emptyMap;
91+
}
92+
93+
/**
94+
* @param input
95+
* the element the model should derived from
96+
* @return derived sub model
97+
* @author fkreis (17.11.2014)
98+
*/
99+
private Map<String, Object> deriveSubModel(Element input) {
100+
// prepare result object
101+
Map<String, Object> submodel = new HashMap<>();
102+
103+
// get all child nodes
104+
NodeList childList = input.getChildNodes();
105+
106+
// put element's name into the model
107+
submodel.put(ModelConstant.NODE_NAME, input.getNodeName());
108+
109+
// put element's attributes into a list and as single attributes into the model
110+
NamedNodeMap attributeNodes = input.getAttributes();
111+
List<Map<String, Object>> attrList = new LinkedList<>();
112+
for (int i = 0; i < attributeNodes.getLength(); i++) {
113+
Map<String, Object> att = new HashMap<>();
114+
Node currentAttrNode = attributeNodes.item(i);
115+
String attrName = currentAttrNode.getNodeName();
116+
String attrValue = currentAttrNode.getNodeValue();
117+
118+
// as list
119+
att.put(ModelConstant.ATTRIBUTE_NAME, attrName);
120+
att.put(ModelConstant.ATTRIBUTE_VALUE, attrValue);
121+
attrList.add(att);
122+
123+
// as single attributes
124+
submodel.put(ModelConstant.SINGLE_ATTRIBUTE + attrName, attrValue);
125+
}
126+
submodel.put(ModelConstant.ATTRIBUTES, attrList);
127+
128+
// put text nodes (pcdata) into the model
129+
List<String> textNodeList = new LinkedList<>();
130+
String textcontent = "";
131+
for (int i = 0; i < childList.getLength(); i++) {
132+
Node currentChild = childList.item(i);
133+
if (currentChild.getNodeType() == Node.TEXT_NODE) {
134+
String currentTextContent = currentChild.getTextContent().trim();
135+
// as String List
136+
if (!currentTextContent.equals("")) {
137+
textNodeList.add(currentTextContent);
138+
}
139+
// as concatenated String
140+
textcontent += currentTextContent;
141+
}
142+
}
143+
submodel.put(ModelConstant.TEXT_NODES, textNodeList);
144+
submodel.put(ModelConstant.TEXT_CONTENT, textcontent);
145+
146+
// put element child nodes into the model as list (call method recursively)
147+
List<Map<String, Object>> modelChildList = new LinkedList<>();
148+
List<String> blackList = new LinkedList<>();
149+
for (int i = 0; i < childList.getLength(); i++) {
150+
Node currentChild = childList.item(i);
151+
if (currentChild.getNodeType() == Node.ELEMENT_NODE) {
152+
Map<String, Object> currentChildModel = deriveSubModel((Element) currentChild);
153+
154+
// as list
155+
modelChildList.add(currentChildModel);
156+
157+
// as single child, only add if its name is unique, otherwise it just can be provided via
158+
// CHILDREN list
159+
String childname = currentChild.getNodeName();
160+
if (blackList.contains(childname)) {
161+
162+
} else if (submodel.containsKey(childname)) {
163+
submodel.remove(childname);
164+
blackList.add(childname);
165+
} else {
166+
submodel.put(childname, currentChildModel);
167+
}
168+
169+
}
170+
}
171+
submodel.put(ModelConstant.CHILDREN, modelChildList);
172+
173+
return new HashMap<>(submodel);
174+
}
175+
}

0 commit comments

Comments
 (0)