diff --git a/linkcheck/project.xml b/linkcheck/project.xml index 124dc0db..b047f05e 100644 --- a/linkcheck/project.xml +++ b/linkcheck/project.xml @@ -78,6 +78,12 @@ root.maven + + + commons-httpclient + SNAPSHOT + + commons-jelly commons-jelly-tags-ant @@ -101,13 +107,6 @@ root.maven - - httpunit - 1.5.1 - - root.maven - - jtidy 4aug2000r7-dev diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java b/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java index 135c70ed..d4d6924e 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/FileToCheck.java @@ -57,6 +57,8 @@ package org.apache.maven.linkcheck; */ import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -66,191 +68,214 @@ import java.util.Map; import org.apache.maven.linkcheck.validation.LinkValidationItem; import org.apache.maven.linkcheck.validation.LinkValidationResult; import org.apache.maven.linkcheck.validation.LinkValidatorManager; - -import com.meterware.httpunit.GetMethodWebRequest; -import com.meterware.httpunit.WebConversation; -import com.meterware.httpunit.WebLink; -import com.meterware.httpunit.WebRequest; -import com.meterware.httpunit.WebResponse; +import org.dom4j.Document; +import org.dom4j.Node; +import org.dom4j.io.DOMReader; +import org.w3c.tidy.Tidy; /** * @author Ben Walding - * @version $Id: FileToCheck.java,v 1.5 2003/02/22 00:37:31 bwalding Exp $ + * @version $Id: FileToCheck.java,v 1.6 2003/03/07 08:27:02 bwalding Exp $ * */ public class FileToCheck { - private File base; - private File fileToCheck; - private String status = STATUS_OK; - private String message = ""; - private int successful; - private int unsuccessful; - public static final String STATUS_UNKNOWN = null; - public static final String STATUS_JTIDY_FAILURE = "Unable to tidy source"; - public static final String STATUS_OK = "OK"; + private File base; + private File fileToCheck; + private String status = STATUS_OK; + private String message = ""; + private int successful; + private int unsuccessful; - public FileToCheck(File base, File fileToCheck) - { - this.base = base; - this.fileToCheck = fileToCheck; - } + public static final String STATUS_UNKNOWN = null; + public static final String STATUS_JTIDY_FAILURE = "Unable to tidy source"; + public static final String STATUS_OK = "OK"; - private List links = new ArrayList(); - - public void check(LinkValidatorManager lvm) throws Exception - { - successful = 0; - unsuccessful = 0; - status = STATUS_OK; - message = ""; - - - - try + public FileToCheck(File base, File fileToCheck) { - WebConversation wc = new WebConversation(); - WebRequest req = new GetMethodWebRequest(fileToCheck.toURL().toString()); - WebResponse resp = wc.getResponse(req); - WebLink[] wl = resp.getLinks(); - Map uniqueLinks = new HashMap(); - for (int i = 0; i < wl.length; i++) - { //It puts the current URL in item 0 - WebLink link = wl[i]; - String href = link.getDOMSubtree().getAttributes().getNamedItem("href").getNodeValue(); + this.base = base; + this.fileToCheck = fileToCheck; + } - uniqueLinks.put(href, href); - } + private List links = new ArrayList(); - Iterator iter = uniqueLinks.keySet().iterator(); - while (iter.hasNext()) - { - String href = (String) iter.next(); + public void check(LinkValidatorManager lvm) throws Exception + { + successful = 0; + unsuccessful = 0; + status = STATUS_OK; + message = ""; - //System.out.println("Link Found: " + href); - - LinkCheckResult lcr = new LinkCheckResult(); - - LinkValidationItem lvi = new LinkValidationItem(fileToCheck, href); - LinkValidationResult result = lvm.validateLink(lvi); - lcr.setTarget(href); - - switch (result.getStatus()) + try { - case LinkValidationResult.UNKNOWN : - unsuccessful++; - lcr.setStatus("UNKNOWN REF"); - break; - case LinkValidationResult.VALID : - successful++; - lcr.setStatus("OK"); - break; - case LinkValidationResult.INVALID : - unsuccessful++; - lcr.setStatus("NOT FOUND"); - break; + Tidy tidy = new Tidy(); + Document doc = null; + + try + { + //System.out.println("Parsing:" + fileToCheck); + FileInputStream in = new FileInputStream(fileToCheck); + tidy.setMakeClean(true); + tidy.setXmlTags(true); + tidy.setXmlOut(true); + tidy.setQuiet(false); + + tidy.setXHTML(true); + org.w3c.dom.Document domDocument = tidy.parseDOM(in, null); + + // now read a dom4j document from + // JTidy's W3C DOM object + DOMReader domReader = new DOMReader(); + doc = domReader.read(domDocument); + + } + catch (IOException e) + { + System.err.println(e.toString()); + } + List xpathResults = new ArrayList(); + + xpathResults.addAll(doc.selectNodes("//a/@href")); + xpathResults.addAll(doc.selectNodes("//img/@src")); + + Map uniqueLinks = new HashMap(); + Iterator linkIter = xpathResults.iterator(); + while (linkIter.hasNext()) + { + Node node = (Node) linkIter.next(); + String href = node.getText(); + uniqueLinks.put(href, href); + } + + Iterator iter = uniqueLinks.keySet().iterator(); + while (iter.hasNext()) + { + String href = (String) iter.next(); + + //System.out.println("Link Found: " + href); + + LinkCheckResult lcr = new LinkCheckResult(); + + LinkValidationItem lvi = new LinkValidationItem(fileToCheck, href); + LinkValidationResult result = lvm.validateLink(lvi); + lcr.setTarget(href); + + switch (result.getStatus()) + { + case LinkValidationResult.UNKNOWN : + unsuccessful++; + lcr.setStatus("UNKNOWN REF"); + break; + case LinkValidationResult.VALID : + successful++; + lcr.setStatus("OK"); + break; + case LinkValidationResult.INVALID : + unsuccessful++; + lcr.setStatus("NOT FOUND"); + break; + } + + this.links.add(lcr); + } + } + catch (Exception e) + { + System.err.println(message); + throw (e); + } + } + + /** + * Returns the message. + * @return String + */ + public String getMessage() + { + return message; + } + + /** + * Returns the status. + * @return int + */ + public String getStatus() + { + return status; + } + + /** + * Sets the message. + * @param message The message to set + */ + public void setMessage(String message) + { + this.message = message; + } + + /** + * Sets the status. + * @param status The status to set + */ + public void setStatus(String status) + { + this.status = status; + } + + public List getResults() + { + return links; + } + + /** + * Returns the successful. + * @return int + */ + public int getSuccessful() + { + return successful; + } + + /** + * Returns the unsuccessful. + * @return int + */ + public int getUnsuccessful() + { + return unsuccessful; + } + + public String getName() + { + String baseName = base.getAbsolutePath(); + String fileName = fileToCheck.getAbsolutePath(); + if (fileName.startsWith(baseName)) + fileName = fileName.substring(baseName.length() + 1); + + fileName = fileName.replace('\\', '/'); + return fileName; + } + + public String toXML() + { + StringBuffer buf = new StringBuffer(); + + buf.append(" \n"); + buf.append(" " + getName() + "\n"); + buf.append(" " + getSuccessful() + "\n"); + buf.append(" " + getUnsuccessful() + "\n"); + + Iterator iter = getResults().iterator(); + while (iter.hasNext()) + { + LinkCheckResult result = (LinkCheckResult) iter.next(); + buf.append(result.toXML()); } - links.add(lcr); - } + buf.append(" \n"); + + return buf.toString(); } - catch (Exception e) - { - System.err.println(message); - throw (e); - } - } - - /** - * Returns the message. - * @return String - */ - public String getMessage() - { - return message; - } - - /** - * Returns the status. - * @return int - */ - public String getStatus() - { - return status; - } - - /** - * Sets the message. - * @param message The message to set - */ - public void setMessage(String message) - { - this.message = message; - } - - /** - * Sets the status. - * @param status The status to set - */ - public void setStatus(String status) - { - this.status = status; - } - - public List getResults() - { - return links; - } - - /** - * Returns the successful. - * @return int - */ - public int getSuccessful() - { - return successful; - } - - /** - * Returns the unsuccessful. - * @return int - */ - public int getUnsuccessful() - { - return unsuccessful; - } - - public String getName() - { - String baseName = base.getAbsolutePath(); - String fileName = fileToCheck.getAbsolutePath(); - if (fileName.startsWith(baseName)) - fileName = fileName.substring(baseName.length() + 1); - - fileName = fileName.replace('\\', '/'); - return fileName; - } - - public String toXML() - { - StringBuffer buf = new StringBuffer(); - - buf.append(" \n"); - buf.append(" " + getName() + "\n"); - buf.append(" " + getSuccessful() + "\n"); - buf.append(" " + getUnsuccessful() + "\n"); - - Iterator iter = getResults().iterator(); - while (iter.hasNext()) - { - LinkCheckResult result = (LinkCheckResult) iter.next(); - buf.append(result.toXML()); - } - - buf.append(" \n"); - - return buf.toString(); - } } diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java b/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java index a8952f3c..cc23c15f 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/LinkCheck.java @@ -74,14 +74,12 @@ import org.apache.maven.linkcheck.validation.LinkValidatorManager; import org.apache.maven.linkcheck.validation.MailtoLinkValidator; import org.apache.maven.project.Project; -import com.meterware.httpunit.HttpUnitOptions; - /** * The main bean to be called whenever a set of documents should have * their links checked. * * @author Ben Walding - * @version $Id: LinkCheck.java,v 1.6 2003/02/22 00:37:31 bwalding Exp $ + * @version $Id: LinkCheck.java,v 1.7 2003/03/07 08:27:02 bwalding Exp $ */ public class LinkCheck { @@ -139,7 +137,8 @@ public class LinkCheck List filesToCheck = null; //of FileToCheck public void doExecute() throws Exception { - HttpUnitOptions.setScriptingEnabled(false); + + if (output == null) { throw new NullPointerException("output must be set"); diff --git a/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java b/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java index 3fbd94a8..845c6c1c 100644 --- a/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/linkcheck/validation/HTTPLinkValidator.java @@ -56,19 +56,24 @@ package org.apache.maven.linkcheck.validation; * ==================================================================== */ +import java.io.File; +import java.net.URL; +import java.net.URLConnection; + +import org.apache.commons.httpclient.Credentials; +import org.apache.commons.httpclient.HostConfiguration; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpState; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - -import com.meterware.httpunit.Base64; -import com.meterware.httpunit.GetMethodWebRequest; -import com.meterware.httpunit.WebConversation; -import com.meterware.httpunit.WebRequest; -import com.meterware.httpunit.WebResponse; +import org.apache.maven.util.HttpUtils; /** * Checks links which are normal URLs * @author Ben Walding - * @version $Id: HTTPLinkValidator.java,v 1.3 2003/02/22 00:37:31 bwalding Exp $ + * @version $Id: HTTPLinkValidator.java,v 1.4 2003/03/07 08:27:02 bwalding Exp $ */ public class HTTPLinkValidator implements LinkValidator { @@ -77,10 +82,10 @@ public class HTTPLinkValidator implements LinkValidator */ private static Log LOG = LogFactory.getLog(HTTPLinkValidator.class); - private final static LinkValidationResult LVR_INVALID = + private static final LinkValidationResult LVR_INVALID = new LinkValidationResult(LinkValidationResult.INVALID, true); - private final static LinkValidationResult LVR_VALID = new LinkValidationResult(LinkValidationResult.VALID, true); + private static final LinkValidationResult LVR_VALID = new LinkValidationResult(LinkValidationResult.VALID, true); private boolean proxy; private String proxyHost; @@ -117,33 +122,48 @@ public class HTTPLinkValidator implements LinkValidator { String link = lvi.getLink(); LOG.debug("Checking web link:" + link); - WebConversation wc = new WebConversation(); - if (proxy) + + HttpClient cl = new HttpClient(); + HostConfiguration hc = new HostConfiguration(); + + if (proxyHost != null) { - wc.setProxyServer(proxyHost, proxyPort); - if (proxyUser != null) - { - String auth = proxyUser + ":" + proxyPass; - wc.setHeaderField("Proxy-Authorization", "Basic " + Base64.encode(auth)); - } + hc.setProxy(proxyHost, proxyPort); + } + HttpState state = new HttpState(); + + if (proxyUser != null && proxyPass != null) + { + state.setProxyCredentials(null, new UsernamePasswordCredentials(proxyUser, proxyPass)); } - wc.setExceptionsThrownOnErrorStatus(false); - WebRequest req = new GetMethodWebRequest(link); - WebResponse resp = wc.getResponse(req); + GetMethod get = new GetMethod(link); + cl.setState(state); + + // execute the GET + int status = 404; + try + { + status = cl.executeMethod(get); + } + catch (Exception e) + { + System.out.println(e); + } //FIXME: This constant is defined somewhere, but I can't remember where... - if (resp.getResponseCode() == 200) + if (status == 200) { return LVR_VALID; } else { - String msg = "Received: [" + resp.getResponseCode() + "] \"" + resp.getResponseMessage() + "\" for " + link; + String msg = "Received: [" + status + "] for " + link; LOG.info(msg); System.out.println(msg); return LVR_INVALID; } + } catch (Exception e) { diff --git a/linkcheck/src/test-resources/test-resources/nolink.html b/linkcheck/src/test-resources/test-resources/nolink.html deleted file mode 100644 index d44382c6..00000000 --- a/linkcheck/src/test-resources/test-resources/nolink.html +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/linkcheck/src/test-resources/test-resources/test1/test1.html b/linkcheck/src/test-resources/test-resources/test1/test1.html deleted file mode 100644 index 3d8e8c26..00000000 --- a/linkcheck/src/test-resources/test-resources/test1/test1.html +++ /dev/null @@ -1 +0,0 @@ -Fred.html \ No newline at end of file