Skip to content

Commit af50e75

Browse files
committed
#54: Merge branch 'dev_xmlplugin'
2 parents 317d50d + 2fc347e commit af50e75

File tree

11 files changed

+283
-228
lines changed

11 files changed

+283
-228
lines changed

cobigen/cobigen-xmlplugin/pom.xml

+14-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<groupId>com.capgemini</groupId>
99
<artifactId>cobigen-xmlplugin</artifactId>
1010
<name>CobiGen - XML Plug-In</name>
11-
<version>1.0.0</version>
11+
<version>1.0.1-SNAPSHOT</version>
1212
<packaging>jar</packaging>
13-
13+
1414
<parent>
1515
<groupId>com.capgemini</groupId>
1616
<artifactId>cobigen-parent</artifactId>
@@ -23,7 +23,7 @@
2323
<artifactId>cobigen-core</artifactId>
2424
<version>1.0.0</version>
2525
</dependency>
26-
26+
2727
<!-- XML Merge for structural merge-->
2828
<dependency>
2929
<groupId>ch.elca.el4j.modules</groupId>
@@ -52,7 +52,7 @@
5252
</exclusion>
5353
</exclusions>
5454
</dependency>
55-
55+
5656
<!-- Adopt legacy logging -->
5757
<dependency>
5858
<groupId>org.slf4j</groupId>
@@ -64,7 +64,7 @@
6464
<artifactId>jcl-over-slf4j</artifactId>
6565
<version>1.7.7</version>
6666
</dependency>
67-
67+
6868
<!-- <dependency>
6969
<groupId>ch.elca.el4j.modules</groupId>
7070
<artifactId>module-jmx</artifactId>
@@ -91,15 +91,22 @@
9191
</exclusion>
9292
</exclusions>
9393
</dependency>
94-
94+
9595
<!-- Explicit definition of dependency due to access restrictions of the original XML Merge library dependencies -->
9696
<dependency>
9797
<groupId>javax.inject</groupId>
9898
<artifactId>javax.inject</artifactId>
9999
<version>1</version>
100100
</dependency>
101+
102+
<!-- Explicit JDom version as result of dependency conflicts -->
103+
<dependency>
104+
<groupId>org.jdom</groupId>
105+
<artifactId>jdom</artifactId>
106+
<version>1.1.3</version>
107+
</dependency>
101108
</dependencies>
102-
109+
103110
<repositories>
104111
<!-- For XML Merge -->
105112
<!-- Build with Maven2 or 3.1.x due to strange dependencies which should be authorized !!! -->

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

+37-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/*******************************************************************************
2-
* Copyright © Capgemini 2013. All rights reserved.
3-
******************************************************************************/
41
package com.capgemini.cobigen.xmlplugin;
52

63
import java.io.File;
@@ -46,11 +43,16 @@
4643

