summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/graphical/FileHelper.java
blob: aa565f98efa5675315dd108af45e81a36aa69cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package graphical;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.StringTokenizer;
import helper.OSHelper;

import java.io.PrintStream;
import javax.swing.JOptionPane;

public class FileHelper
{
    public FileHelper()
        {
            String sOSName = System.getProperty("os.name");
            String sOSArch = System.getProperty("os.arch");
            String sOSVersion = System.getProperty("os.version");

            GlobalLogWriter.println(sOSName);
            GlobalLogWriter.println(sOSArch);
            GlobalLogWriter.println(sOSVersion);

        }

    public static void MessageBox(String _sStr)
        {
            String sVersion = System.getProperty("java.version");
            String sOSName  = System.getProperty("os.name");
            JOptionPane.showMessageDialog( null, _sStr, sVersion + " " + sOSName + " Hello World Debugger", JOptionPane.INFORMATION_MESSAGE );
        }

    public static boolean exists(String _sFile)
        {
            if (_sFile == null)
            {
                return false;
            }

            File aFile = new File(_sFile);
            if (aFile.exists())
            {
                return true;
            }
            return false;
        }

    public static boolean isDir(String _sDir)
        {
            if (_sDir == null)
            {
                return false;
            }
            try
            {
                File aFile = new File(_sDir);
                if (aFile.exists() && aFile.isDirectory())
                {
                    return true;
                }
            }
            catch (NullPointerException e)
            {
                GlobalLogWriter.println("Exception caught. FileHelper.isDir('" + _sDir + "')");
                e.printStackTrace();
            }
            return false;
        }

    public static String getBasename(String _sFilename)
        {
            if (_sFilename == null)
            {
                return "";
            }

            int nIdx = _sFilename.lastIndexOf("\\");
            if (nIdx == -1)
            {
                nIdx = _sFilename.lastIndexOf("/");
            }
            if (nIdx > 0)
            {
                return _sFilename.substring(nIdx + 1);
            }
            return _sFilename;
        }

    public static String getNameNoSuffix(String _sFilename)
        {
            if (_sFilename == null)
            {
                return "";
            }
            int nIdx = _sFilename.lastIndexOf(".");
            if (nIdx > 0)
            {
                return _sFilename.substring(0, nIdx);
            }
            return _sFilename;
        }

    public static String getSuffix(String _sFilename)
        {
            if (_sFilename == null)
            {
                return "";
            }
            int nIdx = _sFilename.lastIndexOf(".");
            if (nIdx > 0)
            {
                return _sFilename.substring(nIdx );
            }
            return "";
        }

    public static String getPath(String _sFilename)
        {
            if (_sFilename == null)
            {
                return "";
            }

            int nIdx = _sFilename.lastIndexOf("\\");
            if (nIdx == -1)
            {
                nIdx = _sFilename.lastIndexOf("/");
            }
            if (nIdx > 0)
            {
                return _sFilename.substring(0, nIdx);
            }
            return "";
        }


    // makeDirectories("", "/tmp/a/b");
    // creates all directories /tmp/a/b

    public static void makeDirectories(String first, String path)
        {
            makeDirectories(first, path, "0777");
        }

    public static void makeDirectories(String first, String path, String _sMode)
        {
            String fs = System.getProperty("file.separator");
            if (path.startsWith(fs + fs)) // starts with UNC Path
            {
                int n = path.indexOf(fs, 2);
                n = path.indexOf(fs, n + 1);
                first = path.substring(0, n);
                path = path.substring(n + 1);
            }

            String already_done = null;
            StringTokenizer path_tokenizer = new StringTokenizer(path,fs,false);
            already_done = first;
            while (path_tokenizer.hasMoreTokens())
            {
                String part = path_tokenizer.nextToken();
                File new_dir = new File(already_done + File.separatorChar + part);
                already_done = new_dir.toString();
                //create the directory
                new_dir.mkdirs();
                if (OSHelper.isUnix() &&
                    _sMode.length() > 0)
                {
                    try
                    {
                        chmod(new_dir, _sMode);
                    }
                    catch (java.io.IOException e)
                    {
                        GlobalLogWriter.println("Exception caught. FileHelper.makeDirectories('" + new_dir.getAbsolutePath() + "')");
                    }
                }
            }
            // return;
        }

