import java.io.*; import java.util.*; //import BufferLoad; //import Title; //import FAQ; //import Folder; //import LinkFolder; //import EndText; //import Image; //import SourceForgeLogo; /** * This type represents a document containing an FAQ * or 'frequently asked questions' section although the questions * and answers are usually written by a programmer. * * 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 faq section is indicated by [faq] and [/faq] tags * * * @see LinkFolder, FAQ, FaqItem, Question, Answer * @author http://bumble.sf.net */ public class FaqDocument extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private FAQ faq; //-------------------------------------------- private Folder folder; //-------------------------------------------- private LinkFolder linkFolder; //-------------------------------------------- private Date startTime; //-------------------------------------------- private Date finishTime; //-------------------------------------------- private StringBuffer text; //-------------------------------------------- private Title title; //-------------------------------------------- private Image image; //-------------------------------------------- private SourceForgeLogo sourceForgeLogo; //-------------------------------------------- private String styleSheet; public FaqDocument() { this.errors = ""; this.faq = new FAQ(); this.folder = new Folder(); this.linkFolder = new LinkFolder(); this.text = new StringBuffer(""); this.title = new Title(); this.image = new Image(); this.sourceForgeLogo = new SourceForgeLogo(); this.errors = ""; this.styleSheet = "/style.css"; } //-------------------------------------------- public FaqDocument(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.isFaqDocument(sText); if (!this.isGood) { this.finishTime = new Date(); return; } String sFaqDocumentContents = sText.trim(); StringReader srText = new StringReader(sFaqDocumentContents); int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); boolean bInFaq = false; 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); } //-- only look for other components if we are //-- not within an faq block. This may speed up //-- parsing as well. if (!bInFaq) { if (Image.recognize(sbText.toString())) { this.text.append( EndText.removeSuffix(sbText.toString(), Image.INDICATOR)); sbText.setLength(0); sbText.append(Image.INDICATOR); } if (SourceForgeLogo.recognize(sbText.toString())) { this.text.append( EndText.removeSuffix(sbText.toString(), SourceForgeLogo.INDICATOR)); sbText.setLength(0); sbText.append(SourceForgeLogo.INDICATOR); } if (Folder.recognize(sbText.toString())) { this.text.append( EndText.removeSuffix(sbText.toString(), Folder.INDICATOR)); sbText.setLength(0); sbText.append(Folder.INDICATOR); } if (LinkFolder.recognize(sbText.toString())) { this.text.append( EndText.removeSuffix(sbText.toString(), LinkFolder.INDICATOR)); sbText.setLength(0); sbText.append(LinkFolder.INDICATOR); } } //-- if not in FAQ if (FAQ.recognize(sbText.toString())) { this.text.append( EndText.removeSuffix(sbText.toString(), FAQ.INDICATOR)); sbText.setLength(0); //-- //-- push the '[faq]' tag back on the buffer //-- sbText.append(FAQ.INDICATOR); bInFaq = true; } if (Image.isImage(sbText.toString())) { //-- System.out.println(""); this.image = new Image(sbText.toString()); sbText.setLength(0); } if (SourceForgeLogo.isSourceForgeLogo(sbText.toString())) { //-- System.out.println(""); this.sourceForgeLogo = new SourceForgeLogo(sbText.toString()); sbText.setLength(0); } if (Folder.isFolder(sbText.toString())) { //-- System.out.println(""); this.folder = new Folder(sbText.toString()); sbText.setLength(0); } if (LinkFolder.isLinkFolder(sbText.toString())) { //-- System.out.println(""); this.linkFolder = new LinkFolder(sbText.toString()); sbText.setLength(0); } if (FAQ.isFAQ(sbText.toString())) { //-- System.out.println(""); this.faq = new FAQ(sbText.toString()); sbText.setLength(0); bInFaq = false; } 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 FaqDocument but * no structure checks will be made now. */ public static boolean isFaqDocument(String sText) { return true; } //-------------------------------------------- /** 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(this.image); 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.faq.toString()); sbReturn.append(NEWLINE); sbReturn.append(this.sourceForgeLogo); 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.faq.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(this.sourceForgeLogo.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append( ""); sbReturn.append( ""); /* ------------- This is an introductory paragraph. Introduction This is a paragraph. -------------- */ sbReturn.append(NEWLINE); sbReturn.append("
"); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(""); sbReturn.append(this.title.getContents()); sbReturn.append(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(NEWLINE); sbReturn.append(this.image.printDocBook()); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(this.text); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.faq.printDocBook()); sbReturn.append(NEWLINE); //sbReturn.append(this.linkFolder.printDocBook()); sbReturn.append(NEWLINE); //sbReturn.append(this.folder.printDocBook()); sbReturn.append(NEWLINE); //sbReturn.append(this.sourceForgeLogo.printDocBook()); 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("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 FaqDocument 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])); } FaqDocument faqTest = new FaqDocument(BufferLoad.loadBuffer(args[0])); System.out.println(faqTest.printHtml()); if (args.length == 2) { System.out.println(NEWLINE); System.out.println(faqTest.toString()); System.out.println(NEWLINE); System.out.println(faqTest.printReport()); } } //-- main() } //-- FaqDocument class