import java.util.*; /** * This type represents a Answer from an faq style chunk of * text. The question and answer form an item. * * @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 String contents; //-------------------------------------------- public static String TERMINATOR = ""; //-------------------------------------------- public static String INDICATOR = "a:"; //-------------------------------------------- private boolean isGood; //-------------------------------------------- //-------------------------------------------- public Answer() { this.contents = ""; } //-------------------------------------------- /** 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 = sText.trim().substring(Answer.INDICATOR.length()); } else { this.contents = sText; } } //-------------------------------------------- public String getContents() { return this.contents; } //-------------------------------------------- public void setContents(String sContents) { this.contents = 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); 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); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("
"); sbReturn.append(Answer.INDICATOR); sbReturn.append(this.contents); sbReturn.append(Answer.TERMINATOR); 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(rTest.printHtml()); System.out.println(NEWLINE); System.out.println(rTest.toString()); System.out.println(NEWLINE); System.out.println(rTest.printReport()); } //-- main() } //-- Answer class