View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2014 ForgeRock AS.
15   */
16  
17  package org.forgerock.maven.plugins.xcite.utils;
18  
19  import org.codehaus.plexus.util.DirectoryScanner;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.util.ArrayList;
26  
27  /**
28   * Utility methods for handling files.
29   */
30  public final class FileUtils {
31  
32      /**
33       * Get the content of a file as an array of Strings.
34       *
35       * @param file          File containing the strings.
36       * @return              The array of strings contained in the file.
37       * @throws IOException  Failed to read the file.
38       */
39      public static ArrayList<String> getStrings(final File file)
40              throws IOException {
41  
42          ArrayList<String> lines = new ArrayList<String>();
43          if (file == null) {
44              return lines;
45          }
46  
47          BufferedReader br = new BufferedReader(new FileReader(file));
48          String line;
49          while ((line = br.readLine()) != null) {
50              lines.add(line);
51          }
52          br.close();
53  
54          return lines;
55      }
56  
57      /**
58       * A subset of formats described in the documentation for the <a
59       * href="http://www.docbook.org/tdg/en/html/imagedata.html">ImageData</a>
60       * element.
61       */
62      private static String[] imageFiles = {"**/*.bmp",
63          "**/*.eps",
64          "**/*.gif",
65          "**/*.jpeg",
66          "**/*.jpg",
67          "**/*.png",
68          "**/*.svg",
69          "**/*.tiff"};
70  
71      /**
72       * Get a list of relative file paths based on inclusion patterns.
73       *
74       * @param baseDirectory     Where to look for files to include.
75       * @param includes          Patterns specifying files to include.
76       *                          If null, match all files
77       *                          except files excluded by default.
78       * @param excludes          Patterns specifying files to exclude.
79       *                          If null, exclude only image files
80       *                          and files excluded by default.
81       * @return                  Relative paths to files.
82       */
83      public static String[] getIncludedFiles(File baseDirectory,
84                                              String[] includes,
85                                              String[] excludes) {
86          DirectoryScanner scanner = new DirectoryScanner();
87          scanner.setBasedir(baseDirectory);
88          scanner.setIncludes(includes);
89  
90          // Exclude at least image files and the defaults.
91          String[] defaultExcludes = new String[imageFiles.length
92                  + DirectoryScanner.DEFAULTEXCLUDES.length];
93          System.arraycopy(imageFiles, 0, defaultExcludes, 0, imageFiles.length);
94          System.arraycopy(DirectoryScanner.DEFAULTEXCLUDES, 0,
95                  defaultExcludes, imageFiles.length, DirectoryScanner.DEFAULTEXCLUDES.length);
96  
97          if (excludes == null) {
98              excludes = defaultExcludes;
99          } else {
100             String[] full = new String[excludes.length + defaultExcludes.length];
101             System.arraycopy(excludes, 0, full, 0, excludes.length);
102             System.arraycopy(defaultExcludes, 0, full, excludes.length, defaultExcludes.length);
103             excludes = full;
104         }
105 
106         scanner.setExcludes(excludes);
107 
108         scanner.scan();
109         return scanner.getIncludedFiles();
110     }
111 
112     /**
113      * Constructor not used.
114      */
115     private FileUtils() {
116         // Not used
117     }
118 }