KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dinamica > parser > FastTemplateEngine


1 package dinamica.parser;
2
3 import java.util.*;
4
5
6 /**
7  * FastTemplateEngine<br>
8  * New fast parser engine, provides high performance
9  * template management. Instances of this class are serializable.
10  * <br><br>
11  * Creation date: 20/06/2004<br>
12  * (c) 2004 Martin Cordova y Asociados<br>
13  * http://www.martincordova.com/fck<br>
14  * @author Martin Cordova dinamica@martincordova.com
15  */

16 public class FastTemplateEngine implements java.io.Serializable JavaDoc
17 {
18
19
20
21     /**
22      *
23      */

24     private static final long serialVersionUID = 1L;
25
26     /** holds markers and its data values */
27     private HashMap markers = new HashMap();
28     
29     /** holds static sections of the template */
30     private ArrayList bodyParts = new ArrayList();
31
32     /** holds static sections of the template */
33     private ArrayList dataParts = new ArrayList();
34
35     public FastTemplateEngine()
36     {
37     }
38
39     public FastTemplateEngine(String JavaDoc templateBody) throws Throwable JavaDoc
40     {
41         setTemplate(templateBody);
42     }
43
44     /**
45      * Set template body
46      * @param body A String that contains the template text
47      * @throws Throwable if body=null
48      */

49     public void setTemplate(String JavaDoc body) throws Throwable JavaDoc
50     {
51         
52         if (body==null)
53             throw new Throwable JavaDoc("Invalid parameter [body]: cannot be null.");
54             
55         parse(body);
56         
57     }
58
59     /**
60      * Set data value to be used to replace a certail marker
61      * defined in the template.
62      * @param key Marker name, must be in the form ${markerName}
63      * @param value Data value
64      * @throws Triggers exception if no template has been set with setTemplate()
65      * or if the key does not match any marker.
66      */

67     public void setValue(String JavaDoc key, String JavaDoc value) throws Throwable JavaDoc
68     {
69         if (markers.containsKey(key))
70             markers.put(key, value);
71         else
72         {
73             String JavaDoc logMsg = "Invalid template key: " + key;
74             throw new Throwable JavaDoc(logMsg);
75         }
76     }
77
78     /**
79      * Parse template body to extract static parts
80      * and data markers; the template will be divided
81      * into several sections in order to assemble the page
82      * using a high performance technique.
83      * @param t Template body
84      * @throws Throwable
85      */

86     private void parse(String JavaDoc t) throws Throwable JavaDoc
87     {
88
89         int pos = 0;
90         int pos1 = 0;
91         int pos2 = 0;
92         int newPos = 0;
93         String JavaDoc str = "${fld:";
94         
95         /* search markers */
96         while ( pos >= 0 )
97         {
98             
99             /* find start of marker */
100             pos1 = t.indexOf(str, pos);
101             if (pos1>=0)
102             {
103                 
104                 /* find end of marker */
105                 newPos = pos1 + str.length();
106                 pos2 = t.indexOf("}", newPos);
107                 
108                 if (pos2>0)
109                 {
110                     // get marker
111
String JavaDoc marker = t.substring(pos1, pos2 + 1);
112                     markers.put(marker, marker);
113                     dataParts.add(marker);
114                     
115                     // get body part before marker
116
String JavaDoc part = t.substring(pos, pos1);
117                     bodyParts.add(part);
118                 }
119                 else
120                 {
121                     String JavaDoc logMsg = "Invalid template - marker not closed with '}'.";
122                     throw new Throwable JavaDoc( logMsg );
123                 }
124                 pos = pos2 + 1;
125             }
126             else
127             {
128                 // get final body part
129
String JavaDoc part = t.substring(pos);
130                 bodyParts.add(part);
131                 pos = -1;
132             }
133         }
134         
135     }
136
137     /**
138      * Clear all markers data values, this is specially
139      * useful if you store Template objects in cache
140      * and need to reuse them with another set of data values.
141      */

142     public void resetValues()
143     {
144         Set keys = markers.keySet();
145         Object JavaDoc k[] = keys.toArray();
146         for (int i=0; i<k.length;i++)
147         {
148             markers.put(k[i], k[i]);
149         }
150
151     }
152
153     /**
154      * Returns the content of the template with all the markers
155      * replaced by the corresponding value of by an empty string of
156      * no value was defined for a certain marker.
157      * @return Template body
158      */

159     public String JavaDoc toString()
160     {
161
162         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
163         
164         int markerCount = dataParts.size();
165         for (int i=0; i<bodyParts.size(); i++)
166         {
167             sb.append(bodyParts.get(i));
168             if (i<markerCount)
169                 sb.append(markers.get(dataParts.get(i)));
170         }
171         
172         return sb.toString();
173         
174     }
175     
176     /**
177      * Returns a list of markers contained in a FastTemplate object
178      * @param prefix The type of marker (fld, lbl, def, inc, seq)
179      * @return ArrayList containing Marker objects
180      * @throws Throwable
181      */

182     public ArrayList getMarkers()
183     {
184         
185         String JavaDoc name = "";
186         String JavaDoc format = "";
187         
188         ArrayList l = new ArrayList();
189         
190         Object JavaDoc k[] = markers.keySet().toArray();
191         for (int i=0; i<k.length;i++)
192         {
193             String JavaDoc key = (String JavaDoc)k[i];
194             String JavaDoc prefix = key.substring(2,5);
195         
196             int pos = key.indexOf("@");
197             if (pos>0)
198             {
199                 name = key.substring(6, pos);
200                 format = key.substring(pos + 1, key.length()-1);
201             }
202             else
203             {
204                 name = key.substring(6, key.length()-1);
205                 format = null;
206             }
207                 
208             Marker m = new Marker(key, prefix, name, format);
209             l.add(m);
210
211         }
212         return l;
213     }
214     
215 }
216
Popular Tags