17 |
17 |
18 |
18 |
19 package com.nokia.helium.diamonds; |
19 package com.nokia.helium.diamonds; |
20 |
20 |
21 import java.io.File; |
21 import java.io.File; |
|
22 import java.io.InputStream; |
|
23 import java.io.FileNotFoundException; |
22 import java.io.FileOutputStream; |
24 import java.io.FileOutputStream; |
|
25 import java.io.IOException; |
|
26 import java.io.UnsupportedEncodingException; |
23 import java.util.Iterator; |
27 import java.util.Iterator; |
24 import java.util.List; |
28 import java.util.List; |
25 import java.io.FileNotFoundException; |
|
26 import java.io.IOException; |
|
27 import java.io.UnsupportedEncodingException; |
|
28 |
29 |
29 import org.apache.log4j.Logger; |
30 import org.apache.log4j.Logger; |
30 import org.dom4j.Attribute; |
31 import org.dom4j.Attribute; |
31 import org.dom4j.Document; |
32 import org.dom4j.Document; |
32 import org.dom4j.DocumentException; |
33 import org.dom4j.DocumentException; |
39 * This class implements an XML file merger. All node from an external XML with |
40 * This class implements an XML file merger. All node from an external XML with |
40 * same format (same root node name) will get added to the source root element. |
41 * same format (same root node name) will get added to the source root element. |
41 */ |
42 */ |
42 public class XMLMerger { |
43 public class XMLMerger { |
43 private Logger log = Logger.getLogger(getClass()); |
44 private Logger log = Logger.getLogger(getClass()); |
44 private File merge; |
45 private InputStream mergeStream; |
45 private Document doc; |
46 private Document doc; |
46 private Element root; |
47 private Element root; |
|
48 private File outputFile; |
47 |
49 |
48 /** |
50 /** |
49 * Create an XMLMerger, the merge file will be used as input and output. |
51 * Create an XMLMerger, the merge file will be used as input and output. |
50 * |
52 * |
51 * @param merge |
53 * @param merge |
52 * @throws XMLMergerException |
54 * @throws XMLMergerException |
53 */ |
55 */ |
54 public XMLMerger(File merge) throws XMLMergerException { |
56 public XMLMerger(InputStream stream, File file) throws XMLMergerException { |
55 log.debug("Merging into: " + merge.getAbsolutePath()); |
57 try { |
56 try { |
58 outputFile = file; |
57 this.merge = merge; |
59 mergeStream = stream; |
58 SAXReader reader = new SAXReader(); |
60 SAXReader reader = new SAXReader(); |
59 doc = reader.read(merge); |
61 doc = reader.read(mergeStream); |
60 root = doc.getRootElement(); |
62 root = doc.getRootElement(); |
61 } catch (DocumentException e) { |
63 } catch (DocumentException e) { |
62 throw new XMLMergerException(e.getMessage()); |
64 throw new XMLMergerException(e.getMessage()); |
63 } |
65 } |
64 } |
66 } |
|
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 } |
65 |
77 |
|
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 } |
66 /** |
90 /** |
67 * Add all sub element of data file into the merged file. If the root |
91 * Add all sub element of data file into the merged file. If the root |
68 * element name is different for the merged file an XMLMergerException is |
92 * element name is different for the merged file an XMLMergerException is |
69 * thrown. |
93 * thrown. |
70 * |
94 * |
76 log.debug("Merging " + data.getAbsolutePath()); |
100 log.debug("Merging " + data.getAbsolutePath()); |
77 try { |
101 try { |
78 SAXReader reader = new SAXReader(); |
102 SAXReader reader = new SAXReader(); |
79 Document dataDoc = reader.read(data); |
103 Document dataDoc = reader.read(data); |
80 Element dataRoot = dataDoc.getRootElement(); |
104 Element dataRoot = dataDoc.getRootElement(); |
81 if (!root.getName().equals(dataRoot.getName())) { |
105 merge(dataDoc); |
82 throw new XMLMergerException( |
|
83 "Trying to merge incompatible xml format ('" |
|
84 + root.getName() + "'!='" + dataRoot.getName() |
|
85 + "')"); |
|
86 } |
|
87 mergeNode(root, dataRoot); |
|
88 write(); |
|
89 } catch (DocumentException e) { |
106 } catch (DocumentException e) { |
90 throw new XMLMergerException(e.getMessage()); |
107 throw new XMLMergerException(e.getMessage()); |
91 } |
108 } |
92 } |
109 } |
93 |
110 |
129 * @return boolean |
146 * @return boolean |
130 */ |
147 */ |
131 @SuppressWarnings("unchecked") |
148 @SuppressWarnings("unchecked") |
132 protected boolean areSame(Element a, Element b) { |
149 protected boolean areSame(Element a, Element b) { |
133 log.debug("areSame:" + a + " <=> " + b); |
150 log.debug("areSame:" + a + " <=> " + b); |
134 if (!a.getName().equals(b.getName())) |
151 if (!a.getName().equals(b.getName())) { |
135 return false; |
152 return false; |
|
153 } |
136 log.debug("same attribute list size?"); |
154 log.debug("same attribute list size?"); |
137 if (a.attributes().size() != b.attributes().size()) |
155 if (a.attributes().size() != b.attributes().size()) { |
138 return false; |
156 return false; |
|
157 } |
139 log.debug("same attribute list?"); |
158 log.debug("same attribute list?"); |
140 for (Iterator<Attribute> at = a.attributes().iterator(); at.hasNext();) { |
159 for (Iterator<Attribute> at = a.attributes().iterator(); at.hasNext();) { |
141 Attribute attra = at.next(); |
160 Attribute attra = at.next(); |
142 Attribute attrb = b.attribute(attra.getName()); |
161 Attribute attrb = b.attribute(attra.getName()); |
143 if (attrb == null || !attra.getValue().equals(attrb.getValue())) |
162 if (attrb == null || !attra.getValue().equals(attrb.getValue())) { |
144 return false; |
163 return false; |
145 } |
164 } |
146 if (!a.getTextTrim().equals(b.getTextTrim())) |
165 } |
|
166 if (!a.getTextTrim().equals(b.getTextTrim())) { |
147 return false; |
167 return false; |
|
168 } |
148 return true; |
169 return true; |
149 } |
170 } |
150 |
171 |
151 /** |
172 /** |
152 * Write the XML content back the file. |
173 * Write the XML content back the file. |
153 * @throws XMLMergerException |
174 * @throws XMLMergerException |
154 */ |
175 */ |
155 protected void write() throws XMLMergerException { |
176 protected void write() throws XMLMergerException { |
156 log.debug("Writing " + merge.getAbsolutePath()); |
177 try { |
157 try { |
178 FileOutputStream fos = new FileOutputStream(outputFile); |
158 FileOutputStream fos = new FileOutputStream(merge); |
|
159 OutputFormat format = OutputFormat.createPrettyPrint(); |
179 OutputFormat format = OutputFormat.createPrettyPrint(); |
160 XMLWriter writer = new XMLWriter(fos, format); |
180 XMLWriter writer = new XMLWriter(fos, format); |
161 writer.write(doc); |
181 writer.write(doc); |
162 writer.flush(); |
182 writer.flush(); |
163 } catch (FileNotFoundException e) { |
183 } catch (FileNotFoundException e) { |