/* * GNTWriter.java * * Created on November 15, 2003, 10:27 PM */ package gnt; import palm.conduit.*; import java.io.*; import java.util.*; /** * * @author Mike Kirschman */ public class GNTWriter{ // Delimiters String fieldDelimiter = "&&GNTFLDDELIM&&"; String GNTBegin = "&&GNTBEG&&"; String GNTEnd = "&&GNTEND&&"; String recBeginner = "&&GNTRECBEGIN&&"; String recDelimiter = "&&GNTRECDELIM&&"; String catListBegin = "&&GNTCATBEG&&"; String catListEnd = "&&GNTCATEND&&"; String catBeginner = "&&GNTCATBEGIN&&"; String catDelimiter = "&&GNTCATDELIM&&"; // Output Streams FileOutputStream out; PrintWriter printWriter; // Miscellaneous File dataFile = null; byte[] fileBytes; String wholeFile = new String(""); String allRecords = null; // String allCategories = null; /** Creates a new instance of GNTWriter */ public GNTWriter(){ } /** Reads the Categories out of the PC's data file. * @param String The file's path. */ public Vector readCategories(String pcFilePath){ // Vector pcCategories = new Vector(); // FileInputStream in; // StringTokenizer lineTokenizer, multiFieldTokenizer; // // boolean allRecordsRead = false; // String nameValue, wholeFile; // String multiFieldString = "temp string"; // int curIndex; // int begIndex = 0; // int bytesRead = 0; // int fileSize = 0; // // // //fill up the pcCategories Vector with the categories stored locally // try{ // // if(dataFile == null || fileBytes == null) { // // dataFile = new File(pcFilePath); // // // check that the file exists. If not return an empty vector // if(!dataFile.exists()) return pcCategories; // in = new FileInputStream(dataFile); // // // Determine the size of the file // fileSize = (int)dataFile.length(); // // // allocate an array for the file's bytes // fileBytes = new byte[fileSize]; // // // Read in the file // bytesRead = in.read(fileBytes); // // // close the input stream // in.close(); // // // If you didn't get all the bytes, throw an exception // if(bytesRead < fileSize) { // throw new IOException("bytes Read doesn't match fileSize"); // } // } // // // convert the bytes into Text // wholeFile = new String(fileBytes, 0, fileBytes.length); // // // Break out the categories using the delimiters // allCategories = wholeFile.substring(wholeFile.indexOf(catListBegin) + // catListBegin.length(), wholeFile.indexOf(catListEnd)); // // // Get setup to read the categories 1 by 1 // multiFieldTokenizer = new StringTokenizer(allCategories, catDelimiter); // int theindex = allCategories.indexOf(catDelimiter); // int theoldindex = 0; // // // loop through all the categories // while(theindex != -1) { // // // Grab one category // multiFieldString = allCategories.substring(theoldindex, theindex); // // // make sure this entry has an ID // if(multiFieldString.indexOf("ID=") != -1 ) { // // // chop off the prelimiter // multiFieldString = multiFieldString.substring(multiFieldString.indexOf("ID")); // // // Create a new category object to hold this entry // Category pcCategory = new Category(); // // // Get set to read each field of the category entry 1 by 1 // lineTokenizer = new StringTokenizer(multiFieldString, fieldDelimiter); // // // Cycle through each field in the entry // while(lineTokenizer.hasMoreElements()) { // nameValue = lineTokenizer.nextToken(); // // // and populate the category object you just created // populateCategory(nameValue, pcCategory); // } // // // once the category is created & populated, add it to the category vector // pcCategories.addElement(pcCategory); // // } // // // iterate the indices // theoldindex = theindex + catDelimiter.length() ; // theindex = allCategories.indexOf(catDelimiter, theoldindex); // } // // // cut off any excess allocation in the vector // pcCategories.trimToSize(); // // try { // return pcCategories; // } catch(Throwable t) { // t.printStackTrace(); // Log.abortSync(); // } // // } catch(Throwable t){ // t.printStackTrace(); // Log.out("Local Database Not Found. It was created."); // } return null; } /** Reads the Records out of the PC's data file. * Takes the file's path as an argument. * * @modified Mike Kirschman */ public Vector readRecords(String pcFilePath){ Vector localRecords = new Vector(); // Vector pcCategories = new Vector(); FileInputStream in; StringTokenizer lineTokenizer, multiFieldTokenizer; boolean allRecordsRead = false; String nameValue; String multiFieldString = "temp"; int curIndex; int begIndex = 0; int bytesRead = 0; int fileSize = 0; //fill up the localRecords Vector with the records stored locally try{ if(dataFile == null || fileBytes == null) { dataFile = new File(pcFilePath); // check that the file exists. If not return an empty vector if(!dataFile.exists()) return localRecords; in = new FileInputStream(dataFile); // Determine the size of the file fileSize = (int)dataFile.length(); // allocate an array for the file's bytes fileBytes = new byte[fileSize]; // Read in the file bytesRead = in.read(fileBytes); // close the input stream in.close(); // If you didn't get all the bytes, throw an exception if(bytesRead < fileSize) { throw new IOException("bytesRead doesn't match fileSize"); } } // convert the bytes into Text wholeFile = new String(fileBytes, 0, fileBytes.length); // Break out the records using the delimiters allRecords = wholeFile.substring(wholeFile.indexOf(GNTBegin) + GNTBegin.length(), wholeFile.lastIndexOf(GNTEnd)); // Get setup to read the records 1 by 1 int theindex = allRecords.indexOf(recDelimiter) + recDelimiter.length(); int theoldindex = 0; // loop through all the records while(theindex != -1) { // Grab one category multiFieldString = allRecords.substring(theoldindex, theindex); // Create a new record object to hold this entry GNTRecord localRecord = new GNTRecord(); // delegate the reading of the record to the record localRecord.readRecord(multiFieldString, fieldDelimiter); // once the record is created & populated add it to the localRecords vector localRecords.addElement(localRecord); // iterate the indices theoldindex = theindex + recDelimiter.length() ; theindex = allRecords.indexOf(recDelimiter, theoldindex); } // cut off any excess allocation in the vector localRecords.trimToSize(); try { return localRecords; } catch(Throwable t) { t.printStackTrace(); Log.abortSync(); } } catch(Throwable t){ t.printStackTrace(); Log.out("Local Database Not Found. It was created."); } return null; } /** Function used to write Category & Record information to the PC, using the * file path specified. This function is used to write both the backup file * and the main data file. */ public void writeData(String pcFilePath, Vector records, Vector categories) { try { // initialize the output streams out = new FileOutputStream(pcFilePath); printWriter = new PrintWriter(out); // write the categories first, then the recs // if(categories != null) { writeCategories(categories); } if(records != null) { writeRecords(records); } // flush the streams & shut them down printWriter.flush(); printWriter.close(); out.flush(); out.close(); } catch(Throwable t) { t.printStackTrace(); } } /** Converts a vector of categories to text and * writes them to the class's printwriter object */ public void writeCategories(Vector categories) throws IOException{ // if (categories != null){ // // Category pcCategory; // categories.trimToSize(); // // printWriter.println(catListBegin); // // for (Iterator i = categories.iterator(); i.hasNext();){ // // printWriter.print(recBeginner); // // pcCategory = (Category) i.next(); // // printWriter.print("ID=" + pcCategory.getId() + fieldDelimiter); // // printWriter.print("INDEX=" + pcCategory.getIndex() + fieldDelimiter); // // printWriter.print("ISMODIFIED=" + pcCategory.isModified() + fieldDelimiter); // // printWriter.print("NAME=" + pcCategory.getName() + fieldDelimiter); // // printWriter.println(catDelimiter); // // } // // printWriter.println(catListEnd); // // printWriter.flush(); // out.flush(); // } } /** Converts a vector of records to text and * writes them to the class's printwriter object * * @author Rewritten by Mike Kirschman */ public void writeRecords(Vector records) throws IOException{ if (records != null){ records.trimToSize(); GNTRecord localRecord; // Print the Opening delimeter printWriter.println(GNTBegin); GNTRecord lastRec = null; for (Iterator i = records.iterator(); i.hasNext();){ // Get the next record and unwrap it localRecord = (GNTRecord) i.next(); // hacking to stop duplicate records from being written. if (localRecord.equals(lastRec)) continue; // Write the record: recBeginner + record data + recDelimeter + '\n' printWriter.print(recBeginner); localRecord.printRecord(printWriter, fieldDelimiter); printWriter.println(recDelimiter); lastRec = localRecord; } // Print the Closing delimeter printWriter.println(GNTEnd); // Flush the PrintWriter and the FileOutputStream printWriter.flush(); out.flush(); } } /** Takes name/value pair and populates corresponding * field of the Category object being passed in. */ private void populateCategory(String nameValue, Category category) { // int index = nameValue.indexOf("="); // String name, value; // if (index != -1) { // name = nameValue.substring(0, index); // value = nameValue.substring(index + 1); // // if (value.equals("null")){} //skip over null values // else if (name.equals("ID")) // category.setId(Integer.parseInt(value)); // // else if (name.equals("INDEX")) // category.setIndex(Integer.parseInt(value)); // // else if (name.equals("ISMODIFIED") && value.equals("true")) // category.setIsModified(true); // // else if (name.equals("NAME")) // category.setName(value); // } } /** Extracts category name from a given record object */ private String getCategoryName(Record record, Vector categories) { // // Category category; // // for (Iterator i = categories.iterator(); i.hasNext();){ // category = (Category) i.next(); // if (category.getIndex() == record.getCategoryIndex()) // return category.getName(); // } // return null; } /** Creates and writes a text file of Archive Records on the PC. * @param archiveFilePath The complete file path and name of the text file * to contain the Archive Records. * @param archivedRecords A vector of the Archive Records to be written to the text file * @param categories A vector of synchronized categories */ public void writeArchiveRecords(String archiveFilePath, Vector archivedRecords, Vector categories) throws IOException{ //this is a hashtable filled with Hashtable archiveFiles = new Hashtable(); //vectors of records to archive Vector recordVector; // Category category; // String categoryName; String filePath; File archiveFile; for (Iterator i = archivedRecords.iterator(); i.hasNext();){ Record archivedRecord = (Record) i.next(); //read the name of the category for the archived record // categoryName = getCategoryName(archivedRecord, categories); // if (categoryName.equals(null)){ // // //there was a problem reading the correct category name. Log // //error and write out an entry to the Unfiled record archive // Log.out("Category Name is null. Archiving Record as 'Unfiled'"); // categoryName = "Unfiled"; // } // if (archiveFiles.containsKey(categoryName)){ // // // the vector of archived records for this category already exists so we // // pull the Vector & add this archive record to it // recordVector = (Vector)archiveFiles.get(categoryName); // recordVector.addElement(archivedRecord); // // } else { // // // if the vector of archived records for this category does not exist, // // we make a new vector, slap the archive record in it & add it to the // // hash // recordVector = new Vector(); // recordVector.addElement(archivedRecord); // archiveFiles.put(categoryName, recordVector); // } // } // for(Iterator i = categories.iterator(); i.hasNext();){ // // category = (Category) i.next(); // categoryName = category.getName(); // // if (archiveFiles.containsKey(categoryName)){ // // //the vector of archived records for this category exists. // filePath = archiveFilePath + categoryName + ".MPA"; // // //now that the Hastable is filled up read in the archive // //file written previously for the particular category // archiveFile = new File(filePath); // // if (archiveFile.exists()){ // // //append the new archived records onto the existing file // dataFile = null; // archivedRecords = readRecords(filePath); // recordVector = (Vector)archiveFiles.get(categoryName); // // for (int k = 0; k < recordVector.size(); k++){ // archivedRecords.addElement(recordVector.elementAt(k)); // } // // } else { //file doesn't exist yet // archivedRecords = (Vector)archiveFiles.get(categoryName); // } // // writeData(filePath, archivedRecords, null); // } } } }