KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > text > TemplateFactory


1 package org.sapia.util.text;
2
3
4 // Import of Sun's JDK classes
5
// ---------------------------
6
import java.util.ArrayList JavaDoc;
7
8
9 /**
10  * An instance of this class is used to parse a "template", and replace the variables
11  * in the template by values that are found in a context. Values are bound in the context
12  * under a given name. Variables names (in the template) correspond to the names
13  * under which the values are bound in the context.
14  * <p>
15  * Variables are delimited by a 'start' delimiter and an 'end' delimiter.
16  * <p>
17  * Usage:
18  * <pre>
19  * String toRender = "your current directory: ${user.dir}";
20  * TemplateFactory fac = new TemplateFactory();
21  * TemplateContextIF ctx = new SystemContext();
22  * TemplateElementIF template = fac.parse(toRender);
23  * System.out.println(template.render(ctx));
24  * </pre>
25  *
26  * @see org.sapia.util.text.TemplateContextIF
27  * @see org.sapia.util.text.SystemContext
28  *
29  * @author JC Descrochers
30  *
31  * <dl>
32  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
33  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
34  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
35  * </dl>
36  */

37 public class TemplateFactory {
38   /////////////////////////////////////////////////////////////////////////////////////////
39
////////////////////////////////// CLASS ATTRIBUTES ///////////////////////////////////
40
/////////////////////////////////////////////////////////////////////////////////////////
41

42   /** Defines the default stating variable delimiter. */
43   public static final String JavaDoc DEFAULT_STARTING_DELIMITER = "${";
44
45   /** Defines the default ending variable delimiter. */
46   public static final String JavaDoc DEFAULT_ENDING_DELIMITER = "}";
47
48   /////////////////////////////////////////////////////////////////////////////////////////
49
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
50
/////////////////////////////////////////////////////////////////////////////////////////
51

52   /** The starting delimiter of this template factory. */
53   private String JavaDoc _theStartingDelimiter = DEFAULT_STARTING_DELIMITER;
54
55   /** The ending delimiter of this template factory. */
56   private String JavaDoc _theEndingDelimiter = DEFAULT_ENDING_DELIMITER;
57   
58   private boolean _throwExcMissingVar = true;
59
60   /////////////////////////////////////////////////////////////////////////////////////////
61
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
62
/////////////////////////////////////////////////////////////////////////////////////////
63

64   /**
65    * Creates a new TemplateFactory with ${ and } as start and end delimiters, respectively.
66    */

67   public TemplateFactory() {
68   }
69
70   /**
71    * Creates a new TemplateFactory instance with the arguments passed in.
72    *
73    * @param aStartingDelimiter The starting variable delimiter.
74    * @param anEndingDelimiter The ending variable delimiter.
75    */

76   public TemplateFactory(String JavaDoc aStartingDelimiter, String JavaDoc anEndingDelimiter) {
77     _theStartingDelimiter = aStartingDelimiter;
78     _theEndingDelimiter = anEndingDelimiter;
79   }
80   
81   /**
82    * @param throwEx if <code>true</code>, indicates that an exception is thrown
83    * if a variable could not be resolved - defaults to true.
84    */

85   public void setThrowExcOnMissingVar(boolean throwEx){
86     _throwExcMissingVar = throwEx;
87   }
88
89   /////////////////////////////////////////////////////////////////////////////////////////
90
/////////////////////////////////// HELPER METHODS ////////////////////////////////////
91
/////////////////////////////////////////////////////////////////////////////////////////
92

93   /**
94    * Parses the string content passed in a creates a template element that
95    * represents it.
96    *
97    * @param aContent The content to parse.
98    * @exception IllegalArgument If the string content passed in is null.
99    */

100   public TemplateElementIF parse(String JavaDoc aContent) {
101     // Validate the argument
102
if (aContent == null) {
103       throw new IllegalArgumentException JavaDoc("The content passed in is null.");
104     }
105
106     String JavaDoc aBuffer;
107     ArrayList JavaDoc someElements = new ArrayList JavaDoc();
108
109     int aLastPosition = 0;
110     boolean isFinished = false;
111
112     while (!isFinished && (aLastPosition < aContent.length())) {
113       // Look for a variable starting delimiter
114
int anIndex = aContent.indexOf(_theStartingDelimiter, aLastPosition);
115
116       // If no delimiter is found the content is constant
117
if (anIndex < 0) {
118         aBuffer = aContent.substring(aLastPosition);
119         someElements.add(new ConstantElement(aBuffer));
120         isFinished = true;
121
122         // A delimiter was found
123
} else {
124         // If there's a content prior to the variable definition
125
if (anIndex > aLastPosition) {
126           // Create a constant element with the content
127
aBuffer = aContent.substring(aLastPosition, anIndex);
128           someElements.add(new ConstantElement(aBuffer));
129         }
130
131         // Searching for the ending variable delimiter
132
aLastPosition = anIndex + _theStartingDelimiter.length();
133         anIndex = aContent.indexOf(_theEndingDelimiter, aLastPosition);
134
135         // If no ending delimiter is found
136
if (anIndex < 0) {
137           // Creating a constant element starting with the starting delimiter
138
aBuffer = aContent.substring(aLastPosition -
139               _theStartingDelimiter.length());
140           someElements.add(new ConstantElement(aBuffer));
141           isFinished = true;
142         }
143         // The variable delimeters are found but without a name
144
else if (anIndex == aLastPosition) {
145           aBuffer = aContent.substring(aLastPosition -
146               _theStartingDelimiter.length(),
147               anIndex + _theEndingDelimiter.length());
148           someElements.add(new ConstantElement(aBuffer));
149           aLastPosition = anIndex + _theEndingDelimiter.length();
150         }
151         else {
152           int aNextDelimiter = aContent.indexOf(_theStartingDelimiter,
153               aLastPosition);
154
155           // There's another starting delimiter between the two position
156
if ((aNextDelimiter != -1) && (anIndex > aNextDelimiter)) {
157             aBuffer = aContent.substring(aLastPosition -
158                 _theStartingDelimiter.length(), aNextDelimiter);
159             someElements.add(new ConstantElement(aBuffer));
160             aLastPosition = aNextDelimiter;
161           }
162           // Creating a variable element with the variable name
163
else {
164             aBuffer = aContent.substring(aLastPosition, anIndex);
165             someElements.add(new VariableElement(aBuffer, _throwExcMissingVar));
166             aLastPosition = anIndex + _theEndingDelimiter.length();
167           }
168         }
169       }
170     }
171
172     return new CompositeElement(someElements);
173   }
174 }
175
Popular Tags