1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package convwatch;
29 
30 import java.io.File;
31 import java.io.FileWriter;
32 import helper.OSHelper;
33 
34 public class HTMLOutputter
35 {
36     FileWriter m_aOut;
37     String m_sFilename;
38     String m_sNamePrefix;              // the HTML files used a suffix to build it's right name
39 
40     /**
41      * ls is the current line separator (carridge return)
42      */
43     String ls;
44 
45     HTMLOutputter() {}
46     public static HTMLOutputter create( String _sOutputPath, String _sHTMLFilename, String _sNamePrefix, String _sTitle )
47         {
48             FileHelper.makeDirectories("", _sOutputPath);
49             HTMLOutputter a = new HTMLOutputter();
50             String fs = System.getProperty("file.separator");
51             String sFilename = _sOutputPath + fs + _sHTMLFilename;
52 
53             try
54             {
55                 File outputFile = new File(sFilename);
56                 a.m_aOut = new FileWriter(outputFile.toString());
57                 a.ls = System.getProperty("line.separator");
58             }
59             catch (java.io.IOException e)
60             {
61                 e.printStackTrace();
62                 GlobalLogWriter.get().println("ERROR: Can't create HTML Outputter");
63                 return null;
64             }
65             a.m_sFilename = sFilename;
66             a.m_sNamePrefix = _sNamePrefix;
67             return a;
68         }
69     public String getFilename() {return m_sFilename;}
70 
71     public void header(String _sTitle)
72         {
73             try
74             {
75                 m_aOut.write( "<html>" + ls);
76                 m_aOut.write( "<head>"  + ls);
77                 m_aOut.write( "<title>" + _sTitle + "</title>" + ls);
78                 m_aOut.write( "<link rel=\"stylesheet\" type=\"text/css\" href=\"/gfxcmp_ui/xmloff.css\" media=\"screen\" />" + ls);
79                 m_aOut.write( "<link rel=\"stylesheet\" type=\"text/css\" href=\"/gfxcmp_ui/style.css\" media=\"screen\" />" + ls);
80                 m_aOut.write( "</head>" + ls);
81                 m_aOut.write( "<body bgcolor=white>" + ls);
82                 m_aOut.flush();
83             }
84             catch (java.io.IOException e)
85             {
86             }
87         }
88 
89     final static String TEST_TABLETITLE = "Test";
90     final static String VISUAL_STATUS_TABLETITLE = "Visual status";
91     final static String VISUAL_STATUS_MESSAGE_TABLETITLE = "Message";
92 
93     public void indexSection(String _sOfficeInfo)
94         {
95             try
96             {
97                 m_aOut.write( "<h2>Results for " + _sOfficeInfo + "</h2>" + ls);
98                 m_aOut.write( "<p>Legend:<br>");
99                 m_aOut.write( stronghtml(FIRSTGFX_TABLETITLE) + " contains the output printed via 'ghostscript' as a jpeg picture.<br>");
100 
101                 m_aOut.write( "<table class=\"infotable\">" + ls);
102                 m_aOut.write( "<TR>");
103                 m_aOut.write( tableHeaderCell(TEST_TABLETITLE));
104                 m_aOut.write( tableHeaderCell(TEST_TABLETITLE));
105                 m_aOut.write( tableHeaderCell(VISUAL_STATUS_TABLETITLE));
106                 m_aOut.write( tableHeaderCell(VISUAL_STATUS_MESSAGE_TABLETITLE));
107                 m_aOut.write( "</TR>" + ls);
108                 m_aOut.flush();
109             }
110             catch (java.io.IOException e)
111             {
112             }
113         }
114 
115     String getHREF(String _sHREF, String _sPathInfo)
116         {
117             StringBuffer a = new StringBuffer();
118             if (! OSHelper.isWindows())
119             {
120                 // System.out.println("Tu'nix system.");
121                 a.append("<A HREF=\"");
122                 a.append(_sHREF);
123                 a.append("\">");
124                 a.append(_sPathInfo);
125                 a.append("</A>");
126             }
127             else
128             {
129                 // System.out.println("Windows system.");
130                 //! this should be replaced by a better method
131                 //! name(WIN|UNIX)
132                 a.append("<A HREF=\"");
133                 a.append(_sHREF);
134                 a.append("\">");
135                 a.append(_sPathInfo);
136                 // a.append("(first)");
137                 a.append("</A>");
138                 // if (_sHREF.charAt(1) == ':' && (_sHREF.charAt(0) == 'x' || _sHREF.charAt(0) == 'X'))
139                 // int index = 0;
140                 // index = _sHREF.indexOf("X:");
141                 // if (index == -1)
142                 // {
143                 //     index = _sHREF.indexOf("x:");
144                 // }
145                 // if (index >= 0)
146                 // {
147                 //     // int index = 0;
148                 //     // remove "X:" and insert "/tausch"
149                 //     StringBuffer sbUNIXPath = new StringBuffer( _sHREF.substring(0, index) );
150                 //     sbUNIXPath.append("/tausch");
151                 //     sbUNIXPath.append(_sHREF.substring(index + 2));
152                 //     String sUNIXPath = sbUNIXPath.toString();
153                 //     sUNIXPath = utils.replaceAll13(sUNIXPath, "\\", "/");
154                 //
155                 //     a.append("<A HREF=\"");
156                 //     a.append(sUNIXPath);
157                 //     a.append("\">");
158                 //     a.append("(second)");
159                 //     a.append("</A>");
160                 // }
161                 // else
162                 // {
163                 //     System.out.println("Path is '" + _sHREF + "'");
164                 // }
165 
166             }
167             return a.toString();
168         }
169 
170     String tableDataCell(String _sValue)
171         {
172             StringBuffer a = new StringBuffer();
173             a.append("<TD>");
174             a.append(_sValue);
175             a.append("</TD>");
176             return a.toString();
177         }
178 
179     String tableHeaderCell(String _sValue)
180         {
181             StringBuffer a = new StringBuffer();
182             a.append("<TH>");
183             a.append(_sValue);
184             a.append("</TH>");
185             return a.toString();
186         }
187 
188     public void indexLine(String _sHTMLFile, String _sHTMLName, String _sHTMLFile2, String _sHTMLName2, String _sStatusRunThrough, String _sStatusMessage)
189         {
190             try
191             {
192                 m_aOut.write( "<TR>");
193                 m_aOut.write(tableDataCell( getHREF(_sHTMLFile, _sHTMLName) ) );
194                 if (_sHTMLFile2.length() > 0)
195                 {
196                     m_aOut.write(tableDataCell( getHREF(_sHTMLFile2, _sHTMLName2) ) );
197                 }
198                 else
199                 {
200                     m_aOut.write(tableDataCell( "" ) );
201                 }
202 
203                 m_aOut.write( tableDataCell(_sStatusRunThrough) );
204                 m_aOut.write( tableDataCell(_sStatusMessage) );
205                 m_aOut.write( "</TR>" + ls);
206 
207                 m_aOut.flush();
208             }
209             catch (java.io.IOException e)
210             {
211             }
212         }
213 
214     public void close()
215         {
216             try
217             {
218                 m_aOut.write( "</TABLE>" + ls);
219                 m_aOut.write( "</BODY></HTML>" + ls);
220                 m_aOut.flush();
221                 m_aOut.close();
222             }
223             catch (java.io.IOException e)
224             {
225             }
226         }
227 
228 // -----------------------------------------------------------------------------
229     String stronghtml(String _sValue)
230         {
231             StringBuffer a = new StringBuffer();
232             a.append("<STRONG>");
233             a.append(_sValue);
234             a.append("</STRONG>");
235             return a.toString();
236         }
237 
238     final static String FIRSTGFX_TABLETITLE = "Original print file as jpeg";
239     final static String SECONDGFX_TABLETITLE = "New print file as jpeg";
240     final static String DIFFER_TABLETITLE = "Difference file";
241     final static String STATUS_TABLETITLE = "Status";
242     final static String PIXELDIFF_TABLETITLE = "Pixel difference in %";
243 
244     final static String PIXELDIFF_BM_TABLETITLE = "P.diff. in % after remove border";
245     final static String DIFFER_BM_TABLETITLE = "Diff file (RB)";
246 
247     final static String OK_TABLETITLE = "OK?";
248     public void checkSection(String _sDocumentName)
249         {
250             try
251             {
252                 m_aOut.write( "<H2>Results for the document " + _sDocumentName + "</H2>" + ls);
253 
254                 m_aOut.write( "<p>Legend:<br>");
255                 m_aOut.write( stronghtml(FIRSTGFX_TABLETITLE) + " contains the output printed via 'ghostscript' as a jpeg picture.<br>");
256                 m_aOut.write( stronghtml(SECONDGFX_TABLETITLE) + " contains the same document opened within OpenOffice.org also printed via ghostscript as jpeg.<br>");
257                 m_aOut.write( stronghtml(DIFFER_TABLETITLE)+" is build via composite from original and new picture. The result should be a whole black picture, if there are no differences.<br>At the moment "+stronghtml(STATUS_TABLETITLE)+" is only ok, if the difference file contains only one color (black).</p>" );
258                 m_aOut.write( stronghtml(DIFFER_BM_TABLETITLE) + " is build via composite from original and new picture after the border of both pictures are removed, so differences based on center problems may solved here");
259                 m_aOut.write( "</p>");
260                 m_aOut.write( "<p>Some words about the percentage value<br>");
261                 m_aOut.write( "If a character is on the original page (a) and on the new page this character is moved to an other position only (b) , this means the difference is 100%.<br>");
262                 m_aOut.write( "If character (b) is also bigger than character (a) the percentage is grow over the 100% mark.<br>");
263                 m_aOut.write( "This tool count only the pixels which are differ to it's background color. It makes no sense to count all pixels, or the difference percentage will most the time in a very low percentage range.");
264                 m_aOut.write( "</p>");
265 
266                 m_aOut.write( "<table class=\"infotable\">" + ls);
267 
268                 m_aOut.write( "<TR>" + ls);
269                 m_aOut.write( tableHeaderCell( FIRSTGFX_TABLETITLE) );
270                 m_aOut.write( tableHeaderCell( SECONDGFX_TABLETITLE ) );
271                 m_aOut.write( tableHeaderCell(DIFFER_TABLETITLE ) );
272                 m_aOut.write( tableHeaderCell(PIXELDIFF_TABLETITLE ) );
273 
274                 m_aOut.write( tableHeaderCell(DIFFER_BM_TABLETITLE) );
275                 m_aOut.write( tableHeaderCell(PIXELDIFF_BM_TABLETITLE ) );
276 
277                 m_aOut.write( tableHeaderCell( OK_TABLETITLE) );
278 
279                 m_aOut.write( "</TR>" + ls);
280                 m_aOut.flush();
281             }
282             catch (java.io.IOException e)
283             {
284             }
285         }
286 
287     public void checkLine(StatusHelper _aStatus, boolean _bCurrentResult)
288         {
289             try
290             {
291                 m_aOut.write( "<TR>" + ls);
292                 String sLink = getHREF(FileHelper.getBasename(_aStatus.m_sOldGfx), FileHelper.getBasename(_aStatus.m_sOldGfx));
293                 m_aOut.write( tableDataCell(sLink) );
294 
295                 sLink = getHREF(FileHelper.getBasename(_aStatus.m_sNewGfx), FileHelper.getBasename(_aStatus.m_sNewGfx));
296                 m_aOut.write( tableDataCell(sLink) );
297 
298                 sLink = getHREF(FileHelper.getBasename(_aStatus.m_sDiffGfx), FileHelper.getBasename(_aStatus.m_sDiffGfx));
299                 m_aOut.write( tableDataCell(sLink) );
300 
301                 String sPercent = String.valueOf(_aStatus.nPercent) + "%";
302                 if (_aStatus.nPercent > 0 && _aStatus.nPercent < 5)
303                 {
304                     sPercent += " (less 5% is ok)";
305                 }
306                 m_aOut.write(tableDataCell( sPercent ) );
307 
308                 if (_aStatus.m_sDiff_BM_Gfx == null)
309                 {
310                     sLink = "No diffs, therefore no moves";
311                     m_aOut.write( tableDataCell(sLink) );
312                     m_aOut.write(tableDataCell( "" ) );
313                 }
314                 else
315                 {
316                     sLink = getHREF(FileHelper.getBasename(_aStatus.m_sDiff_BM_Gfx), FileHelper.getBasename(_aStatus.m_sDiff_BM_Gfx));
317                     m_aOut.write( tableDataCell(sLink) );
318 
319                     String sPercent2 = String.valueOf(_aStatus.nPercent2) + "%";
320                     if (_aStatus.nPercent2 > 0 && _aStatus.nPercent2 < 5)
321                     {
322                         sPercent2 += " (less 5% is ok)";
323                     }
324                     m_aOut.write(tableDataCell( sPercent2 ) );
325                 }
326 
327                 // is the check positiv, in a defined range
328                 if (_bCurrentResult)
329                 {
330                     m_aOut.write(tableDataCell( "YES" ) );
331                 }
332                 else
333                 {
334                     m_aOut.write(tableDataCell( "NO" ) );
335                 }
336 
337                 m_aOut.write( "</TR>" + ls);
338             }
339             catch (java.io.IOException e)
340             {
341             }
342         }
343 
344 // -----------------------------------------------------------------------------
345     public void checkDiffDiffSection(String _sDocumentName)
346         {
347             try
348             {
349                 m_aOut.write( "<H2>Results for the document " + _sDocumentName + "</H2>" + ls);
350 
351                 m_aOut.write( "<p>Legend:<br>");
352                 m_aOut.write( "</p>");
353 
354                 m_aOut.write( "<table class=\"infotable\">" + ls);
355 
356                 m_aOut.write( "<TR>" + ls);
357                 m_aOut.write( tableHeaderCell( "Source to actual difference" ) );
358                 m_aOut.write( tableHeaderCell( "Actual difference" ) );
359                 m_aOut.write( tableHeaderCell(DIFFER_TABLETITLE ) );
360                 m_aOut.write( tableHeaderCell(PIXELDIFF_TABLETITLE ) );
361 
362                 m_aOut.write( tableHeaderCell( OK_TABLETITLE) );
363 
364                 m_aOut.write( "</TR>" + ls);
365                 m_aOut.flush();
366             }
367             catch (java.io.IOException e)
368             {
369             }
370         }
371 
372     public void checkDiffDiffLine(StatusHelper _aStatus, boolean _bCurrentResult)
373         {
374             try
375             {
376                 m_aOut.write( "<TR>" + ls);
377                 // the link to the old difference can't offer here
378                 //  String sLink = getHREF(FileHelper.getBasename(_aStatus.m_sOldGfx), FileHelper.getBasename(_aStatus.m_sOldGfx));
379                 //  m_aOut.write( tableDataCell(sLink) );
380 
381                 String sBasename = FileHelper.getBasename(m_sFilename);
382                 String sNew = sBasename.substring(m_sNamePrefix.length());
383 
384                 String sLink;
385                 sLink = getHREF(sNew, sNew);
386                 m_aOut.write( tableDataCell(sLink) );
387 
388                 sLink = getHREF(FileHelper.getBasename(_aStatus.m_sNewGfx), FileHelper.getBasename(_aStatus.m_sNewGfx));
389                 m_aOut.write( tableDataCell(sLink) );
390 
391                 sLink = getHREF(FileHelper.getBasename(_aStatus.m_sDiffGfx), FileHelper.getBasename(_aStatus.m_sDiffGfx));
392                 m_aOut.write( tableDataCell(sLink) );
393 
394                 String sPercent = String.valueOf(_aStatus.nPercent) + "%";
395                 // if (_aStatus.nPercent > 0 && _aStatus.nPercent < 5)
396                 // {
397                 //     sPercent += " (less 5% is ok)";
398                 // }
399                 m_aOut.write(tableDataCell( sPercent ) );
400 
401                 // is the check positiv, in a defined range
402                 if (_bCurrentResult)
403                 {
404                     m_aOut.write(tableDataCell( "YES" ) );
405                 }
406                 else
407                 {
408                     m_aOut.write(tableDataCell( "NO" ) );
409                 }
410 
411                 m_aOut.write( "</TR>" + ls);
412             }
413             catch (java.io.IOException e)
414             {
415             }
416         }
417 
418 }
419