import java.util.*; //import Html; //import MixedText; /** * This type represents a Answer from an faq style chunk of * text. The contents of the answer may contain links. * * @author http://bumble.sf.net * @See Answer, FAQ, etc */ public class Answer extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** the text of the Answer */ private MixedText contents; //-------------------------------------------- public static String TERMINATOR = ""; //-------------------------------------------- public static String INDICATOR = "a:"; //-------------------------------------------- private boolean isGood; //-------------------------------------------- //-------------------------------------------- public Answer() { this.contents = new MixedText(); } //-------------------------------------------- /** construct with some text */ public Answer(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** load data from text */ public void loadData(String sText) { this.isGood = this.isAnswer(sText); if (!this.isGood) { return; } if (sText.trim().startsWith(Answer.INDICATOR)) { this.contents = new MixedText( sText.trim().substring(Answer.INDICATOR.length())); } else { this.contents = new MixedText(sText); } } //-------------------------------------------- public MixedText getContents() { return this.contents; } //-------------------------------------------- public void setContents(String sContents) { this.contents = new MixedText(sContents); } //-------------------------------------------- /** checks if this looks like a Answer, but an answer * will be whatever is left over */ public static boolean isAnswer(String sText) { /* if (sText.trim().length() == 0) { return false; } if (!sText.trim().startsWith(Answer.INDICATOR)) { return false; } if (!sText.trim().endsWith(Answer.TERMINATOR)) { return false; } */ return true; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(Answer.INDICATOR); sbReturn.append(this.contents.toString()); sbReturn.append(Answer.TERMINATOR); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("Answer Indicator >"); sbReturn.append(Answer.INDICATOR); sbReturn.append(NEWLINE); sbReturn.append("Answer Terminator>"); sbReturn.append(Answer.TERMINATOR); sbReturn.append(NEWLINE); sbReturn.append("Answer contents >"); sbReturn.append(this.contents.toString()); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("
"); sbReturn.append(NEWLINE); sbReturn.append(this.contents.printHtml().trim()); sbReturn.append(NEWLINE); sbReturn.append("
"); return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.contents.toString().trim()); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(""); 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 Answer text"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(" eg java Answer \" just because\" "); sbUsageMessage.append(NEWLINE); StringBuffer sbMessage = new StringBuffer(""); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } Answer rTest = new Answer(args[0]); System.out.println(".printHtml()"); System.out.println(rTest.printHtml()); System.out.println(""); System.out.println(".toString()"); System.out.println(rTest.toString()); System.out.println(""); System.out.println(".printReport()"); System.out.println(rTest.printReport()); } //-- main() } //-- Answer class