587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
package com.nokia.helium.diamonds;
|
|
20 |
|
|
21 |
import java.io.File;
|
628
|
22 |
import java.io.InputStream;
|
|
23 |
import java.io.FileNotFoundException;
|
587
|
24 |
import java.io.FileOutputStream;
|
628
|
25 |
import java.io.IOException;
|
|
26 |
import java.io.UnsupportedEncodingException;
|
587
|
27 |
import java.util.Iterator;
|
|
28 |
import java.util.List;
|
|
29 |
|
|
30 |
import org.apache.log4j.Logger;
|
|
31 |
import org.dom4j.Attribute;
|
|
32 |
import org.dom4j.Document;
|
|
33 |
import org.dom4j.DocumentException;
|
|
34 |
import org.dom4j.Element;
|
|
35 |
import org.dom4j.io.OutputFormat;
|
|
36 |
import org.dom4j.io.SAXReader;
|
|
37 |
import org.dom4j.io.XMLWriter;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* This class implements an XML file merger. All node from an external XML with
|
|
41 |
* same format (same root node name) will get added to the source root element.
|
|
42 |
*/
|
|
43 |
public class XMLMerger {
|
|
44 |
private Logger log = Logger.getLogger(getClass());
|
628
|
45 |
private InputStream mergeStream;
|
587
|
46 |
private Document doc;
|
|
47 |
private Element root;
|
628
|
48 |
private File outputFile;
|
587
|
49 |
|
|
50 |
/**
|
|
51 |
* Create an XMLMerger, the merge file will be used as input and output.
|
|
52 |
*
|
|
53 |
* @param merge
|
|
54 |
* @throws XMLMergerException
|
|
55 |
*/
|
628
|
56 |
public XMLMerger(InputStream stream, File file) throws XMLMergerException {
|
587
|
57 |
try {
|
628
|
58 |
outputFile = file;
|
|
59 |
mergeStream = stream;
|
587
|
60 |
SAXReader reader = new SAXReader();
|
628
|
61 |
doc = reader.read(mergeStream);
|
587
|
62 |
root = doc.getRootElement();
|
|
63 |
} catch (DocumentException e) {
|
|
64 |
throw new XMLMergerException(e.getMessage());
|
|
65 |
}
|
|
66 |
}
|
628
|
67 |
|
|
68 |
public void merge(InputStream stream) throws XMLMergerException {
|
|
69 |
try {
|
|
70 |
SAXReader reader = new SAXReader();
|
|
71 |
Document dataDoc = reader.read(stream);
|
|
72 |
merge(dataDoc);
|
|
73 |
} catch (DocumentException e) {
|
|
74 |
throw new XMLMergerException(e.getMessage());
|
|
75 |
}
|
|
76 |
}
|
587
|
77 |
|
628
|
78 |
private void merge(Document dataDoc) throws XMLMergerException {
|
|
79 |
Element dataRoot = dataDoc.getRootElement();
|
|
80 |
if (!root.getName().equals(dataRoot.getName())) {
|
|
81 |
throw new XMLMergerException(
|
|
82 |
"Trying to merge incompatible xml format ('"
|
|
83 |
+ root.getName() + "'!='" + dataRoot.getName()
|
|
84 |
+ "')");
|
|
85 |
}
|
|
86 |
mergeNode(root, dataRoot);
|
|
87 |
write();
|
|
88 |
|
|
89 |
}
|
587
|
90 |
/**
|
|
91 |
* Add all sub element of data file into the merged file. If the root
|
|
92 |
* element name is different for the merged file an XMLMergerException is
|
|
93 |
* thrown.
|
|
94 |
*
|
|
95 |
* @param data
|
|
96 |
* the input file.
|
|
97 |
* @throws XMLMergerException
|
|
98 |
*/
|
|
99 |
public void merge(File data) throws XMLMergerException {
|
|
100 |
log.debug("Merging " + data.getAbsolutePath());
|
|
101 |
try {
|
|
102 |
SAXReader reader = new SAXReader();
|
|
103 |
Document dataDoc = reader.read(data);
|
|
104 |
Element dataRoot = dataDoc.getRootElement();
|
628
|
105 |
merge(dataDoc);
|
587
|
106 |
} catch (DocumentException e) {
|
|
107 |
throw new XMLMergerException(e.getMessage());
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Merging two XML elements. It only keeps difference.
|
|
113 |
*
|
|
114 |
* @param dest
|
|
115 |
* @param src
|
|
116 |
*/
|
|
117 |
@SuppressWarnings("unchecked")
|
|
118 |
protected void mergeNode(Element dest, Element src) {
|
|
119 |
for (Iterator<Element> node = src.elements().iterator(); node.hasNext();) {
|
588
|
120 |
Element element = node.next();
|
587
|
121 |
|
588
|
122 |
List<Element> ses = dest.elements(element.getName());
|
587
|
123 |
boolean add = true;
|
|
124 |
for (Element se : ses) {
|
588
|
125 |
if (areSame(se, element)) {
|
|
126 |
log.debug("Element " + element.getName() + " already found in dest.");
|
587
|
127 |
add = false;
|
|
128 |
}
|
|
129 |
}
|
|
130 |
if (add) {
|
588
|
131 |
log.debug("Adding node " + element.getName() + " to " + dest.getName());
|
|
132 |
dest.add(element.detach());
|
587
|
133 |
} else if (ses.size() > 0) {
|
588
|
134 |
log.debug("Merging " + ses.get(0).getName() + " to " + element.getName());
|
|
135 |
mergeNode(ses.get(0), element);
|
587
|
136 |
}
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Compare two elements name and attributes. Returns true if name and all
|
|
142 |
* attributes are matching, false otherwise.
|
|
143 |
*
|
|
144 |
* @param a
|
|
145 |
* @param b
|
|
146 |
* @return boolean
|
|
147 |
*/
|
|
148 |
@SuppressWarnings("unchecked")
|
|
149 |
protected boolean areSame(Element a, Element b) {
|
|
150 |
log.debug("areSame:" + a + " <=> " + b);
|
628
|
151 |
if (!a.getName().equals(b.getName())) {
|
587
|
152 |
return false;
|
628
|
153 |
}
|
587
|
154 |
log.debug("same attribute list size?");
|
628
|
155 |
if (a.attributes().size() != b.attributes().size()) {
|
587
|
156 |
return false;
|
628
|
157 |
}
|
587
|
158 |
log.debug("same attribute list?");
|
|
159 |
for (Iterator<Attribute> at = a.attributes().iterator(); at.hasNext();) {
|
|
160 |
Attribute attra = at.next();
|
|
161 |
Attribute attrb = b.attribute(attra.getName());
|
628
|
162 |
if (attrb == null || !attra.getValue().equals(attrb.getValue())) {
|
587
|
163 |
return false;
|
628
|
164 |
}
|
587
|
165 |
}
|
628
|
166 |
if (!a.getTextTrim().equals(b.getTextTrim())) {
|
587
|
167 |
return false;
|
628
|
168 |
}
|
587
|
169 |
return true;
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Write the XML content back the file.
|
|
174 |
* @throws XMLMergerException
|
|
175 |
*/
|
|
176 |
protected void write() throws XMLMergerException {
|
|
177 |
try {
|
628
|
178 |
FileOutputStream fos = new FileOutputStream(outputFile);
|
587
|
179 |
OutputFormat format = OutputFormat.createPrettyPrint();
|
|
180 |
XMLWriter writer = new XMLWriter(fos, format);
|
|
181 |
writer.write(doc);
|
|
182 |
writer.flush();
|
|
183 |
} catch (FileNotFoundException e) {
|
|
184 |
throw new XMLMergerException(e.getMessage());
|
|
185 |
} catch (UnsupportedEncodingException e) {
|
|
186 |
throw new XMLMergerException(e.getMessage());
|
|
187 |
} catch (IOException e) {
|
|
188 |
throw new XMLMergerException(e.getMessage());
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Exception class related to the XMLMerger.
|
|
194 |
*/
|
|
195 |
public class XMLMergerException extends Exception {
|
|
196 |
|
|
197 |
private static final long serialVersionUID = 7624650310086957316L;
|
|
198 |
|
|
199 |
/**
|
|
200 |
* Default constructor.
|
|
201 |
* @param msg error message
|
|
202 |
*/
|
|
203 |
public XMLMergerException(String msg) {
|
|
204 |
super(msg);
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
}
|