    public static void chmod(File file, String mode) throws java.io.IOException
        {
            Runtime.getRuntime().exec
                (new String[]
                    {"chmod", mode, file.getAbsolutePath()});
        }

    public static String removeFirstDirectorysAndBasenameFrom(String _sName, String _sRemovePath)
        {
            // pre: _sName: /a/b/c/d/e/f.g _sRemovePath /a/b/c
            // result: d/e
            String fs = System.getProperty("file.separator");

            String sBasename = FileHelper.getBasename(_sName);
            String sSubDirs = "";
            if (_sName.startsWith(_sRemovePath))
            {
                // if _sName starts with _sRemovePath
                int nRemovePathIndex = _sRemovePath.length();
                if (! _sRemovePath.endsWith(fs))
                {
                    // add 1 if we not ends with file separator
                    nRemovePathIndex ++;
                }
                int nBasenameIndex = _sName.length() - sBasename.length() - 1;
                if (nRemovePathIndex < nBasenameIndex)
                {
                    sSubDirs = _sName.substring(nRemovePathIndex, nBasenameIndex);
                }
            }
            else
            {
                // special case, the _sRemovePath is not part of _sName
                sSubDirs = FileHelper.getPath(_sName);
                if (sSubDirs.startsWith(fs))
                {
                    // remove leading file separator
                    sSubDirs = sSubDirs.substring(1);
                }
            }

            return sSubDirs;
        }

    public static void test_removeFirstDirectorysAndBasenameFrom()
        {
            removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c");
            removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/a/b/c/");
            removeFirstDirectorysAndBasenameFrom("/a/b/c/d/e/f.g", "/b/c");
        }


    public static String getSystemPathFromFileURL( String _sFileURL )
    {
        String sSystemFile = null;

        if(_sFileURL.startsWith("file:///"))
        {
            if (OSHelper.isWindows())
            {
                sSystemFile = _sFileURL.substring(8);
            }
            else
            {
                sSystemFile = _sFileURL.substring(7);
            }
        }
        else if (_sFileURL.startsWith("file://"))
        {
            sSystemFile = _sFileURL.substring(5);
        }
        String fs = System.getProperty("file.separator");
        if (! fs.equals("/"))
        {
            sSystemFile = sSystemFile.replace ('/', fs.toCharArray ()[0]);
        }
// FEATURE FOR UNC NEED!!!
        return sSystemFile;
    }

    private static boolean m_bDebugTextShown = false;
    public static boolean isDebugEnabled()
        {
            boolean bDebug = false;
            String sTmpPath = util.utils.getUsersTempDir();
            //util.utils.getUsersTempDir();
            String fs = System.getProperty("file.separator");
            String sName = sTmpPath + fs + "DOC_COMPARATOR_DEBUG";
            File aFile = new File(sName);
            if (aFile.exists())
            {
                if (m_bDebugTextShown == false)
                {
                    GlobalLogWriter.println("Found file: " + sName);
                    GlobalLogWriter.println("Activate debug mode.");
                    GlobalLogWriter.println("If debug mode is no longer necessary, remove the above file.");
                    m_bDebugTextShown = true;
                }
                bDebug = true;
            }
            return bDebug;
        }

    private static void copyStream(InputStream _aIn, OutputStream _aOut) throws java.io.IOException
    {
        byte[] aBuffer = new byte[0xFFFF];
        for (int len; (len = _aIn.read(aBuffer)) != -1; )
        {
            _aOut.write(aBuffer, 0, len);
        }
    }

    public static void copy(String _sSource, String _sDestination)
        {
            FileInputStream aFIS = null;
            FileOutputStream aFOS = null;

            try
            {
                aFIS = new FileInputStream(_sSource);
                aFOS = new FileOutputStream(_sDestination);
                copyStream(aFIS, aFOS);
            }
            catch (java.io.IOException e)
            {
                System.out.println("Error: caught Exception: " + e.getMessage());
            }
            finally
            {
                if (aFIS != null)
                {
                    try
                    {
                        aFIS.close();
                    }
                    catch (java.io.IOException e)
                    {
                        System.out.println("Error: caught Exception: " + e.getMessage());
                    }
                }
                if (aFOS != null)
                {
                    try
                    {
                        aFOS.close();
                    }
                    catch (java.io.IOException e)
                    {
                        System.out.println("Error: caught Exception: " + e.getMessage());
                    }
                }
            }

        }


