001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2014 ForgeRock AS. 015 */ 016 017package org.forgerock.maven.plugins.xcite.utils; 018 019import org.codehaus.plexus.util.DirectoryScanner; 020 021import java.io.BufferedReader; 022import java.io.File; 023import java.io.FileReader; 024import java.io.IOException; 025import java.util.ArrayList; 026 027/** 028 * Utility methods for handling files. 029 */ 030public final class FileUtils { 031 032 /** 033 * Get the content of a file as an array of Strings. 034 * 035 * @param file File containing the strings. 036 * @return The array of strings contained in the file. 037 * @throws IOException Failed to read the file. 038 */ 039 public static ArrayList<String> getStrings(final File file) 040 throws IOException { 041 042 ArrayList<String> lines = new ArrayList<String>(); 043 if (file == null) { 044 return lines; 045 } 046 047 BufferedReader br = new BufferedReader(new FileReader(file)); 048 String line; 049 while ((line = br.readLine()) != null) { 050 lines.add(line); 051 } 052 br.close(); 053 054 return lines; 055 } 056 057 /** 058 * A subset of formats described in the documentation for the <a 059 * href="http://www.docbook.org/tdg/en/html/imagedata.html">ImageData</a> 060 * element. 061 */ 062 private static String[] imageFiles = {"**/*.bmp", 063 "**/*.eps", 064 "**/*.gif", 065 "**/*.jpeg", 066 "**/*.jpg", 067 "**/*.png", 068 "**/*.svg", 069 "**/*.tiff"}; 070 071 /** 072 * Get a list of relative file paths based on inclusion patterns. 073 * 074 * @param baseDirectory Where to look for files to include. 075 * @param includes Patterns specifying files to include. 076 * If null, match all files 077 * except files excluded by default. 078 * @param excludes Patterns specifying files to exclude. 079 * If null, exclude only image files 080 * and files excluded by default. 081 * @return Relative paths to files. 082 */ 083 public static String[] getIncludedFiles(File baseDirectory, 084 String[] includes, 085 String[] excludes) { 086 DirectoryScanner scanner = new DirectoryScanner(); 087 scanner.setBasedir(baseDirectory); 088 scanner.setIncludes(includes); 089 090 // Exclude at least image files and the defaults. 091 String[] defaultExcludes = new String[imageFiles.length 092 + DirectoryScanner.DEFAULTEXCLUDES.length]; 093 System.arraycopy(imageFiles, 0, defaultExcludes, 0, imageFiles.length); 094 System.arraycopy(DirectoryScanner.DEFAULTEXCLUDES, 0, 095 defaultExcludes, imageFiles.length, DirectoryScanner.DEFAULTEXCLUDES.length); 096 097 if (excludes == null) { 098 excludes = defaultExcludes; 099 } 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}