View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  /**
26   * The Class RequireFilesExist.
27   */
28  public class RequireFilesExist
29      extends AbstractRequireFiles
30  {
31      @Override
32      boolean checkFile( File file )
33      {
34          // if we get here and the handle is null, treat it as a success
35          return file == null ? true : file.exists() && osIndependentNameMatch( file, true );
36      }
37  
38      @Override
39      String getErrorMsg()
40      {
41          return "Some required files are missing:" + System.lineSeparator();
42      }
43  
44      /**
45       * OSes like Windows are case insensitive, so this method will compare the file path with the actual path. A simple
46       * {@link File#exists()} is not enough for such OS.
47       * 
48       * @param file the file to verify
49       * @param defaultValue value to return in case an IO exception occurs, should never happen as the file already
50       *            exists
51       * @return
52       */
53      private boolean osIndependentNameMatch( File file, boolean defaultValue )
54      {
55          try
56          {
57              File absFile;
58              if ( !file.isAbsolute() )
59              {
60                  absFile = new File( new File( "." ).getCanonicalFile(), file.getPath() );
61              }
62              else
63              {
64                  absFile = file;
65              }
66  
67              return absFile.toURI().equals( absFile.getCanonicalFile().toURI() );
68          }
69          catch ( IOException e )
70          {
71              return defaultValue;
72          }
73      }
74  }