1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.gridsystems.config.tools;
25
26 import java.io.BufferedInputStream;
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.PrintWriter;
33 import java.lang.reflect.Method;
34 import java.net.MalformedURLException;
35 import java.net.URL;
36 import java.util.Properties;
37
38
39
40
41
42
43
44
45
46
47 public class Template {
48
49
50
51
52 private static final int CLUSTER_SIZE = 4096;
53
54
55
56
57 private URL src;
58
59
60
61
62
63
64
65
66 public Template(File f) throws IOException {
67 this(f, f.getName());
68 }
69
70
71
72
73
74
75
76
77 public Template(File f, String resourceName) throws IOException {
78 this(f, Thread.currentThread().getContextClassLoader().getResource(resourceName));
79 }
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public Template(File f, URL url) throws IOException {
94
95 if (f.exists() && f.isFile()) {
96 this.src = f.toURI().toURL();
97 }
98
99 if (src == null && url != null) {
100 copyResource(url, f);
101 this.src = f.toURI().toURL();
102 }
103
104 if (src == null) {
105 throw new MalformedURLException("Could not construct a valid URL from " + f);
106 }
107 }
108
109
110
111
112
113
114
115
116 private void copyResource(URL resource, File dest) throws IOException {
117 if (dest.isDirectory()) {
118 throw new IOException("Could not copy resource into a directory");
119 }
120
121 FileOutputStream fos = null;
122 BufferedInputStream is = null;
123 try {
124 fos = new FileOutputStream(dest);
125 is = new BufferedInputStream(resource.openStream());
126 byte[] buf = new byte[CLUSTER_SIZE];
127 int count = is.read(buf);
128 while (count != -1) {
129 fos.write(buf, 0, count);
130 count = is.read(buf);
131 }
132 } finally {
133 close(fos);
134 close(is);
135 }
136 }
137
138
139
140
141
142
143
144
145
146 public void writeTo(Properties props, File f) throws IOException {
147 if (f == null) {
148 throw new NullPointerException("Null file");
149 }
150
151 PrintWriter writer = null;
152 try {
153 writer = new PrintWriter(new FileOutputStream(f));
154 printTo(props, writer);
155 } finally {
156 close(writer);
157 }
158 }
159
160
161
162
163
164
165
166
167
168
169
170 public void printTo(Properties props, PrintWriter out) throws IOException {
171 BufferedReader bis = null;
172 try {
173 bis = new BufferedReader(new InputStreamReader(src.openStream()));
174 String line = bis.readLine();
175 while (line != null) {
176 line = replace(line, props);
177 out.println(line);
178 line = bis.readLine();
179 }
180 } finally {
181 close(bis);
182 }
183 }
184
185
186
187
188
189
190
191
192
193 private String replace(String s, Properties props) {
194 int pos1 = s.indexOf("${");
195 while (pos1 != -1) {
196 int pos2 = s.indexOf("}", pos1 + 2);
197 if (pos2 != -1) {
198 String name = s.substring(pos1 + 2, pos2);
199 String value = props.getProperty(name);
200 if (value != null) {
201 s = s.substring(0, pos1) + value + s.substring(pos2 + 1);
202 }
203 }
204 pos1 = s.indexOf("${", pos1 + 1);
205 }
206 return s;
207 }
208
209
210
211
212
213
214 private static void close(Object obj) {
215 try {
216 Method m = obj.getClass().getMethod("close");
217 m.invoke(obj);
218 } catch (Exception e) { }
219 }
220 }