import java.util.*; //import Html; //import LinkableText; //import BlankText; /** * This is one item from a list. The list does not have to * be about any particular subject. The text of the items can * contain links and ordinary text, but no spaces or other * structures. * * @author http://bumble.sf.net * @See */ public class ListItem extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** the contents of the item */ private LinkableText contents; //-------------------------------------------- public static String INDICATOR = "- "; //-------------------------------------------- public static String TERMINATOR = INDICATOR; //-------------------------------------------- private boolean isGood; //-------------------------------------------- //-------------------------------------------- public ListItem() { this.contents = new LinkableText(); } //-------------------------------------------- public ListItem(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** load data from text */ public void loadData(String sText) { this.isGood = this.isListItem(sText); if (!this.isGood) { return; } this.contents = new LinkableText(sText.substring( this.INDICATOR.length(), sText.length() - this.INDICATOR.length()).trim()); } //-- method: loadData //-------------------------------------------- public String getContents() { return this.contents.toString(); } //-------------------------------------------- public void setContents(LinkableText mtContents) { this.contents = mtContents; } //-------------------------------------------- /** recognizes the beginning of a ListItem at the end * of another string. This needs to be static because * it may be called by a parser before any object is created **/ public static boolean recognize(String sContents) { if (sContents.endsWith(ListItem.INDICATOR)) { return true; } return false; } //-------------------------------------------- /** since the list item uses the start token of the * next item as its end token, it needs to return this * to the parser. This should not be a static method */ public static String restoreToken() { //-- at the end of the list this will be different //-- return ListItem.INDICATOR; } //-------------------------------------------- /** checks if this looks like a list item */ public static boolean isListItem(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.startsWith(ListItem.INDICATOR)) { return false; } if (sText.equals(ListItem.INDICATOR)) { return false; } if (!sText.endsWith(ListItem.TERMINATOR) && !BlankText.lastLineIsBlank(sText)) { return false; } return true; } //-------------------------------------------- /** returns the list item to a canonical text form */ public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(ListItem.INDICATOR); sbReturn.append(this.contents); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("ListItem Indicator >"); sbReturn.append(ListItem.INDICATOR); sbReturn.append(NEWLINE); sbReturn.append("ListItem Terminator>"); sbReturn.append(ListItem.TERMINATOR); sbReturn.append(NEWLINE); sbReturn.append("ListItem contents >"); sbReturn.append(this.contents); sbReturn.append(NEWLINE); sbReturn.append("Last data was ok >"); sbReturn.append(this.isGood); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("
  • "); sbReturn.append(this.contents.printHtml()); sbReturn.append("
  • "); return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(""); sbReturn.append(this.contents.toString()); 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 ListItem text"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(" eg java ListItem "); sbUsageMessage.append("\""); sbUsageMessage.append(ListItem.INDICATOR); sbUsageMessage.append("go to shops"); sbUsageMessage.append(ListItem.TERMINATOR); sbUsageMessage.append("\""); sbUsageMessage.append(NEWLINE); StringBuffer sbMessage = new StringBuffer(""); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } String sTestItem = " - point one " + NEWLINE + " - "; ListItem item = new ListItem(args[0]); //System.out.println("Using data"); System.out.println(".printHtml()"); System.out.println(item.printHtml()); System.out.println(".printDocBook()"); System.out.println(item.printDocBook()); System.out.println(".toString()"); System.out.println(item.toString()); System.out.println(".printReport()"); System.out.println(item.printReport()); } //-- main() } //-- ListItem class