summaryrefslogtreecommitdiff
path: root/xmerge/java/org/openoffice/xmerge/converter/palm/palmtests/qa/comparator/pdbcomparison.java
blob: b5afcf3ae9fa6921872f375bfe88d6ee0010f27f (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
/************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: pdbcomparison.java,v $
 * $Revision: 1.3 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

import java.io.*;
import java.util.*;

public class pdbcomparison
{

  private String LOGTAG  ="LOGFILE";
  private String OUTTAG  ="OUTFILE";
  private String LISTTAG ="LISTFILE";
  private String PDBTAG1 ="PDBNAME1";
  private String PDBTAG2 ="PDBNAME2";

  private String OUTFILE="pdbcomparison.out";
  private String LOGFILE="pdbcomparison.log";

  private String pdbarr1[];
  private String pdbarr2[];


   /**
   * Default Constructor
   *
   *  @param
   *  @return
   *
   */
   public void pdbcomparison()
   {
   }

   /**
   * Prints the command line arguments for this class
   *
   * @param
   *
   * @return void
   *
   */
   public void usage()
   {
      String str = new String();
      str += "********************************************************\n";
      str += " java pdbcomparison.java <propFile> \n";
      str += "   where propFile is name of Property File...\n";
      str += "********************************************************\n";

      System.out.println(str);

   }

   /**
   * This method, read the Property file and validates the
   * entries in that file, and accordingly sets the log file
   * output file and updates the array pdbarr1 and pdbarr2 with
   * list of pdb's to be compared.
   *
   * @param propFile Property filename which list the log/outputfile/list/pdb
   *                 names
   * @return
   *
   */
   public void parsePropertyFile(String propFile)
   {
     Properties defaultProps = new Properties();

     try {
       FileInputStream in = new FileInputStream(propFile);
       defaultProps.load(in);
       in.close();
     } catch (IOException e) {
       System.out.println("Could not open Property File " + propFile);
       return;
     }


     String logFile  = defaultProps.getProperty(this.LOGTAG);
     String outFile  = defaultProps.getProperty(this.OUTTAG);
     String listFile = defaultProps.getProperty(this.LISTTAG);
     String pdbname1 = defaultProps.getProperty(this.PDBTAG1);
     String pdbname2 = defaultProps.getProperty(this.PDBTAG2);

    // validate all command line arguments
    if ((listFile == null) && ((pdbname1 == null) || (pdbname2 == null)))
    {
       System.out.println("Missing listFile or missing pdb filenames in Property file " + propFile);
       return;
    }

    if (logFile == null || logFile.length() == 0)
       logFile = this.LOGFILE;

    if (outFile == null || outFile.length() == 0)
       outFile = this.LOGFILE;


     // validate log and output files
     if (! validateAndCreateFile(logFile)) return;
     if (! validateAndCreateFile(outFile)) return;
     LOGFILE = logFile;
     OUTFILE = outFile;

     System.out.println("Output is written to log file... " + LOGFILE);
     if (listFile != null)
     {
       if (! checkFile(listFile)) return;
       populatePDBArray(listFile);
     } else {
       if (! checkFile(pdbname1)) return;
       if (! checkFile(pdbname2)) return;
       populatePDBArray(pdbname1, pdbname2);
     }
   }

   /**
   * This method validates if the file passed exists.
   * If it does , then it is moved to <filename>.bak and then creates a newFile.
   * Also validates permissions to create.
   *
   *  @param  filename  name of file to be created
   *  @return true, if file could be created
   *          false, if could not.
   *
   */
   private boolean validateAndCreateFile (String filename)
   {
     if (filename == null) return false;

     File f = null;
     try {
       f = new File(filename);
     } catch (NullPointerException e) {
       System.out.println("Could not create a File object for file " + filename);
       return false;
     }

     if (f.exists())
     {
       String newFile = filename + ".bak";
       File newF=null;
       try {
         newF = new File(newFile);
       } catch (Exception ex) {
         System.out.println("Could not get File Object instance for " + newFile);
         return false;
       }

       if (newF.exists())
       {
         try {
           newF.delete();
         } catch ( SecurityException se) {
           System.out.println("Could not get delete " + newFile);
           return false;
         }
       }

       try {
         if (! f.renameTo(newF))
         {
             System.out.println("Could not rename " + filename + "  to " + newFile );
             return false;
         }
       } catch  (SecurityException s) {
             System.out.println("SecurityException: " + s.toString());
             return false;
       } catch  (NullPointerException n) {
             System.out.println("NullPointerException: " + n.toString());
             return false;
       }
     } else {
       try {
        if (! f.createNewFile())
        {
           System.out.println("Could not create " + filename + " Check permissions..");
           return false;
        }
       } catch (IOException e) {
           System.out.println("IOException: " + e.toString());
           return false;
       } catch  (SecurityException s) {
           System.out.println("SecuriityException: " + s.toString() );
           return false;
       }

     }

     return true;

   }

   /**
   * This method validates if the file exists and is readable
   *
   *  @param  filename  name of file to be created
   *  @return true, if file exists and is readable
   *          false, if not.
   *
   */
   private boolean  checkFile(String filename)
   {
     if (filename == null) return false;

     File f = null;
     try {
       f = new File(filename);
     } catch (NullPointerException e) {
       System.out.println("Could not create a File object for file " + filename);
       return false;
     }

     if (! f.exists())
     {
       System.out.println("File " + filename + " does not exist... ");
       return false;
     }

     if (! f.canRead())
     {
       System.out.println("Cannot read file " + filename);
       return false;
     }

     return true;

   }

   /**
   * This method populates the pdb arrays with the names of the pdbs to
   * compare. Ths listFile lists a series of entries, wherein each
   * line indicates the PDB names to be compared.
   * <pdbname1>=<pdbname2>
   *
   *  @param  listFile  name of the listfile
   *  @return
   *
   */
   private void  populatePDBArray(String listFile)
   {
    // open ListFile and populate the PDB list to be compared
    if (listFile != null)
    {
        Properties listProps = new Properties();
        try {
        FileInputStream in = new FileInputStream(listFile);
        listProps.load(in);
        in.close();
        } catch (IOException ex) {
         System.out.println("Could not open List File " + listFile);
         return;
        }

        pdbarr1 = new String[listProps.size()];
        pdbarr2 = new String[listProps.size()];
        Enumeration e = listProps.keys();
        int j=0;
        while (e.hasMoreElements())
        {
            pdbarr1[j] = (String)e.nextElement();
            pdbarr2[j] = listProps.getProperty(pdbarr1[j]);
                    j++;
        }

        }
   }

   /**
   * This method populates the pdb arrays with the names of the pdbs to
   * compare.
   *
   *  @param  pdbname1 Name of 2nd PDB file to be compared
   *  @param  pdbname2 Name of 2nd PDB file to be compared
   *  @return
   *
   */
   private void  populatePDBArray(String pdbname1, String pdbname2)
   {
      if (pdbname1 == null) return;
      if (pdbname2 == null) return;

      if ((pdbname1 != null) && (pdbname2 != null))
      {
     pdbarr1 = new String[1];
     pdbarr2 = new String[1];

     pdbarr1[0] = pdbname1;
     pdbarr2[0] = pdbname2;
      }
   }

   /**
   * This method populates the pdb arrays with the names of the pdbs to
   * compare.
   *
   *  @param  arrayno  Array number which corresponds to the pdb array
   *                   containing  list of pdbs
   *                   If 1 then send pdbarr1, if 2 send pdbarr2 else null
   *
   *  @return PDB string array containing list of PDB's
   *
   */
   private String[]  getPDBArray(int arrayno)
   {
    if (arrayno == 1) return pdbarr1;
    if (arrayno == 2) return pdbarr2;

    return null;
   }

   /**
   * This method comares 2 PDB's and returns true if comparison is equal.
   * It uses the PDB Decoder class to decode to a PDB structure and then
   * does record comparison
   *
   * @param  pdbname1 Name of one  PDB file  to be compared
   * @param  pdbname2 Name of other  PDB file  to be compared
   *
   * @return returns true if both PDB's are equal else returns false
   *
   */
   private boolean  comparePDB(String pdbname1, String pdbname2)
   {
       PalmDB pdb1=null, pdb2=null;
       PDBDecoder decoder = new PDBDecoder();
       try {
         pdb1 = decoder.parse(pdbname1);
       } catch (Exception e) {
         System.out.println("Could not parse PDB " + pdbname1);
         return false;
       }

       try {
         pdb2 = decoder.parse(pdbname2);
       } catch (Exception e) {
         System.out.println("Could not parse PDB " + pdbname2);
         return false;
       }

       if (pdb1.equals(pdb2)) {
        writeToLog("PDB " + pdbname1 + "  and PDB " + pdbname2 + " are equal");

        return true;
       } else {
        writeToLog("PDB " + pdbname1 + "  and PDB " + pdbname2 + " are not equal");
        return false;
       }
   }



   /**
   *  Write message to LOGFILE
   *
   *  @param msg Message to be written to log file
   *  @return
   *
   */
   private void writeToLog(String msg)
   {
     if (msg == null) return;

     // Get Output Stream from Log file
     RandomAccessFile raf=null;
     try {
      raf = new RandomAccessFile(LOGFILE, "rw");
     } catch (Exception e) {
      System.out.println ("Could not open file " + LOGFILE);
      return;
     }

     try {
           long len = raf.length();
           raf.seek(len);
           raf.write(msg.getBytes());
           raf.write("\n".getBytes());
      } catch (IOException e) {
         System.out.println("ERROR: Could not write to File " + LOGFILE);
         return;
      }
   }

   /**
   *  Write status of comparison  to OUTFILE
   *
   *  @param status Indicates whether comparsion of PDB's PASSED or FAILED
   *  @param pdbname1 file name of pdb which was compared.
   *  @param pdbname2 file name of pdb which was compared.
   *
   *  @return
   *
   */
   private void writeToOutputFile(String status, String pdbname1, String pdbname2)
   {
     if (status == null) return;
     if (pdbname1 == null) return;
     if (pdbname2 == null) return;

     String msg = pdbname1 + "=" + pdbname2 + ":" + status;

     // Get Output Stream from Log file
     RandomAccessFile raf=null;
     try {
      raf = new RandomAccessFile(OUTFILE, "rw");
     } catch (Exception e) {
      System.out.println ("Could not open file " + OUTFILE);
      return;
     }

     try {
           long len = raf.length();
           raf.seek(len);

           raf.write(msg.getBytes());
           raf.write("\n".getBytes());
      } catch (IOException e) {
         System.out.println("ERROR: Could not write to File " + OUTFILE);
         return;
      }

     try {
       raf.close();
     } catch (Exception e) {
       System.out.println("ERROR: Could not close File " + OUTFILE);
       return;
     }

   }



   /**
   *  Main starting block of execution
   *
   *  @param command line args captured in an array of Strings
   *  @return
   *
   */
   public static void main(String args[])
   {

     Date startTime = new Date();
     pdbcomparison pdbcmp = new pdbcomparison();
     int nargs = args.length;
     int status=0;

     if (nargs != 1)
     {
         System.out.println("Incorrect no. of arguments passed...");
         pdbcmp.usage();
         System.exit(-1);

     }

     String propFile = args[0];

     File f=null;
     try {
       f = new File(propFile);
     } catch (Exception e) {
       System.out.println("Exception: Could not open file " + propFile);
       System.exit(-1);
     }

     if (! f.canRead()) {
       System.out.println("Exception: " + propFile + " is not a file ");
       System.exit(-1);
     }

     if (! f.canRead()) {
       System.out.println("Exception: Cannot open file for reading. Please check permissions ");
       System.exit(-1);
     }

     // parse Property file
     pdbcmp.parsePropertyFile(propFile);

     String pdbarr1[] = pdbcmp.getPDBArray(1);
     String pdbarr2[] = pdbcmp.getPDBArray(2);
     if ( (pdbarr1 == null) ||
          (pdbarr2 == null) ||
          (pdbarr1.length == 0) ||
          (pdbarr1.length == 0))
     {
       System.out.println("pdbArray is empty. No PDBS to compare... \n");
       System.exit(-1);
     }


     pdbcmp.writeToLog("************** Start *****************");
     pdbcmp.writeToLog("PDB Comparison: start time " + startTime);
     for (int i=0; i<pdbarr1.length; i++)
     {
       Date pdb_startTime = new Date();
       pdbcmp.writeToLog("\n");
       pdbcmp.writeToLog("start time " + pdb_startTime);
       boolean val = pdbcmp.comparePDB(pdbarr1[i], pdbarr2[i]);
       Date pdb_endTime = new Date();
       pdbcmp.writeToLog("end time " + pdb_endTime);

       if (val) {
        pdbcmp.writeToOutputFile("PASSED", pdbarr1[i], pdbarr2[i]);
        status=0;
       } else {
        pdbcmp.writeToOutputFile("FAILED", pdbarr1[i], pdbarr2[i]);
        status=-1;
       }
     }

     Date endTime = new Date();
     pdbcmp.writeToLog("PDB Comparison: end time " + endTime);
     pdbcmp.writeToLog("************** End *****************n");
     pdbcmp.writeToLog("\n");

     System.exit(status);
   }
}