Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileTemplateLoader |
|
| 3.5;3.5 |
1 | /* | |
2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | ||
23 | package org.webmacro.resource; | |
24 | ||
25 | import org.slf4j.Logger; | |
26 | import org.slf4j.LoggerFactory; | |
27 | import org.webmacro.ResourceException; | |
28 | import org.webmacro.Template; | |
29 | ||
30 | import java.io.File; | |
31 | ||
32 | /** | |
33 | * Implementation of TemplateLoader that loads templates from a given directory. | |
34 | * Objects of this class are responsible for searching exactly one | |
35 | * directory for templates. If it handles a request, it takes path as | |
36 | * the base path to find the template. | |
37 | * @author Sebastian Kanthak (sebastian.kanthak@muehlheim.de) | |
38 | */ | |
39 | 0 | public class FileTemplateLoader extends AbstractTemplateLoader |
40 | { | |
41 | ||
42 | 0 | static Logger _log = LoggerFactory.getLogger(FileTemplateLoader.class); |
43 | ||
44 | private String path; | |
45 | ||
46 | public void setConfig (String config) | |
47 | { | |
48 | // leading slash isn't needed, because | |
49 | // we use File constructor. | |
50 | 0 | this.path = config; |
51 | ||
52 | // However, we can check, if the directory exists. | |
53 | 0 | File f = new File(path); |
54 | 0 | if (!f.exists()) |
55 | { | |
56 | 0 | _log.warn("FileTemplateLoader: " + f.getAbsolutePath() + " does not exist."); |
57 | } | |
58 | 0 | else if (!f.isDirectory()) |
59 | { | |
60 | 0 | _log.warn("FileTemplateLoader: " + f.getAbsolutePath() + " is not a directory."); |
61 | } | |
62 | 0 | } |
63 | ||
64 | /** | |
65 | * Tries to load a template by interpreting query as | |
66 | * a path relative to the path set by setPath. | |
67 | */ | |
68 | public final Template load (String query, CacheElement ce) throws ResourceException | |
69 | { | |
70 | 0 | File tFile = new File(path, query); |
71 | 0 | if (tFile.isFile() && tFile.canRead()) |
72 | { | |
73 | 0 | _log.debug("FileTemplateProvider: Found template " + tFile.getAbsolutePath()); |
74 | 0 | return helper.load(tFile, ce); |
75 | } | |
76 | else | |
77 | { | |
78 | 0 | return null; |
79 | } | |
80 | } | |
81 | } |