KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > template > TemplateParser


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.template;
6
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Properties JavaDoc;
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 /**
20  * Subclass of Template Engine that do not generate anything but only parse the document. The TagHandlers have a
21  * callback entry to this method to set in it anything they want to. This class was introduced for parsing .j files and
22  * return a list of merge files needed for the generation. The timestamp checking can then verify all files involved in
23  * a generation and bypass the generation if -nothing has changed-.
24  *
25  * @author Vincent Harcq (vincent.harcq@hubmethods.com)
26  * @created December 27, 2001
27  * @version $Revision: 1.12 $
28  */

29 public class TemplateParser extends TemplateEngine
30 {
31     private static TemplateParser instance = new TemplateParser();
32     private List JavaDoc mergeFiles;
33
34     /**
35      * Initialize the Template Engine. Reads the XDoclet properties file, and loads any XTag handler classes specified.
36      */

37     protected TemplateParser()
38     {
39         super();
40         mergeFiles = new ArrayList JavaDoc();
41         output = null;
42     }
43
44     /**
45      * Gets the ParserInstance attribute of the TemplateParser class
46      *
47      * @return The ParserInstance value
48      */

49     public static TemplateParser getParserInstance()
50     {
51         return instance;
52     }
53
54     public TemplateTagHandler getTagHandlerFor(String JavaDoc prefix) throws TemplateException
55     {
56         return TemplateEngine.getEngineInstance().getTagHandlerFor(prefix);
57     }
58
59     /**
60      * Return the list of merge files involved in the generation.
61      *
62      * @return an array of File
63      */

64     public String JavaDoc[] getMergeFiles()
65     {
66         return (String JavaDoc[]) mergeFiles.toArray(new String JavaDoc[mergeFiles.size()]);
67     }
68
69     /**
70      * A utility method used for generating the dest_file based on template_file template file.
71      *
72      * @exception TemplateException Description of Exception
73      */

74     public void start() throws TemplateException
75     {
76         Log log = LogUtil.getLog(TemplateParser.class, "start");
77
78         String JavaDoc 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 JavaDoc msg = Translator.getString(XDocletTemplateMessages.class, XDocletTemplateMessages.TEMPLATE_NOT_FOUND, new String JavaDoc[]{getTemplateURL().toString()});
88
89             log.error(msg);
90             throw new TemplateException(msg);
91         }
92     }
93
94     /**
95      * In this class, this method does not -generate- anything but only parse the files. Callback to this class can be
96      * made by specific TagHandlers during the process.
97      *
98      * @param template Description of Parameter
99      * @exception TemplateException Description of Exception
100      */

101     public void generate(final String JavaDoc template) throws TemplateException
102     {
103         int index = 0;
104         int previousIndex = 0;
105
106         while (true) {
107             // Look for the next tag that we haven't yet handled.
108
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     /**
121      * Callback by the MergeTagsHandler to give the parser the list of merge files involved.
122      *
123      * @param file one merge file involved
124      */

125     public void addMergeFile(String JavaDoc file)
126     {
127         mergeFiles.add(file);
128     }
129
130     /**
131      * Callback by the MergeTagsHandler to know if a merge file has already been taken into account.
132      *
133      * @param file
134      * @return
135      */

136     public boolean hasMergeFile(String JavaDoc file)
137     {
138         return mergeFiles.contains(file);
139     }
140
141     /**
142      * Describe what the method does
143      *
144      * @param cmd Describe what the parameter does
145      * @param attributes Describe what the parameter does
146      * @param template Describe what the parameter does
147      * @param i Describe what the parameter does
148      * @exception TemplateException Describe the exception
149      */

150     protected void invokeContentMethod(String JavaDoc cmd, Properties JavaDoc attributes, String JavaDoc template, int i) throws TemplateException
151     {
152         // We do not need anything when parsing (e.g. isGenerationNeeded run)
153
}
154
155     /**
156      * Describe what the method does
157      *
158      * @param m Describe what the parameter does
159      * @param cmdImplProvider Describe what the parameter does
160      * @param params1 Describe what the parameter does
161      * @return Describe the return value
162      * @exception InvocationTargetException Describe the exception
163      * @exception IllegalAccessException Describe the exception
164      * @exception TemplateException Describe the exception
165      */

166     protected Object JavaDoc invoke(Method JavaDoc m, Object JavaDoc cmdImplProvider, Object JavaDoc[] params1)
167          throws InvocationTargetException JavaDoc, IllegalAccessException JavaDoc, 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         // The trick to have a quick parsing is to only take care of merging.
176
// The rest is not important and the tag handlers are bypassed by simply
177
// calling generate()
178
if (!m.getName().equals("merge")) {
179             generate((String JavaDoc) params1[0]);
180             return null;
181         }
182         else {
183             return m.invoke(cmdImplProvider, params1);
184         }
185     }
186 }
187
Popular Tags