01: /*
02: * Created on Feb 20, 2005
03: *
04: */
05: package net.sf.thingamablog.generator;
06:
07: import java.io.File;
08: import java.io.FileInputStream;
09: import java.io.InputStream;
10: import java.net.MalformedURLException;
11: import java.net.URL;
12: import java.util.Hashtable;
13:
14: import net.atlanticbb.tantlinger.io.IOUtils;
15: import net.sf.thingamablog.TimeoutInputStream;
16:
17: /**
18: * @author Bob Tantlinger
19: */
20: public class IncludeContainer extends BasicContainer {
21: public static final String FILE = "file";
22:
23: private HyperTextTag includeText = new HyperTextTag("IncludeText");
24: private Hashtable def = new Hashtable();
25: private String text = null;
26:
27: public IncludeContainer() {
28: super ("Include");
29: registerTag(includeText);
30: def.put(FILE, "");
31: }
32:
33: public void initialize(Hashtable at) {
34: text = null;
35: InputStream is = null;
36: try {
37: String src = at.get(FILE).toString();
38: if (isValidURL(src)) {
39: URL url = new URL(src);
40: is = new TimeoutInputStream(url.openStream(), 1024,
41: 10000, 10000);
42: } else {
43: File f = new File(src);
44: if (f.isFile() && f.canRead())
45: is = new FileInputStream(f);
46: }
47:
48: if (is != null) {
49: text = IOUtils.read(is);
50: is.close();
51: }
52: } catch (Exception ex) {
53: ex.printStackTrace();
54: } finally {
55: IOUtils.close(is);
56: }
57: }
58:
59: private boolean isValidURL(String src) {
60: try {
61: new URL(src);
62: return true;
63: } catch (MalformedURLException ex) {
64: }
65:
66: return false;
67: }
68:
69: /* public void initialize(Hashtable at)
70: {
71: if(at.get(FILE) != null && !at.get(FILE).equals(""))
72: file = new File(at.get(FILE).toString());
73: else
74: file = null;
75: }*/
76:
77: public Hashtable getDefaultAttributes() {
78: return def;
79: }
80:
81: public boolean isVisible() {
82:
83: return text != null;
84: /* try
85: {
86: return file != null && file.canRead();
87: }
88: catch(Exception ex)
89: {
90: return false;
91: }*/
92: }
93:
94: public Object getValueForTag(TemplateTag t) {
95: return text;
96: }
97: }
|