View Javadoc

1   package org.apache.maven.plugin.linkcheck.validation;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
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          // If we find an http(s) link or a mail link, it's not good
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              // If the link was just #fred or similar, then the file is the file it came from
75              // XXX: Theoretically we could even validate the anchor tag?
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              // If the link was just ?param=something or similar, then the file is the file it came from
86              // XXX: Theoretically we could even validate the anchor tag?
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  }