1 package org.apache.maven.plugin.resource.loader;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedInputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.InputStream;
27 import java.util.Hashtable;
28 import java.util.Vector;
29
30 import org.apache.commons.collections.ExtendedProperties;
31 import org.apache.velocity.exception.ResourceNotFoundException;
32 import org.apache.velocity.runtime.resource.Resource;
33 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
34 import org.apache.velocity.util.StringUtils;
35
36
37
38
39
40
41 public class ProjectResourceLoader
42 extends ResourceLoader
43 {
44
45
46
47 private Vector paths = null;
48
49
50
51
52
53
54 private Hashtable templatePaths = new Hashtable();
55
56 public void init( ExtendedProperties configuration )
57 {
58 rsvc.info( "ProjectResourceLoader : initialization starting." );
59
60 String separator = System.getProperty( "file.separator" );
61
62 String path = System.getProperty( "user.dir" ) + separator + "src" + separator + "main" + separator
63 + "resources" + separator;
64
65 rsvc.info( "path :" + path );
66
67 paths = new Vector();
68
69 paths.add( path );
70
71 int sz = paths.size();
72
73 for ( int i = 0; i < sz; i++ )
74 {
75 rsvc.info( "ProjectResourceLoader : adding path '" + (String) paths.get( i ) + "'" );
76 }
77 rsvc.info( "ProjectResourceLoader : initialization complete." );
78 }
79
80
81
82
83
84
85
86
87
88
89 public synchronized InputStream getResourceStream( String templateName )
90 throws ResourceNotFoundException
91 {
92
93
94
95 if ( templateName == null || templateName.length() == 0 )
96 {
97
98
99
100
101
102 throw new ResourceNotFoundException( "Need to specify a file name or file path!" );
103 }
104
105 String template = StringUtils.normalizePath( templateName );
106 if ( template == null || template.length() == 0 )
107 {
108 String msg = "Project Resource loader error : argument " + template
109 + " contains .. and may be trying to access " + "content outside of template root. Rejected.";
110
111 rsvc.error( "ProjectResourceLoader : " + msg );
112
113 throw new ResourceNotFoundException( msg );
114 }
115
116
117
118
119 if ( template.startsWith( "/" ) )
120 {
121 template = template.substring( 1 );
122 }
123
124 int size = paths.size();
125 for ( int i = 0; i < size; i++ )
126 {
127 String path = (String) paths.get( i );
128 InputStream inputStream = findTemplate( path, template );
129
130 if ( inputStream != null )
131 {
132
133
134
135
136
137
138 templatePaths.put( templateName, path );
139 return inputStream;
140 }
141 }
142
143
144
145
146
147
148 String msg = "ProjectResourceLoader Error: cannot find resource " + template;
149
150 throw new ResourceNotFoundException( msg );
151 }
152
153
154
155
156
157
158
159
160 private InputStream findTemplate( String path, String template )
161 {
162 try
163 {
164 File file = new File( path, template );
165
166 if ( file.canRead() )
167 {
168 return new BufferedInputStream( new FileInputStream( file.getAbsolutePath() ) );
169 }
170 else
171 {
172 return null;
173 }
174 }
175 catch ( FileNotFoundException fnfe )
176 {
177
178
179
180 return null;
181 }
182 }
183
184
185
186
187
188
189
190
191
192 public boolean isSourceModified( Resource resource )
193 {
194
195
196
197
198
199 boolean modified = true;
200
201 String fileName = resource.getName();
202 String path = (String) templatePaths.get( fileName );
203 File currentFile = null;
204
205 for ( int i = 0; currentFile == null && i < paths.size(); i++ )
206 {
207 String testPath = (String) paths.get( i );
208 File testFile = new File( testPath, fileName );
209 if ( testFile.canRead() )
210 {
211 currentFile = testFile;
212 }
213 }
214 File file = new File( path, fileName );
215 if ( currentFile == null || !file.exists() )
216 {
217
218
219
220
221
222
223
224
225 }
226 else if ( currentFile.equals( file ) && file.canRead() )
227 {
228
229
230
231
232
233
234 modified = ( file.lastModified() != resource.getLastModified() );
235 }
236
237
238
239
240 return modified;
241 }
242
243 public long getLastModified( Resource resource )
244 {
245 String path = (String) templatePaths.get( resource.getName() );
246 File file = new File( path, resource.getName() );
247
248 if ( file.canRead() )
249 {
250 return file.lastModified();
251 }
252 else
253 {
254 return 0;
255 }
256 }
257 }