diff --git a/linkcheck/project.xml b/linkcheck/project.xml index 18ae664d..a87a04b1 100644 --- a/linkcheck/project.xml +++ b/linkcheck/project.xml @@ -200,29 +200,10 @@ This library is already loaded by maven's core. Be careful to use the same version number as in the core. - - dom4j - dom4j - 1.4 - - This library is already loaded by maven's core. Be careful to use the same version number as in the core. - - - - jtidy - jtidy - 4aug2000r7-dev - maven maven 1.0.2 - - js-1.5R4-RC3.jar - rhino - rhino - 1.5R4-RC3 - diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/FileToCheck.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/FileToCheck.java index 74ed0771..d474345b 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/FileToCheck.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/FileToCheck.java @@ -17,29 +17,19 @@ package org.apache.maven.plugin.linkcheck; * ==================================================================== */ -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.InputStream; import java.io.OutputStream; -import java.io.PrintWriter; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; -import java.util.TreeSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.maven.plugin.linkcheck.validation.LinkValidationItem; import org.apache.maven.plugin.linkcheck.validation.LinkValidationResult; import org.apache.maven.plugin.linkcheck.validation.LinkValidatorManager; -import org.dom4j.Document; -import org.dom4j.Node; -import org.dom4j.io.DOMReader; -import org.w3c.tidy.Tidy; /** * @author Ben Walding @@ -73,30 +63,6 @@ public final class FileToCheck private int unsuccessful; - private Set getLinks() - throws FileNotFoundException - { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintWriter errOut = new PrintWriter( baos ); - BufferedInputStream bin = new BufferedInputStream( new FileInputStream( fileToCheck ) ); - try - { - Tidy tidy = getTidy(); - tidy.setErrout( errOut ); - org.w3c.dom.Document domDocument = tidy.parseDOM( bin, null ); - // now read a dom4j document from - // JTidy's W3C DOM object - final DOMReader domReader = new DOMReader(); - final Document doc = domReader.read( domDocument ); - return findUniqueLinks( doc ); - } - finally - { - close( bin ); - close( baos ); - } - } - /** * Returns the message. * @return String @@ -172,7 +138,7 @@ public final class FileToCheck final Set hrefs; try { - hrefs = getLinks(); + hrefs = LinkMatcher.match( fileToCheck ); } catch ( Throwable t ) { @@ -259,69 +225,4 @@ public final class FileToCheck { this.links.add( lcr ); } - - private void close( InputStream is ) - { - try - { - is.close(); - } - catch ( Exception e ) - { - //Don't really care. - } - } - - private void close( OutputStream os ) - { - try - { - os.close(); - } - catch ( Exception e ) - { - //Don't really care. - } - } - - private Set findUniqueLinks( Document doc ) - { - List xpathResults = new LinkedList(); - - xpathResults.addAll( doc.selectNodes( "//a/@href" ) ); - xpathResults.addAll( doc.selectNodes( "//img/@src" ) ); - - xpathResults.addAll( doc.selectNodes( "//link/@href" ) ); - - xpathResults.addAll( doc.selectNodes( "//script/@src" ) ); - - Set results = new TreeSet(); - Iterator linkIter = xpathResults.iterator(); - Node node = null; - String href = null; - while ( linkIter.hasNext() ) - { - node = (Node) linkIter.next(); - href = node.getText(); - results.add( href ); - } - xpathResults = null; - linkIter = null; - node = null; - href = null; - return results; - } - - private Tidy getTidy() - { - Tidy tidy = new Tidy(); - tidy.setMakeClean( true ); - tidy.setXmlTags( true ); - tidy.setXmlOut( true ); - tidy.setXHTML( true ); - tidy.setQuiet( true ); - tidy.setShowWarnings( false ); - return tidy; - } - -} \ No newline at end of file +} diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkMatcher.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkMatcher.java new file mode 100644 index 00000000..fed3a788 --- /dev/null +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkMatcher.java @@ -0,0 +1,122 @@ +package org.apache.maven.plugin.linkcheck; + +/* ==================================================================== + * Copyright 2001-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.Set; +import java.util.TreeSet; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Link matcher. Reads the contents of a file and tries to match the following: + * + * + * + * @author Ignacio G. Mac Dowell + */ +class LinkMatcher +{ + + /** + * Regexp for link matching. + */ + private final static Pattern p = Pattern + .compile( "<(?>link|a|img|script)[^>]*?(?>href|src)\\s*?=\\s*?[\\\"'](.*?)[\\\"'][^>]*?", + Pattern.CASE_INSENSITIVE ); + + /** + * No need to create a new object each time a file is processed. Just clear + * it. + */ + private final static Set linkList = new TreeSet(); + + /** + * Reads a file and returns a StringBuffer with its contents. + * + * TODO: Check for encoding issues + * + * TODO: Better exception handling? + * + * @param file + * the file we are reading + * @return a StringBuffer with file's contents. + * @throws IOException + */ + private static StringBuffer fileToStringBuffer( File file ) + throws IOException + { + BufferedReader reader = null; + final StringBuffer pageBuffer = new StringBuffer(); + try + { + reader = new BufferedReader( new FileReader( file ) ); + String line; + while ( ( line = reader.readLine() ) != null ) + { + pageBuffer.append( line ); + } + } + finally + { + reader.close(); + } + return pageBuffer; + } + + /** + * Performs the actual matching. + * + * @param file + * the file to check + * @return a set with all links to check + * @throws IOException + */ + static Set match( File file ) + throws IOException + { + linkList.clear(); + final Matcher m = p.matcher( fileToStringBuffer( file ) ); + String link; + while ( m.find() ) + { + link = m.group( 1 ).trim(); + if ( link.length() < 1 ) + { + continue; + } + else if ( link.toLowerCase().indexOf( "javascript" ) != -1 ) + { + continue; + } + // else if (link.toLowerCase().indexOf("mailto:") != -1) { + // continue; + // } + linkList.add( link ); + } + return linkList; + } + +} diff --git a/linkcheck/xdocs/changes.xml b/linkcheck/xdocs/changes.xml index 88ab91dd..03e4b21e 100644 --- a/linkcheck/xdocs/changes.xml +++ b/linkcheck/xdocs/changes.xml @@ -26,6 +26,8 @@ + Improve performance getting rid of jtidy dependency via regexps. + StackOverflowError processing apidocs/index-all.html. If maven is in offline mode the report doesn't test external urls. A warning is displayed in the report. "Moved Permanently" sites are reported as a warning and not as an error. Speed and stability enhancement [better usage of httpClient].