Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.osgi; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | public class PreprocessResource extends AbstractResource { |
| 6 | final Resource resource; |
| 7 | final Processor processor; |
| 8 | |
| 9 | public PreprocessResource(Processor processor, Resource r) { |
| 10 | super(r.lastModified()); |
| 11 | this.processor = processor; |
| 12 | this.resource = r; |
| 13 | setExtra(resource.getExtra()); |
| 14 | } |
| 15 | |
| 16 | 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 { |
| 23 | in = resource.openInputStream(); |
| 24 | 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 | } finally { |
| 36 | if (rdr != null) { |
| 37 | rdr.close(); |
| 38 | } |
| 39 | if (in != null) { |
| 40 | in.close(); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |