1
2
3
4
5 package zipdiff.output;
6
7 import zipdiff.Differences;
8 import java.io.PrintWriter;
9 import java.io.OutputStream;
10 import java.util.Set;
11 import java.util.Iterator;
12
13 /***
14 *
15 * Generates xml output for a Differences instance
16 *
17 * @author Sean C. Sullivan
18 *
19 */
20 public class XmlBuilder extends AbstractBuilder {
21
22 public void build(OutputStream out, Differences d) {
23 PrintWriter pw = new PrintWriter(out);
24
25 pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
26 pw.print("<zipdiff filename1=\"");
27
28 String filename1 = d.getFilename1();
29
30 if (filename1 == null) {
31 filename1 = "filename1.zip";
32 }
33 pw.print(filename1);
34 pw.print("\" filename2=\"");
35
36 String filename2 = d.getFilename2();
37
38 if (filename2 == null) {
39 filename2 = "filename2.zip";
40 }
41 pw.print(filename2);
42 pw.println("\">");
43
44 pw.println("<differences>");
45 writeAdded(pw, d.getAdded().keySet());
46 writeRemoved(pw, d.getRemoved().keySet());
47 writeChanged(pw, d.getChanged().keySet());
48 pw.println("</differences>");
49 pw.println("</zipdiff>");
50
51 pw.flush();
52 }
53
54 protected void writeAdded(PrintWriter pw, Set added) {
55 Iterator iter = added.iterator();
56 while (iter.hasNext()) {
57 String key = (String) iter.next();
58 pw.print("<added>");
59 pw.print(key);
60 pw.println("</added>");
61 }
62
63 }
64
65 protected void writeRemoved(PrintWriter pw, Set removed) {
66 Iterator iter = removed.iterator();
67 while (iter.hasNext()) {
68 String key = (String) iter.next();
69 pw.print("<removed>");
70 pw.print(key);
71 pw.println("</removed>");
72 }
73 }
74
75 protected void writeChanged(PrintWriter pw, Set changed) {
76 Iterator iter = changed.iterator();
77 while (iter.hasNext()) {
78 String key = (String) iter.next();
79 pw.print("<changed>");
80 pw.print(key);
81 pw.println("</changed>");
82 }
83 }
84
85 }