]> jaekl.net Git - cfb.git/blob - prod/net/jaekl/cfb/analyze/HtmlReport.java
c1ff762d60e6b052a4cafa91a9495439a1f7670c
[cfb.git] / prod / net / jaekl / cfb / analyze / HtmlReport.java
1 package net.jaekl.cfb.analyze;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6
7 import net.jaekl.cfb.CfbBundle;
8 import net.jaekl.cfb.store.Location;
9 import net.jaekl.cfb.util.Command;
10 import net.jaekl.cfb.xml.BugInstance;
11 import net.jaekl.cfb.xml.messages.BugPattern;
12 import net.jaekl.cfb.xml.messages.MessageCollection;
13
14 public class HtmlReport {
15         CfbBundle m_bundle;
16         Delta m_delta;
17         MessageCollection m_msgColl;
18         
19         public HtmlReport(CfbBundle bundle, MessageCollection msgColl, Delta delta)
20         {
21                 m_bundle = bundle;
22                 m_delta = delta;
23                 m_msgColl = msgColl;
24         }
25         
26         public Delta getDelta() { return m_delta; }
27         
28         public void write(File output) throws IOException
29         {
30                 try ( PrintWriter pw = new PrintWriter(output, Command.UTF_8) )
31                 {
32                         write(pw);
33                         pw.flush();
34                 }
35         }
36         
37         void write(PrintWriter pw) 
38         {
39                 startPage(pw);
40         }
41         
42         void startPage(PrintWriter pw) 
43         {
44                 writeHeader(pw);
45                 pw.println("  <BODY>");
46                 writeBody(pw);
47                 pw.println("  </BODY>");
48                 pw.println("</HTML>");
49         }
50         
51         String trans(String key, Object... arguments) {
52                 return m_bundle.get(key, arguments);
53         }
54         
55         void writeBody(PrintWriter pw)
56         {
57                 writeSummary(pw);
58                 writeBugs(pw, CfbBundle.NEW_BUGS,   m_delta.getNew());
59                 writeBugs(pw, CfbBundle.FIXED_BUGS, m_delta.getFixed());
60                 writeBugs(pw, CfbBundle.OLD_BUGS,   m_delta.getCommon());
61         }
62         
63         void writeBugLocations(PrintWriter pw, BugInstance bug)
64         {
65                 for (Location loc : bug.getLocations()) {
66                         StringBuffer sb = new StringBuffer();
67                         
68                         if (null != loc) {
69                                 if (null != loc.getClassName()) {
70                                         sb.append(loc.getClassName());
71                                 }
72                                 if ((null != loc.getMethodName()) && (loc.getMethodName().length() > 0)) {
73                                         sb.append(".").append(loc.getMethodName()).append("()");
74                                 }
75                                 int start = loc.getStart();
76                                 int end = loc.getEnd();
77                                 if (start > 0) {
78                                         sb.append(":").append("" + start);
79                                         if ((end > 0) && (end > start)) {
80                                                 sb.append("-").append("" + end);
81                                         }
82                                 }
83                                 
84                                 if (null != loc.getMethodRole()) {
85                                         sb.append(" (" + loc.getMethodRole() + ")");
86                                 }
87                         }
88                         
89                         pw.write("        <TR>");
90                         pw.write("          <TD COLSPAN=\"2\" CLASS=\"Loc\">" + sb.toString() + "</TD>");
91                         pw.write("        </TR>");                                      
92                 }
93         }
94         
95         void writeBugs(PrintWriter pw, String key, BugInstance[] bugs)
96         {
97                 if (null == bugs || bugs.length < 1) {
98                         return;
99                 }
100                 
101                 writeSectionHeading(pw, trans(key));
102                 
103                 for (BugInstance bug : bugs) {
104                         BugPattern pattern = m_msgColl.getPattern(bug.getType());
105                         
106                         pw.write("    <P>");
107                         pw.write("      <TABLE CLASS=\"bug\">");
108                         pw.write("        <TR>");
109                         pw.write("          <TD WIDTH=\"20%\">" + bug.getCategory() + "</TD>");
110                         pw.write("          <TD>" + bug.getType() + "</TD>");
111                         pw.write("        </TR>");
112                         writeBugLocations(pw, bug);
113                         pw.write("        <TR>");
114                         pw.write("          <TD COLSPAN=\"2\">" + pattern.getShort() + "</TD>");
115                         pw.write("        </TR>");
116                         pw.write("        <TR>");
117                         pw.write("          <TD COLSPAN=\"2\">" + pattern.getDetails() + "</TD>");
118                         pw.write("        </TR>");
119                         pw.write("      </TABLE>");
120                         pw.write("    </P>");
121                 }
122         }
123         
124         void writeHeader(PrintWriter pw)
125         {
126                 String title = trans(CfbBundle.CFB_REPORT);
127                 
128                 pw.println("<HTML>");
129                 pw.println("  <HEAD>");
130                 pw.println("    <META CHARSET=\"UTF-8\"/>");
131                 pw.println("    <TITLE>" + title + "</TITLE>");
132                 writeStyle(pw);
133                 pw.println("  </HEAD>");
134         }
135         
136         void writeSectionHeading(PrintWriter pw, String heading)
137         {
138                 pw.println("    <P CLASS=\"SectionHead\">");
139                 pw.println("      <TABLE WIDTH=\"100%\"><TR><TD>" + heading + "</TD></TR></TABLE>");
140                 pw.println("    </P>");
141         }
142         
143         void writeSummary(PrintWriter pw)
144         {
145                 final String SEP = ":&nbsp;&nbsp;";
146                 
147                 writeSectionHeading(pw, trans(CfbBundle.CFB_REPORT));
148                 pw.println("    <P>");
149                 pw.println("      <TABLE>");
150                 pw.println("        <TR>");
151                 pw.println("          <TD CLASS=\"CategoryName\">" + trans(CfbBundle.NEW_VERSION) + SEP + "</TD>");
152                 pw.println("          <TD CLASS=\"CategoryValue\">" + m_delta.getLater().constructVersionText(m_bundle) + "</TD>");
153                 pw.println("        </TR>");
154                 pw.println("        <TR>");
155                 pw.println("          <TD CLASS=\"CategoryName\">" + trans(CfbBundle.OLD_VERSION) + SEP + "</TD>");
156                 pw.println("          <TD CLASS=\"CategoryValue\">" + m_delta.getEarlier().constructVersionText(m_bundle) + "</TD>");
157                 pw.println("        </TR>");
158                 pw.println("        <TR><TD>&nbsp;</TD></TR>");
159                 pw.println("        <TR>");
160                 pw.println("          <TD CLASS=\"CategoryName\">" + trans(CfbBundle.NEW_BUGS) + SEP + "</TD>");
161                 pw.println("          <TD CLASS=\"CategoryValue\">" + trans(CfbBundle.NUM_BUGS, m_delta.getNumNew()) + "</TD>");
162                 pw.println("        </TR>");
163                 pw.println("        <TR>");
164                 pw.println("          <TD CLASS=\"CategoryName\">" + trans(CfbBundle.FIXED_BUGS) + SEP + "</TD>");
165                 pw.println("          <TD CLASS=\"CategoryValue\">" + trans(CfbBundle.NUM_BUGS, m_delta.getNumFixed()) + "</TD>");
166                 pw.println("        </TR>");
167                 pw.println("        <TR>");
168                 pw.println("          <TD CLASS=\"CategoryName\">" + trans(CfbBundle.OLD_BUGS) + SEP + "</TD>");
169                 pw.println("          <TD CLASS=\"CategoryValue\">" + trans(CfbBundle.NUM_BUGS_OLD, m_delta.getNumCommon()) + "</TD>");
170                 pw.println("        </TR>");
171                 pw.println("      </TABLE>");
172                 pw.println("    </P>");
173         }
174         
175         void writeStyle(PrintWriter pw)
176         {
177                 pw.println("    <STYLE>");
178                 pw.println("                   body { background-color: #F0F0FF; }");
179             pw.println("    .CategoryName  { text-align: right; }");
180             pw.println("    .CategoryValue { text-align: left; }");
181                 pw.println("    .Loc           { font-family: monospace; }");
182             pw.println("    .SectionHead   td { background-color: #0000FF; color: #FFFFFF; font-size: 1.25em; font-weight: bold; }");
183                 pw.println("    </STYLE>");
184         }
185 }