package nettext.file; import nettext.document.Document; import nettext.document.DocumentSection; import java.io.BufferedReader; /** * This class is the ASCII-format Document Reader * which is used by DocumentReader to read-in ASCII Documents * * Parsing Format: * "\n\n" is a new section * */ public class ASCIIDocumentReader extends DocumentReader { /** * This stores the Document to be read. */ private Document document; /** * Current Document Section being doing stuff to. */ private DocumentSection docSection; /** * Read in a Document * @return success in reading the Document */ public Document readDocument() { document = new Document(); BufferedReader br; docSection = null; String sCurrSect = ""; String sCurrSectName = ""; String sCurrText; int iSectionOrder = 0; br = new BufferedReader(this.getInFileReader()); try { sCurrText = br.readLine(); if (sCurrText == null) { return null; } } catch (java.io.IOException ioe) { //readLine failed, returning blank document return null; } document.setDocumentName(sCurrText); try { while ((sCurrText = br.readLine()) != null) { // Checking current line for "..." if (sCurrText.startsWith("...")) { //Creating a new section if (docSection == null) { docSection = new DocumentSection(document.getDocumentId()); sCurrSectName = sCurrText.substring(3); continue; } else { //Adding section and resetting section info docSection.setSectionOrder(iSectionOrder); docSection.setSectionName(sCurrSectName); docSection.setSectionContents(sCurrSect); docSection.updateTimeStamp(); docSection.setUserName("Server"); document.addSection(docSection, iSectionOrder++); //Done adding section, creating new docsec docSection = new DocumentSection(document.getDocumentId()); sCurrSectName = sCurrText.substring(3); sCurrSect = ""; continue; } } sCurrSect += sCurrText; } // No more lines but we need to add current section if (docSection != null) { docSection.setSectionOrder(iSectionOrder); docSection.setSectionName(sCurrSectName); docSection.setSectionContents(sCurrSect); docSection.updateTimeStamp(); docSection.setUserName("Server"); document.addSection(docSection, iSectionOrder++); } } catch (java.io.IOException ioe) { //readLine failed, returning what we have of the document document.setDocumentLoaded(true); return document; } document.setDocumentLoaded(true); return document; } }