1package graphical_compare;
2
3#**************************************************************
4#
5#  Licensed to the Apache Software Foundation (ASF) under one
6#  or more contributor license agreements.  See the NOTICE file
7#  distributed with this work for additional information
8#  regarding copyright ownership.  The ASF licenses this file
9#  to you under the Apache License, Version 2.0 (the
10#  "License"); you may not use this file except in compliance
11#  with the License.  You may obtain a copy of the License at
12#
13#    http://www.apache.org/licenses/LICENSE-2.0
14#
15#  Unless required by applicable law or agreed to in writing,
16#  software distributed under the License is distributed on an
17#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18#  KIND, either express or implied.  See the License for the
19#  specific language governing permissions and limitations
20#  under the License.
21#
22#**************************************************************
23
24
25
26use CallExternals;
27use stringhelper;
28use timehelper;
29use filehelper;
30use loghelper;
31use oshelper;
32use cwstestresulthelper;
33use solarenvhelper;
34use ConvwatchHelper;
35
36use strict;
37use Cwd;
38# use File::Basename;
39use Getopt::Long;
40use English;                  # $OSNAME, ...
41use File::Path;
42use Cwd 'chdir';
43use Sys::Hostname;
44use Time::localtime;
45
46# my $cwd = getcwd();
47
48BEGIN {
49    use Exporter   ();
50    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
51
52    $VERSION     = 1.00;
53    # if using RCS/CVS, this may be preferred
54    $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
55    @ISA         = qw(Exporter);
56    @EXPORT      = qw(&SingleDocumentCompare &setPrefix &setConnectionString);
57    %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
58    # your exported package globals go here,
59    # as well as any optionally exported functions
60    @EXPORT_OK   = ( ); # qw($Var1 %Hashit &func3);
61}
62
63
64our $nTimeOut = 300 * 1000;
65our $viewable = 1;
66our $port;
67our $resolution;
68our $overwritereference;
69our $fixreference;
70our $sConncectionString;
71
72sub setConnectionString($)
73{
74    $sConncectionString=shift;
75}
76
77sub getOOoRunnerClasspath()
78{
79    my $sSourceRoot;
80    my $sUPDExtensions = "";
81    if (defined ($ENV{SOL_TMP}) && defined ($ENV{SOLARVERSION}))
82    {
83        $sSourceRoot = $ENV{SOLARVERSION};
84    }
85    elsif (defined $ENV{SOURCE_ROOT})
86    {
87        $sSourceRoot = $ENV{SOURCE_ROOT};
88        $sSourceRoot = appendPath($sSourceRoot, $ENV{WORK_STAMP});
89    }
90    else
91    {
92        $sSourceRoot = $ENV{SOLARVERSION};
93        $sUPDExtensions = ".$ENV{UPDMINOR}";
94    }
95    $sSourceRoot = appendPath($sSourceRoot, $ENV{INPATH});
96    my $sSourceRootBin = appendPath($sSourceRoot, "bin" . $sUPDExtensions);
97    my $sSourceRootLib = appendPath($sSourceRoot, "lib" . $sUPDExtensions);
98
99    if (! -d $sSourceRoot )
100    {
101        log_print( "SourceRoot not found, search it in '$sSourceRoot'\n");
102        return "";
103    }
104
105    my $sOOoRunnerPath = $sSourceRootBin;
106    my $sUnoilPath   = $sSourceRootBin;
107    my $sRidlPath    = $sSourceRootBin;
108    my $sJurtPath    = $sSourceRootBin;
109    my $sJuhPath     = $sSourceRootBin;
110    my $sJavaUnoPath = $sSourceRootBin;
111
112    my $sOOoRunnerClasspath =
113        appendPath( $sRidlPath,      "ridl.jar") . getJavaPathSeparator() .
114        appendPath( $sUnoilPath,     "unoil.jar") . getJavaPathSeparator() .
115        appendPath( $sJurtPath,      "jurt.jar") . getJavaPathSeparator() .
116        appendPath( $sJuhPath,       "juh.jar") . getJavaPathSeparator() .
117        appendPath( $sJavaUnoPath,   "java_uno.jar") . getJavaPathSeparator() .
118        appendPath( $sOOoRunnerPath, "OOoRunnerLight.jar");
119    if (isWindowsEnvironment())
120    {
121        $sOOoRunnerClasspath .= getJavaPathSeparator() . $sSourceRootBin;
122    }
123    else
124    {
125        $sOOoRunnerClasspath .= getJavaPathSeparator() . $sSourceRootLib;
126    }
127    return $sOOoRunnerClasspath;
128}
129
130# ------------------------------------------------------------------------------
131sub getTempPath()
132{
133    my $sTempPath;
134    if (isWindowsEnvironment())
135    {
136        $sTempPath = "C:/temp";
137    }
138    elsif (isUnixEnvironment())
139    {
140        $sTempPath = "/tmp";
141    }
142    else
143    {
144        die "getTempPath() Failed, due to unsupported environment.\n";
145    }
146    return $sTempPath;
147}
148# ------------------------------------------------------------------------------
149
150sub getProjectOutput()
151{
152    my $sOutput = appendPath(getProjectRoot(), $ENV{INPATH});
153    $sOutput = appendPath($sOutput, "misc");
154    return $sOutput;
155}
156
157# ------------------------------------------------------------------------------
158sub getProjectOutputReference()
159{
160    my $sOutput = appendPath(getProjectRoot(), $ENV{INPATH});
161    $sOutput = appendPath($sOutput, "reference");
162    return $sOutput;
163}
164
165
166sub searchForReference($)
167{
168    my $sFile = shift;
169    if ( -e $sFile )
170    {
171        return 0;
172    }
173    if ( -e $sFile . ".ps")
174    {
175        return 0;
176    }
177    if ( -e $sFile . ".pdf")
178    {
179        return 0;
180    }
181    return 1;
182}
183# ------------------------------------------------------------------------------
184
185# my $sOfficeName = $officeprefixname . $officename;
186sub SingleDocumentCompare($$$$$$)
187{
188    # get all about the document to compare
189    my $sDocumentPoolPath = shift;
190    my $sDocumentPool = shift;
191    my $sDocumentName = shift;
192    my $sDebug = "";
193
194    # get all about the destination office
195    my $sCreatorType = shift;
196    if (! $sCreatorType)
197    {
198        # log_print( "parameter -creatortype not given. Use 'OOo'\n");
199        $sCreatorType = "ps";
200    }
201    my $prepareonly = shift;
202    my $show = shift;
203
204    # my $nSimpleCompareTime = getTime();
205
206    my $nConvwatchFailed = 0;
207    set_logfile( appendPath(getProjectOutput(), $sDocumentName . ".txt" ));
208
209    print("$sDocumentName");
210    log_print("\n");
211    log_print("Graphical compare on document: '$sDocumentName'\n");
212    # ------------------------------------------------------------------------------
213    # create postscript or pdf from first installed office
214    # ------------------------------------------------------------------------------
215
216    my $sOOoRunnerClasspath = quoteIfNeed(getOOoRunnerClasspath());
217    if ($OSNAME eq "cygwin")
218    {
219        if (!startswith($sOOoRunnerClasspath, "\""))
220        {
221            $sOOoRunnerClasspath = quote($sOOoRunnerClasspath);
222        }
223    }
224    if (length($sOOoRunnerClasspath) == 0)
225    {
226        $nConvwatchFailed == 1;
227    }
228    # ------------------------------------------------------------------------------
229    # create postscript or pdf from second installed office
230    # ------------------------------------------------------------------------------
231
232    my $sPathesIni = appendPath(getProjectOutput(), "pathes.ini");
233    my $gspath = getFromPathes($sPathesIni, "gs.path");
234    my $gsexe = getFromPathes($sPathesIni, "gs.exe");
235    my $impath = getFromPathes($sPathesIni, "imagemagick.path");
236    my $javaexe = getFromPathes($sPathesIni, "java.exe");
237    setJavaExecutable($javaexe);
238
239    log_print("----- CREATE POSTSCRIPT OR PDF WITH RUNNING OFFICE -----\n");
240    # my $nPrepareSecondPostscriptTime = getTime();
241    if ($nConvwatchFailed == 0)
242    {
243        my $sInputPath = $sDocumentPoolPath;
244        $sInputPath = appendPath($sInputPath, $sDocumentPool);
245        $sInputPath = appendPath($sInputPath, $sDocumentName);
246
247        if (! -f $sInputPath )
248        {
249            $nConvwatchFailed = 1;
250            log_print("ERROR: File '$sInputPath' doesn't exists.\n");
251        }
252        else
253        {
254            my $sOutputPath = getProjectOutput();
255            my $sPropertyFile = appendPath(getProjectOutput() , $sDocumentName . ".build.props");
256
257            local *PROPERTYFILE;
258            if (open(PROPERTYFILE, ">$sPropertyFile"))
259            {
260                print PROPERTYFILE "# This file is automatically created by graphical_compare.pl\n";
261                print PROPERTYFILE "DOC_COMPARATOR_PRINT_MAX_PAGE=9999\n";
262                print PROPERTYFILE "DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION=180\n";
263                print PROPERTYFILE "DOC_COMPARATOR_REFERENCE_CREATOR_TYPE=$sCreatorType\n";
264                print PROPERTYFILE "TEMPPATH=" . getTempPath() . "\n";
265                if ($sConncectionString)
266                {
267                    print PROPERTYFILE "ConnectionString=$sConncectionString\n";
268                }
269                else
270                {
271                    print PROPERTYFILE "ConnectionString=pipe,name=" . getUsername() . "\n";
272                }
273                print PROPERTYFILE "OFFICE_VIEWABLE=true\n";
274                print PROPERTYFILE "CREATE_DEFAULT_REFERENCE=true\n";
275                print PROPERTYFILE "DOC_COMPARATOR_INPUT_PATH=$sInputPath\n";
276                print PROPERTYFILE "DOC_COMPARATOR_OUTPUT_PATH=$sOutputPath\n";
277                if (isWindowsEnvironment())
278                {
279                    print PROPERTYFILE "DOC_COMPARATOR_PRINTER_NAME=CrossOffice Generic Printer\n";
280                }
281                print PROPERTYFILE "NoOffice=true\n";
282
283                close(PROPERTYFILE);
284            }
285            else
286            {
287                print "Can't open '$sPropertyFile' for write.\n";
288            }
289            if ( -e "$sPropertyFile")
290            {
291                # start OOoRunner
292                # sleep 10;
293                # $sOOoRunnerClasspathFromDestinationName = quoteIfNeed(getOOoRunnerClasspath());
294                my $sParams;
295                if ( $ENV{PERL} )
296                {
297                    $sParams = "-Dperl.exe=" . convertCygwinPath($ENV{PERL});
298                }
299
300                $sParams .= " -cp " . $sOOoRunnerClasspath .
301                    " org.openoffice.Runner" .
302                    " -TimeOut $nTimeOut" .
303                    " -tb java_complex" .
304                    " -ini $sPropertyFile" .
305                    " -o graphical.PostscriptCreator";
306                # $sParams .= " -cs pipe,name=$USER";
307
308                # $sDebug = "-Xdebug  -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
309                my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
310                $sDebug = "";
311                log_print( "\n\n");
312                if ($err != 0)
313                {
314                    my $sFailure = "Failed after try to create Postscript/pdf document for " . $sDocumentName;
315                    log_print("ERROR: $sFailure\n");
316                    $nConvwatchFailed = 1;
317                }
318            }
319            else
320            {
321                my $sFailure = "There is no propertyfile: $sPropertyFile";
322                log_print( "ERROR: $sFailure\n");
323                $nConvwatchFailed=1;
324            }
325        }
326
327        # set prepareonly and it is possible to only create ps or pdf files
328        if ($prepareonly)
329        {
330            print(" [only create ");
331            if ($sCreatorType eq "ps" || $sCreatorType eq "pdf")
332            {
333                print(" $sCreatorType");
334            }
335            else
336            {
337                print(" (${sCreatorType}?)");
338            }
339            if ($nConvwatchFailed == 0)
340            {
341                print(" ok");
342            }
343            else
344            {
345                print(" failed")
346            }
347            print("]\n");
348            return $nConvwatchFailed;
349        }
350
351
352        # ------------------------------------------------------------------------------
353        # create jpeg from postscript or pdf from second installed office
354        # ------------------------------------------------------------------------------
355
356        if ($nConvwatchFailed == 0)
357        {
358            log_print("----- CREATE JPEG FROM POSTSCRIPT OR PDF FROM RUNNING OFFICE -----\n");
359            # start OOoRunner
360            my $sInputPath = getProjectOutput();
361            $sInputPath = appendPath($sInputPath, $sDocumentName);
362
363            my $sOutputPath = getProjectOutput();
364
365            my $sParams = "-cp " . $sOOoRunnerClasspath .
366                " org.openoffice.Runner" .
367                " -TimeOut $nTimeOut" .
368                " -tb java_complex" .
369                " -DOC_COMPARATOR_INPUT_PATH " . quoteIfNeed($sInputPath) .
370                " -DOC_COMPARATOR_OUTPUT_PATH "  . quoteIfNeed($sOutputPath) .
371                " -DOC_COMPARATOR_REFERENCE_CREATOR_TYPE $sCreatorType" .
372                " -NoOffice" .
373                " -NoSmallPictures" .
374                " -o graphical.JPEGCreator";
375            if ($gspath)
376            {
377                $sParams .= " -gs.path " . quoteIfNeed($gspath);
378            }
379            if ($gsexe)
380            {
381                $sParams .= " -gs.exe $gsexe";
382            }
383
384            # $sDebug = "-Xdebug  -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
385            my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
386            $sDebug = "";
387            # log_print( "\n\n");
388            if ($err != 0)
389            {
390                my $sFailure = "Failed after try to create JPEG from Postscript/pdf document for " . $sDocumentName;
391                log_print("ERROR: $sFailure\n");
392                $nConvwatchFailed = 1;
393            }
394        }
395    }
396
397    # ------------------------------------------------------------------------------
398    # create jpeg from postscript or pdf from references
399    # ------------------------------------------------------------------------------
400
401    if ($nConvwatchFailed == 0)
402    {
403        log_print("----- CREATE JPEG FROM POSTSCRIPT OR PDF FROM REFERENCE -----\n");
404
405        # start OOoRunner
406        my $sInputPath = appendPath(getProjectRoot(), "references");
407        $sInputPath = appendPath($sInputPath, getEnvironment());
408        $sInputPath = appendPath($sInputPath, $sDocumentPool);
409        $sInputPath = appendPath($sInputPath, $sDocumentName);
410
411        my $err = searchForReference($sInputPath);
412        if ($err != 0)
413        {
414            log_print("ERROR: Can't find Postscript or PDF reference for '$sInputPath'\n");
415            $nConvwatchFailed = 1;
416        }
417        else
418        {
419            my $sOutputPath = getProjectOutputReference();
420            rmkdir $sOutputPath;
421
422            my $sIndexFile = appendPath($sOutputPath, "index.ini");
423            # we need the index.ini for better run through
424            local *INDEXINI;
425            if ( ! -e $sIndexFile)
426            {
427                if (open(INDEXINI, ">$sIndexFile"))
428                {
429                    # print INDEXINI "[$sDocumentName]\n";
430                    close(INDEXINI);
431                }
432            }
433            my $sParams = "-cp " . $sOOoRunnerClasspath .
434                " org.openoffice.Runner" .
435                " -TimeOut $nTimeOut" .
436                " -tb java_complex" .
437                " -DOC_COMPARATOR_INPUT_PATH " . quoteIfNeed($sInputPath) .
438                " -DOC_COMPARATOR_OUTPUT_PATH "  . quoteIfNeed($sOutputPath) .
439                " -DOC_COMPARATOR_REFERENCE_CREATOR_TYPE $sCreatorType" .
440                " -NoOffice" .
441                " -NoSmallPictures" .
442                " -o graphical.JPEGCreator";
443            if ($gspath)
444            {
445                $sParams .= " -gs.path " . quoteIfNeed($gspath);
446            }
447            if ($gsexe)
448            {
449                $sParams .= " -gs.exe $gsexe";
450            }
451
452            # $sDebug = "-Xdebug  -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
453            my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
454            $sDebug = "";
455            # log_print( "\n\n");
456            if ($err != 0)
457            {
458                my $sFailure = "Failed after try to create JPEG from Postscript/pdf document for references.";
459                log_print("ERROR: $sFailure\n");
460                $nConvwatchFailed = 1;
461            }
462        }
463    }
464    # ------------------------------------------------------------------------------
465    # compare JPEGs
466    # ------------------------------------------------------------------------------
467
468    if ($nConvwatchFailed == 0)
469    {
470        log_print("----- COMPARE JPEGS -----\n");
471        my $sInputPath = appendPath(getProjectOutputReference(), $sDocumentName);
472
473        my $sOutputPath = getProjectOutput();
474
475        my $sParams = "-Xmx512m" .
476            " -cp " . $sOOoRunnerClasspath .
477            " org.openoffice.Runner" .
478            " -TimeOut $nTimeOut" .
479            " -tb java_complex" .
480            " -DOC_COMPARATOR_INPUT_PATH "  . quoteIfNeed($sInputPath) .
481            " -DOC_COMPARATOR_OUTPUT_PATH " . quoteIfNeed($sOutputPath) .
482            " -NoOffice" .
483            " -NoSmallPictures" .
484            " -o graphical.JPEGComparator";
485        if ($impath)
486        {
487            $sParams .= " -imagemagick.path " . quoteIfNeed($impath);
488        }
489
490        # start OOoRunner
491        # $sDebug = "-Xdebug  -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
492        my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
493        $sDebug = "";
494        log_print( "\n\n");
495        if ($err != 0)
496        {
497            my $sFailure = "Failed after compare JPEGs $sDocumentName\n";
498            log_print("ERROR: $sFailure\n");
499            $nConvwatchFailed = 1;
500
501            if ($show)
502            {
503                # try to execute new java tool to show graphical compare
504                my $sJavaProgram = appendPath(getProjectRoot(), $ENV{INPATH});
505                $sJavaProgram = appendPath($sJavaProgram, "class");
506                $sJavaProgram = appendPath($sJavaProgram, "ConvwatchGUIProject.jar");
507                if ( -e "$sJavaProgram")
508                {
509                    my $sInputPath = appendPath(getProjectOutput(), $sDocumentName . ".ps.ini");
510                    if (! -e $sInputPath)
511                    {
512                        $sInputPath = appendPath(getProjectOutput(), $sDocumentName . ".pdf.ini");
513                        if (! -e $sInputPath)
514                        {
515                            $sInputPath = 0;
516                        }
517                    }
518                    if ($sInputPath)
519                    {
520                        my $sParams = "-Xms128m -Xmx512m -jar $sJavaProgram $sInputPath";
521                        # $sParams .= " -cs pipe,name=$USER";
522                        # my $sJavaExe = "C:/Program Files/Java/jdk1.6.0_16/bin/java.exe"; # getJavaExecutable()
523                        my $sJavaExe = getJavaExecutable();
524                        # $sDebug = "-Xdebug  -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
525                        my $err = calljava($sJavaExe, $sParams, $sDebug);
526                        # $sDebug = "";
527                        # log_print( "\n\n");
528                        # if ($err != 0)
529                        # {
530                        #     my $sFailure = "Failed after try to create Postscript/pdf document for " . $sDocumentName;
531                        #     log_print("ERROR: $sFailure\n");
532                        #     $nConvwatchFailed = 1;
533                        # }
534                    }
535                }
536                else
537                {
538                    print "WARNING: The show program '$sJavaProgram' doesn't exists.\n";
539                }
540            }
541        }
542    }
543
544    log_print( "\n\n");
545    close_logfile();
546
547    if ($nConvwatchFailed == 0)
548    {
549        print(" [ok]\n");
550    }
551    else
552    {
553        print(" [FAILED]\n");
554        print("\nPrint output of test: $sDocumentName\n");
555        my $sLogFile = appendPath(getProjectOutput(), $sDocumentName . ".txt");
556        showFile($sLogFile);
557    }
558    # printTime(endTime($nSimpleCompareTime));
559
560    return $nConvwatchFailed;
561}
562
563# ------------------------------------------------------------------------------
564# cat $file
565sub showFile($)
566{
567    my $logfile = shift;
568    local *LOGFILE;
569    if (open(LOGFILE, "$logfile"))
570    {
571        my $line;
572        while ($line = <LOGFILE>)
573        {
574            chomp($line);
575            print $line ."\n";
576        }
577        close(LOGFILE);
578    }
579}
580
581
5821;
583