01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd;
04:
05: import java.io.File;
06: import java.io.FileInputStream;
07: import java.io.IOException;
08: import java.io.InputStream;
09:
10: /**
11: * DataSource implementation to read data from a file.
12: */
13: public class FileDataSource implements DataSource {
14:
15: private static final String fileSeparator = System
16: .getProperty("file.separator");
17:
18: private File file;
19:
20: /**
21: * @param file the file to read
22: */
23: public FileDataSource(File file) {
24: this .file = file;
25: }
26:
27: public InputStream getInputStream() throws IOException {
28: return new FileInputStream(file);
29: }
30:
31: public String getNiceFileName(boolean shortNames,
32: String inputFileName) {
33: return glomName(shortNames, inputFileName, file);
34: }
35:
36: private String glomName(boolean shortNames, String inputFileName,
37: File file) {
38: if (shortNames && inputFileName.indexOf(',') == -1) {
39: if ((new File(inputFileName)).isDirectory()) {
40: return trimAnyPathSep(file.getAbsolutePath().substring(
41: inputFileName.length()));
42: } else {
43: if (inputFileName.indexOf(fileSeparator.charAt(0)) == -1) {
44: return inputFileName;
45: }
46: return trimAnyPathSep(inputFileName
47: .substring(inputFileName.lastIndexOf(System
48: .getProperty("file.separator"))));
49: }
50: }
51:
52: try {
53: return file.getCanonicalFile().getAbsolutePath();
54: } catch (Exception e) {
55: return file.getAbsolutePath();
56: }
57: }
58:
59: private String trimAnyPathSep(String name) {
60:
61: return name.startsWith(fileSeparator) ? name.substring(1)
62: : name;
63: }
64: }
|