import java.net.*; import java.io.*; import java.util.*; //-- can only import packages in jdk 1.4 //import Html; //import TextTool; //import QuoteText; //import EndText; //import DocumentElement; /** * * This class represent an example given in a document. * The nature of the example would probably be a code block * or something similar. The block is terminated with a * blank line. * * http://dict.org/ 'search the dictionary' * * * @author http://bumble.sf.net * @See Answer, FAQ, etc */ public class Example extends Object implements DocumentElement { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** */ private String contents; //-------------------------------------------- private String indicatorText; //-------------------------------------------- public static String TERMINATOR = ""; //-------------------------------------------- /** the text which will indicate the start of an example */ public static String[] startTokens = { "for example:", "for example-", "type:", "type-", "try-", "por ejemplo:", "por ejemplo-" }; //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String returnToken; //-------------------------------------------- public Example() { this.contents = ""; this.returnToken = ""; } //-------------------------------------------- /** construct with some text */ public Example(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** load data from text */ public void loadData(String sText) { String sTextTrimmed; String sTarget; this.isGood = this.isExample(sText); if (!this.isGood) { return; } //-- The EndText functions are not case sensitive. //-- this.indicatorText = EndText.getPrefix(sText, Example.startTokens); this.contents = EndText.removePrefix(sText, this.indicatorText); } //-- method: loadData //-------------------------------------------- /** this method sees if the end of another string looks * like the start of a Example. this might be called * by a parsing routine */ public static boolean recognize(String sText) { String sSuffix; String sWithoutSuffix; if (!TextTool.endsWithIgnoreCase(sText, Example.startTokens)) { return false; } sSuffix = EndText.getSuffix(sText, Example.startTokens); sWithoutSuffix = EndText.removeSuffix(sText, sSuffix); //-- allow a single quote to escape the example block //-- start token. This is useful for including the token //-- in explanatory text. //-- if (sWithoutSuffix.endsWith("'")) { return false; } return true; } //-------------------------------------------- /** needed for resetting the parse string */ public static String startToken(String sText) { return EndText.getSuffix(sText, Example.startTokens); } //-------------------------------------------- /** may not be necessary */ public static String returnEndToken(String sText) { return NEWLINE + NEWLINE; } //-------------------------------------------- public String getContents() { return this.contents; } //-------------------------------------------- public void setContents(String sText) { this.contents = sText; } //-------------------------------------------- /** checks if this looks like an example. * */ public static boolean isExample(String sText) { if (sText.trim().length() == 0) { return false; } if (!TextTool.startsWithIgnoreCase(sText.trim(), Example.startTokens)) { return false; } String sPrefix = EndText.getPrefix(sText, Example.startTokens); if (sText.trim().equals(sPrefix)) { return false; } if (!BlankText.lastLineIsBlank(sText)) { return false; } return true; } //-- method: isExample //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(this.indicatorText); sbReturn.append(NEWLINE); sbReturn.append(this.contents); sbReturn.append(NEWLINE); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("The indicator text>"); sbReturn.append(this.indicatorText); sbReturn.append(NEWLINE); sbReturn.append("Example contents >"); sbReturn.append(this.contents); sbReturn.append(NEWLINE); sbReturn.append("Last data ok >"); sbReturn.append(this.isGood); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(Html.encode(this.indicatorText.trim())); sbReturn.append(NEWLINE); sbReturn.append("
");
    sbReturn.append(NEWLINE);
    sbReturn.append(Html.encode(this.contents.trim()));
    sbReturn.append(NEWLINE);
    sbReturn.append("
"); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(Html.encode(this.indicatorText.trim())); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(""); sbReturn.append(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(Html.encode(this.contents.trim())); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(" "); 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 Example ."); sbUsageMessage.append(NEWLINE); StringBuffer sbMessage = new StringBuffer(""); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbTest = new StringBuffer(""); sbTest.append("For example: "); sbTest.append(NEWLINE); sbTest.append(" java FaqDocument text-file > htmlfile "); sbTest.append(NEWLINE); sbTest.append(NEWLINE); System.out.println("using text:"); System.out.println(sbTest); Example egTest = new Example(sbTest.toString()); System.out.println(".printHtml()"); System.out.println(egTest.printHtml()); System.out.println(".toString()"); System.out.println(egTest.toString()); System.out.println(".printReport()"); System.out.println(egTest.printReport()); } //-- main() } //-- Example class