204 lines
4.6 KiB
Java
204 lines
4.6 KiB
Java
package nettext.file;
|
|
|
|
import java.io.PrintWriter;
|
|
import java.io.IOException;
|
|
import java.io.FileOutputStream;
|
|
|
|
import nettext.document.Document;
|
|
import nettext.document.DocumentList;
|
|
import nettext.document.DocumentListIterator;
|
|
|
|
/**
|
|
* Writer for the available list of Documents
|
|
*/
|
|
public class DocumentListWriter {
|
|
/**
|
|
* The Writer.
|
|
*/
|
|
private PrintWriter oStream;
|
|
|
|
/**
|
|
* The Writee.
|
|
*/
|
|
private String fileName;
|
|
|
|
/**
|
|
* Standard Initialization function that initializes all fields
|
|
* to default values
|
|
*@param fileN afilename
|
|
*@return boolean
|
|
*/
|
|
public boolean initialize(String fileN) {
|
|
if (fileN == null) {
|
|
return false;
|
|
}
|
|
|
|
this.fileName = fileN;
|
|
|
|
//initialize the PrintWriter
|
|
try {
|
|
oStream = new PrintWriter(new FileOutputStream(fileName));
|
|
} catch (IOException ioe) {
|
|
System.err.println("Error initializing list writer:");
|
|
System.err.println("\t" + ioe.getMessage());
|
|
System.err.flush();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Flush/Close Buffers/Streams
|
|
*/
|
|
public void cleanUp() {
|
|
oStream.flush();
|
|
oStream.close();
|
|
|
|
oStream = null;
|
|
}
|
|
|
|
/**
|
|
* Writes the document list if possible.
|
|
*
|
|
* @param list a <code>DocumentList</code> value
|
|
* @return a <code>boolean</code> value
|
|
*/
|
|
public boolean writeDocumentList(final DocumentList list) {
|
|
DocumentListIterator dli = list.getIterator();
|
|
|
|
if (oStream == null) {
|
|
return false;
|
|
}
|
|
|
|
//Start file
|
|
if (!startFile()) { return false; }
|
|
|
|
//Start list
|
|
if (!startList(list)) { return false; }
|
|
|
|
//Write documents
|
|
while (dli.hasNext()) {
|
|
if (!writeDocument(dli.next())) { return false; }
|
|
}
|
|
|
|
//End list
|
|
if (!endList()) { return false; }
|
|
|
|
//End file
|
|
if (!endFile()) { return false; }
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Write the things at the start of the File
|
|
*@return boolean
|
|
*/
|
|
public boolean startFile() {
|
|
try {
|
|
//Print out the start of the document.
|
|
this.writeOut("<?xml version='1.0'?>");
|
|
this.nl();
|
|
this.nl();
|
|
} catch (IOException ioe) {
|
|
System.err.println("IO EXCEPTION!!! BAD!!!!, startFile()-NTOH");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Write out the things related to ending the file
|
|
* @return a <code>boolean</code> value
|
|
*/
|
|
public boolean endFile() {
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Writes the start of the document list into the file.
|
|
*
|
|
* @param list a <code>DocumentList</code> value
|
|
* @return a <code>boolean</code> value
|
|
*/
|
|
public boolean startList(final DocumentList list) {
|
|
try {
|
|
this.writeOut("<" + XMLSettings.DOCUMENT_LIST);
|
|
this.writeOut(" numDocuments=\"" + list.size() + "\"");
|
|
this.writeOut(">");
|
|
this.nl();
|
|
} catch (IOException ioe) {
|
|
System.err.println("IO Exception!! startList() - NTOH");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Writes out the end of the document list into the file.
|
|
*
|
|
* @return a <code>boolean</code> value
|
|
*/
|
|
public boolean endList() {
|
|
try {
|
|
this.writeOut("</" + XMLSettings.DOCUMENT_LIST + ">");
|
|
this.nl();
|
|
} catch (IOException ioe) {
|
|
System.err.println("IO Exception!! endList() - NTOH");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Write out the document.
|
|
* @param toWrite a <code>Document</code> value
|
|
* @return a <code>boolean</code> value
|
|
*/
|
|
public boolean writeDocument(Document toWrite) {
|
|
try {
|
|
// Figure out the writeOut (the docName shouldn't be fileName!)
|
|
this.writeOut("\t<"
|
|
+ XMLSettings.DOCUMENT_KEY
|
|
+ " name=\""
|
|
+ toWrite.getDocumentName()
|
|
+ "\" id=\""
|
|
+ toWrite.getDocumentId()
|
|
+ "\" />");
|
|
this.nl();
|
|
} catch (IOException ioe) {
|
|
System.err.println("IO Exception!! startDocument() - NTOH");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Helper function
|
|
* Writes the String toWrite to the stdOut
|
|
*@param toWrite toWrite
|
|
*@throws IOException exception
|
|
*/
|
|
private void writeOut(String toWrite) throws IOException {
|
|
oStream.print(toWrite);
|
|
oStream.flush();
|
|
}
|
|
|
|
/**
|
|
* Helper function
|
|
* Writes a newline to the file
|
|
*@throws IOException exception
|
|
*/
|
|
private void nl() throws IOException {
|
|
//flush once for the bulk, twice for the left-overs
|
|
oStream.print("\n");
|
|
oStream.flush();
|
|
}
|
|
|
|
}
|