1 package com.genimen.djeneric.tools.generator.util; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileReader ; 6 import java.io.IOException ; 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 _loadLog = ""; 20 String _location; 21 long _timestamp = -1; 22 23 public Template(TemplateSet templateSet, String location) 24 { 25 _templateSet = templateSet; 26 _location = location; 27 try 28 { 29 load(location); 30 } 31 catch (Exception x) 32 { 33 _valid = false; 34 _loadLog = "Error loading template " + _location + "\n" + x.getMessage() + "\n"; 35 } 36 } 37 38 private void load(String location) throws ParseException, IOException 39 { 40 _valid = true; 41 File tplFile = new File (location); 42 _timestamp = tplFile.lastModified(); 43 String tplCode = readFile(tplFile); 44 _parseTree = DjentelParserEngine.getTree(tplCode); 45 } 46 47 private void reloadIfModified() throws ParseException, IOException 48 { 49 File tplFile = new File (_location); 50 if (_timestamp != tplFile.lastModified()) 51 { 52 load(_location); 53 } 54 } 55 56 private String readFile(File inFile) throws IOException 57 { 58 BufferedReader br = new BufferedReader (new FileReader (inFile)); 59 StringBuffer src = new StringBuffer (100); 60 String 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 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 getObjectType() 87 { 88 if (_parseTree == null) return "<Unknown>"; 89 90 return _parseTree.getObjectType(); 91 } 92 93 public String getModuleName() 94 { 95 if (_parseTree == null) return "<Unknown>"; 96 return _parseTree.getModuleName(); 97 } 98 99 public String generate(DjPersistenceManager mgr) throws DjenericException, ParseException, IOException 100 { 101 reloadIfModified(); 102 return _parseTree.evaluate(mgr); 103 } 104 105 public String generate(DjPersistenceManager mgr, long objectId) throws DjenericException, ParseException, IOException 106 { 107 reloadIfModified(); 108 return _parseTree.evaluate(mgr, objectId); 109 } 110 111 public String getFileName() throws ParseException 112 { 113 return _parseTree.getFileName(); 114 } 115 116 public String toString() 117 { 118 return _location; 119 } 120 121 public String getLocation() 122 { 123 return _location; 124 } 125 126 } | Popular Tags |