1 package org.apache.maven.plugin.linkcheck.validation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.File;
22
23 /**
24 * A link validator solely for files on the local filesystem.
25 *
26 * @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
27 * @author <a href="mailto:aheritier@apache.org">Arnaud Heritier</a>
28 * @version $Id: FileLinkValidator.java 532339 2007-04-25 12:28:56Z ltheussl $
29 */
30 public final class FileLinkValidator implements LinkValidator
31 {
32 /**
33 * @see org.apache.maven.plugin.linkcheck.LinkValidator#validateLink(org.apache.maven.plugin.linkcheck.LinkValidationItem)
34 */
35 public LinkValidationResult validateLink( LinkValidationItem lvi )
36 {
37 File f = getFile( lvi );
38 if ( f.exists() )
39 {
40 return new LinkValidationResult( LinkValidationResult.VALID, false, "" );
41 }
42 else
43 {
44 return new LinkValidationResult( LinkValidationResult.ERROR, false, "doesn't exist." );
45 }
46 }
47
48 /**
49 * @see org.apache.maven.plugin.linkcheck.LinkValidator#getResourceKey(org.apache.maven.plugin.linkcheck.LinkValidationItem)
50 */
51 public Object getResourceKey( LinkValidationItem lvi )
52 {
53 String link = lvi.getLink();
54
55 if ( link.startsWith( "http://" ) || link.startsWith( "https://" ) || link.indexOf( '@' ) != -1 )
56 {
57 return null;
58 }
59 return getFile( lvi ).getAbsolutePath();
60 }
61
62 /**
63 *
64 * @param lvi
65 * @return File
66 */
67 protected File getFile( LinkValidationItem lvi )
68 {
69 String link = lvi.getLink();
70 if ( link.indexOf( '#' ) != -1 )
71 {
72 link = link.substring( 0, link.indexOf( '#' ) );
73
74
75
76 if ( link.trim().length() == 0 )
77 {
78 return lvi.getSource();
79 }
80 }
81 if ( link.indexOf( '?' ) != -1 )
82 {
83 link = link.substring( 0, link.indexOf( '?' ) );
84
85
86
87 if ( link.trim().length() == 0 )
88 {
89 return lvi.getSource();
90 }
91 }
92 File f = new File( lvi.getSource().getParentFile(), link );
93 return f;
94 }
95
96 }