You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by lt...@apache.org on 2007/08/28 00:15:00 UTC

svn commit: r570257 - in /maven/sandbox/trunk/doxia/doxia-linkcheck/src: main/java/org/apache/maven/doxia/linkcheck/ main/java/org/apache/maven/doxia/linkcheck/validation/ site/xdoc/ test/java/org/apache/maven/doxia/linkcheck/

Author: ltheussl
Date: Mon Aug 27 15:14:59 2007
New Revision: 570257

URL: http://svn.apache.org/viewvc?rev=570257&view=rev
Log:
Make cache a File, not a String. Clean up Exception handling. Remove some unused methods.

Modified:
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/FileToCheck.java
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidator.java
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidatorManager.java
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/MailtoLinkValidator.java
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/site/xdoc/usage.xml
    maven/sandbox/trunk/doxia/doxia-linkcheck/src/test/java/org/apache/maven/doxia/linkcheck/LinkCheckTest.java

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/FileToCheck.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/FileToCheck.java?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/FileToCheck.java (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/FileToCheck.java Mon Aug 27 15:14:59 2007
@@ -44,44 +44,22 @@
     /** Log for debug output. */
     private static final Log LOG = LogFactory.getLog( FileToCheck.class );
 
-    /** Unknown validation status. Initialized as null. */
-    public static final String STATUS_UNKNOWN = null;
-
-    /** Validation status ok. */
-    public static final String STATUS_OK = "OK";
-
     /** The base of this FileToCheck. */
     private String base;
 
     /** The File object of this FileToCheck. */
     private File fileToCheck;
 
-    /** A list of links found in this FileToCheck. */
+    /** The list of LinkCheckResults of the links found in this FileToCheck. */
     private List links = new LinkedList();
 
-    /** A message. */
-    private String message = "";
-
-    /** The status. */
-    private String status = STATUS_OK;
-
-    /** successful. */
+    /** The number of successfully validated links. */
     private int successful;
 
-    /** unsuccessful. */
+    /** The number of unsuccessfully validated links. */
     private int unsuccessful;
 
     /**
-     * Returns the message.
-     *
-     * @return String
-     */
-    public String getMessage()
-    {
-        return this.message;
-    }
-
-    /**
      * Returns the fileName.
      *
      * @return String
@@ -99,7 +77,7 @@
     }
 
     /**
-     * Returns the list of results.
+     * Returns the list of {@link LinkCheckResult LinkCheckResults}.
      *
      * @return List
      */
@@ -109,17 +87,6 @@
     }
 
     /**
-     * Returns the status.
-     *
-     * @return int
-     */
-    // TODO: replace by LinkValidationResult status.
-    public String getStatus()
-    {
-        return this.status;
-    }
-
-    /**
      * Returns the number of successfully validated links.
      *
      * @return int
@@ -155,132 +122,117 @@
      * Validates this fileToCheck.
      *
      * @param lvm The LinkValidatorManager to use.
-     * @throws Exception if something goes wrong.
      */
-    public void check( LinkValidatorManager lvm ) throws Exception
+    public void check( LinkValidatorManager lvm )
     {
         this.successful = 0;
 
         this.unsuccessful = 0;
 
-        this.status = STATUS_OK;
-
-        this.message = "";
-
         if ( LOG.isDebugEnabled() )
         {
             LOG.debug( "Validating " + getName() );
         }
 
+        final Set hrefs;
+
         try
         {
-            final Set hrefs;
+            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
 
-            try
+            if ( LOG.isDebugEnabled() )
             {
-                hrefs = LinkMatcher.match( this.fileToCheck );
+                LOG.error( "Received: [" + t + "] in page [" + getName() + "]", t );
             }
-            catch ( Throwable t )
+            else
             {
-                // 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() + "]" );
-                }
+                LOG.error( "Received: [" + t + "] in page [" + getName() + "]" );
+            }
 
-                LinkCheckResult lcr = new LinkCheckResult();
+            LinkCheckResult lcr = new LinkCheckResult();
 
-                lcr.setStatus( "PARSE FAILURE" );
+            lcr.setStatus( "PARSE FAILURE" );
 
-                lcr.setTarget( "N/A" );
+            lcr.setTarget( "N/A" );
 
-                addResult( lcr );
+            addResult( lcr );
 
-                return;
-            }
+            return;
+        }
 
-            String href;
+        String href;
 
