blob: 8b3f79e64549674fcdb6a55c0584adaaf24fc398 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.osgi;
2
3import java.io.*;
4
5public class PreprocessResource extends AbstractResource {
Stuart McCulloch2286f232012-06-15 13:27:53 +00006 final Resource resource;
7 final Processor processor;
Stuart McCullochbb014372012-06-07 21:57:32 +00008
Stuart McCulloch2286f232012-06-15 13:27:53 +00009 public PreprocessResource(Processor processor, Resource r) {
10 super(r.lastModified());
11 this.processor = processor;
12 this.resource = r;
13 setExtra(resource.getExtra());
14 }
Stuart McCullochbb014372012-06-07 21:57:32 +000015
Stuart McCulloch2286f232012-06-15 13:27:53 +000016 protected byte[] getBytes() throws Exception {
17 ByteArrayOutputStream bout = new ByteArrayOutputStream(2000);
18 OutputStreamWriter osw = new OutputStreamWriter(bout, Constants.DEFAULT_CHARSET);
19 PrintWriter pw = new PrintWriter(osw);
20 InputStream in = null;
21 BufferedReader rdr = null;
22 try {
Stuart McCullochbb014372012-06-07 21:57:32 +000023 in = resource.openInputStream();
Stuart McCulloch2286f232012-06-15 13:27:53 +000024 rdr = new BufferedReader(new InputStreamReader(in, "UTF8"));
25 String line = rdr.readLine();
26 while (line != null) {
27 line = processor.getReplacer().process(line);
28 pw.println(line);
29 line = rdr.readLine();
30 }
31 pw.flush();
32 byte[] data = bout.toByteArray();
33 return data;
34
35 }
36 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +000037 if (rdr != null) {
38 rdr.close();
39 }
40 if (in != null) {
41 in.close();
42 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000043 }
44 }
Stuart McCullochbb014372012-06-07 21:57:32 +000045}