import java.util.*; /** * This type represents a plain link. This is a link * which is not part of a text block. The code should be * be runnable under jvm11. * * @author http://bumble.sf.net * @See Answer, FAQ, FaqDocument, Title, Question */ public class PlainLink extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** where the link points to */ private String target; //-------------------------------------------- private String text; //-------------------------------------------- public static String INDICATOR = "*:"; //-------------------------------------------- public static String TERMINATOR = ":*"; //-------------------------------------------- private boolean isGood; //-------------------------------------------- //-------------------------------------------- public PlainLink() { this.target = ""; this.text = ""; } //-------------------------------------------- /** construct with some text */ public PlainLink(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** load data from text */ public void loadData(String sText) { String sContents; this.isGood = this.isPlainLink(sText); if (!this.isGood) { return; } sContents = sText.trim().substring( this.INDICATOR.length(), sText.trim().length() - this.TERMINATOR.length()); //-- the target and the text should be separated //-- by a space. if (sContents.trim().indexOf(" ") == -1) { this.target = sContents.trim(); this.text = sContents.trim(); } else { this.target = sContents.trim().substring(0, sContents.trim().indexOf(" ")); this.text = sContents.trim().substring(sContents.trim().indexOf(" ")); } } //-- method: loadData //-------------------------------------------- public String getText() { return this.text; } //-------------------------------------------- public String getTarget() { return this.target; } //-------------------------------------------- public void setTarge(String sTarget) { this.target = sTarget; } //-------------------------------------------- /** checks if this looks like a PlainLink */ public static boolean isPlainLink(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.trim().startsWith(PlainLink.INDICATOR)) { return false; } if (!sText.trim().endsWith(PlainLink.TERMINATOR)) { return false; } return true; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(PlainLink.INDICATOR); sbReturn.append(this.target); sbReturn.append(" "); sbReturn.append(this.text); sbReturn.append(PlainLink.TERMINATOR); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("PlainLink Indicator :"); sbReturn.append(PlainLink.INDICATOR); sbReturn.append(NEWLINE); sbReturn.append("PlainLink Terminator:"); sbReturn.append(PlainLink.TERMINATOR); sbReturn.append(NEWLINE); sbReturn.append("Last data was good :"); sbReturn.append(this.isGood); sbReturn.append(NEWLINE); sbReturn.append("target:"); sbReturn.append(this.target); sbReturn.append(NEWLINE); sbReturn.append("text :"); sbReturn.append(this.text); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(this.text.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 PlainLink text"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(" eg java PlainLink \""); sbUsageMessage.append(PlainLink.INDICATOR); sbUsageMessage.append(" google.com search "); sbUsageMessage.append(PlainLink.TERMINATOR); sbUsageMessage.append("\""); sbUsageMessage.append(NEWLINE); StringBuffer sbMessage = new StringBuffer(""); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } PlainLink rTest = new PlainLink(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() } //-- PlainLink class