View Javadoc

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 html output for a Differences instance
16   * 
17   * @author Sean C. Sullivan
18   *
19   * 
20   */
21  public class HtmlBuilder extends AbstractBuilder {
22  
23      public void build(OutputStream out, Differences d) {
24          PrintWriter pw = new PrintWriter(out);
25  
26          pw.println("<html>");
27          pw.println("<META http-equiv=\"Content-Type\" content=\"text/html\">");
28          pw.println("<head>");
29          pw.println("<title>File differences</title>");
30          pw.println("</head>");
31  
32          pw.println("<body text=\"#000000\" vlink=\"#000000\" alink=\"#000000\" link=\"#000000\">");
33          
34          pw.println(getStyleTag());
35          pw.print("<p>First file: ");
36          String filename1 = d.getFilename1();
37  
38          if (filename1 == null) {
39              filename1 = "filename1.zip";
40          }
41          pw.print(filename1);
42          pw.println("<br>");
43  
44          pw.print("Second file: ");
45  
46          String filename2 = d.getFilename2();
47  
48          if (filename2 == null) {
49              filename2 = "filename2.zip";
50          }
51          pw.print(filename2);
52          pw.println("</p>");
53  
54          writeAdded(pw, d.getAdded().keySet());
55          writeRemoved(pw, d.getRemoved().keySet());
56          writeChanged(pw, d.getChanged().keySet());
57          pw.println("<hr>");
58          pw.println("<p>");
59          pw.println("Generated at " + new java.util.Date());
60          pw.println("</p>");
61          pw.println("</body>");
62  
63          pw.println("</html>");
64  
65          pw.flush();
66  
67      }
68  
69      protected void writeAdded(PrintWriter pw, Set added) {
70      	writeDiffSet(pw, "Added", added);
71      }
72  
73      protected void writeRemoved(PrintWriter pw, Set removed) {
74      	writeDiffSet(pw, "Removed", removed);
75      }
76  
77      protected void writeChanged(PrintWriter pw, Set changed) {
78      	writeDiffSet(pw, "Changed", changed);
79      }
80  
81      protected void writeDiffSet(PrintWriter pw, String name, Set s) {
82      	pw.println("<TABLE CELLSPACING=\"1\" CELLPADDING=\"3\" WIDTH=\"100%\" BORDER=\"0\">");
83      	pw.println("<tr>");
84          pw.println("<td class=\"diffs\" colspan=\"2\">" + name  + " (" + s.size() + " entries)</td>");
85          pw.println("</tr>");
86          pw.println("<tr>");
87          pw.println("<td width=\"20\">");
88          pw.println("</td>");
89          pw.println("<td>");
90          if (s.size() > 0) {
91              pw.println("<ul>");
92              Iterator iter = s.iterator();
93              while (iter.hasNext()) {
94                  String key = (String) iter.next();
95                  pw.print("<li>");
96                  pw.print(key);
97                  pw.println("</li>");
98              }
99              pw.println("</ul>");
100         }
101         pw.println("</td>");
102         pw.println("</tr>");
103         pw.println("</table>");
104 
105     }
106 
107     protected String getStyleTag()
108     {
109         StringBuffer sb = new StringBuffer();
110         
111         sb.append("<style type=\"text/css\">");
112         sb.append(" body, p { ");
113         sb.append(" font-family: verdana,arial,helvetica; ");
114         sb.append(" font-size: 80%; ");
115         sb.append(" color:#000000; ");
116         sb.append(" } \n");
117         sb.append(" 	  .diffs { \n");
118         sb.append("         font-family: verdana,arial,helvetica; \n");
119         sb.append("         font-size: 80%; \n");
120         sb.append(" font-weight: bold; \n");
121         sb.append(" text-align:left; \n");
122         sb.append(" background:#a6caf0; \n");
123 	  	sb.append(" } \n"); 
124         sb.append(" tr, td { \n");
125         sb.append(" font-family: verdana,arial,helvetica; \n");
126         sb.append(" font-size: 80%; \n");
127         sb.append(" background:#eeeee0; \n");
128         sb.append(" } \n");  
129         sb.append(" </style>\n");
130         
131         return sb.toString();
132     }
133 
134 }