DOXIA-216: Linkcheck broken

o read the content of a file and try to find the anchor

git-svn-id: https://svn.apache.org/repos/asf/maven/doxia/doxia-tools/trunk@704516 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java b/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java
index fec26e1..f105f4f 100644
--- a/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java
+++ b/src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java
@@ -20,9 +20,13 @@
  */
 
 import java.io.File;
+import java.io.IOException;
+import java.io.Reader;
 import java.util.Locale;
 
 import org.apache.maven.doxia.linkcheck.model.LinkcheckFileResult;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.ReaderFactory;
 
 /**
  * A link validator solely for files on the local filesystem.
@@ -62,27 +66,50 @@
         return getFile( lvi ).getAbsolutePath();
     }
 
+    // ----------------------------------------------------------------------
+    // Private methods
+    // ----------------------------------------------------------------------
+
     /**
      * Returns the link of the given LinkValidationItem as a File.
      *
      * @param lvi The LinkValidationItem.
      * @return File the link as a File.
      */
-    protected File getFile( LinkValidationItem lvi )
+    private File getFile( LinkValidationItem lvi )
     {
         String link = lvi.getLink();
 
         if ( link.indexOf( '#' ) != -1 )
         {
+            String anchor = link.substring( link.indexOf( '#' ) + 1 );
             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 ( link.trim().length() == 0 )
+            if ( link.trim().length() == 0 ) // in the same file
             {
-                return lvi.getSource();
+                // the anchor exists?
+                String content = read( lvi.getSource() );
+                if ( content != null && content.indexOf( "name=\"" + anchor + "\"" ) != -1 )
+                {
+                    return lvi.getSource();
+                }
+
+                // return an invalid file
+                return new File( lvi.getLink() );
             }
+
+            // the anchor exists?
+            String content = read( new File( lvi.getSource().getParentFile(), link ) );
+            if ( content != null && content.indexOf( "name=\"" + anchor + "\"" ) != -1 )
+            {
+                return new File( lvi.getSource().getParentFile(), link );
+            }
+
+            // return an invalid file
+            return new File( lvi.getLink() );
         }
+
         if ( link.indexOf( '?' ) != -1 )
         {
             link = link.substring( 0, link.indexOf( '?' ) );
@@ -98,4 +125,29 @@
         return new File( lvi.getSource().getParentFile(), link );
     }
 
+    /**
+     * TODO take care of encoding
+     *
+     * @param f not null
+     * @return the content of the file or null if an error occurred.
+     */
+    private static String read( File f )
+    {
+        Reader reader = null;
+        try
+        {
+            reader = ReaderFactory.newPlatformReader( f );
+            return IOUtil.toString( reader );
+        }
+        catch ( IOException e )
+        {
+            // nop;
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
+
+        return null;
+    }
 }