-            LinkCheckResult lcr;
+        LinkCheckResult lcr;
 
-            LinkValidationItem lvi;
+        LinkValidationItem lvi;
 
-            LinkValidationResult result;
+        LinkValidationResult result;
 
-            for ( Iterator iter = hrefs.iterator(); iter.hasNext(); )
-            {
-                href = (String) iter.next();
+        for ( Iterator iter = hrefs.iterator(); iter.hasNext(); )
+        {
+            href = (String) iter.next();
 
-                lcr = new LinkCheckResult();
+            lcr = new LinkCheckResult();
 
-                lvi = new LinkValidationItem( this.fileToCheck, href );
+            lvi = new LinkValidationItem( this.fileToCheck, href );
 
-                result = lvm.validateLink( lvi );
+            result = lvm.validateLink( lvi );
 
-                lcr.setTarget( href );
+            lcr.setTarget( href );
 
-                lcr.setErrorMessage( result.getErrorMessage() );
+            lcr.setErrorMessage( result.getErrorMessage() );
 
-                switch ( result.getStatus() )
-                {
-                    case LinkValidationResult.VALID:
-                        this.successful++;
+            switch ( result.getStatus() )
+            {
+                case LinkValidationResult.VALID:
+                    this.successful++;
 
-                        lcr.setStatus( "valid" );
+                    lcr.setStatus( "valid" );
 
-                        addResult( lcr ); // At some point we won't want to store valid links. The tests require that
-                        // we do at present
+                    // At some point we won't want to store valid links. The tests require that we do at present.
+                    addResult( lcr );
 
-                        break;
-                    case LinkValidationResult.ERROR:
-                        this.unsuccessful++;
+                    break;
+                case LinkValidationResult.ERROR:
+                    this.unsuccessful++;
 
-                        lcr.setStatus( "error" );
+                    lcr.setStatus( "error" );
 
-                        addResult( lcr );
+                    addResult( lcr );
 
-                        break;
-                    case LinkValidationResult.WARNING:
-                        this.unsuccessful++;
+                    break;
+                case LinkValidationResult.WARNING:
+                    this.unsuccessful++;
 
-                        lcr.setStatus( "warning" );
+                    lcr.setStatus( "warning" );
 
-                        addResult( lcr );
+                    addResult( lcr );
 
-                        break;
-                    case LinkValidationResult.UNKNOWN:
-                    default:
-                        this.unsuccessful++;
+                    break;
+                case LinkValidationResult.UNKNOWN:
+                default:
+                    this.unsuccessful++;
 
-                        lcr.setStatus( "unknown" );
+                    lcr.setStatus( "unknown" );
 
-                        addResult( lcr );
+                    addResult( lcr );
 
-                        break;
-                }
+                    break;
             }
+        }
 
-            href = null;
-
-            lcr = null;
-
-            lvi = null;
+        href = null;
 
-            result = null;
+        lcr = null;
 
-        }
-        catch ( Exception e )
-        {
-            LOG.error( this.message );
+        lvi = null;
 
-            throw e;
-        }
+        result = null;
     }
 
     /**

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkCheck.java Mon Aug 27 15:14:59 2007
@@ -32,7 +32,6 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FilenameFilter;
-import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
@@ -66,7 +65,7 @@
     private File basedir;
 
     /** cache. */
-    private String cache;
+    private File cache;
 
     /** excludes. */
     private String[] excludes = null;
@@ -131,21 +130,21 @@
     }
 
     /**
-     * Returns the name of the cacheFile.
+     * Returns the cache File.
      *
-     * @return String
+     * @return File
      */
-    public String getCache()
+    public File getCache()
     {
         return this.cache;
     }
 
     /**
-     * Sets the name of the cacheFile.
+     * Sets the cache File.
      *
      * @param cacheFile The cacheFile to set. Set this to null to ignore storing the cache.
      */
-    public void setCache( String cacheFile )
+    public void setCache( File cacheFile )
     {
         this.cache = cacheFile;
     }