4744
/**
4845
* The {@link XmlMerger} combines all functionality for merging XML structures
49-
*
46+
*
5047
* @author mbrunnli (12.03.2013)
5148
*/
5249
public class XmlMerger implements IMerger {
5350

51+
/**
52+
* Assigning logger to XmlMerger
53+
*/
54+
private static final Logger LOG = LoggerFactory.getLogger(XmlMerger.class);
55+
5456
/**
5557
* Merger type to be registered
5658
*/
@@ -61,11 +63,6 @@ public class XmlMerger implements IMerger {
6163
*/
6264
private XmlMerge xmlMerge;
6365

64-
/**
65-
* Assigning logger to XmlMerger
66-
*/
67-
private Logger logger = LoggerFactory.getLogger(XmlMerger.class);
68-
6966
/**
7067
* Creates a new {@link XmlMerger} with the given {@link BasicMergeAction} to be performed when merging
7168
* xml elements
@@ -76,7 +73,7 @@ public class XmlMerger implements IMerger {
7673
* @author mbrunnli (08.04.2014)
7774
*/
7875
public XmlMerger(String type, BasicMergeAction action) {
79-
this.xmlMerge = new BasicXmlMerge(action, new IdentityMapper(), new XmlMatcher());
76+
xmlMerge = new BasicXmlMerge(action, new IdentityMapper(), new XmlMatcher());
8077
this.type = type;
8178
}
8279

@@ -91,15 +88,14 @@ public String getType() {
9188

9289
/**
9390
* {@inheritDoc}
94-
* @throws IOException
95-
* If the specified template could not be found
96-
* @throws MergeException
9791
* @author trippl (05.03.2013)
9892
*/
93+
@Override
9994
public String merge(File base, String patch, String targetCharset) throws IOException, MergeException {
10095
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
10196

10297
DocumentBuilder docBuilder;
98+
String source = "base file"; // just for better error handling
10399
try {
104100
docBuilderFactory.setNamespaceAware(true);
105101
docBuilderFactory.setValidating(false);
@@ -109,25 +105,13 @@ public String merge(File base, String patch, String targetCharset) throws IOExce
109105
false);
110106
docBuilder = docBuilderFactory.newDocumentBuilder();
111107

112-
Document baseDoc;
113-
try {
114-
baseDoc =
115-
docBuilder.parse(new InputSource(new InputStreamReader(new FileInputStream(base),
116-
targetCharset)));
117-
} catch (SAXException e) {
118-
logger.error("{}{}{}", "An exception occured while parsing the base file ",
119-
base.getAbsolutePath(), ":\n", e.getMessage());
120-
throw new MergeException("An exception occured while parsing the base file "
121-
+ base.getAbsolutePath() + ":\n" + e.getMessage());
122-
}
108+
Document baseDoc =
109+
docBuilder.parse(new InputSource(new InputStreamReader(new FileInputStream(base),
110+
targetCharset)));
111+
source = "patch"; // base doc parsed correctly, next should be patch
112+
113+
Document patchDoc = docBuilder.parse(new InputSource(new StringReader(patch)));
123114

124-
Document patchDoc;
125-
try {
126-
patchDoc = docBuilder.parse(new InputSource(new StringReader(patch)));
127-
} catch (SAXException e) {
128-
logger.error("{}", "An exception occured while parsing the patch:\n", e.getMessage());
129-
throw new MergeException("An exception occured while parsing the patch:\n" + e.getMessage());
130-
}
131115
// removeRedundantComments(baseDoc, patchDoc); <-- BasicXmlMerge combined with
132116
// CompletMergeAction takes care of that
133117

@@ -137,17 +121,25 @@ public String merge(File base, String patch, String targetCharset) throws IOExce
137121
return prettyPrintDocument(resultXml);
138122
} catch (ParserConfigurationException e) {
139123
// ignore - developer fault
140-
logger.error(e.toString());
124+
LOG.error("This might be a bug.", e);
141125
} catch (AbstractXmlMergeException e) {
142-
logger.error("{}{}{}", "An exception occured while merging the file ", base.getAbsolutePath(),
143-
":\n", e.getMessage());
126+
LOG.error("An exception occured while merging the file '{}'", base.getAbsolutePath(), e);
144127
throw new MergeException("An exception occured while merging the file " + base.getAbsolutePath()
145128
+ ":\n" + e.getMessage());
146129
} catch (TransformerException e) {
147-
logger.error("{}{}{}", "An exception occured while merging the file ", base.getAbsolutePath(),
148-
":\n", e.getMessage());
130+
LOG.error("An exception occured while merging the file '{}'", base.getAbsolutePath(), e);
149131
throw new MergeException("An exception occured while printing the merged file "
150132
+ base.getAbsolutePath() + ":\n" + e.getMessage());
133+
} catch (SAXException e) {
134+
LOG.error("An exception occured while parsing the patch.", e);
135+
if (e.getMessage().contains(
136+
"The processing instruction target matching \"[xX][mM][lL]\" is not allowed")) {
137+
throw new MergeException("An exception occured while parsing the " + source + ".\n"
138+
+ "Please check whether the first line of the " + source + "(" + base.getName() + ") "
139+
+ " starts with the xml declaration like:\n"
140+
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\nOtherwise you will get this error.");
141+
}
142+
throw new MergeException("An exception occured while parsing the patch:\n" + e.getMessage());
151143
}
152144
return null;
153145
}
@@ -216,12 +208,13 @@ private void removeRedundantComments(Document base, Document patch) {
216208

217209
for (int i = 0; i < patchCommentNodes.getLength(); i++) {
218210
Node commentNode = patchCommentNodes.item(i);
219-
if (containsComment(origianlCommentNodes, commentNode))
211+
if (containsComment(origianlCommentNodes, commentNode)) {
220212
commentNode.getParentNode().removeChild(commentNode);
213+
}
221214
}
222215
} catch (XPathExpressionException e) {
223216
// ignore - developer fault
224-
logger.error(e.toString());
217+
LOG.error("This might be a bug.", e);
225218
}
226219
}
227220

@@ -237,8 +230,9 @@ private void removeRedundantComments(Document base, Document patch) {
237230
*/
238231
private boolean containsComment(NodeList comments, Node comment) {
239232
for (int i = 0; i < comments.getLength(); i++) {
240-
if (comments.item(i).getNodeValue().equals(comment.getNodeValue()))
233+
if (comments.item(i).getNodeValue().equals(comment.getNodeValue())) {
241234
return true;
235+
}
242236
}
243237
return false;
244238
}
@@ -266,7 +260,7 @@ private void removeEmptyLines(Document doc) {
266260
}
267261
} catch (XPathExpressionException e) {
268262
// ignore - developer fault
269-
logger.error(e.toString());
263+
LOG.error("This might be a bug.", e);
270264
}
271265
}
272266

@@ -280,8 +274,9 @@ private void removeEmptyLines(Document doc) {
280274
private void addEmptyLinesBetweenRootChildNodes(Document doc) {
281275

282276
Node root = doc.getDocumentElement();
283-
if (root == null)
277+
if (root == null) {
284278
return;
279+
}
285280

286281
List<Node> nodes = copyNodeList(root.getChildNodes());
287282

@@ -300,7 +295,7 @@ private void addEmptyLinesBetweenRootChildNodes(Document doc) {
300295
* @author trippl (03.04.2013)
301296
*/
302297
private List<Node> copyNodeList(NodeList nodes) {
303-
List<Node> copy = new ArrayList<Node>();
298+
List<Node> copy = new ArrayList<>();
304299

305300
for (int i = 0; i < nodes.getLength(); i++) {
306301
copy.add(nodes.item(i));

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/*
2-
* Copyright © Capgemini 2013. All rights reserved.
3-
*/
41
package com.capgemini.cobigen.xmlplugin;
52

63
import java.util.List;
@@ -13,7 +10,7 @@
1310
import com.google.common.collect.Lists;
1411

1512
/**
16-
*
13+
*
1714
* @author mbrunnli (06.04.2014)
1815
*/
1916
public class XmlPluginActivator implements IGeneratorPluginActivator {

0 commit comments

Comments
 (0)