1
- /*******************************************************************************
2
- * Copyright © Capgemini 2013. All rights reserved.
3
- ******************************************************************************/
4
1
package com .capgemini .cobigen .xmlplugin ;
5
2
6
3
import java .io .File ;
46
43
47
44
/**
48
45
* The {@link XmlMerger} combines all functionality for merging XML structures
49
- *
46
+ *
50
47
* @author mbrunnli (12.03.2013)
51
48
*/
52
49
public class XmlMerger implements IMerger {
53
50
51
+ /**
52
+ * Assigning logger to XmlMerger
53
+ */
54
+ private static final Logger LOG = LoggerFactory .getLogger (XmlMerger .class );
55
+
54
56
/**
55
57
* Merger type to be registered
56
58
*/
@@ -61,11 +63,6 @@ public class XmlMerger implements IMerger {
61
63
*/
62
64
private XmlMerge xmlMerge ;
63
65
64
- /**
65
- * Assigning logger to XmlMerger
66
- */
67
- private Logger logger = LoggerFactory .getLogger (XmlMerger .class );
68
-
69
66
/**
70
67
* Creates a new {@link XmlMerger} with the given {@link BasicMergeAction} to be performed when merging
71
68
* xml elements
@@ -76,7 +73,7 @@ public class XmlMerger implements IMerger {
76
73
* @author mbrunnli (08.04.2014)
77
74
*/
78
75
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 ());
80
77
this .type = type ;
81
78
}
82
79
@@ -91,15 +88,14 @@ public String getType() {
91
88
92
89
/**
93
90
* {@inheritDoc}
94
- * @throws IOException
95
- * If the specified template could not be found
96
- * @throws MergeException
97
91
* @author trippl (05.03.2013)
98
92
*/
93
+ @ Override
99
94
public String merge (File base , String patch , String targetCharset ) throws IOException , MergeException {
100
95
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance ();
101
96
102
97
DocumentBuilder docBuilder ;
98
+ String source = "base file" ; // just for better error handling
103
99
try {
104
100
docBuilderFactory .setNamespaceAware (true );
105
101
docBuilderFactory .setValidating (false );
@@ -109,25 +105,13 @@ public String merge(File base, String patch, String targetCharset) throws IOExce
109
105
false );
110
106
docBuilder = docBuilderFactory .newDocumentBuilder ();
111
107
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 )));
123
114
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
- }
131
115
// removeRedundantComments(baseDoc, patchDoc); <-- BasicXmlMerge combined with
132
116
// CompletMergeAction takes care of that
133
117
@@ -137,17 +121,25 @@ public String merge(File base, String patch, String targetCharset) throws IOExce
137
121
return prettyPrintDocument (resultXml );
138
122
} catch (ParserConfigurationException e ) {
139
123
// ignore - developer fault
140
- logger .error (e . toString () );
124
+ LOG .error ("This might be a bug." , e );
141
125
} 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 );
144
127
throw new MergeException ("An exception occured while merging the file " + base .getAbsolutePath ()
145
128
+ ":\n " + e .getMessage ());
146
129
} 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 );
149
131
throw new MergeException ("An exception occured while printing the merged file "
150
132
+ 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\" ?>\n Otherwise you will get this error." );
141
+ }
142
+ throw new MergeException ("An exception occured while parsing the patch:\n " + e .getMessage ());
151
143
}
152
144
return null ;
153
145
}
@@ -216,12 +208,13 @@ private void removeRedundantComments(Document base, Document patch) {
216
208
217
209
for (int i = 0 ; i < patchCommentNodes .getLength (); i ++) {
218
210
Node commentNode = patchCommentNodes .item (i );
219
- if (containsComment (origianlCommentNodes , commentNode ))
211
+ if (containsComment (origianlCommentNodes , commentNode )) {
220
212
commentNode .getParentNode ().removeChild (commentNode );
213
+ }
221
214
}
222
215
} catch (XPathExpressionException e ) {
223
216
// ignore - developer fault
224
- logger .error (e . toString () );
217
+ LOG .error ("This might be a bug." , e );
225
218
}
226
219
}
227
220
@@ -237,8 +230,9 @@ private void removeRedundantComments(Document base, Document patch) {
237
230
*/
238
231
private boolean containsComment (NodeList comments , Node comment ) {
239
232
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 ())) {
241
234
return true ;
235
+ }
242
236
}
243
237
return false ;
244
238
}
@@ -266,7 +260,7 @@ private void removeEmptyLines(Document doc) {
266
260
}
267
261
} catch (XPathExpressionException e ) {
268
262
// ignore - developer fault
269
- logger .error (e . toString () );
263
+ LOG .error ("This might be a bug." , e );
270
264
}
271
265
}
272
266
@@ -280,8 +274,9 @@ private void removeEmptyLines(Document doc) {
280
274
private void addEmptyLinesBetweenRootChildNodes (Document doc ) {
281
275
282
276
Node root = doc .getDocumentElement ();
283
- if (root == null )
277
+ if (root == null ) {
284
278
return ;
279
+ }
285
280
286
281
List <Node > nodes = copyNodeList (root .getChildNodes ());
287
282
@@ -300,7 +295,7 @@ private void addEmptyLinesBetweenRootChildNodes(Document doc) {
300
295
* @author trippl (03.04.2013)
301
296
*/
302
297
private List <Node > copyNodeList (NodeList nodes ) {
303
- List <Node > copy = new ArrayList <Node >();
298
+ List <Node > copy = new ArrayList <>();
304
299
305
300
for (int i = 0 ; i < nodes .getLength (); i ++) {
306
301
copy .add (nodes .item (i ));
0 commit comments