1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
31 public class pdbcomparison
34 private String LOGTAG
="LOGFILE";
35 private String OUTTAG
="OUTFILE";
36 private String LISTTAG
="LISTFILE";
37 private String PDBTAG1
="PDBNAME1";
38 private String PDBTAG2
="PDBNAME2";
40 private String OUTFILE
="pdbcomparison.out";
41 private String LOGFILE
="pdbcomparison.log";
43 private String pdbarr1
[];
44 private String pdbarr2
[];
54 public void pdbcomparison()
59 * Prints the command line arguments for this class
68 String str
= new String();
69 str
+= "********************************************************\n";
70 str
+= " java pdbcomparison.java <propFile> \n";
71 str
+= " where propFile is name of Property File...\n";
72 str
+= "********************************************************\n";
74 System
.out
.println(str
);
79 * This method, read the Property file and validates the
80 * entries in that file, and accordingly sets the log file
81 * output file and updates the array pdbarr1 and pdbarr2 with
82 * list of pdb's to be compared.
84 * @param propFile Property filename which list the log/outputfile/list/pdb
89 public void parsePropertyFile(String propFile
)
91 Properties defaultProps
= new Properties();
94 FileInputStream in
= new FileInputStream(propFile
);
95 defaultProps
.load(in
);
97 } catch (IOException e
) {
98 System
.out
.println("Could not open Property File " + propFile
);
103 String logFile
= defaultProps
.getProperty(this.LOGTAG
);
104 String outFile
= defaultProps
.getProperty(this.OUTTAG
);
105 String listFile
= defaultProps
.getProperty(this.LISTTAG
);
106 String pdbname1
= defaultProps
.getProperty(this.PDBTAG1
);
107 String pdbname2
= defaultProps
.getProperty(this.PDBTAG2
);
109 // validate all command line arguments
110 if ((listFile
== null) && ((pdbname1
== null) || (pdbname2
== null)))
112 System
.out
.println("Missing listFile or missing pdb filenames in Property file " + propFile
);
116 if (logFile
== null || logFile
.length() == 0)
117 logFile
= this.LOGFILE
;
119 if (outFile
== null || outFile
.length() == 0)
120 outFile
= this.LOGFILE
;
123 // validate log and output files
124 if (! validateAndCreateFile(logFile
)) return;
125 if (! validateAndCreateFile(outFile
)) return;
129 System
.out
.println("Output is written to log file... " + LOGFILE
);
130 if (listFile
!= null)
132 if (! checkFile(listFile
)) return;
133 populatePDBArray(listFile
);
135 if (! checkFile(pdbname1
)) return;
136 if (! checkFile(pdbname2
)) return;
137 populatePDBArray(pdbname1
, pdbname2
);
142 * This method validates if the file passed exists.
143 * If it does , then it is moved to <filename>.bak and then creates a newFile.
144 * Also validates permissions to create.
146 * @param filename name of file to be created
147 * @return true, if file could be created
148 * false, if could not.
151 private boolean validateAndCreateFile (String filename
)
153 if (filename
== null) return false;
157 f
= new File(filename
);
158 } catch (NullPointerException e
) {
159 System
.out
.println("Could not create a File object for file " + filename
);
165 String newFile
= filename
+ ".bak";
168 newF
= new File(newFile
);
169 } catch (Exception ex
) {
170 System
.out
.println("Could not get File Object instance for " + newFile
);
178 } catch ( SecurityException se
) {
179 System
.out
.println("Could not get delete " + newFile
);
185 if (! f
.renameTo(newF
))
187 System
.out
.println("Could not rename " + filename
+ " to " + newFile
);
190 } catch (SecurityException s
) {
191 System
.out
.println("SecurityException: " + s
.toString());
193 } catch (NullPointerException n
) {
194 System
.out
.println("NullPointerException: " + n
.toString());
199 if (! f
.createNewFile())
201 System
.out
.println("Could not create " + filename
+ " Check permissions..");
204 } catch (IOException e
) {
205 System
.out
.println("IOException: " + e
.toString());
207 } catch (SecurityException s
) {
208 System
.out
.println("SecuriityException: " + s
.toString() );
219 * This method validates if the file exists and is readable
221 * @param filename name of file to be created
222 * @return true, if file exists and is readable
226 private boolean checkFile(String filename
)
228 if (filename
== null) return false;
232 f
= new File(filename
);
233 } catch (NullPointerException e
) {
234 System
.out
.println("Could not create a File object for file " + filename
);
240 System
.out
.println("File " + filename
+ " does not exist... ");
246 System
.out
.println("Cannot read file " + filename
);
255 * This method populates the pdb arrays with the names of the pdbs to
256 * compare. Ths listFile lists a series of entries, wherein each
257 * line indicates the PDB names to be compared.
258 * <pdbname1>=<pdbname2>
260 * @param listFile name of the listfile
264 private void populatePDBArray(String listFile
)
266 // open ListFile and populate the PDB list to be compared
267 if (listFile
!= null)
269 Properties listProps
= new Properties();
271 FileInputStream in
= new FileInputStream(listFile
);
274 } catch (IOException ex
) {
275 System
.out
.println("Could not open List File " + listFile
);
279 pdbarr1
= new String
[listProps
.size()];
280 pdbarr2
= new String
[listProps
.size()];
281 Enumeration e
= listProps
.keys();
283 while (e
.hasMoreElements())
285 pdbarr1
[j
] = (String
)e
.nextElement();
286 pdbarr2
[j
] = listProps
.getProperty(pdbarr1
[j
]);
294 * This method populates the pdb arrays with the names of the pdbs to
297 * @param pdbname1 Name of 2nd PDB file to be compared
298 * @param pdbname2 Name of 2nd PDB file to be compared
302 private void populatePDBArray(String pdbname1
, String pdbname2
)
304 if (pdbname1
== null) return;
305 if (pdbname2
== null) return;
307 if ((pdbname1
!= null) && (pdbname2
!= null))
309 pdbarr1
= new String
[1];
310 pdbarr2
= new String
[1];
312 pdbarr1
[0] = pdbname1
;
313 pdbarr2
[0] = pdbname2
;
318 * This method populates the pdb arrays with the names of the pdbs to
321 * @param arrayno Array number which corresponds to the pdb array
322 * containing list of pdbs
323 * If 1 then send pdbarr1, if 2 send pdbarr2 else null
325 * @return PDB string array containing list of PDB's
328 private String
[] getPDBArray(int arrayno
)
330 if (arrayno
== 1) return pdbarr1
;
331 if (arrayno
== 2) return pdbarr2
;
337 * This method comares 2 PDB's and returns true if comparison is equal.
338 * It uses the PDB Decoder class to decode to a PDB structure and then
339 * does record comparison
341 * @param pdbname1 Name of one PDB file to be compared
342 * @param pdbname2 Name of other PDB file to be compared
344 * @return returns true if both PDB's are equal else returns false
347 private boolean comparePDB(String pdbname1
, String pdbname2
)
349 PalmDB pdb1
=null, pdb2
=null;
350 PDBDecoder decoder
= new PDBDecoder();
352 pdb1
= decoder
.parse(pdbname1
);
353 } catch (Exception e
) {
354 System
.out
.println("Could not parse PDB " + pdbname1
);
359 pdb2
= decoder
.parse(pdbname2
);
360 } catch (Exception e
) {
361 System
.out
.println("Could not parse PDB " + pdbname2
);
365 if (pdb1
.equals(pdb2
)) {
366 writeToLog("PDB " + pdbname1
+ " and PDB " + pdbname2
+ " are equal");
370 writeToLog("PDB " + pdbname1
+ " and PDB " + pdbname2
+ " are not equal");
378 * Write message to LOGFILE
380 * @param msg Message to be written to log file
384 private void writeToLog(String msg
)
386 if (msg
== null) return;
388 // Get Output Stream from Log file
389 RandomAccessFile raf
=null;
391 raf
= new RandomAccessFile(LOGFILE
, "rw");
392 } catch (Exception e
) {
393 System
.out
.println ("Could not open file " + LOGFILE
);
398 long len
= raf
.length();
400 raf
.write(msg
.getBytes());
401 raf
.write("\n".getBytes());
402 } catch (IOException e
) {
403 System
.out
.println("ERROR: Could not write to File " + LOGFILE
);
409 * Write status of comparison to OUTFILE
411 * @param status Indicates whether comparsion of PDB's PASSED or FAILED
412 * @param pdbname1 file name of pdb which was compared.
413 * @param pdbname2 file name of pdb which was compared.
418 private void writeToOutputFile(String status
, String pdbname1
, String pdbname2
)
420 if (status
== null) return;
421 if (pdbname1
== null) return;
422 if (pdbname2
== null) return;
424 String msg
= pdbname1
+ "=" + pdbname2
+ ":" + status
;
426 // Get Output Stream from Log file
427 RandomAccessFile raf
=null;
429 raf
= new RandomAccessFile(OUTFILE
, "rw");
430 } catch (Exception e
) {
431 System
.out
.println ("Could not open file " + OUTFILE
);
436 long len
= raf
.length();
439 raf
.write(msg
.getBytes());
440 raf
.write("\n".getBytes());
441 } catch (IOException e
) {
442 System
.out
.println("ERROR: Could not write to File " + OUTFILE
);
448 } catch (Exception e
) {
449 System
.out
.println("ERROR: Could not close File " + OUTFILE
);
458 * Main starting block of execution
460 * @param command line args captured in an array of Strings
464 public static void main(String args
[])
467 Date startTime
= new Date();
468 pdbcomparison pdbcmp
= new pdbcomparison();
469 int nargs
= args
.length
;
474 System
.out
.println("Incorrect no. of arguments passed...");
480 String propFile
= args
[0];
484 f
= new File(propFile
);
485 } catch (Exception e
) {
486 System
.out
.println("Exception: Could not open file " + propFile
);
491 System
.out
.println("Exception: " + propFile
+ " is not a file ");
496 System
.out
.println("Exception: Cannot open file for reading. Please check permissions ");
500 // parse Property file
501 pdbcmp
.parsePropertyFile(propFile
);
503 String pdbarr1
[] = pdbcmp
.getPDBArray(1);
504 String pdbarr2
[] = pdbcmp
.getPDBArray(2);
505 if ( (pdbarr1
== null) ||
507 (pdbarr1
.length
== 0) ||
508 (pdbarr1
.length
== 0))
510 System
.out
.println("pdbArray is empty. No PDBS to compare... \n");
515 pdbcmp
.writeToLog("************** Start *****************");
516 pdbcmp
.writeToLog("PDB Comparison: start time " + startTime
);
517 for (int i
=0; i
<pdbarr1
.length
; i
++)
519 Date pdb_startTime
= new Date();
520 pdbcmp
.writeToLog("\n");
521 pdbcmp
.writeToLog("start time " + pdb_startTime
);
522 boolean val
= pdbcmp
.comparePDB(pdbarr1
[i
], pdbarr2
[i
]);
523 Date pdb_endTime
= new Date();
524 pdbcmp
.writeToLog("end time " + pdb_endTime
);
527 pdbcmp
.writeToOutputFile("PASSED", pdbarr1
[i
], pdbarr2
[i
]);
530 pdbcmp
.writeToOutputFile("FAILED", pdbarr1
[i
], pdbarr2
[i
]);
535 Date endTime
= new Date();
536 pdbcmp
.writeToLog("PDB Comparison: end time " + endTime
);
537 pdbcmp
.writeToLog("************** End *****************n");
538 pdbcmp
.writeToLog("\n");