import java.util.*; //import Html; //import MixedText; //import BlankText; /** * This is one item from a list. The list does not have to * be about any particular subject. This type differs from a * ListItem only in that a list item may contain links since * its content is a MixedText type, whereas the contents * of this class is just ordinary text. * * @author http://bumble.sf.net * @See ListItem, PlainList, PlainListDocument. */ public class PlainListItem extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** the contents of the item */ private String contents; //-------------------------------------------- public static String INDICATOR = "- "; //-------------------------------------------- public static String TERMINATOR = INDICATOR; //-------------------------------------------- private boolean isGood; //-------------------------------------------- //-------------------------------------------- public PlainListItem() { this.contents = ""; } //-------------------------------------------- public PlainListItem(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** load data from text */ public void loadData(String sText) { this.isGood = this.isPlainListItem(sText); if (!this.isGood) { return; } this.contents = sText.substring( this.INDICATOR.length(), sText.length() - this.INDICATOR.length()).trim(); } //-- method: loadData //-------------------------------------------- public String getContents() { return this.contents; } //-------------------------------------------- public void setContents(String sContents) { this.contents = sContents; } //-------------------------------------------- /** recognizes the beginning of a PlainListItem 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(PlainListItem.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 PlainListItem.INDICATOR; } //-------------------------------------------- /** checks if this looks like a list item */ public static boolean isPlainListItem(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.startsWith(PlainListItem.INDICATOR)) { return false; } if (sText.equals(PlainListItem.INDICATOR)) { return false; } if (!sText.endsWith(PlainListItem.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(PlainListItem.INDICATOR); sbReturn.append(this.contents); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("PlainListItem Indicator >"); sbReturn.append(PlainListItem.INDICATOR); sbReturn.append(NEWLINE); sbReturn.append("PlainListItem Terminator>"); sbReturn.append(PlainListItem.TERMINATOR); sbReturn.append(NEWLINE); sbReturn.append("PlainListItem 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(Html.encode(this.contents.trim())); 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 PlainListItem text"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(" eg java PlainListItem "); sbUsageMessage.append("\""); sbUsageMessage.append(PlainListItem.INDICATOR); sbUsageMessage.append("go to shops"); sbUsageMessage.append(PlainListItem.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 + " - "; PlainListItem item = new PlainListItem(args[0]); //System.out.println("Using data"); System.out.println(".printHtml()"); System.out.println(item.printHtml()); System.out.println(".toString()"); System.out.println(item.toString()); System.out.println(".printReport()"); System.out.println(item.printReport()); } //-- main() } //-- PlainListItem class