import java.net.*; import java.io.*; import java.util.*; import Html; import ReferenceDocument; /** * This type represents a reference to a link folder. * This is a concept where the links in a web page * are treated as if they were members of a folder. * * @see FaqItem, Question, Answer * @author http://bumble.sf.net */ public class LinkFolder extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private String linkPage; //-------------------------------------------- public static String INDICATOR = "[webdir"; //-------------------------------------------- public static String TERMINATOR = "]"; public LinkFolder() { this.errors = ""; this.linkPage = ""; } //-------------------------------------------- public LinkFolder(String sText) { this(); this.loadData(sText); } //-------------------------------------------- public String getAbsoluteName() { if (!this.isGood) { return ""; } return this.linkPage; } //-------------------------------------------- public String getCanonicalName() { return ""; } //-------------------------------------------- public String getLinkPage() { return this.linkPage; } //-------------------------------------------- public void setLinkPage(String sLinkPage) { this.linkPage = sLinkPage; } //-------------------------------------------- public void loadData(String sText) { this.isGood = this.isLinkFolder(sText); if (!this.isGood) { return; } this.linkPage = sText.trim().substring( this.INDICATOR.length(), sText.trim().length() - this.TERMINATOR.length()).trim(); if (this.linkPage.equals("")) { this.linkPage = "."; } } //-- m: loadData() //-------------------------------------------- /** checks if this looks like a LinkFolder indicator */ public static boolean isLinkFolder(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.trim().startsWith(LinkFolder.INDICATOR)) { return false; } if (!sText.trim().endsWith(LinkFolder.TERMINATOR)) { return false; } return true; } //-------------------------------------------- /** creates a string suitable for a plain text doc */ public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(this.INDICATOR); sbReturn.append(" "); sbReturn.append(this.linkPage.toString()); sbReturn.append(this.TERMINATOR); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { if (!this.isGood) { return ""; } StringBuffer sbReturn = new StringBuffer(""); ReferenceDocument document; InputStreamReader stream; URL urlLinkPage; Reference[] rrLinks; try { urlLinkPage = new URL(this.linkPage); } catch (MalformedURLException e) { return ""; } try { stream = new InputStreamReader(urlLinkPage.openStream()); } catch (IOException e) { return ""; } document = new ReferenceDocument(stream); rrLinks = document.getReferences(); //return document.toString(); Reference rCurrentLink = new Reference(); String sDisplayName = new String(); String sName = new String(); sbReturn.append(""); return sbReturn.toString(); } //-- m: printHtml() //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append("Link page name >"); sbReturn.append(this.linkPage); sbReturn.append(NEWLINE); sbReturn.append("Absolute name >"); sbReturn.append(this.getAbsoluteName()); sbReturn.append(NEWLINE); sbReturn.append("last data good >"); sbReturn.append(this.isGood); sbReturn.append(NEWLINE); sbReturn.append("error messages >"); sbReturn.append(this.errors); 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 LinkFolder link-page-text"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbTest = new StringBuffer(""); LinkFolder dirTest = new LinkFolder(args[0]); System.out.println(".printHtml()"); System.out.println(dirTest.printHtml()); System.out.println(".toString()"); System.out.println(dirTest.toString()); System.out.println(".printReport()"); System.out.println(dirTest.printReport()); } //-- main() } //-- FAQ class