/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ package net.sourceforge.pmd.jedit; import java.io.*; import net.sourceforge.pmd.util.datasource.DataSource; /** * DataSource implementation to read data from a file. This one is different than * the one provided by PMD in that it uses buffering and gives access to the file * directly. */ public class FileDataSource implements DataSource { private static final String FILE_SEPARATOR = System.getProperty("file.separator"); private File file; /** * @param file the file to read */ public FileDataSource(File file) { this.file = file; } public InputStream getInputStream() throws IOException { return new BufferedInputStream(new FileInputStream(file)); } public void setFile(File file) { this.file = file; } public File getFile() { return file; } public String getNiceFileName(boolean shortNames, String inputFileName) { return glomName(shortNames, inputFileName, file); } private String glomName(boolean shortNames, String inputFileName, File file) { if (shortNames && inputFileName.indexOf(',') == -1) { if (new File(inputFileName).isDirectory()) { return trimAnyPathSep(file.getAbsolutePath().substring(inputFileName.length())); } else { if (inputFileName.indexOf(FILE_SEPARATOR.charAt(0)) == -1) { return inputFileName; } return trimAnyPathSep(inputFileName.substring(inputFileName.lastIndexOf(FILE_SEPARATOR))); } } try { return file.getCanonicalFile().getAbsolutePath(); } catch (Exception e) { return file.getAbsolutePath(); } } private String trimAnyPathSep(String name) { return name.startsWith(FILE_SEPARATOR) ? name.substring(1) : name; } }