    /**
     * Within the directory run through, it's possible to say which file extension types should not
     * consider like '*.prn' because it's not a document.
     *
     * @return a FileFilter function
     */
    public static FileFilter getFileFilter()
        {
            FileFilter aFileFilter = new FileFilter()
                {
                    public boolean accept( File pathname )
                        {
                            // leave out files which started by '~$' these are Microsoft Office temp files
                            if (pathname.getName().startsWith("~$"))
                            {
                                return false;
                            }
                            // leave out files starts with '.~lock.' these are OpenOffice.org lock files
                            if (pathname.getName().startsWith(".~lock."))
                            {
                                return false;
                            }
                            // leave out files ends with '#' these could be temp files
                            if (pathname.getName().endsWith("#"))
                            {
                                return false;
                            }
                            if (pathname.getName().endsWith(".prn"))
                            {
                                return false;
                            }
                            if (pathname.getName().endsWith(".ps"))
                            {
                                return false;
                            }
                            // This type of document no one would like to load.
                            if (pathname.getName().endsWith(".zip"))
                            {
                                return false;
                            }
                            // just a hack
                            if (pathname.getName().endsWith("_"))
                            {
                                return false;
                            }
                            return true;
                        }
                };
            return aFileFilter;
        }
    /**
     * Within the directory run through, it's possible to say which file extension types should not
     * consider like '*.prn' because it's not a document.
     *
     * @return a FileFilter function
     */
    public static FileFilter getFileFilterPSorPDF()
        {
            FileFilter aFileFilter = new FileFilter()
                {
                    public boolean accept( File pathname )
                        {
                            if (pathname.getName().endsWith(".ps"))
                            {
                                return true;
                            }
                            if (pathname.getName().endsWith(".pdf"))
                            {
                                return true;
                            }
                            return false;
                        }
                };
            return aFileFilter;
        }
    /**
     * Within the directory run through, it's possible to say which file extension types should not
     * consider like '*.prn' because it's not a document.
     *
     * @return a FileFilter function
     */
    public static FileFilter getFileFilterJPEG()
        {
            FileFilter aFileFilter = new FileFilter()
                {
                    public boolean accept( File pathname )
                        {
                            if (pathname.getName().toLowerCase().endsWith(".jpg"))
                            {
                                return true;
                            }
                            if (pathname.getName().toLowerCase().endsWith(".jpeg"))
                            {
                                return true;
                            }
                            return false;
                        }
                };
            return aFileFilter;
        }
    /**
     * Within the directory run through, it's possible to say which file extension types should not
     * consider like '*.ini' because it's not a document.
     *
     * @return a FileFilter function
     */
    public static FileFilter getFileFilterINI()
        {
            FileFilter aFileFilter = new FileFilter()
                {
                    public boolean accept( File pathname )
                        {
                            String sPathname = pathname.getName().toLowerCase();
                            if (sPathname.endsWith("index.ini"))
                            {
                                // don't consider the index.ini file
                                return false;
                            }
                            if (sPathname.endsWith(".ini"))
                            {
                                return true;
                            }
                            return false;
                        }
                };
            return aFileFilter;
        }

        public static String appendPath(String _sPath, String _sRelativePathToAdd)
        {
            String sNewPath = _sPath;
            String fs = System.getProperty("file.separator");
            if (_sPath.startsWith("file:"))
            {
                fs = "/";                                  // we use a file URL so only '/' is allowed.
            }
            if (! (sNewPath.endsWith("/") || sNewPath.endsWith("\\") ) )
            {
                sNewPath += fs;
            }
            sNewPath += _sRelativePathToAdd;
            return sNewPath;
        }


    public static void createInfoFile(String _sFile, ParameterHelper _aGTA)
        {
            createInfoFile(_sFile, _aGTA, "");
        }

