1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail2.javax.resolver;
18
19 import java.io.File;
20 import java.io.IOException;
21
22 import javax.activation.DataSource;
23 import javax.activation.FileDataSource;
24
25
26
27
28
29
30 public class DataSourceFileResolver extends DataSourceBaseResolver {
31
32
33 private final File baseDir;
34
35
36
37
38 public DataSourceFileResolver() {
39 baseDir = new File(".");
40 }
41
42
43
44
45
46
47 public DataSourceFileResolver(final File baseDir) {
48 this.baseDir = baseDir;
49 }
50
51
52
53
54
55
56
57 public DataSourceFileResolver(final File baseDir, final boolean lenient) {
58 super(lenient);
59 this.baseDir = baseDir;
60 }
61
62
63
64
65
66
67 public File getBaseDir() {
68 return baseDir;
69 }
70
71
72 @Override
73 public DataSource resolve(final String resourceLocation) throws IOException {
74 return resolve(resourceLocation, isLenient());
75 }
76
77
78 @Override
79 public DataSource resolve(final String resourceLocation, final boolean isLenient) throws IOException {
80 File file;
81 DataSource result = null;
82
83 if (!isCid(resourceLocation)) {
84 file = new File(resourceLocation);
85
86 if (!file.isAbsolute()) {
87 file = getBaseDir() != null ? new File(getBaseDir(), resourceLocation) : new File(resourceLocation);
88 }
89
90 if (file.exists()) {
91 result = new FileDataSource(file);
92 } else if (!isLenient) {
93 throw new IOException("Cant resolve the following file resource :" + file.getAbsolutePath());
94 }
95 }
96
97 return result;
98 }
99 }