import java.io.*; import java.util.*; //import BufferLoad; //import Title; //import Folder; /** * This class represents a document which is intended to * display the contents of a directory, probably located * on the web-server where the document came from. * The document may have a title and some text as well. * * * @see FaqItem, Question, Answer * @author http://bumble.sf.net */ public class FolderDocument extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private Folder folder; //-------------------------------------------- private Date startTime; //-------------------------------------------- private Date finishTime; //-------------------------------------------- private StringBuffer text; //-------------------------------------------- private Title title; //-------------------------------------------- private String styleSheet; public FolderDocument() { this.errors = ""; this.folder = new Folder(); this.text = new StringBuffer(""); this.title = new Title(); this.errors = ""; this.styleSheet = "/style.css"; } //-------------------------------------------- public FolderDocument(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** return the number of milliseconds taken to load. */ public long getLoadDuration() { if (this.finishTime == null) { return 0; } if (this.startTime == null) { return 0; } return this.finishTime.getTime() - this.startTime.getTime(); } //-------------------------------------------- public void loadData(String sText) { this.startTime = new Date(); this.isGood = this.isFolderDocument(sText); if (!this.isGood) { this.finishTime = new Date(); return; } String sFolderDocumentContents = sText.trim(); StringReader srText = new StringReader(sFolderDocumentContents); int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); while (iCurrentCharacter != -1) { sbText.append((char)iCurrentCharacter); //-- System.out.println("sbText=" + sbText); //-- The title must be the first thing in the //-- document //-- if (Title.isTitle(sbText.toString().trim())) { //-- System.out.println(""); this.title = new Title(sbText.toString()); sbText.setLength(0); } if (sbText.toString().trim().endsWith(Folder.INDICATOR)) { this.text.append( sbText.toString().substring(0, sbText.toString().length() - Folder.INDICATOR.length())); sbText.setLength(0); sbText.append(Folder.INDICATOR); } if (Folder.isFolder(sbText.toString())) { //-- System.out.println(""); this.folder = new Folder(sbText.toString()); sbText.setLength(0); } try { iCurrentCharacter = srText.read(); } catch (IOException e) { this.errors = "An IO Exception occurred while reading " + e; this.finishTime = new Date(); return; } } //-- while more characters srText.close(); this.finishTime = new Date(); } //-- m: loadData() //-------------------------------------------- /** checks if this looks like an FolderDocument but * no structure checks will be made now. */ public static boolean isFolderDocument(String sText) { return true; } //-------------------------------------------- /** is there any data */ public boolean isEmpty() { return false; } //-------------------------------------------- /** is there a folder listing */ public boolean hasFolder() { //return this.folder.isGood; return true; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(NEWLINE); sbReturn.append(this.title); sbReturn.append(NEWLINE); sbReturn.append(NEWLINE); sbReturn.append(this.text); sbReturn.append(this.folder.toString()); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.title.toString()); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.title.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(this.text); sbReturn.append("
"); sbReturn.append(NEWLINE); sbReturn.append(this.folder.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append("Document title >"); sbReturn.append(this.title.toString()); sbReturn.append(NEWLINE); sbReturn.append("has a folder >"); sbReturn.append(this.hasFolder()); sbReturn.append(NEWLINE); sbReturn.append("Load time >"); sbReturn.append(this.getLoadDuration()); sbReturn.append(NEWLINE); sbReturn.append("last data good >"); sbReturn.append(this.isGood); sbReturn.append(NEWLINE); sbReturn.append("error messages >"); sbReturn.append(this.errors); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String print() { StringBuffer sbReturn = new StringBuffer(""); return this.toString(); } //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java FolderDocument text-file [show]"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(""); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbTest = new StringBuffer(""); sbTest.append(NEWLINE); //-- show all data, not just the html. if (args.length == 2) { System.out.println("Using data"); System.out.println(NEWLINE); System.out.println(BufferLoad.loadBuffer(args[0])); } FolderDocument document = new FolderDocument(BufferLoad.loadBuffer(args[0])); if (args.length == 2) { System.out.println(NEWLINE); System.out.println(document.toString()); System.out.println(NEWLINE); System.out.println(document.printReport()); } System.out.println(document.printHtml()); } //-- main() } //-- FAQ class