@@ -400,15 +399,9 @@
         {
             ftc = (FileToCheck) fileIter.next();
 
-            try
-            {
-                this.filesToCheck.add( ftc );
-                ftc.check( validator );
-            }
-            catch ( Exception e )
-            {
-                LOG.error( "Error while checking : " + ftc.getName(), e );
-            }
+            this.filesToCheck.add( ftc );
+
+            ftc.check( validator );
         }
 
         ftc = null;

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/LinkMatcher.java Mon Aug 27 15:14:59 2007
@@ -55,8 +55,6 @@
      *
      * 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 if something goes wrong.

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidator.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidator.java?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidator.java (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidator.java Mon Aug 27 15:14:59 2007
@@ -31,9 +31,8 @@
      *
      * @param lvi The LinkValidationItem to validate.
      * @return The LinkValidationResult.
-     * @throws Exception if something goes wrong.
      */
-    LinkValidationResult validateLink( LinkValidationItem lvi ) throws Exception;
+    LinkValidationResult validateLink( LinkValidationItem lvi );
 
     /**
      * The resource key is used by the cache to determine if it really needs to validate the link. No actual validation

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidatorManager.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidatorManager.java?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidatorManager.java (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/LinkValidatorManager.java Mon Aug 27 15:14:59 2007
@@ -25,6 +25,7 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InvalidClassException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
@@ -104,9 +105,8 @@
      *
      * @param lvi The LinkValidationItem to validate.
      * @return A LinkValidationResult.
-     * @throws Exception if something goes wrong.
      */
-    public LinkValidationResult validateLink( LinkValidationItem lvi ) throws Exception
+    public LinkValidationResult validateLink( LinkValidationItem lvi )
     {
         LinkValidationResult cachedResult = getCachedResult( lvi );
 
@@ -176,99 +176,121 @@
     /**
      * Loads a cache file.
      *
-     * @param cacheFilename The name of the cache file.
+     * @param cacheFile The cache file.
      * May be null, in which case the request is ignored.
      */
-    public void loadCache( String cacheFilename )
+    public void loadCache( File cacheFile )
     {
-        if ( cacheFilename == null )
+        if ( cacheFile == null )
         {
             LOG.debug( "No cache file specified! Ignoring request to load." );
             return;
         }
 
-        try
+        if ( !cacheFile.exists() )
         {
-            File f = new File( cacheFilename );
+            LOG.debug( "Specified cache file does not exist! Ignoring request to load." );
+            return;
+        }
 
-            if ( f.exists() )
-            {
-                ObjectInputStream is = new ObjectInputStream( new FileInputStream( cacheFilename ) );
+        ObjectInputStream is = null;
 
-                this.cache = (Map) is.readObject();
+        try
+        {
+            is = new ObjectInputStream( new FileInputStream( cacheFile ) );
 
-                is.close();
+            this.cache = (Map) is.readObject();
 
-                if ( LOG.isDebugEnabled() )
-                {
-                    LOG.debug( "Cache file loaded: " + cacheFilename );
-                }
+            if ( LOG.isDebugEnabled() )
+            {
+                LOG.debug( "Cache file loaded: " + cacheFile.getAbsolutePath() );
             }
         }
         catch ( InvalidClassException e )
         {
-            LOG.warn( "Your cache is incompatible with this new release of linkcheck. It will be recreated." );
+            LOG.warn( "Your cache is incompatible with this version of linkcheck. It will be recreated." );
+        }
+        catch ( ClassNotFoundException e )
+        {
+            LOG.error( "Unable to load the cache: " + cacheFile.getAbsolutePath(), e );
         }
-        catch ( Throwable t )
+        catch ( IOException t )
         {
-            LOG.error( "Unable to load the cache: " + cacheFilename, t );
+            LOG.error( "Unable to load the cache: " + cacheFile.getAbsolutePath(), t );
+        }
+        finally
+        {
+            try
+            {
+                is.close();
+            }
+            catch ( IOException e )
+            {
+                LOG.debug( "Unable to close stream!", e );
+
+                is = null;
+            }
         }
     }
 
     /**
      * Saves a cache file.
      *
-     * @param cacheFilename The name of the cache file.
+     * @param cacheFile The name of the cache file.
      * May be null, in which case the request is ignored.
      */
-    public void saveCache( String cacheFilename )
+    public void saveCache( File cacheFile )
     {
-        if ( cacheFilename == null )
+        if ( cacheFile == null )
         {
             LOG.warn( "No cache file specified! Ignoring request to store results." );
             return;
         }
 
-        try
-        {
-            // Remove non-persistent items from cache
-            Map persistentCache = new HashMap();
+        // Remove non-persistent items from cache
+        Map persistentCache = new HashMap();
 
-            Iterator iter = this.cache.keySet().iterator();
+        Iterator iter = this.cache.keySet().iterator();
 
-            Object resourceKey;
+        Object resourceKey;
 
-            while ( iter.hasNext() )
+        while ( iter.hasNext() )
+        {
+            resourceKey = iter.next();
+
+            if ( ( (LinkValidationResult) this.cache.get( resourceKey ) ).isPersistent() )
             {
-                resourceKey = iter.next();
+                persistentCache.put( resourceKey, this.cache.get( resourceKey ) );
 
-                if ( ( (LinkValidationResult) this.cache.get( resourceKey ) ).isPersistent() )
+                if ( LOG.isDebugEnabled() )
                 {
-                    persistentCache.put( resourceKey, this.cache.get( resourceKey ) );
-
-                    if ( LOG.isDebugEnabled() )
-                    {
-                        LOG.debug( "[" + resourceKey + "] with result [" + this.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 );
+        File dir = cacheFile.getParentFile();
 
-            File dir = cacheFile.getParentFile();
+        if ( dir != null )
+        {
+            dir.mkdirs();
+        }
 
-            if ( dir != null )
-            {
-                dir.mkdirs();
-            }
+        ObjectOutputStream os = null;
 
-            ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream( cacheFilename ) );
+        try
+        {
+            os = new ObjectOutputStream( new FileOutputStream( cacheFile ) );
 
             os.writeObject( persistentCache );
-
-            os.close();
-
+        }
+        catch ( IOException e )
+        {
+            LOG.error( "Unable to save the cache: " + cacheFile.getAbsolutePath(), e );
+        }
+        finally
+        {
             persistentCache = null;
 
             iter = null;
@@ -279,11 +301,16 @@
 
             dir = null;
 
-            os = null;
-        }
-        catch ( Throwable t )
-        {
-            LOG.error( "Unable to save the cache: " + cacheFilename, t );
+            try
+            {
+                os.close();
+            }
+            catch ( IOException e )
+            {
+                LOG.debug( "Unable to close stream!", e );
+
+                os = null;
+            }
         }
     }
 

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/MailtoLinkValidator.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/MailtoLinkValidator.java?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/MailtoLinkValidator.java (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/main/java/org/apache/maven/doxia/linkcheck/validation/MailtoLinkValidator.java Mon Aug 27 15:14:59 2007
@@ -32,7 +32,7 @@
     private static final LinkValidationResult LVR = new LinkValidationResult( LinkValidationResult.VALID, false, "" );
 
     /** {@inheritDoc} */
-    public LinkValidationResult validateLink( LinkValidationItem lvi ) throws Exception
+    public LinkValidationResult validateLink( LinkValidationItem lvi )
     {
         return LVR;
     }

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/site/xdoc/usage.xml
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/site/xdoc/usage.xml?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/site/xdoc/usage.xml (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/site/xdoc/usage.xml Mon Aug 27 15:14:59 2007
@@ -41,7 +41,7 @@
 
         lc.setOutput( new File( "target/linkcheck/linkcheck.xml" ) );
 
-        lc.setCache( "target/linkcheck/linkcheck.cache" );
+        lc.setCache( new File( "target/linkcheck/linkcheck.cache" ) );
 
         String[] excludes = new String[]
             {

Modified: maven/sandbox/trunk/doxia/doxia-linkcheck/src/test/java/org/apache/maven/doxia/linkcheck/LinkCheckTest.java
URL: http://svn.apache.org/viewvc/maven/sandbox/trunk/doxia/doxia-linkcheck/src/test/java/org/apache/maven/doxia/linkcheck/LinkCheckTest.java?rev=570257&r1=570256&r2=570257&view=diff
==============================================================================
--- maven/sandbox/trunk/doxia/doxia-linkcheck/src/test/java/org/apache/maven/doxia/linkcheck/LinkCheckTest.java (original)
+++ maven/sandbox/trunk/doxia/doxia-linkcheck/src/test/java/org/apache/maven/doxia/linkcheck/LinkCheckTest.java Mon Aug 27 15:14:59 2007
@@ -45,7 +45,7 @@
 
         lc.setOutputEncoding( "UTF-8" );
 
-        lc.setCache( "target/linkcheck/linkcheck.cache" ); // TODO
+        lc.setCache( new File( "target/linkcheck/linkcheck.cache" ) ); // TODO
 
         String[] excludes = new String[]
             {