1*cdf0e10cSrcweir /*
2*cdf0e10cSrcweir  * ************************************************************************
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
11*cdf0e10cSrcweir  *
12*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
13*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
14*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
15*cdf0e10cSrcweir  *
16*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
17*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
20*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
21*cdf0e10cSrcweir  *
22*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
23*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
24*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
25*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
26*cdf0e10cSrcweir  *
27*cdf0e10cSrcweir  * ***********************************************************************
28*cdf0e10cSrcweir  */
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir package graphical;
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir import helper.OSHelper;
33*cdf0e10cSrcweir import helper.ProcessHandler;
34*cdf0e10cSrcweir import helper.StringHelper;
35*cdf0e10cSrcweir import java.io.File;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir /**
38*cdf0e10cSrcweir  *
39*cdf0e10cSrcweir  * @author ll93751
40*cdf0e10cSrcweir  */
41*cdf0e10cSrcweir public class JPEGCreator extends EnhancedComplexTestCase
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir     // @Override
44*cdf0e10cSrcweir     public String[] getTestMethodNames()
45*cdf0e10cSrcweir     {
46*cdf0e10cSrcweir         return new String[]{"PostscriptOrPDFToJPEG"};
47*cdf0e10cSrcweir     }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     /**
50*cdf0e10cSrcweir      * test function.
51*cdf0e10cSrcweir      */
52*cdf0e10cSrcweir     public void PostscriptOrPDFToJPEG()
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         GlobalLogWriter.set(log);
55*cdf0e10cSrcweir         ParameterHelper aParam = new ParameterHelper(param);
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir         // run through all documents found in Inputpath
58*cdf0e10cSrcweir         foreachPSorPDFinInputPath(aParam);
59*cdf0e10cSrcweir     }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     public void checkOneFile(String _sDocumentName, String _sResult, ParameterHelper _aParams) throws OfficeException
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir          GlobalLogWriter.println("  Document: " + _sDocumentName);
65*cdf0e10cSrcweir          GlobalLogWriter.println("   results: " + _sResult);
66*cdf0e10cSrcweir //         IOffice aOffice = new Office(_aParams, _sResult);
67*cdf0e10cSrcweir //         aOffice.start();
68*cdf0e10cSrcweir //         aOffice.load(_sDocumentName);
69*cdf0e10cSrcweir //         aOffice.storeAsPostscript();
70*cdf0e10cSrcweir //         aOffice.close();
71*cdf0e10cSrcweir          String sJPEGNameSchema = createJPEG(_sDocumentName, "", _aParams);
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir          // store information only if jpeg files exists
74*cdf0e10cSrcweir          int nPages = countPages(sJPEGNameSchema);
75*cdf0e10cSrcweir          if (nPages > 0)
76*cdf0e10cSrcweir          {
77*cdf0e10cSrcweir              createSmallPictures(sJPEGNameSchema);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir              // read out tolerance file
80*cdf0e10cSrcweir              String sFileDir = FileHelper.getPath(_sDocumentName);
81*cdf0e10cSrcweir              String sBasename = FileHelper.getBasename(_sDocumentName);
82*cdf0e10cSrcweir              int nTolerance = 0;
83*cdf0e10cSrcweir              String sToleranceFile = FileHelper.appendPath(sFileDir, "tolerance.ini");
84*cdf0e10cSrcweir              File aToleranceFile = new File(sToleranceFile);
85*cdf0e10cSrcweir              if (aToleranceFile.exists())
86*cdf0e10cSrcweir              {
87*cdf0e10cSrcweir                  IniFile aIniFile = new IniFile(sToleranceFile);
88*cdf0e10cSrcweir                  nTolerance = aIniFile.getIntValue(sBasename, "accept", 0); // default for all pages
89*cdf0e10cSrcweir                  aIniFile.close();
90*cdf0e10cSrcweir              }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir              String sIndexFile = FileHelper.appendPath(_sResult, "index.ini");
93*cdf0e10cSrcweir              File aIndexFile = new File(sIndexFile);
94*cdf0e10cSrcweir              if (aIndexFile.exists())
95*cdf0e10cSrcweir              {
96*cdf0e10cSrcweir                  // store only if an index file exists
97*cdf0e10cSrcweir                  IniFile aIniFile = new IniFile(sIndexFile);
98*cdf0e10cSrcweir                  aIniFile.insertValue(sBasename, "jpegschema", sJPEGNameSchema);
99*cdf0e10cSrcweir                  aIniFile.insertValue(sBasename, "pages", nPages);
100*cdf0e10cSrcweir                  aIniFile.insertValue(sBasename, "tolerance", nTolerance);
101*cdf0e10cSrcweir                  aIniFile.close();
102*cdf0e10cSrcweir              }
103*cdf0e10cSrcweir          }
104*cdf0e10cSrcweir          else
105*cdf0e10cSrcweir          {
106*cdf0e10cSrcweir              assure("There are no pages in document:'" + _sDocumentName + "', maybe document currupt?", false, true);
107*cdf0e10cSrcweir          }
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir /**
111*cdf0e10cSrcweir  * Create a lot of smaller and nicer Pictures of the big fat pages.
112*cdf0e10cSrcweir  * Looks better
113*cdf0e10cSrcweir  * @param _sJPEGSchema
114*cdf0e10cSrcweir  */
115*cdf0e10cSrcweir     public void createSmallPictures(String _sJPEGSchema)
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         ParameterHelper aParam = new ParameterHelper(param);
118*cdf0e10cSrcweir         if (! aParam.createSmallPictures())
119*cdf0e10cSrcweir         {
120*cdf0e10cSrcweir             return;
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         int nPages = 0;
124*cdf0e10cSrcweir         if (_sJPEGSchema.length() > 0)
125*cdf0e10cSrcweir         {
126*cdf0e10cSrcweir             // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
127*cdf0e10cSrcweir             for (int i=1;i<10000;i++)
128*cdf0e10cSrcweir             {
129*cdf0e10cSrcweir                 String sJPEGFilename = getFilenameForJPEGSchema(_sJPEGSchema, i);
130*cdf0e10cSrcweir                 if (FileHelper.exists(sJPEGFilename))
131*cdf0e10cSrcweir                 {
132*cdf0e10cSrcweir                     convertToNearSameFileWithWidth340(sJPEGFilename);
133*cdf0e10cSrcweir                     // m_aFileList.add(sNewJPEGFilename); // as long as the files exist, fill the array
134*cdf0e10cSrcweir                     nPages ++;
135*cdf0e10cSrcweir                 }
136*cdf0e10cSrcweir                 else
137*cdf0e10cSrcweir                 {
138*cdf0e10cSrcweir                     break;                             // stop file check
139*cdf0e10cSrcweir                 }
140*cdf0e10cSrcweir             }
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         // return nPages;
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir /**
146*cdf0e10cSrcweir  * convert a picture to a new picture with 340 pixel width.
147*cdf0e10cSrcweir  * @param _sJPEGFilename
148*cdf0e10cSrcweir  */
149*cdf0e10cSrcweir public static void convertToNearSameFileWithWidth340(String _sJPEGFilename)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     ParameterHelper aParam = new ParameterHelper(param);
152*cdf0e10cSrcweir     if (! aParam.createSmallPictures())
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         return;
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir     String sJPEGFilename = _sJPEGFilename.replaceAll("\\\\", "/");
157*cdf0e10cSrcweir //    if (OSHelper.isWindows())
158*cdf0e10cSrcweir //    {
159*cdf0e10cSrcweir //        sJPEGFilename = sJPEGFilename.replaceAll("/", "\\\\");
160*cdf0e10cSrcweir //    }
161*cdf0e10cSrcweir     String sNewJPEGFilename;
162*cdf0e10cSrcweir     sNewJPEGFilename = util.utils.replaceAll13(sJPEGFilename, ".jpg", "_w340.jpg");
163*cdf0e10cSrcweir     convertToWidth340(sJPEGFilename, sNewJPEGFilename);
164*cdf0e10cSrcweir }
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     /**
167*cdf0e10cSrcweir  * convert chart2_Regression.ods.ps_180DPI_0001.jpg -filter Catrom -resize
168*cdf0e10cSrcweir 340x chart2_Regression.ods.ps_180DPI_0001_w340.jpg
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir Point wie bisher
171*cdf0e10cSrcweir Cubic schlecht, weil unscharf
172*cdf0e10cSrcweir ...
173*cdf0e10cSrcweir Triangle ganz brauchbar (default?)
174*cdf0e10cSrcweir Catrom am besten
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir  * @param _sFrom
177*cdf0e10cSrcweir  * @param _To
178*cdf0e10cSrcweir  */
179*cdf0e10cSrcweir private static void convertToWidth340(String _sFrom, String _To)
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir             // int nResult = 0;
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             String sConvertEXE = "convert";
184*cdf0e10cSrcweir             if (OSHelper.isLinuxIntel())
185*cdf0e10cSrcweir             {
186*cdf0e10cSrcweir                 sConvertEXE = "convert";
187*cdf0e10cSrcweir             }
188*cdf0e10cSrcweir             if (OSHelper.isWindows())
189*cdf0e10cSrcweir             {
190*cdf0e10cSrcweir                 // TODO!
191*cdf0e10cSrcweir                 // HACK Hard coded!
192*cdf0e10cSrcweir                 // sConvertEXE = "C:\\Programme\\ImageMagick-6.0.3-q8\\convert.exe";
193*cdf0e10cSrcweir                 sConvertEXE = "convert.exe";
194*cdf0e10cSrcweir                 String sConvertPath = (String)param.get("imagemagick.path");
195*cdf0e10cSrcweir                 if (sConvertPath != null)
196*cdf0e10cSrcweir                 {
197*cdf0e10cSrcweir                     sConvertEXE = FileHelper.appendPath(sConvertPath, sConvertEXE);
198*cdf0e10cSrcweir                 }
199*cdf0e10cSrcweir             }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir             String[] sCommandArray =
202*cdf0e10cSrcweir                 {
203*cdf0e10cSrcweir                     sConvertEXE,
204*cdf0e10cSrcweir                     _sFrom,
205*cdf0e10cSrcweir                      "-filter", "Catrom",
206*cdf0e10cSrcweir                     "-resize", "340x",
207*cdf0e10cSrcweir                     _To
208*cdf0e10cSrcweir                 };
209*cdf0e10cSrcweir             ProcessHandler aHandler = new ProcessHandler(sCommandArray);
210*cdf0e10cSrcweir             boolean bBackValue = aHandler.executeSynchronously();
211*cdf0e10cSrcweir             int nExitCode = aHandler.getExitCode();
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir             String sBack = aHandler.getOutputText();
214*cdf0e10cSrcweir             if (sBack.length() > 0)
215*cdf0e10cSrcweir             {
216*cdf0e10cSrcweir                 GlobalLogWriter.println("'" + sBack + "'");
217*cdf0e10cSrcweir             }
218*cdf0e10cSrcweir             // try to interpret the result, which we get as a String
219*cdf0e10cSrcweir //            try
220*cdf0e10cSrcweir //            {
221*cdf0e10cSrcweir //                int nIdx = sBack.indexOf("\n");
222*cdf0e10cSrcweir //                if (nIdx > 0)
223*cdf0e10cSrcweir //                {
224*cdf0e10cSrcweir //                    sBack = sBack.substring(0, nIdx);
225*cdf0e10cSrcweir //                }
226*cdf0e10cSrcweir //
227*cdf0e10cSrcweir //                nResult = Integer.valueOf(sBack).intValue();
228*cdf0e10cSrcweir //            }
229*cdf0e10cSrcweir //            catch(java.lang.NumberFormatException e)
230*cdf0e10cSrcweir //            {
231*cdf0e10cSrcweir //                GlobalLogWriter.get().println("Number format exception");
232*cdf0e10cSrcweir //                nResult = 0;
233*cdf0e10cSrcweir //            }
234*cdf0e10cSrcweir             // return nResult;
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir /**
238*cdf0e10cSrcweir  * create out of a given Postscript/PDF _sFile a list of JPEGs, one for every page
239*cdf0e10cSrcweir  * @param _sFile
240*cdf0e10cSrcweir  * @param _sAdditional
241*cdf0e10cSrcweir  * @param _aParam
242*cdf0e10cSrcweir  * @return the schema of the first jpeg name
243*cdf0e10cSrcweir  */
244*cdf0e10cSrcweir     String createJPEG(String _sFile, String _sAdditional, ParameterHelper _aParam)
245*cdf0e10cSrcweir         {
246*cdf0e10cSrcweir             if (_sFile.startsWith("file:///"))
247*cdf0e10cSrcweir             {
248*cdf0e10cSrcweir                 _sFile = FileHelper.getSystemPathFromFileURL(_sFile);
249*cdf0e10cSrcweir             }
250*cdf0e10cSrcweir             File aFile = new File(_sFile);
251*cdf0e10cSrcweir             if (aFile.exists())
252*cdf0e10cSrcweir             {
253*cdf0e10cSrcweir                 String sAbsFile = aFile.getAbsolutePath();
254*cdf0e10cSrcweir                 if (!sAbsFile.equals(_sFile))
255*cdf0e10cSrcweir                 {
256*cdf0e10cSrcweir                     _sFile = sAbsFile;
257*cdf0e10cSrcweir                 }
258*cdf0e10cSrcweir             }
259*cdf0e10cSrcweir             else
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 GlobalLogWriter.println("File: '" + _sFile + "' doesn't exist.");
262*cdf0e10cSrcweir                 return "";
263*cdf0e10cSrcweir             }
264*cdf0e10cSrcweir             String sFileDir = FileHelper.getPath(_sFile);
265*cdf0e10cSrcweir             String sBasename = FileHelper.getBasename(_sFile);
266*cdf0e10cSrcweir //            String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir             String sTmpDir = util.utils.getUsersTempDir();
269*cdf0e10cSrcweir             if (_aParam.getOutputPath() != null)
270*cdf0e10cSrcweir             {
271*cdf0e10cSrcweir                 sTmpDir = _aParam.getOutputPath();
272*cdf0e10cSrcweir             }
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir             String sJPEGNameSchema = "";
275*cdf0e10cSrcweir             if (_sFile.toLowerCase().endsWith("ps") ||
276*cdf0e10cSrcweir                 _sFile.toLowerCase().endsWith("prn") ||
277*cdf0e10cSrcweir                 _sFile.toLowerCase().endsWith("pdf"))
278*cdf0e10cSrcweir             {
279*cdf0e10cSrcweir                 // seems to be a Postscript of PDF file
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir                 sJPEGNameSchema = createJPEGsFromPostscript(sTmpDir, sFileDir, sBasename, _aParam.getResolutionInDPI());
282*cdf0e10cSrcweir             }
283*cdf0e10cSrcweir             else if (_sFile.toLowerCase().endsWith("jpg") ||
284*cdf0e10cSrcweir                      _sFile.toLowerCase().endsWith("jpeg"))
285*cdf0e10cSrcweir             {
286*cdf0e10cSrcweir                 // do nothing, it's already a picture.
287*cdf0e10cSrcweir                 return _sFile;
288*cdf0e10cSrcweir             }
289*cdf0e10cSrcweir             else
290*cdf0e10cSrcweir             {
291*cdf0e10cSrcweir                 // we assume it's an office document.
292*cdf0e10cSrcweir                 // Office files will not handled here, we need a PS or PDF file
293*cdf0e10cSrcweir             }
294*cdf0e10cSrcweir             return sJPEGNameSchema;
295*cdf0e10cSrcweir         }
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir         private String getJPEGName(String _sOutputPath, String _sBasename, int _nResolutionInDPI, String _sGS_PageOutput)
298*cdf0e10cSrcweir         {
299*cdf0e10cSrcweir             // String fs = System.getProperty("file.separator");
300*cdf0e10cSrcweir             String sName = _sBasename + "_" + String.valueOf(_nResolutionInDPI) + "DPI_" + _sGS_PageOutput + ".jpg";
301*cdf0e10cSrcweir             String sJPEGName = FileHelper.appendPath(_sOutputPath, sName);
302*cdf0e10cSrcweir             return sJPEGName;
303*cdf0e10cSrcweir         }
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir         /**
306*cdf0e10cSrcweir      * Create via ghostscript (gs) from the reference file for every page a JPEG file
307*cdf0e10cSrcweir      *
308*cdf0e10cSrcweir      * MUST set:
309*cdf0e10cSrcweir      * m_sOutputPath, m_sReferenceFile, m_sReferencePath
310*cdf0e10cSrcweir      *
311*cdf0e10cSrcweir      * return exit code from gs command
312*cdf0e10cSrcweir      */
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir      private static final String m_sGS_PageOutput = "%04d";
315*cdf0e10cSrcweir 
316*cdf0e10cSrcweir      private String createJPEGsFromPostscript(String _sOutputPath, String _sSourcePath, String _sSourceFile, int _nResolutionInDPI)
317*cdf0e10cSrcweir         {
318*cdf0e10cSrcweir             FileHelper.makeDirectories("", _sOutputPath);
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir             // create a jpeg from original prn
321*cdf0e10cSrcweir             // String fs = System.getProperty("file.separator");
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir             String sJPEGNameSchema = getJPEGName(_sOutputPath, _sSourceFile, _nResolutionInDPI, m_sGS_PageOutput);
324*cdf0e10cSrcweir             String sPostscriptOrPDFFile = FileHelper.appendPath(_sSourcePath, _sSourceFile);
325*cdf0e10cSrcweir             String sGhostscriptEXE = "gs";
326*cdf0e10cSrcweir             if (OSHelper.isWindows())
327*cdf0e10cSrcweir             {
328*cdf0e10cSrcweir                 sGhostscriptEXE = "gswin32c.exe";
329*cdf0e10cSrcweir                 String sGhostscriptEXE2 = (String)param.get("gs.exe");
330*cdf0e10cSrcweir                 if (sGhostscriptEXE2 != null)
331*cdf0e10cSrcweir                 {
332*cdf0e10cSrcweir                     sGhostscriptEXE = sGhostscriptEXE2;
333*cdf0e10cSrcweir                 }
334*cdf0e10cSrcweir                 String sGhostscriptPath = (String)param.get("gs.path");
335*cdf0e10cSrcweir                 if (sGhostscriptPath != null)
336*cdf0e10cSrcweir                 {
337*cdf0e10cSrcweir                     sGhostscriptEXE = FileHelper.appendPath(sGhostscriptPath, sGhostscriptEXE);
338*cdf0e10cSrcweir                 }
339*cdf0e10cSrcweir             }
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir //            String sCommand = sGhostscriptEXE + " -dNOPROMPT -dBATCH -sDEVICE=jpeg -r" + String.valueOf(_nResolutionInDPI) + " -dNOPAUSE -sOutputFile=" + StringHelper.doubleQuoteIfNeed(sJPGFilename) + " " + StringHelper.doubleQuoteIfNeed(sOriginalFile);
342*cdf0e10cSrcweir             String[] sCommandArray =
343*cdf0e10cSrcweir                 {
344*cdf0e10cSrcweir                     sGhostscriptEXE,
345*cdf0e10cSrcweir                     "-dNOPROMPT",
346*cdf0e10cSrcweir                     "-dBATCH",
347*cdf0e10cSrcweir                     "-sDEVICE=jpeg",
348*cdf0e10cSrcweir                     "-r" + String.valueOf(_nResolutionInDPI),
349*cdf0e10cSrcweir                     "-dNOPAUSE",
350*cdf0e10cSrcweir                     "-sOutputFile=" + sJPEGNameSchema,
351*cdf0e10cSrcweir                     sPostscriptOrPDFFile
352*cdf0e10cSrcweir 
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir 
355*cdf0e10cSrcweir                 };
356*cdf0e10cSrcweir             // System.out.println("Start Command array");
357*cdf0e10cSrcweir             // try
358*cdf0e10cSrcweir             // {
359*cdf0e10cSrcweir             //     Runtime.getRuntime().exec(sCommandArray);
360*cdf0e10cSrcweir             // } catch (Exception e) {
361*cdf0e10cSrcweir             //     System.out.println("FAILED");
362*cdf0e10cSrcweir             // }
363*cdf0e10cSrcweir             // System.out.println("done");
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir // TODO: gives ghostscript an error we can handle?
366*cdf0e10cSrcweir             ProcessHandler aHandler = new ProcessHandler(sCommandArray);
367*cdf0e10cSrcweir             boolean bBackValue = aHandler.executeSynchronously();
368*cdf0e10cSrcweir             assure("There seems to be a problem with ghostscript", bBackValue, true);
369*cdf0e10cSrcweir             int nExitCode = aHandler.getExitCode();
370*cdf0e10cSrcweir             // ArrayList m_aFileList = new ArrayList();
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir             // String sJPEGNameSchema = "";
373*cdf0e10cSrcweir             if (nExitCode == 0)
374*cdf0e10cSrcweir             {
375*cdf0e10cSrcweir                 // TODO: return a real filename, due to the fact we don't know how much files are created, maybe better to return a list
376*cdf0e10cSrcweir                 int nPages = countPages(sJPEGNameSchema);
377*cdf0e10cSrcweir                 if (nPages == 0)
378*cdf0e10cSrcweir                 {
379*cdf0e10cSrcweir                     // return only a valid schema name if there at least one page.
380*cdf0e10cSrcweir                     sJPEGNameSchema = "";
381*cdf0e10cSrcweir                     assure("Document '" + sPostscriptOrPDFFile + "' doesn't create pages.", false, true);
382*cdf0e10cSrcweir                 }
383*cdf0e10cSrcweir             }
384*cdf0e10cSrcweir             else
385*cdf0e10cSrcweir             {
386*cdf0e10cSrcweir                 assure("There seems to be a problem with ghostscript and '" + sPostscriptOrPDFFile + "' exit code: " + nExitCode, false, true);
387*cdf0e10cSrcweir                 GlobalLogWriter.println("Warning: There seems to be a problem with '" + sGhostscriptEXE + "'...");
388*cdf0e10cSrcweir             }
389*cdf0e10cSrcweir             // String[] aList = new String[m_aFileList.size()];
390*cdf0e10cSrcweir             // aList = (String[])m_aFileList.toArray(aList);
391*cdf0e10cSrcweir             return sJPEGNameSchema; // sNewJPEGFilename;
392*cdf0e10cSrcweir         }
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir     public static String getFilenameForJPEGSchema(String _sJPEGSchema, int _nPage)
395*cdf0e10cSrcweir         {
396*cdf0e10cSrcweir             if (_nPage < 1 || _nPage > 9999)
397*cdf0e10cSrcweir             {
398*cdf0e10cSrcweir                 return "";
399*cdf0e10cSrcweir             }
400*cdf0e10cSrcweir             final String sFilename = util.utils.replaceAll13(_sJPEGSchema, m_sGS_PageOutput, StringHelper.createValueString(_nPage, 4));
401*cdf0e10cSrcweir             return sFilename;
402*cdf0e10cSrcweir         }
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir         /**
405*cdf0e10cSrcweir          * Return the number of really exists JPEG files
406*cdf0e10cSrcweir          * Give a JPEG Schema which contains something like '%04d' which will be converted by a number between 0001 and 9999 always
407*cdf0e10cSrcweir          * with leading '0' (zero)
408*cdf0e10cSrcweir          * @param _sJPEGSchema
409*cdf0e10cSrcweir          * @return number of really existing pages of a given JPEG schema
410*cdf0e10cSrcweir          */
411*cdf0e10cSrcweir     public int countPages(String _sJPEGSchema)
412*cdf0e10cSrcweir     {
413*cdf0e10cSrcweir         int nPages = 0;
414*cdf0e10cSrcweir         if (_sJPEGSchema.length() > 0)
415*cdf0e10cSrcweir         {
416*cdf0e10cSrcweir             // TODO: if there doesn't exists a '%04d' in the schema we will return 9999 which is a little bit wrong here.
417*cdf0e10cSrcweir             for (int i=1;i<10000;i++)
418*cdf0e10cSrcweir             {
419*cdf0e10cSrcweir                 String sNewJPEGFilename = getFilenameForJPEGSchema(_sJPEGSchema, i);
420*cdf0e10cSrcweir                 if (FileHelper.exists(sNewJPEGFilename))
421*cdf0e10cSrcweir                 {
422*cdf0e10cSrcweir                     // m_aFileList.add(sNewJPEGFilename); // as long as the files exist, fill the array
423*cdf0e10cSrcweir                     nPages ++;
424*cdf0e10cSrcweir                 }
425*cdf0e10cSrcweir                 else
426*cdf0e10cSrcweir                 {
427*cdf0e10cSrcweir                     break;                             // stop file check
428*cdf0e10cSrcweir                 }
429*cdf0e10cSrcweir             }
430*cdf0e10cSrcweir         }
431*cdf0e10cSrcweir         return nPages;
432*cdf0e10cSrcweir     }
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir //    public static void main(String [] _args)
435*cdf0e10cSrcweir //    {
436*cdf0e10cSrcweir //// DONE: give an index.ini file ok
437*cdf0e10cSrcweir ////        String args[] = {
438*cdf0e10cSrcweir ////            "-TimeOut", "3600000",
439*cdf0e10cSrcweir ////            "-tb", "java_complex",
440*cdf0e10cSrcweir ////            "-o", "graphical.JPEGCreator",
441*cdf0e10cSrcweir ////            "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\index.ini",
442*cdf0e10cSrcweir ////            "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output",
443*cdf0e10cSrcweir ////            "-DOC_COMPARATOR_PRINT_MAX_PAGE", "9999",
444*cdf0e10cSrcweir ////            "-DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION", "180",
445*cdf0e10cSrcweir ////            "-DOC_COMPARATOR_HTML_OUTPUT_PREFIX", "http://so-gfxcmp-lin.germany.sun.com/gfxcmp_ui/cw.php?inifile=",
446*cdf0e10cSrcweir //////            "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "PDF",      /* default: "OOo" */
447*cdf0e10cSrcweir //////            "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "msoffice", /* default: "OOo" */
448*cdf0e10cSrcweir //////            "-OFFICE_VIEWABLE", "false",
449*cdf0e10cSrcweir ////            "-AppExecutionCommand", "\"C:/Programme/sun/staroffice 9/program/soffice.exe\"  -norestore -nocrashreport -accept=pipe,name=ll93751;urp;",
450*cdf0e10cSrcweir ////            "-NoOffice"
451*cdf0e10cSrcweir ////        };
452*cdf0e10cSrcweir //
453*cdf0e10cSrcweir //// Done: give a directory, where exist pdf/ps files ok.
454*cdf0e10cSrcweir //// Done: inputpath (given file) doesn't exists, ok.
455*cdf0e10cSrcweir //// Done: give a ps/pdf file. ok.
456*cdf0e10cSrcweir //
457*cdf0e10cSrcweir //        String args[] = {
458*cdf0e10cSrcweir //            "-TimeOut", "3600000",
459*cdf0e10cSrcweir //            "-tb", "java_complex",
460*cdf0e10cSrcweir //            "-o", "graphical.JPEGCreator",
461*cdf0e10cSrcweir //            "-DOC_COMPARATOR_INPUT_PATH", "C:\\CWS\\temp\\output\\Names7.odt.pdf",
462*cdf0e10cSrcweir //            "-DOC_COMPARATOR_OUTPUT_PATH", "C:\\CWS\\temp\\output",
463*cdf0e10cSrcweir //            "-DOC_COMPARATOR_PRINT_MAX_PAGE", "9999",
464*cdf0e10cSrcweir //            "-DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION", "180",
465*cdf0e10cSrcweir //            "-DOC_COMPARATOR_HTML_OUTPUT_PREFIX", "http://so-gfxcmp-lin.germany.sun.com/gfxcmp_ui/cw.php?inifile=",
466*cdf0e10cSrcweir ////            "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "PDF",      /* default: "OOo" */
467*cdf0e10cSrcweir ////            "-DOC_COMPARATOR_REFERENCE_CREATOR_TYPE", "msoffice", /* default: "OOo" */
468*cdf0e10cSrcweir ////            "-OFFICE_VIEWABLE", "false",
469*cdf0e10cSrcweir //            "-AppExecutionCommand", "\"C:/Programme/sun/staroffice 9/program/soffice.exe\"  -norestore -nocrashreport -accept=pipe,name=ll93751;urp;",
470*cdf0e10cSrcweir //            "-NoOffice"
471*cdf0e10cSrcweir //        };
472*cdf0e10cSrcweir //
473*cdf0e10cSrcweir //        org.openoffice.Runner.main(args);
474*cdf0e10cSrcweir //    }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir }
477