import java.net.*; import java.io.*; import java.util.*; /** This class is a reference from a web link. In an html context this * is the data contained by the href attribute of a hyperlink. In other * contexts it will mean other things. * @author matth3wbishopyahoo!com * @See Document, Form, Value, Link, Field */ public class Reference extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- /** what the reference refers to, sometimes a Url */ private Value target; //-------------------------------------------- private boolean isGood; //-------------------------------------------- public Reference() { //this.target = new Value(); } //-- constr: () //-------------------------------------------- /** construct with html text */ public Reference(String sText) { this(); this.loadData(sText); } //-- constr: (string) //-------------------------------------------- /** load data from text */ public void loadData(String sText) { this.isGood = this.isReference(sText); if (!this.isGood) { this.target = new Value(); return; } this.target = new Value(sText.substring(sText.indexOf("=") + 1)); } //-- method: loadData //-------------------------------------------- public String getPointer() { return this.target.getContent(); } //-------------------------------------------- public Value getTarget() { return this.target; } //-------------------------------------------- public void setTarget(String sTarget) { this.target.setContent(sTarget); } //-------------------------------------------- /** checks if this looks like a Reference */ public static boolean isReference(String sText) { if (sText.trim().length() == 0) { return false; } if (sText.indexOf("=") == -1) { return false; } if (!sText.toLowerCase().trim().startsWith("href")) { return false; } String sTest = sText.trim().substring("HREF".length()); //System.out.println("stest =" + sTest); if (!sTest.trim().startsWith("=")) { return false; } String sValueText; sValueText = sText.substring(sText.indexOf("=") + 1); //System.out.println("sValueText=" + sValueText); if (!Value.isValue(sValueText)) { return false; } return true; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("href=" + this.target.toString()); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("Content of the target:" + this.target.getContent()); sbReturn.append(NEWLINE); sbReturn.append("Reference is in good format:" + this.isGood); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String print() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append("target=" + this.target); return sbReturn.toString(); } //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java Reference test"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(" eg java Reference 'href = http://bumble.sf.net' "); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } /* BufferedReader brInput = new BufferedReader(new InputStreamReader(System.in)); int iInput = 0; char cInput = ' '; StringBuffer sbInput = new StringBuffer(""); System.out.println("Testing the Reference Class"); System.out.println("Type * to finish"); while (cInput != '*') { sbInput.append(cInput); cInput = (char)brInput.read(); } Reference rTest = new Reference(sbInput.toString()); */ Reference rTest = new Reference(args[0]); StringBuffer sbMessage = new StringBuffer(""); sbMessage.append(rTest.print()); sbMessage.append(NEWLINE); sbMessage.append(rTest.printReport()); System.out.println(sbMessage); } //-- main() } //-- Reference class