import java.io.*; import java.util.*; /** * This type represents a chunk of text containing * a question and answer section, known as an faq. * The text is not 'marked up' particularly, or at least * only in a minimalistic way. * * @see FaqItem, Question, Answer * @author matth3wbishopyahoo!com */ public class FAQ extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private Vector faqItems; //-------------------------------------------- public static String INDICATOR; //-------------------------------------------- public static String TERMINATOR; public FAQ() { this.errors = ""; } //-------------------------------------------- public FAQ(String sText) { this(); this.loadData(sText); } //-------------------------------------------- public void loadData(String sText) { StringReader srText = new StringReader(sText); this.isGood = this.isFAQ(sText); if (!this.isGood) { return; } int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); while (iCurrentCharacter != -1) { sbText.append((char)iCurrentCharacter); //System.out.println("sbText=" + sbText); if (FaqItem.isFaqItem(sbText.toString())) { this.faqItems.addElement(new FaqItem(sbText.toString())); sbText.setLength(0); return; } try { iCurrentCharacter = srText.read(); } catch (IOException e) { this.errors = "An IO Exception occurred while reading " + e; return; } } //-- while srText.close(); } //-- m: loadData() //-------------------------------------------- /** checks if this looks like an FAQ */ public static boolean isFAQ(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.trim().startsWith(FAQ.INDICATOR)) { return false; } if (!sText.trim().endsWith(FAQ.TERMINATOR)) { return false; } return true; } //-------------------------------------------- /** how many question and answers are there */ public boolean countItems() { return this.faqItems.size(); } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(this.question.toString()); sbReturn.append(NEWLINE); sbReturn.append(this.answer.toString()); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.question.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(this.answer.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(""); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append("Question :"); sbReturn.append(this.question.getContents()); sbReturn.append(NEWLINE); sbReturn.append("Answer :"); sbReturn.append(this.answer.getContents()); sbReturn.append(NEWLINE); sbReturn.append("Errors :"); sbReturn.append(this.errors); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String print() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); return sbReturn.toString(); } //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java FAQ ."); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(" tests from standard in"); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbTest = new StringBuffer(""); sbTest.append(" q: what is an interface ?"); sbTest.append(NEWLINE); sbTest.append(" an interface is a type"); sbTest.append(NEWLINE); sbTest.append(NEWLINE); sbTest.append(" [/faq]"); sbTest.append(NEWLINE); System.out.println("Using data:"); System.out.println(NEWLINE); System.out.println(sbTest); FAQ rTest = new FAQ(sbTest.toString()); System.out.println(rTest.printHtml()); System.out.println(NEWLINE); System.out.println(rTest.toString()); System.out.println(NEWLINE); System.out.println(rTest.printReport()); } //-- main() } //-- FAQ class