import java.io.*; import java.util.*; //import BufferLoad; //import Title; //import PlainList; //import Folder; //import LinkFolder; //import EndText; //import Image; /** * This type represents a document which contains a list * This document type is perhaps not very useful. * * The document may also contain the listing of the * contents of a directory or a 'webdirectory'. A webdirectory * is all the links from some other web-page reproduces as * a list in the current page. * * The list is started by u- or something similar. * * * @see LinkFolder, FaqDocument, Folder * @author http://bumble.sf.net */ public class PlainListDocument extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private PlainList list; //-------------------------------------------- private Folder folder; //-------------------------------------------- private LinkFolder linkFolder; //-------------------------------------------- private Date startTime; //-------------------------------------------- private Date finishTime; //-------------------------------------------- private StringBuffer text; //-------------------------------------------- private Title title; //-------------------------------------------- private Image image; //-------------------------------------------- private String styleSheet; public PlainListDocument() { this.errors = ""; this.list = new PlainList(); this.folder = new Folder(); this.linkFolder = new LinkFolder(); this.text = new StringBuffer(""); this.title = new Title(); this.image = new Image(); this.errors = ""; this.styleSheet = "/style.css"; } //-------------------------------------------- public PlainListDocument(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(); } //-------------------------------------------- /** parse the document and try to find the component * objects in their text representation. * */ public void loadData(String sText) { this.startTime = new Date(); this.isGood = this.isPlainListDocument(sText); if (!this.isGood) { this.finishTime = new Date(); return; } StringReader srText = new StringReader(sText); boolean bDebug = false; String sToken = ""; int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); while (iCurrentCharacter != -1) { sbText.append((char)iCurrentCharacter); if (bDebug) { System.out.println("sbText=" + sbText); } //-- The title must be the first thing in the //-- document //-- if (Title.isTitle(sbText.toString().trim())) { if (bDebug) { System.out.println(""); } this.title = new Title(sbText.toString()); sbText.setLength(0); } if (sbText.toString().endsWith(Image.INDICATOR)) { if (bDebug) { System.out.println(""); } this.text.append( EndText.removeSuffix(sbText.toString(), Image.INDICATOR)); sbText.setLength(0); sbText.append(Image.INDICATOR); } if (sbText.toString().endsWith(Folder.INDICATOR)) { if (bDebug) { System.out.println(""); } this.text.append( EndText.removeSuffix(sbText.toString(), Folder.INDICATOR)); sbText.setLength(0); sbText.append(Folder.INDICATOR); } if (sbText.toString().endsWith(LinkFolder.INDICATOR)) { if (bDebug) { System.out.println(""); } this.text.append( EndText.removeSuffix(sbText.toString(), LinkFolder.INDICATOR)); sbText.setLength(0); sbText.append(LinkFolder.INDICATOR); } if (sbText.toString().endsWith(FAQ.INDICATOR)) { this.text.append( EndText.removeSuffix(sbText.toString(), FAQ.INDICATOR)); sbText.setLength(0); sbText.append(FAQ.INDICATOR); } if (PlainList.recognize(sbText.toString())) { sToken = PlainList.returnToken(sbText.toString()); this.text.append( EndText.removeSuffix(sbText.toString(), sToken)); sbText.setLength(0); sbText.append(sToken); } if (Image.isImage(sbText.toString())) { if (bDebug) { System.out.println(""); } this.image = new Image(sbText.toString()); sbText.setLength(0); } if (Folder.isFolder(sbText.toString())) { if (bDebug) { System.out.println(""); } this.folder = new Folder(sbText.toString()); sbText.setLength(0); } if (LinkFolder.isLinkFolder(sbText.toString())) { if (bDebug) { System.out.println(""); } this.linkFolder = new LinkFolder(sbText.toString()); sbText.setLength(0); } if (PlainList.isPlainList(sbText.toString())) { if (bDebug) { System.out.println(""); } this.list = new PlainList(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 srText.close(); this.finishTime = new Date(); } //-- m: loadData() //-------------------------------------------- /** checks if this looks like an PlainListDocument but * no structure checks will be made now. */ public static boolean isPlainListDocument(String sText) { return true; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(NEWLINE); sbReturn.append(this.title); sbReturn.append(NEWLINE); sbReturn.append(this.text); sbReturn.append(NEWLINE); sbReturn.append(this.folder.toString()); sbReturn.append(NEWLINE); sbReturn.append(this.linkFolder.toString()); sbReturn.append(NEWLINE); sbReturn.append(this.list.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(this.title.getContents()); 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.image.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.text); sbReturn.append("
"); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append("
"); sbReturn.append(NEWLINE); sbReturn.append(this.list.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.linkFolder.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(this.folder.printHtml()); sbReturn.append(NEWLINE); sbReturn.append("
"); 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("folder listing >"); sbReturn.append(this.folder.getAbsoluteName()); sbReturn.append(NEWLINE); sbReturn.append("link folder list>"); sbReturn.append(this.folder.getAbsoluteName()); 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 PlainListDocument 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])); } PlainListDocument listTest = new PlainListDocument(BufferLoad.loadBuffer(args[0])); System.out.println(listTest.printHtml()); if (args.length == 2) { System.out.println(NEWLINE); System.out.println(listTest.toString()); System.out.println(NEWLINE); System.out.println(listTest.printReport()); } } //-- main() } //-- PlainListDocument class