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