    public static void createInfoFile(String _sFile, ParameterHelper _aGTA, String _sSpecial)
        {
            String sFilename;
            if (_sFile.startsWith("file://"))
            {
                sFilename = FileHelper.getSystemPathFromFileURL(_sFile);
                GlobalLogWriter.println("CreateInfoFile: '" + sFilename + "'" );
            }
            else
            {
                sFilename = _sFile;
            }
            String sFileDir = FileHelper.getPath(sFilename);
            String sBasename = FileHelper.getBasename(sFilename);
            String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);

            String sIniFile = FileHelper.appendPath(sFileDir, sBasename + ".ini");
            IniFile aIniFile = new IniFile(sIniFile);

            // OLD INFO FILE

            System.getProperty("line.separator");
            String sInfoFilename = FileHelper.appendPath(sFileDir, sNameNoSuffix + ".info");
            File aInfoFile = new File(sInfoFilename);

            String sBuildID = "";

            try
            {
                FileOutputStream out2 = new FileOutputStream(aInfoFile.toString());
                PrintStream out = new PrintStream(out2);

                out.println("# automatically created file by graphical compare");
                if (_aGTA != null)
                {
                    if (_sSpecial != null && _sSpecial.equals("msoffice"))
                    {
                        out.println("# buildid from wordloadfile");
                        sBuildID = _aGTA.getPerformance().getMSOfficeVersion();
                        out.println("buildid=" + sBuildID);
                    }
                    else
                    {
                        out.println("# buildid is read out of the bootstrap file");
                        sBuildID = _aGTA.getBuildID();
                        out.println("buildid=" + sBuildID);
                    }
                    aIniFile.insertValue("global", "buildid", sBuildID);

                    out.println();
                    out.println("# resolution given in DPI");
                    out.println("resolution=" + _aGTA.getResolutionInDPI());
                    aIniFile.insertValue("global", "resolution", _aGTA.getResolutionInDPI());
                }
                else
                {
                    out.println("buildid=" + _sSpecial);
                    aIniFile.insertValue("global", "buildid", _sSpecial);
                }

                out.println();
                out.println("# Values out of System.getProperty(...)");
                out.println("os.name=" + System.getProperty("os.name"));
                out.println("os.arch=" + System.getProperty("os.arch"));
                out.println("os.version=" + System.getProperty("os.version"));

                aIniFile.insertValue("global", "os.name", System.getProperty("os.name"));
                aIniFile.insertValue("global", "os.arch", System.getProperty("os.arch"));
                aIniFile.insertValue("global", "os.version", System.getProperty("os.version"));

                if (_aGTA != null)
                {
                    out.println();
                    out.println("# Performance output, values are given in milli sec.");
                    _aGTA.getPerformance().print(out);
                    _aGTA.getPerformance().print(aIniFile, "global");
                }

                out.flush();
                out.close();
                out2.close();
            }
            catch (java.io.IOException e)
            {
                GlobalLogWriter.println("can't create Info file.");
                e.printStackTrace();
            }
            aIniFile.close();

        }

        public static void addBasenameToFile(String _sIndexFilename, String _sBasename, String _sCreator, String _sType, String _sSource)
        {
            String sPath;
            if (_sIndexFilename.startsWith("file:"))
            {
                sPath = FileHelper.getSystemPathFromFileURL(_sIndexFilename);
            }
            else
            {
                sPath = _sIndexFilename;
            }
            String sIndexFilename = sPath; // FileHelper.appendPath(sPath, _sFilename);
            IniFile aIniFile = new IniFile(sIndexFilename);
            aIniFile.insertValue(_sBasename, "creator", _sCreator);
            aIniFile.insertValue(_sBasename, "type", _sType);
            aIniFile.insertValue(_sBasename, "source", _sSource);
            aIniFile.close();
        }

        public static void addBasenameToPostscript(String _sOutputFilename)
        {
            String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "postscript.ini");
            String sBasename = FileHelper.getBasename(_sOutputFilename);
            addBasenameToFile(sIndexFilename, sBasename, "", "", "");
        }
        public static void addBasenameToIndex(String _sOutputFilename, String _sBasename, String _sCreator, String _sType, String _sSource)
        {
            String sIndexFilename = FileHelper.appendPath(_sOutputFilename, "index.ini");
            addBasenameToFile(sIndexFilename, _sBasename, _sCreator, _sType, _sSource);
        }

}