1 5 package xdoclet.template; 6 7 import java.lang.reflect.InvocationTargetException ; 8 import java.lang.reflect.Method ; 9 import java.util.ArrayList ; 10 import java.util.List ; 11 import java.util.Properties ; 12 13 import org.apache.commons.logging.Log; 14 15 import xdoclet.util.FileManager; 16 import xdoclet.util.LogUtil; 17 import xdoclet.util.Translator; 18 19 29 public class TemplateParser extends TemplateEngine 30 { 31 private static TemplateParser instance = new TemplateParser(); 32 private List mergeFiles; 33 34 37 protected TemplateParser() 38 { 39 super(); 40 mergeFiles = new ArrayList (); 41 output = null; 42 } 43 44 49 public static TemplateParser getParserInstance() 50 { 51 return instance; 52 } 53 54 public TemplateTagHandler getTagHandlerFor(String prefix) throws TemplateException 55 { 56 return TemplateEngine.getEngineInstance().getTagHandlerFor(prefix); 57 } 58 59 64 public String [] getMergeFiles() 65 { 66 return (String []) mergeFiles.toArray(new String [mergeFiles.size()]); 67 } 68 69 74 public void start() throws TemplateException 75 { 76 Log log = LogUtil.getLog(TemplateParser.class, "start"); 77 78 String content = FileManager.getURLContent(getTemplateURL()); 79 80 if (content != null) { 81 log.debug("content.length()=" + content.length()); 82 83 setCurrentLineNum(0); 84 generate(content); 85 } 86 else { 87 String msg = Translator.getString(XDocletTemplateMessages.class, XDocletTemplateMessages.TEMPLATE_NOT_FOUND, new String []{getTemplateURL().toString()}); 88 89 log.error(msg); 90 throw new TemplateException(msg); 91 } 92 } 93 94 101 public void generate(final String template) throws TemplateException 102 { 103 int index = 0; 104 int previousIndex = 0; 105 106 while (true) { 107 index = template.indexOf(XDOCLET_HEAD, previousIndex); 109 110 if (index == -1) { 111 break; 112 } 113 else { 114 previousIndex = handleTag(index, template); 115 } 116 } 117 118 } 119 120 125 public void addMergeFile(String file) 126 { 127 mergeFiles.add(file); 128 } 129 130 136 public boolean hasMergeFile(String file) 137 { 138 return mergeFiles.contains(file); 139 } 140 141 150 protected void invokeContentMethod(String cmd, Properties attributes, String template, int i) throws TemplateException 151 { 152 } 154 155 166 protected Object invoke(Method m, Object cmdImplProvider, Object [] params1) 167 throws InvocationTargetException , IllegalAccessException , TemplateException 168 { 169 Log log = LogUtil.getLog(TemplateParser.class, "invoke"); 170 171 if (log.isDebugEnabled()) { 172 log.debug(m.getName() + params1[0]); 173 } 174 175 if (!m.getName().equals("merge")) { 179 generate((String ) params1[0]); 180 return null; 181 } 182 else { 183 return m.invoke(cmdImplProvider, params1); 184 } 185 } 186 } 187 | Popular Tags |