1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package net.sourceforge.pmd; 5 6 import java.io.File; 7 import java.io.FileInputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 11 /*** 12 * DataSource implementation to read data from a file. 13 */ 14 public class FileDataSource implements DataSource { 15 private File file; 16 17 /*** 18 * @param file the file to read 19 */ 20 public FileDataSource(File file) { 21 this.file = file; 22 } 23 24 public InputStream getInputStream() throws IOException { 25 return new FileInputStream(file); 26 } 27 28 public String getNiceFileName(boolean shortNames, String inputFileName) { 29 return glomName(shortNames, inputFileName, file); 30 } 31 32 private String glomName(boolean shortNames, String inputFileName, File file) { 33 if (shortNames && inputFileName.indexOf(',') == -1) { 34 if ((new File(inputFileName)).isDirectory()) { 35 return trimAnyPathSep(file.getAbsolutePath().substring(inputFileName.length())); 36 } else { 37 if (inputFileName.indexOf(System.getProperty("file.separator").charAt(0)) == -1) { 38 return inputFileName; 39 } 40 return trimAnyPathSep(inputFileName.substring(inputFileName.lastIndexOf(System.getProperty("file.separator")))); 41 } 42 } else { 43 return file.getAbsolutePath(); 44 } 45 } 46 47 private String trimAnyPathSep(String name) { 48 if (name.startsWith(System.getProperty("file.separator"))) { 49 return name.substring(1); 50 } 51 return name; 52 } 53 54 55 }