import java.io.*; import java.util.*; //import BufferLoad; //import Title; //import FAQ; /** * This type represents a chunk of text containing * a question and answer section, known as an PlainFaqDocument. * The text is not 'marked up' particularly, or at least * only in a minimalistic way. * * @see FaqItem, Question, Answer * @author http://bumble.sf.net */ public class PlainFaqDocument extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private FAQ faq; //-------------------------------------------- private Date startTime; //-------------------------------------------- private Date finishTime; //-------------------------------------------- private StringBuffer text; //-------------------------------------------- private Title title; //-------------------------------------------- private String styleSheet; public PlainFaqDocument() { this.errors = ""; this.faq = new FAQ(); this.text = new StringBuffer(""); this.title = new Title(); this.errors = ""; this.styleSheet = "/style.css"; } //-------------------------------------------- public PlainFaqDocument(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.isPlainFaqDocument(sText); if (!this.isGood) { this.finishTime = new Date(); return; } String sPlainFaqDocumentContents = sText.trim(); StringReader srText = new StringReader(sPlainFaqDocumentContents); 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(FAQ.INDICATOR)) { this.text.append( sbText.toString().substring(0, sbText.toString().length() - FAQ.INDICATOR.length())); sbText.setLength(0); sbText.append(FAQ.INDICATOR); } if (FAQ.isFAQ(sbText.toString())) { //-- System.out.println(""); this.faq = new FAQ(sbText.toString()); //-- //-- push the 'q:' back on the buffer since we are //-- sbText.setLength(0); } //-- if faqItem 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 PlainFaqDocument but * no structure checks will be made now. */ public static boolean isPlainFaqDocument(String sText) { return true; } //-------------------------------------------- /** is there any data */ public boolean isEmpty() { return false; } //-------------------------------------------- /** is there an faq */ public boolean hasFAQ() { return this.faq.isEmpty(); } //-------------------------------------------- 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.faq.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.faq.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 an faq >"); sbReturn.append(this.hasFAQ()); 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 PlainFaqDocument faq-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])); } PlainFaqDocument faqTest = new PlainFaqDocument(BufferLoad.loadBuffer(args[0])); if (args.length == 2) { System.out.println(NEWLINE); System.out.println(faqTest.toString()); System.out.println(NEWLINE); System.out.println(faqTest.printReport()); } System.out.println(faqTest.printHtml()); } //-- main() } //-- FAQ class