home *** CD-ROM | disk | FTP | other *** search
/ Palm Utilities / Palm_Utilities_CD-ROM_2001_2001.iso / files / utils text / MakeDocJ 3.3 / MakeDocJ.exe / Source / DatabaseHeader.java < prev    next >
Encoding:
Java Source  |  2000-05-31  |  11.0 KB  |  385 lines

  1. /*
  2.  * DatabaseHeader.java
  3.  *
  4.  * Copyright 2000 by BRiSK Software,
  5.  * 8702 Switzer Road, Overland Park, KS 66214
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of BRiSK Software. ("Confidential Information").
  10.  * You shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with BRiSK Software.
  13.  *
  14.  * $Id$
  15.  */
  16.  
  17. import java.io.*;
  18. import java.util.*;
  19.  
  20. /**
  21.  * <code>DatabaseHeader</code> class is used to read/write Palm database header records
  22.  * NOTE: Information about the various fields was taken from <a href="www.roadcoders.com/pdb.html">Road Coders</a>
  23.  * @author Jeffrey A. Krzysztow
  24.  * @author Pat Beirne
  25.  * @version 1.3.1
  26.  */
  27. public class DatabaseHeader {
  28.     /**
  29.      * database ID for TealDoc PilotDoc reader
  30.      * @since 1.0
  31.      */
  32.     public final static int TealDocID = 0x546c4463;    // 'TlDc'
  33.  
  34.     /**
  35.      * database ID for generic PilotDoc reader
  36.      * @since 1.0
  37.      */
  38.     public final static int ReaderID = 0x52454164;    // 'REAd'
  39.  
  40.     /**
  41.      * indicates type of data stored in database
  42.      * @since 1.0
  43.      */
  44.     public final static int TEXt = 0x54455874;        // 'TEXt'
  45.  
  46.     /**
  47.      * name of database
  48.      * @since 1.0
  49.      */
  50.     public String name = "";                    // 32
  51.  
  52.     /**
  53.      * attribute of database
  54.      *    0x0002 Read-Only
  55.      *    0x0004 Dirty AppInfoArea
  56.      *    0x0008 Backup this database (i.e. no conduit exists)
  57.      *    0x0010 (16 decimal) Okay to install newer over existing copy, if present on PalmPilot
  58.      *    0x0020 (32 decimal) Force the PalmPilot to reset after this database is installed
  59.      *    0x0040 (64 decimal) Don't allow copy of file to be beamed to other Pilot.
  60.      * @since 1.0
  61.      */
  62.     public short attribute = 0;                    // 2 Word
  63.  
  64.     /**
  65.      * version of database
  66.      * defined by application
  67.      * @since 1.0
  68.      */
  69.     public short version = 0;                    // 2 Word
  70.  
  71.     /**
  72.      * creation date of database
  73.      * Expressed as the number of seconds since January 1, 1904.
  74.      * <b>The database will not install if this value is zero.</b>
  75.      * @since 1.0
  76.      */
  77.     public int creationDate = 0;                // 4 DWord
  78.  
  79.     /**
  80.      * last modification date of database
  81.      * Expressed as the number of seconds since January 1, 1904.
  82.      * <b>The database will not install if this value is zero.</b>
  83.      * @since 1.0
  84.      */
  85.     public int modificationDate = 0;            // 4 DWord
  86.  
  87.     /**
  88.      * last date database was HotSynced
  89.      * Expressed as the number of seconds since January 1, 1904.
  90.      * The database will install if this value is zero.
  91.      * @since 1.0
  92.      */
  93.     public int lastBackupDate = 0;                // 4 DWord
  94.  
  95.     /**
  96.      * modification number
  97.      * Set to zero
  98.      * @since 1.0
  99.      */
  100.     public int modificationNumber = 0;            // 4 DWord
  101.  
  102.     /**
  103.      * appInfoID
  104.      * The byte number in the PDB file (counting from zero) at
  105.      * which the AppInfoArea is located. This must be the first
  106.      * entry in the Data portion of the PDB file. If this
  107.      * database does not have an AppInfoArea, set this value to
  108.      * zero.
  109.      * @since 1.0
  110.      */
  111.     public int appInfoID = 0;                    // 4 DWord
  112.  
  113.     /**
  114.      * sortInfoID
  115.      * The byte number in the PDB file (counting from zero) at
  116.      * which the SortInfoArea is located. This must be placed
  117.      * immediately after the AppInfoArea, if one exists, within
  118.      * the Data portion of the PDB file. If this database does
  119.      * not have a SortInfoArea, set this value to zero. Do not
  120.      * use this.
  121.      * @since 1.0
  122.      */
  123.     public int sortInfoID = 0;                    // 4 DWord
  124.  
  125.     /**
  126.      * ID of application that can update database
  127.      * @since 1.0
  128.      */
  129.     public int typeID = 0;                        // 4 DWord
  130.  
  131.     /**
  132.      * ID of application
  133.      * @since 1.0
  134.      */
  135.     public int creatorID = 0;                    // 4 DWord
  136.  
  137.     /**
  138.      * uniqueIDSeed
  139.      * This is used to generate the Unique ID number of
  140.      * subsequent records. This should be set to zero.
  141.      * @since 1.0
  142.      */
  143.     public int uniqueIDSeed = 0;                // 4 DWord
  144.  
  145.     /**
  146.      * nextRecordListID
  147.      * Set this to zero. The element is used only in the
  148.      * in-memory representation of a PDB file, but exists
  149.      * in the external version for consistency.
  150.      * @since 1.0
  151.      */
  152.     public int nextRecordListID = 0;            // 4 DWord
  153.  
  154.     /**
  155.      * number of records in database
  156.      * @since 1.0
  157.      */
  158.     public short numRecords = 0;                // 2 Word
  159.                                                 // 78 bytes for Database Header
  160.  
  161.     /**
  162.      * Default constructor
  163.      * @since 1.1
  164.      */
  165.     DatabaseHeader() {
  166.         // find the current time, and convert into number of seconds since Jan 1, 1904
  167.         Calendar cl = Calendar.getInstance();
  168.         Date now = new Date();
  169.         creationDate = (int)((now.getTime() + cl.get(Calendar.ZONE_OFFSET) + cl.get(Calendar.DST_OFFSET))
  170.             / 1000 + SecondsSince1904);
  171.         modificationDate = creationDate;
  172.     }
  173.  
  174.     /**
  175.      * The size of the DatabaseHeader in bytes
  176.      * @returns the number of bytes in the DatabaseHeader
  177.      * @since 1.0
  178.      */
  179.     public static int getSize() {
  180.         return(78);
  181.     }
  182.  
  183.     /**
  184.      * Reads the DatabaseHeader from the DataInput
  185.      * @param the DataInput to read from
  186.      * @since 1.0
  187.      */
  188.     public void read(DataInput di) throws IOException {
  189.         byte[] b = new byte[32];
  190.         di.readFully(b);
  191.         name = new String(b);
  192.         int i = name.indexOf(0);
  193.         name = name.substring(0, i);
  194.         attribute = di.readShort();
  195.         version = di.readShort();
  196.         creationDate = di.readInt();
  197.         modificationDate = di.readInt();
  198.         lastBackupDate = di.readInt();
  199.         modificationNumber = di.readInt();
  200.         appInfoID = di.readInt();
  201.         sortInfoID = di.readInt();
  202.         typeID = di.readInt();
  203.         creatorID = di.readInt();
  204.         uniqueIDSeed = di.readInt();
  205.         nextRecordListID = di.readInt();
  206.         numRecords = di.readShort();
  207.     }
  208.  
  209.     /**
  210.      * Writes the DatabaseHeader to the DataInput
  211.      * @param the DataInput to write to
  212.      * @since 1.0
  213.      */
  214.     public void write(DataOutput out) throws IOException {
  215.         byte[] b = new byte[32];
  216.         b[b.length - 1] = 0;
  217.         byte[] bName = name.getBytes();
  218.         for(int x = 0; x < b.length - 1; x++) {
  219.             if(x < bName.length) {
  220.                 b[x] = bName[x];
  221.             }
  222.             else {
  223.                 b[x] = 0;
  224.             }
  225.         }
  226.         if(bName.length >= b.length) {
  227.             b[b.length - 2] = (byte)'.';
  228.             b[b.length - 3] = (byte)'.';
  229.             b[b.length - 4] = (byte)'.';
  230.         }
  231.         out.write(b);
  232.         out.writeShort(attribute);
  233.         out.writeShort(version);
  234.         out.writeInt(creationDate);
  235.         out.writeInt(modificationDate);
  236.         out.writeInt(lastBackupDate);
  237.         out.writeInt(modificationNumber);
  238.         out.writeInt(appInfoID);
  239.         out.writeInt(sortInfoID);
  240.         out.writeInt(typeID);
  241.         out.writeInt(creatorID);
  242.         out.writeInt(uniqueIDSeed);
  243.         out.writeInt(nextRecordListID);
  244.         out.writeShort(numRecords);
  245.     }
  246.  
  247.     /**
  248.      * override Object.toString()
  249.      * @since 1.0
  250.      */
  251.     public String toString()  {
  252.         char[] creatorIDa = new char[4];
  253.         creatorIDa[0] = (char)((creatorID >> 24) & 0xff);
  254.         creatorIDa[1] = (char)((creatorID >> 16) & 0xff);
  255.         creatorIDa[2] = (char)((creatorID >> 8) & 0xff);
  256.         creatorIDa[3] = (char)(creatorID & 0xff);
  257.         char[] typeIDa = new char[4];
  258.         typeIDa[0] = (char)((typeID >> 24) & 0xff);
  259.         typeIDa[1] = (char)((typeID >> 16) & 0xff);
  260.         typeIDa[2] = (char)((typeID >> 8) & 0xff);
  261.         typeIDa[3] = (char)(typeID & 0xff);
  262.         return ">> DatabaseHeader <<"
  263.             + "\nname = `" + name + "`"
  264.             + "\nattribute = " + attribute
  265.             + "\nversion = " + version
  266.             + "\ncreationDate = " + creationDate
  267.             + "\nmodificationDate = " + modificationDate
  268.             + "\nlastBackupDate = " + lastBackupDate
  269.             + "\nmodificationNumber = " + modificationNumber
  270.             + "\nappInfoID = " + appInfoID
  271.             + "\nsortInfoID = " + sortInfoID
  272.             + "\ntypeID = " + typeID + " 0x" + Integer.toHexString(typeID) + " `" + new String(typeIDa) + "`"
  273.             + "\ncreatorID = " + creatorID + " 0x" + Integer.toHexString(creatorID) + " `" + new String(creatorIDa) + "`"
  274.             + "\nuniqueIDSeed = " + uniqueIDSeed + "\nnextRecordListID = " + nextRecordListID
  275.             + "\nnumRecords = " + numRecords;
  276.     }
  277.  
  278.     /**
  279.      * Sets the modification date member to now
  280.      * @since 1.2
  281.      */
  282.     public void setModificationDate() {
  283.         // find the current time, and convert into number of seconds since Jan 1, 1904
  284.         Calendar cl = Calendar.getInstance();
  285.         Date now = new Date();
  286.         modificationDate = (int)((now.getTime() + cl.get(Calendar.ZONE_OFFSET) + cl.get(Calendar.DST_OFFSET))
  287.             / 1000 + SecondsSince1904);
  288.     }
  289.  
  290.     /**
  291.      * returns the modification date as a Date
  292.      * @returns Date indicating modification date
  293.      * @since 1.2
  294.      */
  295.     public Date getModificationDate() {
  296.         Calendar cl = Calendar.getInstance();
  297.         long temp = new Integer(modificationDate).longValue();
  298.         if(temp < 0) {
  299.             temp += ((long) 1) << 32;
  300.         }
  301.         return new Date((temp - SecondsSince1904) * 1000 - cl.get(Calendar.ZONE_OFFSET) - cl.get(Calendar.DST_OFFSET));
  302.     }
  303.  
  304.     /**
  305.      * returns the creation date as a Date
  306.      * @returns Date indicating creation date
  307.      * @since 1.2
  308.      */
  309.     public Date getCreationDate() {
  310.         Calendar cl = Calendar.getInstance();
  311.         long temp = new Integer(creationDate).longValue();
  312.         if(temp < 0) {
  313.             temp += ((long) 1) << 32;
  314.         }
  315.         return new Date((temp - SecondsSince1904) * 1000 - cl.get(Calendar.ZONE_OFFSET) - cl.get(Calendar.DST_OFFSET));
  316.     }
  317.  
  318.     /**
  319.      * Seconds since 1904
  320.      * @see SecondsSince1904
  321.      * @since 1.2
  322.      */
  323.     private final static long SecondsSince1904 = 2082844800;
  324.  
  325.     /**
  326.      * converts the String representation of typeID to an int representation
  327.      * @param typeID String representation of typeID
  328.      * @return int representation of typeID
  329.      * @since 1.3
  330.      */
  331.     public static int convertStringToTypeID(String typeID) {
  332.         byte typeIDa[] = typeID.getBytes();
  333.         return ((0xff & typeIDa[0]) << 24)
  334.             + ((0xff & typeIDa[1]) << 16)
  335.             + ((0xff & typeIDa[2]) << 8)
  336.             + (0xff & typeIDa[3]);
  337.     }
  338.  
  339.     /**
  340.      * converts the int representation of typeID to a String representation
  341.      * @param typeID int representation of typeID
  342.      * @return String representation of typeID
  343.      * @since 1.3
  344.      */
  345.     public static String convertIntToStringTypeID(int typeID)     {
  346.         char[] typeIDa = new char[4];
  347.         typeIDa[0] = (char)((typeID >> 24) & 0xff);
  348.         typeIDa[1] = (char)((typeID >> 16) & 0xff);
  349.         typeIDa[2] = (char)((typeID >> 8) & 0xff);
  350.         typeIDa[3] = (char)(typeID & 0xff);
  351.         return new String(typeIDa);
  352.     }
  353.  
  354.     /**
  355.      * converts the String representation of creatorID to an int representation
  356.      * @param creatorID String representation of creatorID
  357.      * @return int representation of creatorID
  358.      * @since 1.3
  359.      */
  360.     public static int convertStringToCreatorID(String creatorID) {
  361.         byte creatorIDa[] = creatorID.getBytes();
  362.         return ((0xff & creatorIDa[0]) << 24)
  363.             + ((0xff & creatorIDa[1]) << 16)
  364.             + ((0xff & creatorIDa[2]) << 8)
  365.             + (0xff & creatorIDa[3]);
  366.     }
  367.  
  368.     /**
  369.      * converts the int representation of creatorID to a String representation
  370.      * @param creatorID int representation of creatorID
  371.      * @return String representation of creatorID
  372.      * @since 1.3
  373.      */
  374.     public static String convertIntToStringCreatorID(int creatorID)     {
  375.         char[] creatorIDa = new char[4];
  376.         creatorIDa[0] = (char)((creatorID >> 24) & 0xff);
  377.         creatorIDa[1] = (char)((creatorID >> 16) & 0xff);
  378.         creatorIDa[2] = (char)((creatorID >> 8) & 0xff);
  379.         creatorIDa[3] = (char)(creatorID & 0xff);
  380.         return new String(creatorIDa);
  381.     }
  382.  
  383. }
  384.  
  385.