01: package com.sun.portal.proxylet.servlet;
02:
03: import java.util.*;
04: import java.util.logging.Logger;
05: import java.io.*;
06:
07: import com.sun.portal.proxylet.util.*;
08:
09: public class Template {
10:
11: private Map tokens = null;
12: private String fileName = null;
13: private Logger logger = null;
14:
15: public Template(String fileName) throws Exception {
16: logger = Logger.getLogger("com.sun.portal.sra.proxylet");
17:
18: this .fileName = fileName;
19: File f = new File(fileName);
20: if (!f.exists() || !f.canRead()) {
21: logger.info("Could not read file " + fileName);
22: throw new Exception("File " + fileName + " not readable");
23: }
24: tokens = new Hashtable();
25: }
26:
27: public void set(String field, String value) {
28: if (value == null)
29: return;
30: tokens.put(field, value);
31: }
32:
33: public String render() throws Exception {
34: //
35: // replace all the tokens in the file and
36: // return the string
37: //
38: FileInputStream inStrm = new FileInputStream(fileName);
39: int available = inStrm.available();
40: byte[] data = new byte[available];
41: inStrm.read(data, 0, available);
42:
43: String template = new String(data);
44:
45: Iterator keys = tokens.keySet().iterator();
46: while (keys.hasNext()) {
47: String token = (String) keys.next();
48: String value = (String) tokens.get(token);
49:
50: template = StringHelper.searchAndReplace(template, token,
51: value);
52: }
53:
54: return template;
55: }
56: }
|