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 32807562..47bbeea5 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/FileToCheck.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/FileToCheck.java @@ -17,18 +17,18 @@ package org.apache.maven.plugin.linkcheck; * ==================================================================== */ -import java.io.File; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - 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 java.io.File; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + /** * @author Ben Walding * @author Arnaud Heritier @@ -63,19 +63,20 @@ public final class FileToCheck /** * Returns the message. + * * @return String */ public String getMessage() { - return message; + return this.message; } public String getName() { - String fileName = fileToCheck.getAbsolutePath(); - if ( fileName.startsWith( base ) ) + String fileName = this.fileToCheck.getAbsolutePath(); + if ( fileName.startsWith( this.base ) ) { - fileName = fileName.substring( base.length() + 1 ); + fileName = fileName.substring( this.base.length() + 1 ); } fileName = fileName.replace( '\\', '/' ); @@ -84,34 +85,37 @@ public final class FileToCheck public List getResults() { - return links; + return this.links; } /** * Returns the status. + * * @return int */ public String getStatus() { - return status; + return this.status; } /** * Returns the successful. + * * @return int */ public int getSuccessful() { - return successful; + return this.successful; } /** * Returns the unsuccessful. + * * @return int */ public int getUnsuccessful() { - return unsuccessful; + return this.unsuccessful; } public FileToCheck( File baseFile, File fileToCheck ) @@ -120,13 +124,12 @@ public final class FileToCheck this.fileToCheck = fileToCheck; } - public void check( LinkValidatorManager lvm ) - throws Exception + public void check( LinkValidatorManager lvm ) throws Exception { - successful = 0; - unsuccessful = 0; - status = STATUS_OK; - message = ""; + this.successful = 0; + this.unsuccessful = 0; + this.status = STATUS_OK; + this.message = ""; if ( LOG.isDebugEnabled() ) { LOG.debug( "Validating " + getName() ); @@ -136,16 +139,20 @@ public final class FileToCheck final Set hrefs; try { - hrefs = LinkMatcher.match( fileToCheck ); + hrefs = LinkMatcher.match( this.fileToCheck ); } catch ( Throwable t ) { - //We catch Throwable, because there is a chance that the domReader will throw - //a stack overflow exception for some files + // We catch Throwable, because there is a chance that the domReader will throw + // a stack overflow exception for some files if ( LOG.isDebugEnabled() ) + { LOG.error( "Received: [" + t + "] in page [" + getName() + "]", t ); + } else + { LOG.error( "Received: [" + t + "] in page [" + getName() + "]" ); + } LinkCheckResult lcr = new LinkCheckResult(); lcr.setStatus( "PARSE FAILURE" ); lcr.setTarget( "N/A" ); @@ -160,29 +167,30 @@ public final class FileToCheck { href = (String) iter.next(); lcr = new LinkCheckResult(); - lvi = new LinkValidationItem( fileToCheck, href ); + lvi = new LinkValidationItem( this.fileToCheck, href ); result = lvm.validateLink( lvi ); lcr.setTarget( href ); lcr.setErrorMessage( result.getErrorMessage() ); switch ( result.getStatus() ) { case LinkValidationResult.VALID: - successful++; + this.successful++; lcr.setStatus( "valid" ); - addResult( lcr ); //At some point we won't want to store valid links. The tests require that we do at present + addResult( lcr ); // At some point we won't want to store valid links. The tests require that + // we do at present break; case LinkValidationResult.UNKNOWN: - unsuccessful++; + this.unsuccessful++; lcr.setStatus( "unknown" ); addResult( lcr ); break; case LinkValidationResult.ERROR: - unsuccessful++; + this.unsuccessful++; lcr.setStatus( "error" ); addResult( lcr ); break; case LinkValidationResult.WARNING: - unsuccessful++; + this.unsuccessful++; lcr.setStatus( "warning" ); addResult( lcr ); break; @@ -196,8 +204,8 @@ public final class FileToCheck } catch ( Exception e ) { - LOG.error( message ); - throw ( e ); + LOG.error( this.message ); + throw e; } } diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheck.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheck.java index 46b53bfb..9044f025 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheck.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheck.java @@ -17,6 +17,16 @@ package org.apache.maven.plugin.linkcheck; * ==================================================================== */ +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.maven.jelly.MavenJellyContext; +import org.apache.maven.plugin.linkcheck.validation.FileLinkValidator; +import org.apache.maven.plugin.linkcheck.validation.LinkValidatorManager; +import org.apache.maven.plugin.linkcheck.validation.MailtoLinkValidator; +import org.apache.maven.plugin.linkcheck.validation.OfflineHTTPLinkValidator; +import org.apache.maven.plugin.linkcheck.validation.OnlineHTTPLinkValidator; +import org.apache.maven.project.Project; + import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -30,19 +40,8 @@ import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.maven.jelly.MavenJellyContext; -import org.apache.maven.plugin.linkcheck.validation.FileLinkValidator; -import org.apache.maven.plugin.linkcheck.validation.LinkValidatorManager; -import org.apache.maven.plugin.linkcheck.validation.MailtoLinkValidator; -import org.apache.maven.plugin.linkcheck.validation.OfflineHTTPLinkValidator; -import org.apache.maven.plugin.linkcheck.validation.OnlineHTTPLinkValidator; -import org.apache.maven.project.Project; - /** - * The main bean to be called whenever a set of documents should have - * their links checked. + * The main bean to be called whenever a set of documents should have their links checked. * * @author Ben Walding * @author Carlos Sanchez @@ -83,12 +82,12 @@ public final class LinkCheck private LinkValidatorManager lvm = null; /** - * Output file for xml document + * Output file for xml document */ private File output; /** - * Output encoding for the xml document + * Output encoding for the xml document */ private String outputEncoding; @@ -96,17 +95,19 @@ public final class LinkCheck /** * Get the base directory for the change log generator. - * + * * @return the base directory */ public File getBasedir() { - return basedir; + return this.basedir; } /** * Set the base directory for the change log generator. - * @param base the base directory + * + * @param base + * the base directory */ public void setBasedir( File base ) { @@ -115,16 +116,19 @@ public final class LinkCheck /** * Returns the cacheFile. + * * @return String */ public String getCache() { - return cache; + return this.cache; } /** * Sets the cacheFile. - * @param cacheFile The cacheFile to set + * + * @param cacheFile + * The cacheFile to set */ public void setCache( String cache ) { @@ -133,17 +137,17 @@ public final class LinkCheck /** * Returns the exclude. + * * @return String */ public String getExclude() { - return exclude; + return this.exclude; } /** - * Sets the exclude, a string with exclude locations delimited by the space - * character, the comma character, the tab character, the newline character, - * the carriage-return character, and the form-feed character. + * Sets the exclude, a string with exclude locations delimited by the space character, the comma character, the tab + * character, the newline character, the carriage-return character, and the form-feed character. * * @param exclude * The exclude to set @@ -158,11 +162,12 @@ public final class LinkCheck */ public String getMethod() { - return method; + return this.method; } /** - * @param method the method to set + * @param method + * the method to set */ public void setMethod( String method ) { @@ -171,60 +176,59 @@ public final class LinkCheck public List getFiles() { - return filesToCheck; + return this.filesToCheck; } public LinkValidatorManager getLinkValidatorManager() { - if ( lvm == null ) + if ( this.lvm == null ) { - lvm = new LinkValidatorManager(); - if ( exclude != null ) + this.lvm = new LinkValidatorManager(); + if ( this.exclude != null ) { - StringTokenizer st = new StringTokenizer( exclude, " ,\t\n\r\f" ); + StringTokenizer st = new StringTokenizer( this.exclude, " ,\t\n\r\f" ); String[] tokens = new String[st.countTokens()]; for ( int i = 0; i < tokens.length; i++ ) { tokens[i] = st.nextToken(); } - lvm.setExcludes( tokens ); + this.lvm.setExcludes( tokens ); tokens = null; st = null; } - lvm.addLinkValidator( new FileLinkValidator() ); + this.lvm.addLinkValidator( new FileLinkValidator() ); // Project is only null in test cases if ( getProject() != null ) { MavenJellyContext ctx = ( (Project) getProject() ).getContext(); if ( ctx.getOnline().booleanValue() ) { - lvm - .addLinkValidator( new OnlineHTTPLinkValidator( - getMethod(), - (String) ctx.getVariable( MAVEN_PROXY_HOST ), - (String) ctx.getVariable( MAVEN_PROXY_PORT ), - (String) ctx.getVariable( MAVEN_PROXY_USERNAME ), - (String) ctx.getVariable( MAVEN_PROXY_PASSWORD ), - (String) ctx - .getVariable( MAVEN_PROXY_NTLM_HOST ), - (String) ctx - .getVariable( MAVEN_PROXY_NTLM_DOMAIN ) ) ); + this.lvm.addLinkValidator( new OnlineHTTPLinkValidator( + getMethod(), + (String) ctx.getVariable( MAVEN_PROXY_HOST ), + (String) ctx.getVariable( MAVEN_PROXY_PORT ), + (String) ctx.getVariable( MAVEN_PROXY_USERNAME ), + (String) ctx.getVariable( MAVEN_PROXY_PASSWORD ), + (String) ctx.getVariable( MAVEN_PROXY_NTLM_HOST ), + (String) ctx.getVariable( MAVEN_PROXY_NTLM_DOMAIN ) ) ); } else { - lvm.addLinkValidator( new OfflineHTTPLinkValidator() ); + this.lvm.addLinkValidator( new OfflineHTTPLinkValidator() ); } ctx = null; } - lvm.addLinkValidator( new MailtoLinkValidator() ); + this.lvm.addLinkValidator( new MailtoLinkValidator() ); } - return lvm; + return this.lvm; } /** * Set the output file for the log. - * @param output the output file + * + * @param output + * the output file */ public void setOutput( File output ) { @@ -233,16 +237,19 @@ public final class LinkCheck /** * Returns the outputEncoding. + * * @return String */ public String getOutputEncoding() { - return outputEncoding; + return this.outputEncoding; } /** * Sets the outputEncoding. - * @param outputEncoding The outputEncoding to set + * + * @param outputEncoding + * The outputEncoding to set */ public void setOutputEncoding( String outputEncoding ) { @@ -254,12 +261,14 @@ public final class LinkCheck */ public Object getProject() { - return project; + return this.project; } /** * Sets the project. - * @param project The project to set + * + * @param project + * The project to set */ public void setProject( Object project ) { @@ -285,7 +294,7 @@ public final class LinkCheck { LOG.debug( " File - " + file ); } - allFiles.add( new FileToCheck( basedir, file ) ); + allFiles.add( new FileToCheck( this.basedir, file ) ); if ( allFiles.size() % 100 == 0 ) { LOG.info( "Found " + allFiles.size() + " files so far." ); @@ -299,16 +308,18 @@ public final class LinkCheck /** * Execute task. - * @throws FileNotFoundException if {@link ChangeLog#base} doesn't exist - * @throws IOException if there are problems running CVS - * @throws UnsupportedEncodingException if the underlying platform doesn't - * support ISO-8859-1 encoding + * + * @throws FileNotFoundException + * if {@link ChangeLog#base} doesn't exist + * @throws IOException + * if there are problems running CVS + * @throws UnsupportedEncodingException + * if the underlying platform doesn't support ISO-8859-1 encoding */ - public void doExecute() - throws FileNotFoundException, IOException, UnsupportedEncodingException + public void doExecute() throws FileNotFoundException, IOException, UnsupportedEncodingException { - if ( output == null ) + if ( this.output == null ) { throw new NullPointerException( "output must be set" ); } @@ -317,11 +328,11 @@ public final class LinkCheck displayMemoryConsumption(); } LinkValidatorManager validator = getLinkValidatorManager(); - filesToCheck = new LinkedList(); - validator.loadCache( cache ); + this.filesToCheck = new LinkedList(); + validator.loadCache( this.cache ); List files = new LinkedList(); LOG.debug( "Locating all files to be checked..." ); - findFiles( files, basedir ); + findFiles( files, this.basedir ); LOG.debug( "Located all files to be checked." ); LOG.info( "Found " + files.size() + " files to check." ); displayMemoryConsumption(); @@ -333,7 +344,7 @@ public final class LinkCheck flc = (FileToCheck) fileIter.next(); try { - filesToCheck.add( flc ); + this.filesToCheck.add( flc ); flc.check( validator ); } catch ( Exception e ) @@ -345,7 +356,7 @@ public final class LinkCheck LOG.info( "Links checked." ); displayMemoryConsumption(); createDocument( files ); - validator.saveCache( cache ); + validator.saveCache( this.cache ); displayMemoryConsumption(); } @@ -369,28 +380,29 @@ public final class LinkCheck if ( LOG.isDebugEnabled() ) { Runtime r = Runtime.getRuntime(); - LOG.debug( "Memory: " + ( ( r.totalMemory() - r.freeMemory() ) / MEG ) + "M/" + ( r.totalMemory() / MEG ) - + "M" ); + LOG.debug( "Memory: " + ( r.totalMemory() - r.freeMemory() ) / MEG + "M/" + r.totalMemory() / MEG + + "M" ); } } /** * Create the XML document from the currently available details - * @throws FileNotFoundException when the output file previously provided - * does not exist - * @throws UnsupportedEncodingException when the platform doesn't support - * ISO-8859-1 encoding + * + * @throws FileNotFoundException + * when the output file previously provided does not exist + * @throws UnsupportedEncodingException + * when the platform doesn't support ISO-8859-1 encoding */ - private void createDocument( List files ) - throws FileNotFoundException, UnsupportedEncodingException + private void createDocument( List files ) throws FileNotFoundException, UnsupportedEncodingException { - File dir = output.getParentFile(); + File dir = this.output.getParentFile(); if ( dir != null ) { dir.mkdirs(); } - PrintWriter out = new PrintWriter( new OutputStreamWriter( new FileOutputStream( output ), getOutputEncoding() ) ); + PrintWriter out = + new PrintWriter( new OutputStreamWriter( new FileOutputStream( this.output ), getOutputEncoding() ) ); StringBuffer buffer = new StringBuffer(); buffer.append( "\n" ); out.write( buffer.toString() ); @@ -402,8 +414,7 @@ public final class LinkCheck } /** Custom FilenameFilter used to search html files */ - static class CustomFilenameFilter - implements FilenameFilter + static class CustomFilenameFilter implements FilenameFilter { /** * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String) @@ -412,7 +423,9 @@ public final class LinkCheck { File n = new File( dir, name ); if ( n.isDirectory() ) + { return true; + } if ( name.endsWith( ".html" ) ) { return true; diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheckResult.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheckResult.java index 31fa9506..49b25cec 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheckResult.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkCheckResult.java @@ -21,6 +21,7 @@ import org.apache.commons.lang.StringEscapeUtils; /** * An class containing the results of a single check of a link. + * * @author Ben Walding * @author Arnaud Heritier * @version $Id$ @@ -35,16 +36,19 @@ public final class LinkCheckResult /** * Returns the status. + * * @return String */ public String getStatus() { - return status; + return this.status; } /** * Sets the status. - * @param status The status to set + * + * @param status + * The status to set */ public void setStatus( String status ) { @@ -53,16 +57,19 @@ public final class LinkCheckResult /** * Returns the target. + * * @return String */ public String getTarget() { - return target; + return this.target; } /** * Sets the target. - * @param target The target to set + * + * @param target + * The target to set */ public void setTarget( String target ) { @@ -74,11 +81,12 @@ public final class LinkCheckResult */ public String getErrorMessage() { - return errorMessage; + return this.errorMessage; } /** - * @param errorMessage The errorMessage to set. + * @param errorMessage + * The errorMessage to set. */ public void setErrorMessage( String errorMessage ) { @@ -87,6 +95,7 @@ public final class LinkCheckResult /** * Creates an XML representation of this link check result + * * @return xml fragment representation of this result */ public String toXML() diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkMatcher.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkMatcher.java index fed3a788..fb83bf3e 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkMatcher.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/LinkMatcher.java @@ -27,8 +27,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * Link matcher. Reads the contents of a file and tries to match the following: - * + * Link matcher. Reads the contents of a file and tries to match the following: * link|a|img|script)[^>]*?(?>href|src)\\s*?=\\s*?[\\\"'](.*?)[\\\"'][^>]*?", - Pattern.CASE_INSENSITIVE ); + 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. + * No need to create a new object each time a file is processed. Just clear it. */ private final static Set linkList = new TreeSet(); @@ -65,8 +63,7 @@ class LinkMatcher * @return a StringBuffer with file's contents. * @throws IOException */ - private static StringBuffer fileToStringBuffer( File file ) - throws IOException + private static StringBuffer fileToStringBuffer( File file ) throws IOException { BufferedReader reader = null; final StringBuffer pageBuffer = new StringBuffer(); @@ -94,8 +91,7 @@ class LinkMatcher * @return a set with all links to check * @throws IOException */ - static Set match( File file ) - throws IOException + static Set match( File file ) throws IOException { linkList.clear(); final Matcher m = p.matcher( fileToStringBuffer( file ) ); diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/FileLinkValidator.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/FileLinkValidator.java index d1e054e6..3ab73618 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/FileLinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/FileLinkValidator.java @@ -26,8 +26,7 @@ import java.io.File; * @author Arnaud Heritier * @version $Id$ */ -public final class FileLinkValidator - implements LinkValidator +public final class FileLinkValidator implements LinkValidator { /** * @see org.apache.maven.plugin.linkcheck.LinkValidator#validateLink(org.apache.maven.plugin.linkcheck.LinkValidationItem) @@ -36,9 +35,13 @@ public final class FileLinkValidator { File f = getFile( lvi ); if ( f.exists() ) + { return new LinkValidationResult( LinkValidationResult.VALID, false, "" ); + } else + { return new LinkValidationResult( LinkValidationResult.ERROR, false, "doesn't exist." ); + } } /** @@ -49,7 +52,9 @@ public final class FileLinkValidator String link = lvi.getLink(); // If we find an http(s) link or a mail link, it's not good if ( link.startsWith( "http://" ) || link.startsWith( "https://" ) || link.indexOf( '@' ) != -1 ) + { return null; + } return getFile( lvi ).getAbsolutePath(); } @@ -65,19 +70,23 @@ public final class FileLinkValidator { link = link.substring( 0, link.indexOf( '#' ) ); - //If the link was just #fred or similar, then the file is the file it came from - //XXX: Theoretically we could even validate the anchor tag? + // If the link was just #fred or similar, then the file is the file it came from + // XXX: Theoretically we could even validate the anchor tag? if ( link.trim().length() == 0 ) + { return lvi.getSource(); + } } if ( link.indexOf( '?' ) != -1 ) { link = link.substring( 0, link.indexOf( '?' ) ); - //If the link was just ?param=something or similar, then the file is the file it came from - //XXX: Theoretically we could even validate the anchor tag? + // If the link was just ?param=something or similar, then the file is the file it came from + // XXX: Theoretically we could even validate the anchor tag? if ( link.trim().length() == 0 ) + { return lvi.getSource(); + } } File f = new File( lvi.getSource().getParentFile(), link ); return f; diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidator.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidator.java index 7d7edd49..edf275c4 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidator.java @@ -17,15 +17,14 @@ package org.apache.maven.plugin.linkcheck.validation; * ==================================================================== */ - /** * Checks links which are normal URLs + * * @author Ben Walding * @author Arnaud Heritier * @version $Id$ */ -public abstract class HTTPLinkValidator - implements LinkValidator +public abstract class HTTPLinkValidator implements LinkValidator { /** * @see org.apache.maven.plugin.linkcheck.LinkValidator#getResourceKey(org.apache.maven.plugin.linkcheck.LinkValidationItem) @@ -34,10 +33,14 @@ public abstract class HTTPLinkValidator { String link = lvi.getLink(); if ( !link.startsWith( "http://" ) && !link.startsWith( "https://" ) ) + { return null; + } int hashPos = link.indexOf( "#" ); if ( hashPos != -1 ) + { link = link.substring( 0, hashPos ); + } return link; } diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationItem.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationItem.java index 6b584538..39a9798a 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationItem.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationItem.java @@ -16,7 +16,7 @@ package org.apache.maven.plugin.linkcheck.validation; * limitations under the License. * ==================================================================== */ - + import java.io.File; /** @@ -24,59 +24,74 @@ import java.io.File; * @author Arnaud Heritier * @version $Id$ */ -public class LinkValidationItem { +public class LinkValidationItem +{ private final File source; + private final String link; - - public LinkValidationItem(File source, String link) { - if (source == null) { - throw new NullPointerException("source can't be null"); + + public LinkValidationItem( File source, String link ) + { + if ( source == null ) + { + throw new NullPointerException( "source can't be null" ); } - - if (link == null) { - throw new NullPointerException("link can't be null"); + + if ( link == null ) + { + throw new NullPointerException( "link can't be null" ); } - + this.source = source; this.link = link; } /** * Returns the link. + * * @return String */ - public String getLink() { - return link; + public String getLink() + { + return this.link; } /** * Returns the source. + * * @return File */ - public File getSource() { - return source; + public File getSource() + { + return this.source; } /** * @see java.lang.Object#equals(java.lang.Object) */ - public boolean equals(Object obj) { + public boolean equals( Object obj ) + { LinkValidationItem lvi = (LinkValidationItem) obj; - - if (!lvi.link.equals(link)) + + if ( !lvi.link.equals( this.link ) ) + { return false; - - if (!lvi.source.equals(source)) + } + + if ( !lvi.source.equals( this.source ) ) + { return false; - + } + return true; } /** * @see java.lang.Object#hashCode() */ - public int hashCode() { - return source.hashCode() ^ link.hashCode(); + public int hashCode() + { + return this.source.hashCode() ^ this.link.hashCode(); } - + } diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationResult.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationResult.java index bc08d8ab..5ea876d6 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationResult.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidationResult.java @@ -21,17 +21,21 @@ import java.io.Serializable; /** *

- * This class is used to return status responses from the - * validation handlers. A persistent result means that it - * can be stored in the persistent cache and used across runs. + * This class is used to return status responses from the validation handlers. A persistent result means that it can be + * stored in the persistent cache and used across runs. *

+ * * @author Ben Walding * @author Arnaud Heritier * @version $Id$ */ -public class LinkValidationResult - implements Serializable +public class LinkValidationResult implements Serializable { + /** + * + */ + private static final long serialVersionUID = -8346824125135406813L; + public static final int NOTMINE = 0; public static final int ERROR = 1; @@ -50,16 +54,17 @@ public class LinkValidationResult public boolean isPersistent() { - return persistent; + return this.persistent; } /** * Returns the status. + * * @return int */ public int getStatus() { - return status; + return this.status; } /** @@ -67,7 +72,7 @@ public class LinkValidationResult */ public String getErrorMessage() { - return errorMessage; + return this.errorMessage; } public LinkValidationResult( int status, boolean persistent, String errorMessage ) @@ -77,12 +82,14 @@ public class LinkValidationResult this.errorMessage = errorMessage; } - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see java.lang.Object#toString() */ public String toString() { - return persistent + "/" + status + "/" + errorMessage; + return this.persistent + "/" + this.status + "/" + this.errorMessage; } } diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidator.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidator.java index 808de93b..fde44528 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidator.java @@ -27,16 +27,17 @@ public interface LinkValidator /** * If getResource(lvi) returned null, this will NOT be called. + * * @param lvi * @return int * @throws Exception */ - public LinkValidationResult validateLink( LinkValidationItem lvi ) - throws Exception; + public LinkValidationResult validateLink( LinkValidationItem lvi ) throws Exception; /** - * The resource key is used by the cache to determine if it really needs to - * validate the link. No actual validation should be done at this point. + * The resource key is used by the cache to determine if it really needs to validate the link. No actual validation + * should be done at this point. + * * @param lvi * @return Object null if this validator should not be doing this work. * @throws Exception diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidatorManager.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidatorManager.java index 71a7b374..6176cda4 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidatorManager.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/LinkValidatorManager.java @@ -17,6 +17,9 @@ package org.apache.maven.plugin.linkcheck.validation; * ==================================================================== */ +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -30,9 +33,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - /** * @author Ben Walding * @author Carlos Sanchez @@ -40,9 +40,13 @@ import org.apache.commons.logging.LogFactory; * @version $Id$ */ -public class LinkValidatorManager - implements Serializable +public class LinkValidatorManager implements Serializable { + /** + * + */ + private static final long serialVersionUID = 2467928182206500945L; + /** * Log for debug output */ @@ -60,22 +64,25 @@ public class LinkValidatorManager public List getValidators() { - return validators; + return this.validators; } /** * Returns the exclude. + * * @return String * @deprecated use getExcludes() */ public String getExclude() { - return excludes[0]; + return this.excludes[0]; } /** * Sets the exclude. - * @param exclude The exclude to set + * + * @param exclude + * The exclude to set * @deprecated use setExcludes() */ public void setExclude( String exclude ) @@ -85,16 +92,19 @@ public class LinkValidatorManager /** * Returns the excludes. + * * @return String[] */ public String[] getExcludes() { - return excludes; + return this.excludes; } /** * Sets the excludes. - * @param excludes The excludes to set + * + * @param excludes + * The excludes to set */ public void setExcludes( String[] excludes ) { @@ -103,11 +113,10 @@ public class LinkValidatorManager public void addLinkValidator( LinkValidator lv ) { - validators.add( lv ); + this.validators.add( lv ); } - public LinkValidationResult validateLink( LinkValidationItem lvi ) - throws Exception + public LinkValidationResult validateLink( LinkValidationItem lvi ) throws Exception { { LinkValidationResult status = getCachedResult( lvi ); @@ -117,9 +126,9 @@ public class LinkValidatorManager } } - for ( int i = 0; i < excludes.length; i++ ) + for ( int i = 0; i < this.excludes.length; i++ ) { - if ( excludes[i] != null && lvi.getLink().startsWith( excludes[i] ) ) + if ( this.excludes[i] != null && lvi.getLink().startsWith( this.excludes[i] ) ) { if ( LOG.isDebugEnabled() ) { @@ -128,7 +137,7 @@ public class LinkValidatorManager return new LinkValidationResult( LinkValidationResult.VALID, false, "" ); } } - Iterator iter = validators.iterator(); + Iterator iter = this.validators.iterator(); LinkValidator lv; Object resourceKey; LinkValidationResult lvr; @@ -139,7 +148,9 @@ public class LinkValidatorManager if ( resourceKey != null ) { if ( LOG.isDebugEnabled() ) + { LOG.debug( lv.getClass().getName() + " - Checking link " + lvi.getLink() ); + } lvr = lv.validateLink( lvi ); if ( lvr.getStatus() == LinkValidationResult.NOTMINE ) { @@ -189,12 +200,14 @@ public class LinkValidatorManager while ( iter.hasNext() ) { resourceKey = iter.next(); - if ( ( (LinkValidationResult) cache.get( resourceKey ) ).isPersistent() ) + if ( ( (LinkValidationResult) this.cache.get( resourceKey ) ).isPersistent() ) { - persistentCache.put( resourceKey, cache.get( resourceKey ) ); + persistentCache.put( resourceKey, this.cache.get( resourceKey ) ); if ( LOG.isDebugEnabled() ) - LOG.debug( "[" + resourceKey + "] with result [" + cache.get( resourceKey ) - + "] is stored in the cache." ); + { + LOG.debug( "[" + resourceKey + "] with result [" + this.cache.get( resourceKey ) + + "] is stored in the cache." ); + } } } File cacheFile = new File( cacheFilename ); @@ -233,12 +246,14 @@ public class LinkValidatorManager { lv = (LinkValidator) iter.next(); resourceKey = lv.getResourceKey( lvi ); - if ( resourceKey != null && cache.containsKey( resourceKey ) ) + if ( resourceKey != null && this.cache.containsKey( resourceKey ) ) { if ( LOG.isDebugEnabled() ) - LOG.debug( "The cache returns for [" + resourceKey + "] the result [" + cache.get( resourceKey ) - + "]." ); - return (LinkValidationResult) cache.get( resourceKey ); + { + LOG.debug( "The cache returns for [" + resourceKey + "] the result [" + this.cache.get( resourceKey ) + + "]." ); + } + return (LinkValidationResult) this.cache.get( resourceKey ); } } lv = null; @@ -248,6 +263,6 @@ public class LinkValidatorManager public void setCachedResult( Object resourceKey, LinkValidationResult lvr ) { - cache.put( resourceKey, lvr ); + this.cache.put( resourceKey, lvr ); } } diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/MailtoLinkValidator.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/MailtoLinkValidator.java index 79806d22..ead6d459 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/MailtoLinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/MailtoLinkValidator.java @@ -24,16 +24,14 @@ package org.apache.maven.plugin.linkcheck.validation; * * Validates mailto links */ -public final class MailtoLinkValidator - implements LinkValidator +public final class MailtoLinkValidator implements LinkValidator { private static final LinkValidationResult LVR = new LinkValidationResult( LinkValidationResult.VALID, false, "" ); /** * @see org.apache.maven.plugin.linkcheck.validation.LinkValidator#validateLink(org.apache.maven.plugin.linkcheck.validation.LinkValidationItem) */ - public LinkValidationResult validateLink( LinkValidationItem lvi ) - throws Exception + public LinkValidationResult validateLink( LinkValidationItem lvi ) throws Exception { return LVR; } @@ -44,9 +42,13 @@ public final class MailtoLinkValidator public Object getResourceKey( LinkValidationItem lvi ) { if ( lvi.getLink().indexOf( '@' ) != -1 ) + { return lvi.getLink(); + } else + { return null; + } } } diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OfflineHTTPLinkValidator.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OfflineHTTPLinkValidator.java index ec31b668..7aef4953 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OfflineHTTPLinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OfflineHTTPLinkValidator.java @@ -19,12 +19,11 @@ package org.apache.maven.plugin.linkcheck.validation; /** * Fake links check when maven is offline + * * @author Arnaud Heritier * @version $Id: HTTPLinkValidator.java 155277 2005-02-24 23:23:29Z aheritier $ */ -public final class OfflineHTTPLinkValidator - extends HTTPLinkValidator - implements LinkValidator +public final class OfflineHTTPLinkValidator extends HTTPLinkValidator implements LinkValidator { /** diff --git a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java index 10a1f38d..1df44aa6 100644 --- a/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java +++ b/linkcheck/src/main/org/apache/maven/plugin/linkcheck/validation/OnlineHTTPLinkValidator.java @@ -3,9 +3,6 @@ */ package org.apache.maven.plugin.linkcheck.validation; -import java.io.IOException; -import java.net.URL; - import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HostConfiguration; @@ -23,14 +20,17 @@ import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.io.IOException; +import java.net.URL; + /** * Checks links which are normal URLs + * * @author Ben Walding * @author Arnaud Heritier * @version $Id: HTTPLinkValidator.java 155277 2005-02-24 23:23:29Z aheritier $ */ -public final class OnlineHTTPLinkValidator - extends HTTPLinkValidator +public final class OnlineHTTPLinkValidator extends HTTPLinkValidator { /** * Log for debug output @@ -66,7 +66,9 @@ public final class OnlineHTTPLinkValidator String proxyPass, String proxyNtlmHost, String proxyNtlmDomain ) { if ( LOG.isDebugEnabled() ) + { LOG.debug( "Will use method : [" + method + "]" ); + } this.method = method; if ( proxyHost == null || proxyHost.trim().equals( "" ) ) { @@ -104,56 +106,58 @@ public final class OnlineHTTPLinkValidator */ public LinkValidationResult validateLink( LinkValidationItem lvi ) { - if ( cl == null ) + if ( this.cl == null ) + { initHttpClient(); + } try { String link = lvi.getLink(); HttpMethod hm = null; try { - hm = checkLink( cl, link, 0 ); + hm = checkLink( this.cl, link, 0 ); } catch ( Throwable t ) { if ( LOG.isDebugEnabled() ) { - LOG - .error( "Received: [" + t + "] for [" + lvi.getLink() + "] in page [" + lvi.getSource() + "]", - t ); + LOG.error( "Received: [" + t + "] for [" + lvi.getLink() + "] in page [" + lvi.getSource() + "]", t ); } else { LOG.error( "Received: [" + t + "] for [" + lvi.getLink() + "] in page [" + lvi.getSource() + "]" ); } return new LinkValidationResult( LinkValidationResult.ERROR, false, t.getClass().getName() + " : " - + t.getMessage() ); + + t.getMessage() ); } if ( hm == null ) + { return new LinkValidationResult( LinkValidationResult.ERROR, false, "Cannot retreive HTTP Status" ); + } if ( hm.getStatusCode() == HttpStatus.SC_OK ) { return new LinkValidationResult( LinkValidationResult.VALID, true, hm.getStatusCode() + " " - + hm.getStatusText() ); + + hm.getStatusText() ); } else { // If there's a redirection ... add a warning if ( hm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY - || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY - || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT ) + || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY + || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT ) { LOG.warn( "Received: [" + hm.getStatusCode() + "] for [" + lvi.getLink() + "] in page [" - + lvi.getSource() + "]" ); + + lvi.getSource() + "]" ); return new LinkValidationResult( LinkValidationResult.WARNING, true, hm.getStatusCode() + " " - + hm.getStatusText() ); + + hm.getStatusText() ); } else { LOG.error( "Received: [" + hm.getStatusCode() + "] for [" + lvi.getLink() + "] in page [" - + lvi.getSource() + "]" ); + + lvi.getSource() + "]" ); return new LinkValidationResult( LinkValidationResult.ERROR, false, hm.getStatusCode() + " " - + hm.getStatusText() ); + + hm.getStatusText() ); } } @@ -161,9 +165,13 @@ public final class OnlineHTTPLinkValidator catch ( Throwable t ) { if ( LOG.isDebugEnabled() ) + { LOG.error( "Received: [" + t + "] for [" + lvi.getLink() + "] in page [" + lvi.getSource() + "]", t ); + } else + { LOG.error( "Received: [" + t + "] for [" + lvi.getLink() + "] in page [" + lvi.getSource() + "]" ); + } return new LinkValidationResult( LinkValidationResult.ERROR, false, t.getMessage() ); } } @@ -173,7 +181,7 @@ public final class OnlineHTTPLinkValidator LOG.debug( "A new HttpClient instance is needed ..." ); // Some web servers doesn't allow the default user-agent sent by httpClient System.setProperty( "httpclient.useragent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" ); - cl = new HttpClient( new MultiThreadedHttpConnectionManager() ); + this.cl = new HttpClient( new MultiThreadedHttpConnectionManager() ); HostConfiguration hc = new HostConfiguration(); HttpState state = new HttpState(); if ( this.proxyHost != null ) @@ -181,18 +189,20 @@ public final class OnlineHTTPLinkValidator hc.setProxy( this.proxyHost, this.proxyPort ); if ( LOG.isDebugEnabled() ) { - LOG.debug( "Proxy Host:" + proxyHost ); - LOG.debug( "Proxy Port:" + proxyPort ); + LOG.debug( "Proxy Host:" + this.proxyHost ); + LOG.debug( "Proxy Port:" + this.proxyPort ); } if ( this.proxyUser != null && this.proxyPass != null ) { if ( LOG.isDebugEnabled() ) - LOG.debug( "Proxy User:" + proxyUser ); + { + LOG.debug( "Proxy User:" + this.proxyUser ); + } Credentials credentials; if ( this.proxyNtlmHost != null ) { - credentials = new NTCredentials( this.proxyUser, this.proxyPass, this.proxyNtlmHost, - this.proxyNtlmDomain ); + credentials = + new NTCredentials( this.proxyUser, this.proxyPass, this.proxyNtlmHost, this.proxyNtlmDomain ); } else { @@ -206,23 +216,26 @@ public final class OnlineHTTPLinkValidator { LOG.debug( "Not using a proxy" ); } - cl.setHostConfiguration( hc ); - cl.setState( state ); + this.cl.setHostConfiguration( hc ); + this.cl.setState( state ); LOG.debug( "New HttpClient instance created." ); } - private HttpMethod checkLink( HttpClient cl, String link, int nbRedirect ) - throws HttpException, IOException + private HttpMethod checkLink( HttpClient cl, String link, int nbRedirect ) throws HttpException, IOException { if ( nbRedirect > MAX_NB_REDIRECT ) { throw new HttpException( "Maximum number of redirections (" + MAX_NB_REDIRECT + ") exceeded" ); } HttpMethod hm; - if ( HEAD_METHOD.equals( method ) ) + if ( HEAD_METHOD.equals( this.method ) ) + { hm = new HeadMethod( link ); + } else + { hm = new GetMethod( link ); + } try { // We want to do it manually @@ -239,8 +252,8 @@ public final class OnlineHTTPLinkValidator } if ( hm.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY - || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY - || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT ) + || hm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY + || hm.getStatusCode() == HttpStatus.SC_TEMPORARY_REDIRECT ) { Header locationHeader = hm.getResponseHeader( "location" ); if ( locationHeader == null ) @@ -257,8 +270,9 @@ public final class OnlineHTTPLinkValidator if ( newLink.startsWith( "/" ) ) { URL oldUrl = new URL( link ); - newLink = oldUrl.getProtocol() + "://" + oldUrl.getHost() - + ( oldUrl.getPort() > 0 ? ":" + oldUrl.getPort() : "" ) + newLink; + newLink = + oldUrl.getProtocol() + "://" + oldUrl.getHost() + + ( oldUrl.getPort() > 0 ? ":" + oldUrl.getPort() : "" ) + newLink; } else { @@ -269,9 +283,9 @@ public final class OnlineHTTPLinkValidator LOG.info( "[" + link + "] is redirected to [" + newLink + "]" ); oldHm.releaseConnection(); hm = checkLink( cl, newLink, nbRedirect + 1 ); - // Restore the hm to "Moved permanently" | "Moved temporarily" | "Temporary redirect" + // Restore the hm to "Moved permanently" | "Moved temporarily" | "Temporary redirect" // if the new location is found to allow us to report it - if ( hm.getStatusCode() == HttpStatus.SC_OK && nbRedirect == 0) + if ( hm.getStatusCode() == HttpStatus.SC_OK && nbRedirect == 0 ) { return oldHm; } diff --git a/linkcheck/src/test/org/apache/maven/plugin/linkcheck/LinkCheckTest.java b/linkcheck/src/test/org/apache/maven/plugin/linkcheck/LinkCheckTest.java index e08463e8..f028d1fd 100644 --- a/linkcheck/src/test/org/apache/maven/plugin/linkcheck/LinkCheckTest.java +++ b/linkcheck/src/test/org/apache/maven/plugin/linkcheck/LinkCheckTest.java @@ -17,6 +17,10 @@ package org.apache.maven.plugin.linkcheck; * ==================================================================== */ +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + import java.io.File; import java.util.HashMap; import java.util.Iterator; @@ -24,10 +28,6 @@ import java.util.Map; import junit.framework.TestCase; -import org.apache.log4j.BasicConfigurator; -import org.apache.log4j.Level; -import org.apache.log4j.Logger; - /** * @author Ben Walding * @author Carlos Sanchez @@ -44,7 +44,7 @@ public class LinkCheckTest extends TestCase protected void setUp() throws Exception { super.setUp(); - baseDir = System.getProperty( "basedir" ); + this.baseDir = System.getProperty( "basedir" ); // Setup log4J (We are in a forked JVM for tests) BasicConfigurator.configure(); Logger.getLogger( "org.apache.maven.plugin.linkcheck" ).setLevel( Level.DEBUG ); @@ -52,10 +52,10 @@ public class LinkCheckTest extends TestCase public void testScan() throws Exception { - File f = new File( baseDir + "/src/test-resources" ); + File f = new File( this.baseDir + "/src/test-resources" ); LinkCheck lc = new LinkCheck(); lc.setBasedir( f ); - lc.setOutput( new File( baseDir + "/target/linkcheck.xml" ) ); + lc.setOutput( new File( this.baseDir + "/target/linkcheck.xml" ) ); lc.setOutputEncoding( "ISO8859-1" ); lc.setCache( System.getProperty( "maven.linkcheck.cache" ) ); lc.setExclude( "http://cvs.apache.org/viewcvs.cgi/maven-pluginszz/," diff --git a/linkcheck/src/test/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidatorTest.java b/linkcheck/src/test/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidatorTest.java index 0b850f2e..963c312c 100644 --- a/linkcheck/src/test/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidatorTest.java +++ b/linkcheck/src/test/org/apache/maven/plugin/linkcheck/validation/HTTPLinkValidatorTest.java @@ -17,14 +17,14 @@ package org.apache.maven.plugin.linkcheck.validation; * ==================================================================== */ -import java.io.File; - -import junit.framework.TestCase; - import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; +import java.io.File; + +import junit.framework.TestCase; + /** * @author Ben Walding * @author Arnaud Heritier @@ -51,9 +51,9 @@ public class HTTPLinkValidatorTest extends TestCase public void testValidateLink() throws Exception { - if ( mavenOnline ) + if ( this.mavenOnline ) { - hlv = + this.hlv = new OnlineHTTPLinkValidator( System.getProperty( "maven.linkcheck.method" ), System.getProperty( "maven.linkcheck.proxy.host" ), System.getProperty( "maven.linkcheck.proxy.port" ), @@ -67,7 +67,7 @@ public class HTTPLinkValidatorTest extends TestCase } else { - hlv = new OfflineHTTPLinkValidator(); + this.hlv = new OfflineHTTPLinkValidator(); assertEquals( LinkValidationResult.WARNING, checkLink( "http://www.apache.org" ).getStatus() ); assertEquals( LinkValidationResult.WARNING, checkLink( "http://www.example.com>);" ).getStatus() ); @@ -79,7 +79,7 @@ public class HTTPLinkValidatorTest extends TestCase { LinkValidationItem lvi = new LinkValidationItem( new File( "." ), link ); - return hlv.validateLink( lvi ); + return this.hlv.validateLink( lvi ); } }