private File getFile()

in src/main/java/org/apache/maven/doxia/linkcheck/validation/FileLinkValidator.java [97:144]


    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
            if ( link.trim().length() == 0 ) // in the same file
            {
                // the anchor exists?
                String content = read( lvi.getSource(), encoding );
                if ( Anchors.matchesAnchor( content, anchor ) )
                {
                    return lvi.getSource();
                }

                // return an invalid file
                return new File( lvi.getLink() );
            }

            // the anchor exists?
            String content = read( new File( lvi.getSource().getParentFile(), link ), encoding );
            if ( Anchors.matchesAnchor( content, anchor ) )
            {
                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( '?' ) );

            // 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();
            }
        }

        return new File( lvi.getSource().getParentFile(), link );
    }