Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileTemplate |
|
| 1.1666666666666667;1.167 |
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 | ||
24 | package org.webmacro.engine; | |
25 | ||
26 | import java.io.BufferedReader; | |
27 | import java.io.File; | |
28 | import java.io.FileInputStream; | |
29 | import java.io.IOException; | |
30 | import java.io.InputStreamReader; | |
31 | import java.io.Reader; | |
32 | ||
33 | import org.webmacro.Broker; | |
34 | ||
35 | /** | |
36 | * FileTemplate objects read their template data from a text file. | |
37 | */ | |
38 | ||
39 | public class FileTemplate extends WMTemplate | |
40 | { | |
41 | ||
42 | /** | |
43 | * The name of the file to read this template from. | |
44 | */ | |
45 | private final File myFile; | |
46 | ||
47 | /** | |
48 | * What encoding I use to read my templates. | |
49 | */ | |
50 | private final String myEncoding; | |
51 | ||
52 | /** | |
53 | * Instantiate a template based on the specified filename using | |
54 | * the default encoding from WebMacro.properties (TemplateEncoding), | |
55 | * or if not specified there then the UTF-8 encoding. | |
56 | */ | |
57 | public FileTemplate (Broker broker, String filename) | |
58 | { | |
59 | 0 | this(broker, new File(filename), null); |
60 | 0 | } |
61 | ||
62 | /** | |
63 | * Instantiate a template based on the specified file using | |
64 | * the default encoding from WebMacro.properties (TemplateEncoding), | |
65 | * if not specified there then the UTF-8 encoding. | |
66 | */ | |
67 | public FileTemplate (Broker broker, File templateFile) | |
68 | { | |
69 | 228 | this(broker, templateFile, null); |
70 | 228 | } |
71 | ||
72 | /** | |
73 | * Instantiate a template based on the specified file using | |
74 | * the specified encoding to read the template. | |
75 | */ | |
76 | public FileTemplate (Broker broker, File tmplFile, String encoding) | |
77 | { | |
78 | 228 | super(broker); |
79 | 228 | myFile = tmplFile; |
80 | 228 | if (encoding == null) |
81 | { | |
82 | 228 | myEncoding = getDefaultEncoding(); |
83 | } | |
84 | else | |
85 | { | |
86 | 0 | myEncoding = encoding; |
87 | } | |
88 | 228 | } |
89 | ||
90 | /** | |
91 | * Get the stream the template should be read from. Parse will | |
92 | * call this method in order to locate a stream. | |
93 | */ | |
94 | protected Reader getReader () throws IOException | |
95 | { | |
96 | 228 | return new BufferedReader(new InputStreamReader( |
97 | new FileInputStream(myFile), myEncoding)); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Return a name for this template. For example, if the template reads | |
102 | * from a file you might want to mention which it is--will be used to | |
103 | * produce error messages describing which template had a problem. | |
104 | */ | |
105 | public String toString () | |
106 | { | |
107 | 8 | return "FileTemplate:" + myFile; |
108 | } | |
109 | ||
110 | public String getName () | |
111 | { | |
112 | 228 | return myFile.getPath(); |
113 | } | |
114 | ||
115 | ||
116 | } |