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 com.sun.star.lang.XMultiServiceFactory;
31 import lib.TestParameters;
32 import java.io.File;
33 
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38  * This class object is more a Helper or Controller.
39  * It stores information like:
40  * - How to create a document (with a OpenOffice.org method, or with MS Word, or with OpenOffice.org as pdf)
41  * - some more infos for OpenOffice.org method
42  *   - a service factory pointer
43  *   - if hidden mode should use
44  *   - target name
45  *
46  * - printer name
47  *
48  * - how to handle .xml files, which in Microsoft could be Excel or Word documents
49  *
50  * HOWTO USE:
51  * For OOo,
52  *   create an GraphicalTestArguments with a set of TestParameters
53  *    GraphicalTestArguments a = new GraphicalTestArguments(params);
54  *
55  *  If you wish to use pdf export instead of normal printer output, set also the reference type to 'pdf'
56  *    a.setReferenceType("pdf");
57  *
58  *
59  * For MS Office:
60  *   create an GraphicalTestArguments and set the reference type to 'msoffice'
61  *    GraphicalTestArguments a = new GraphicalTestArguments(params);
62  *    a.setReferenceType("msoffice");
63  *
64  * within windows it's better to set also a printer name so it's simply possible to use for normal work the default printer
65  * and for such tests with ConvWatch a extra printer.
66  *    a.setPrinterName("CrossOffice Printer");
67  *
68  */
69 
70 public class GraphicalTestArguments
71 {
72     /**
73     2DO:
74     Possible reference types are currently
75     // ooo
76     // pdf
77     // msoffice
78     */
79     String m_sReferenceType = "OOo";
80 
81     String m_sTargetFrameName = "_blank";
82 
83     String m_sPrinterName = null;
84 
85     // Hidden = true hiddes a used OpenOffice.org, all code is executed in the background
86     // This parameter is not used for RefType: msoffice
87     boolean m_bHidden = true;
88 
89     String m_sDefaultXMLFormatApplication = null;
90 
91     boolean m_bIncludeSubdirectories;
92 
93     TestParameters m_aCurrentParams;
94 
95     int m_nMaxPages = 0;                           // default is 0 (print all pages)
96     String m_sOnlyPage = "";                       // default is "", there is no page which we want to print only.
97 
98     int m_nResolutionInDPI = 0;
99 
100     boolean m_bStoreFile = true;
101     boolean m_bResuseOffice = false;
102 
103     boolean m_bDebugMode = false;
104 
105     String m_sLeaveOutNames = null;
106 
107     String m_sDistinct = null;
108 
109     boolean m_bCreateDefaultReference = false;
110 
111     // CONSTRUCTOR
112     private GraphicalTestArguments(){}
113 
114     public GraphicalTestArguments(TestParameters param)
115         {
116             m_aCurrentParams = param;
117             // collect interesting information from the ComplexTestCase
118             // ....
119 
120             // REFERENCE_TYPE ----------
121             String sReferenceType = (String)param.get( PropertyName.DOC_COMPARATOR_REFERENCE_TYPE );
122             if (sReferenceType == null || sReferenceType.length() == 0)
123             {
124             }
125             else
126             {
127                 // log.println("found REFERENCE_TYPE " + sReferenceType );
128                 setReferenceType(sReferenceType);
129             }
130 
131             // PRINTER_NAME ----------
132             String sPrinterName = (String)param.get( PropertyName.DOC_COMPARATOR_PRINTER_NAME );
133             if (sPrinterName == null || sPrinterName.length() == 0)
134             {
135             }
136             else
137             {
138                 // log.println("found PRINTER_NAME " + sPrinterName );
139                 setPrinterName(sPrinterName);
140             }
141             // DEFAULT_XML_FORMAT_APP ------
142             String sDefaultXMLFormatApp = (String)param.get( PropertyName.DOC_COMPARATOR_DEFAULT_XML_FORMAT_APP );
143             if (sDefaultXMLFormatApp == null || sDefaultXMLFormatApp.length() == 0)
144             {
145             }
146             else
147             {
148                 setDefaultXMLFormatApp(sDefaultXMLFormatApp);
149             }
150 
151             m_bIncludeSubdirectories = true;
152             String sRECURSIVE = (String)param.get( PropertyName.DOC_COMPARATOR_INCLUDE_SUBDIRS );
153 // TODO: I need to get the boolean value with get("name") because, if it is not given getBool() returns
154 //       with a default of 'false' which is not very helpful if the default should be 'true'
155 //       maybe a getBoolean("name", true) could be a better choise.
156             if (sRECURSIVE == null)
157             {
158                 sRECURSIVE = "true";
159             }
160             if (sRECURSIVE.toLowerCase().equals("no") ||
161                 sRECURSIVE.toLowerCase().equals("false"))
162             {
163                 m_bIncludeSubdirectories = false;
164             }
165 
166             // ----------------------------------------
167             m_nMaxPages = param.getInt( PropertyName.DOC_COMPARATOR_PRINT_MAX_PAGE );
168             m_sOnlyPage = (String)param.get(PropertyName.DOC_COMPARATOR_PRINT_ONLY_PAGE);
169 
170             m_nResolutionInDPI = param.getInt( PropertyName.DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION );
171             if (m_nResolutionInDPI == 0)
172             {
173                 // 212 DPI is 1754 x 2474 pixel for DIN A4
174                 m_nResolutionInDPI = 212;
175             }
176 
177             // ----------------------------------------
178             String sImportFilterName = (String)param.get(PropertyName.DOC_CONVERTER_IMPORT_FILTER_NAME);
179             if (sImportFilterName != null && sImportFilterName.length() > 0)
180             {
181                 // System.out.println("found " + PropertyName.DOC_CONVERTER_IMPORT_FILTER_NAME + " " + sImportFilterName );
182                 m_sImportFilterName = sImportFilterName;
183 
184                 if (sImportFilterName.toLowerCase().equals("help"))
185                 {
186                     showInternalFilterName(sImportFilterName, getMultiServiceFactory() );
187                     GlobalLogWriter.get().println("Must quit.");
188                 }
189             }
190             // ----------------------------------------
191             String sExportFilterName = (String)param.get(PropertyName.DOC_CONVERTER_EXPORT_FILTER_NAME);
192             if (sExportFilterName != null && sExportFilterName.length() > 0)
193             {
194                 // System.out.println("found " + PropertyName.DOC_CONVERTER_EXPORT_FILTER_NAME + " " + sExportFilterName );
195                 m_sExportFilterName = sExportFilterName;
196                 if (sExportFilterName.toLowerCase().equals("help"))
197                 {
198                     showInternalFilterName(sExportFilterName, getMultiServiceFactory() );
199                     GlobalLogWriter.get().println("Must quit.");
200                 }
201             }
202 
203             // ----------------------------------------
204             String sOfficeProgram = (String)param.get(PropertyName.DOC_CONVERTER_OFFICE_PROGRAM);
205             if (sOfficeProgram != null && sOfficeProgram.length() > 0)
206             {
207                 m_sOfficeProgram = sOfficeProgram;
208             }
209             // ----------------------------------------
210             String sREUSE_OFFICE = (String)param.get( PropertyName.DOC_CONVERTER_REUSE_OFFICE);
211             if (sREUSE_OFFICE == null)
212             {
213                 sREUSE_OFFICE = "false";
214             }
215             if (sREUSE_OFFICE.toLowerCase().equals("yes") ||
216                 sREUSE_OFFICE.toLowerCase().equals("true"))
217             {
218                 m_bResuseOffice = true;
219             }
220             else
221             {
222                 m_bResuseOffice = false;
223             }
224 
225 
226             String sHTMLOutputPrefix = (String)param.get( PropertyName.DOC_COMPARATOR_HTML_OUTPUT_PREFIX);
227             if (sHTMLOutputPrefix == null)
228             {
229                 m_sHTMLOutputPrefix = "";
230             }
231             else
232             {
233                 m_sHTMLOutputPrefix = sHTMLOutputPrefix;
234             }
235 
236             String sWithBorderMove = (String)param.get( PropertyName.DOC_COMPARATOR_GFXCMP_WITH_BORDERMOVE);
237             if (sWithBorderMove == null)
238             {
239                 sWithBorderMove = "";
240                 // m_tWithBorderMove = TriState.UNSET;
241                 m_tWithBorderMove = TriState.FALSE;
242             }
243             if (sWithBorderMove.toLowerCase().equals("yes") ||
244                 sWithBorderMove.toLowerCase().equals("true"))
245             {
246                 m_tWithBorderMove = TriState.TRUE;
247             }
248             else if (sWithBorderMove.toLowerCase().equals("no") ||
249                      sWithBorderMove.toLowerCase().equals("false"))
250             {
251                 m_tWithBorderMove = TriState.FALSE;
252             }
253             else
254             {
255                 m_tWithBorderMove = TriState.FALSE;
256                 // m_tWithBorderMove = TriState.UNSET;
257             }
258 
259             String sLeaveOutNames = (String)param.get(PropertyName.DOC_COMPARATOR_LEAVE_OUT_FILES);
260             if (sLeaveOutNames != null)
261             {
262                 m_sLeaveOutNames = sLeaveOutNames;
263             }
264 
265             String sDBInfoString = (String)param.get(PropertyName.DOC_COMPARATOR_DB_INFO_STRING);
266             if (sDBInfoString != null)
267             {
268                 m_sDBInfoString = sDBInfoString;
269             }
270 
271             // DISTINCT ----------
272             String sDistinct = (String)param.get( "DISTINCT" );
273             if (sDistinct == null || sDistinct.length() == 0)
274             {
275                 sDistinct = "";
276             }
277             else
278             {
279                 m_sDistinct = sDistinct;
280             }
281             // HIDDEN
282             String sOfficeViewable = (String)param.get(PropertyName.OFFICE_VIEWABLE);
283             if (sOfficeViewable != null)
284             {
285                 if (sOfficeViewable.toLowerCase().equals("yes") ||
286                     sOfficeViewable.toLowerCase().equals("true"))
287                 {
288                     setViewable();
289                 }
290                 else
291                 {
292                     setHidden();
293                 }
294             }
295             // CREATE_DEFAULT
296             String sCreateDefault = (String)param.get(PropertyName.CREATE_DEFAULT);
297             if (sCreateDefault != null)
298             {
299                 if (sCreateDefault.toLowerCase().equals("yes") ||
300                     sCreateDefault.toLowerCase().equals("true"))
301                 {
302                     m_bCreateDefaultReference = true;
303                 }
304                 else
305                 {
306                     m_bCreateDefaultReference = false;
307                 }
308             }
309 
310         }
311 
312     public boolean checkIfUsableDocumentType(String _sName)
313         {
314             // @todo
315             // check if the name is in the leave out list and then return 'false'
316             if (_sName.toLowerCase().endsWith(".jpg") ||
317                 _sName.toLowerCase().endsWith(".png") ||
318                 _sName.toLowerCase().endsWith(".gif") ||
319                 _sName.toLowerCase().endsWith(".bmp") ||
320                 _sName.toLowerCase().endsWith(".prn") ||
321                 _sName.toLowerCase().endsWith(".ps"))
322             {
323                 return false;
324             }
325 
326             return true;
327         }
328 
329     static void showInternalFilterName(String _sFilterName, XMultiServiceFactory _xMSF)
330         {
331             if (_sFilterName.length() == 0)
332             {
333                 // System.out.println("No FilterName set.");
334                 return;
335             }
336 
337             if (_xMSF == null)
338             {
339                 GlobalLogWriter.get().println("MultiServiceFactory not set.");
340                 return;
341             }
342             // XFilterFactory aFilterFactory = null;
343             Object aObj = null;
344             try
345             {
346                 aObj = _xMSF.createInstance("com.sun.star.document.FilterFactory");
347             }
348             catch(com.sun.star.uno.Exception e)
349             {
350                 GlobalLogWriter.get().println("Can't get com.sun.star.document.FilterFactory.");
351                 return;
352             }
353             if (aObj != null)
354             {
355                 XNameAccess aNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, aObj);
356                 if (aNameAccess != null)
357                 {
358 
359                     if (_sFilterName.toLowerCase().equals("help"))
360                     {
361                         GlobalLogWriter.get().println("Show all possible ElementNames from current version." );
362                         String[] aElementNames = aNameAccess.getElementNames();
363                         for (int i = 0; i<aElementNames.length; i++)
364                         {
365                             GlobalLogWriter.get().println(aElementNames[i]);
366                         }
367                     }
368                 }
369             }
370         }
371 
372     /*
373     public GraphicalTestArguments(TestParameters param, Log xxx)
374     {
375         // collect interesting information from the ComplexTestCase
376         // ....
377     }
378     */
379 
380     // set methods
381     public void setReferenceType(String _sType)
382         {
383             // special casse, null is not allowed, set to default.
384             if (_sType == null)
385             {
386                 m_sReferenceType = "OOo";
387             }
388             else
389             {
390                 m_sReferenceType = _sType;
391             }
392         }
393     public void setTargetFrameName(String _sTargetFrameName) {m_sTargetFrameName = _sTargetFrameName;}
394     public void setPrinterName(String _sName) {m_sPrinterName = _sName;}
395     public void setHidden() { m_bHidden = true;}
396     public void setViewable() {m_bHidden = false;}
397     public void setDefaultXMLFormatApp(String _sNameOfApp) {m_sDefaultXMLFormatApplication = _sNameOfApp;}
398 
399     // get methods
400     public XMultiServiceFactory getMultiServiceFactory()
401         {
402             XMultiServiceFactory xMSF = (XMultiServiceFactory)m_aCurrentParams.getMSF();
403 
404             // check if MultiServiceFactory is given
405             if (getReferenceType().toLowerCase().equals("pdf") ||
406                 getReferenceType().toLowerCase().equals("ooo"))
407             {
408                 if (xMSF == null)
409                 {
410                     GlobalLogWriter.get().println("ERROR! MultiServiceFactory not given.");
411                 }
412             }
413             return xMSF;
414         }
415 
416     public String getReferenceType() {return m_sReferenceType;}
417     public String getTargetFrameName() {return m_sTargetFrameName;}
418     public String getPrinterName() {return m_sPrinterName;}
419     public boolean isHidden() {return m_bHidden;}
420     public String getDefaultXMLFormatApp() {return m_sDefaultXMLFormatApplication;}
421 
422 
423     /**
424      * @return true, if subdirectories should run through
425      */
426     public boolean includeSubDirectories() {return m_bIncludeSubdirectories;}
427 
428     /**
429      * @return the number of pages to be print
430      */
431     public int getMaxPages() {return m_nMaxPages;}
432 
433     /**
434      * @return as string, which pages should be print, e.g. '1-4;6' here, page 1 to 4 and page 6.
435      */
436     public String getOnlyPages()
437     {
438         if (m_sOnlyPage == null)
439         {
440             return "";
441         }
442         return m_sOnlyPage;
443     }
444 
445     /**
446      * @return true, if there should not print all pages at all, use getMaxPages() and or getOnlyPages() to get which pages to print
447      */
448     public boolean printAllPages()
449         {
450             if ( (getMaxPages() > 0) ||
451                  (getOnlyPages().length() != 0))
452             {
453                 return false;
454             }
455             return true;
456         }
457 
458     /**
459      * @return integer value, which contain resolution in DPI.
460      */
461     public int getResolutionInDPI() {return m_nResolutionInDPI;}
462 
463     public static void checkIfMSWindowsConformPath(String _sPath)
464         {
465             if (_sPath != null  && _sPath.length() > 1)
466             {
467                 if (_sPath.charAt(1) == ':')
468                 {
469                     if (_sPath.charAt(2) != '\\')
470                     {
471                         GlobalLogWriter.get().println("This is not a Microsoft Windows conform path: '" + _sPath + "' please fix.");
472                         System.exit(1);
473                     }
474                 }
475             }
476         }
477 
478 
479     /**
480      * @return the INPUT_PATH out of the TestParameters
481      */
482     public String getInputPath()
483         {
484             String sInputPath;
485             sInputPath = (String)m_aCurrentParams.get(PropertyName.DOC_COMPARATOR_INPUT_PATH);
486             checkIfMSWindowsConformPath(sInputPath);
487             return sInputPath;
488         }
489     /**
490      * @return the OUTPUT_PATH out of the TestParameters
491      */
492     public String getOutputPath()
493         {
494             String sOutputPath;
495             sOutputPath = (String)m_aCurrentParams.get(PropertyName.DOC_COMPARATOR_OUTPUT_PATH);
496             checkIfMSWindowsConformPath(sOutputPath);
497             return sOutputPath;
498         }
499     /**
500      * @return the REFERENCE_PATH out of the TestParameters
501      */
502     public String getReferencePath()
503         {
504             String sReferencePath;
505             sReferencePath = (String)m_aCurrentParams.get(PropertyName.DOC_COMPARATOR_REFERENCE_PATH);
506             checkIfMSWindowsConformPath(sReferencePath);
507             return sReferencePath;
508         }
509     /**
510      * @return the DIFF_PATH out of the TestParameters
511      */
512     public String getDiffPath()
513         {
514             String sDiffPath;
515             sDiffPath = (String)m_aCurrentParams.get(PropertyName.DOC_COMPARATOR_DIFF_PATH);
516             checkIfMSWindowsConformPath(sDiffPath);
517             return sDiffPath;
518         }
519 
520     public boolean getOverwrite()
521         {
522             boolean bOverwrite = m_aCurrentParams.getBool( PropertyName.DOC_COMPARATOR_OVERWRITE_REFERENCE);
523             return bOverwrite;
524         }
525     public String getReferenceInputPath()
526         {
527             String sReferenceInputPath;
528             sReferenceInputPath = (String)m_aCurrentParams.get(PropertyName.DOC_COMPARATOR_REFERENCE_INPUT_PATH);
529             return sReferenceInputPath;
530         }
531 
532     /**
533      * Helper function to get the buildid of the current used OpenOffice.org
534      * out of the AppExecutionCommand the build ID
535      */
536     public String getBuildID()
537         {
538             String sAPP = (String)m_aCurrentParams.get(util.PropertyName.APP_EXECUTION_COMMAND);
539             // return getBuildID(sAPP);
540 //  TODO: here we need the getBuildID(string) method
541             String sBuildID = convwatch.BuildID.getBuildID(sAPP);
542             return sBuildID;
543         }
544 
545     public boolean shouldOfficeStart()
546         {
547             String sNoOffice = (String)m_aCurrentParams.get( "NoOffice" );
548             if (sNoOffice != null)
549             {
550                 if (sNoOffice.toLowerCase().startsWith("t") || sNoOffice.toLowerCase().startsWith("y"))
551                 {
552                     return false;
553                 }
554             }
555             return true;
556         }
557 
558         // Handle for Reference Build ID, is set in ConvWatch.createPostscriptStartCheck()
559     private String m_sRefBuildID;
560 
561     public void setRefBuildID(String _sRef)
562         {
563             m_sRefBuildID = _sRef;
564         }
565     public String getRefBuildID()
566         {
567             return m_sRefBuildID;
568         }
569 
570     public void disallowStore()
571         {
572             m_bStoreFile = false;
573         }
574     public void allowStore()
575         {
576             m_bStoreFile = true;
577         }
578     public boolean isStoreAllowed()
579         {
580             return m_bStoreFile;
581         }
582     public boolean createDefaultReference()
583         {
584             return m_bCreateDefaultReference;
585         }
586 
587 
588     // get/set for FilterName
589     // get the right Filtername (internal Name) from
590     // http://framework.openoffice.org/files/documents/25/897/filter_description.html
591 
592     String m_sImportFilterName = "";
593     String m_sExportFilterName = "";
594     public void setImportFilterName(String _sImportFilterName)
595         {
596             m_sImportFilterName = _sImportFilterName;
597         }
598     public String getImportFilterName()
599         {
600             return m_sImportFilterName;
601         }
602     public void setExportFilterName(String _sExportFilterName)
603         {
604             m_sExportFilterName = _sExportFilterName;
605         }
606     public String getExportFilterName()
607         {
608             return m_sExportFilterName;
609         }
610 
611     String m_sOfficeProgram = "";
612     public void setOfficeProgram(String _sName)
613         {
614             m_sOfficeProgram = _sName;
615         }
616     public String getOfficeProgram()
617         {
618             return m_sOfficeProgram;
619         }
620 
621     public boolean restartOffice()
622         {
623             if (m_bResuseOffice == false)
624             {
625                 return true;
626             }
627             return false;
628         }
629 
630     String m_sHTMLOutputPrefix = "";
631     public String getHTMLOutputPrefix()
632         {
633             return m_sHTMLOutputPrefix;
634         }
635 
636     TriState m_tWithBorderMove = TriState.UNSET;
637     // public TriState isBorderMove()
638     //     {
639     //         return m_tWithBorderMove;
640     //     }
641     public TriState getBorderMove()
642         {
643             return m_tWithBorderMove;
644         }
645     public void setBorderMove(TriState _tBorderMove)
646         {
647             m_tWithBorderMove = _tBorderMove;
648         }
649 
650     String m_sDocumentType = "";
651     public void setDocumentType(String _sName)
652         {
653             m_sDocumentType = _sName;
654         }
655     public String getDocumentType()
656         {
657             return m_sDocumentType;
658         }
659 
660     /*
661       helper class for performance analyser features
662      */
663     PerformanceContainer m_aPerformanceContainer = null;
664     public PerformanceContainer getPerformance()
665         {
666             if (m_aPerformanceContainer == null)
667             {
668                 m_aPerformanceContainer = new PerformanceContainer();
669             }
670             return m_aPerformanceContainer;
671         }
672 
673     private String m_aInputFile;
674     public void setInputFile(String _sInputFile)
675         {
676             m_aInputFile = _sInputFile;
677         }
678     public String getInputFile()
679         {
680             return m_aInputFile;
681         }
682 
683     private String m_sDBInfoString;
684     public String getDBInfoString()
685         {
686             if (m_sDBInfoString != null)
687             {
688                 if (m_sDBInfoString.length() == 0)
689                 {
690                     return null;
691                 }
692             }
693 
694             return m_sDBInfoString;
695         }
696 
697     public boolean cancelRequest()
698         {
699             File aCancelFile = null;
700             String fs;
701             fs = System.getProperty("file.separator");
702             String sTempPath = (String)m_aCurrentParams.get( PropertyName.TEMPPATH );
703             if (sTempPath != null)
704             {
705                 String sGDC_Dir = sTempPath;
706 
707                 if (m_sDistinct.length() > 0)
708                 {
709                     sGDC_Dir = sGDC_Dir + fs + m_sDistinct;
710                 }
711 
712                 String sCancelFile = sGDC_Dir + fs + "cancel_compare.txt";
713                 aCancelFile = new File(sCancelFile);
714 
715                 if (aCancelFile.exists())
716                 {
717                     GlobalLogWriter.get().println("ATTENTION: Found file: '" + sCancelFile + "'.");
718                     GlobalLogWriter.get().println("User has canceled the program flow.");
719                     return true;
720                 }
721             }
722             return false;
723         }
724 
725 }
726 
727 
728 /*
729 public class MSGraphicalTestArguments extends GraphicalTestArguments
730 {
731     MSGraphicalTestArguments()
732         {
733             setReferenceType("msoffice");
734         }
735 }
736 
737 public class OOoGraphicalTestArguments extends GraphicalTestArguments
738 {
739     OOoGraphicalTestArguments(XMultiServiceFactory _aFactory)
740         {
741             setMultiServiceFactory(_aFactory);
742         }
743 }
744 */
745