1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 
29 import java.io.RandomAccessFile;
30 import java.io.IOException;
31 
32 
33 /**
34  *  <p>Provides functionality to decode a pdb formatted file into
35  *  a <code>PalmDB</code> object given a file input stream</p>
36  *
37  *  <p>Sample usage:</p>
38  *
39  *  <p><blockquote><pre>
40  *     PDBDecoder decoder = new PDBDecoder("sample.pdb");
41  *     PalmDB palmDB = decoder.parse();
42  *  </pre></blockquote></p>
43  *
44  *  <p>Refer to the
45  *  <a href="http://starlite.eng/zensync/eng/converters/palmfileformats.pdf">
46  *  Palm file format specification</a> for details on the pdb format.</p>
47  *
48  *  <p>This decoder has the following assumptions on the pdb file ...</p>
49  *  <ol>
50  *  <li><p>There is only one RecordList section in the pdb.</p></li>
51  *  <li><p>The record indices in the RecordList are sorted in order, i.e. the
52  *      first record index refers to record 0, and so forth.</p></li>
53  *  <li><p>The raw records in the record section are sorted as well in order,
54  *      i.e. first record comes ahead of second record, etc.</p></li>
55  *  </ol>
56  *
57  *  Other decoders assume these as well.
58  *
59  *  @author    Herbie Ong
60  *  @see    PalmDB
61  *  @see    PDBHeader
62  *
63  *  @author    Herbie Ong
64  */
65 
66 public final class PDBDecoder {
67 
68     /**
69      *  <p>This method decodes a pdb file into a PalmDB object.</p>
70      *
71      *  <p>First, read in the header data using <code>PDBHeader</code>'s
72      *  <code>read</code> method</p>.  Next, read in the record list
73      *  section.  Store the record offsets for use when parsing the records.
74      *  Based on these offsets, read in each record's bytes and store
75      *  each in a <code>Record</code> object.  Lastly, create a
76      *  <code>PalmDB</code> object with the read in <code>Record</code>s.
77      *
78      *  @param   fileName    pdb file name
79      *  @throws   IOException    if I/O error occurs
80      */
81 
82     public PalmDB parse(String fileName) throws IOException {
83 
84         RandomAccessFile file = new RandomAccessFile(fileName, "r");
85 
86         // read the pdb header
87         PDBHeader header = new PDBHeader();
88         header.read(file);
89 
90         Record recArray[] = new Record[header.numRecords];
91 
92         if (header.numRecords != 0) {
93 
94             // read in the record indices + offsets
95 
96             int recOffset[] = new int[header.numRecords];
97 
98             for (int i = 0; i < header.numRecords; i++) {
99 
100                 recOffset[i] = file.readInt();
101                 int attr = file.readInt();    // read in attribute.
102             }
103 
104             // read the records
105 
106             int len = 0;
107             byte[] bytes = null;
108 
109             int lastIndex = header.numRecords - 1;
110 
111             for (int i = 0; i < lastIndex; i++) {
112 
113                 file.seek(recOffset[i]);
114                 len = recOffset[i+1] - recOffset[i];
115                 bytes = new byte[len];
116                 file.readFully(bytes);
117                 recArray[i] = new Record(bytes);
118             }
119 
120             // last record
121             file.seek(recOffset[lastIndex]);
122             len = (int) file.length() - recOffset[lastIndex];
123             bytes = new byte[len];
124             file.readFully(bytes);
125             recArray[lastIndex] = new Record(bytes);
126         }
127 
128         file.close();
129 
130         // create PalmDB and return it
131         PalmDB pdb = new PalmDB(header.pdbName, recArray);
132         return pdb;
133     }
134 }
135 
136