KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > util > Template


1 package com.genimen.djeneric.tools.generator.util;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileReader JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 import com.genimen.djeneric.repository.DjPersistenceManager;
9 import com.genimen.djeneric.repository.exceptions.DjenericException;
10 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine;
11 import com.genimen.djeneric.tools.generator.core.ParseException;
12 import com.genimen.djeneric.tools.generator.core.nodes.TemplateNode;
13
14 public class Template
15 {
16   TemplateNode _parseTree = null;
17   TemplateSet _templateSet;
18   boolean _valid = true;
19   String JavaDoc _loadLog = "";
20   String JavaDoc _location;
21   long _timestamp = -1;
22
23   public Template(TemplateSet templateSet, String JavaDoc location)
24   {
25     _templateSet = templateSet;
26     _location = location;
27     try
28     {
29       load(location);
30     }
31     catch (Exception JavaDoc x)
32     {
33       _valid = false;
34       _loadLog = "Error loading template " + _location + "\n" + x.getMessage() + "\n";
35     }
36   }
37
38   private void load(String JavaDoc location) throws ParseException, IOException JavaDoc
39   {
40     _valid = true;
41     File JavaDoc tplFile = new File JavaDoc(location);
42     _timestamp = tplFile.lastModified();
43     String JavaDoc tplCode = readFile(tplFile);
44     _parseTree = DjentelParserEngine.getTree(tplCode);
45   }
46
47   private void reloadIfModified() throws ParseException, IOException JavaDoc
48   {
49     File JavaDoc tplFile = new File JavaDoc(_location);
50     if (_timestamp != tplFile.lastModified())
51     {
52       load(_location);
53     }
54   }
55
56   private String JavaDoc readFile(File JavaDoc inFile) throws IOException JavaDoc
57   {
58     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(inFile));
59     StringBuffer JavaDoc src = new StringBuffer JavaDoc(100);
60     String JavaDoc ln;
61     while ((ln = br.readLine()) != null)
62     {
63       src.append(ln);
64       src.append("\n");
65     }
66     br.close();
67     return src.toString();
68   }
69
70   public boolean isValid()
71   {
72     return _valid;
73   }
74
75   public String JavaDoc getLoadLog()
76   {
77     return _loadLog;
78   }
79
80   public boolean isRootBased()
81   {
82     if (_parseTree == null) return true;
83     return _parseTree.isRootBased();
84   }
85
86   public String JavaDoc getObjectType()
87   {
88     if (_parseTree == null) return "<Unknown>";
89
90     return _parseTree.getObjectType();
91   }
92
93   public String JavaDoc getModuleName()
94   {
95     if (_parseTree == null) return "<Unknown>";
96     return _parseTree.getModuleName();
97   }
98
99   public String JavaDoc generate(DjPersistenceManager mgr) throws DjenericException, ParseException, IOException JavaDoc
100   {
101     reloadIfModified();
102     return _parseTree.evaluate(mgr);
103   }
104
105   public String JavaDoc generate(DjPersistenceManager mgr, long objectId) throws DjenericException, ParseException, IOException JavaDoc
106   {
107     reloadIfModified();
108     return _parseTree.evaluate(mgr, objectId);
109   }
110
111   public String JavaDoc getFileName() throws ParseException
112   {
113     return _parseTree.getFileName();
114   }
115
116   public String JavaDoc toString()
117   {
118     return _location;
119   }
120
121   public String JavaDoc getLocation()
122   {
123     return _location;
124   }
125
126 }
Popular Tags