KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.util.text;
2
3
4 /**
5  * This instance holds a variable that is interpolated at rendering time.
6  *
7  * @author JC Desrochers
8  *
9  * <dl>
10  * <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>
11  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
12  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
13  * </dl>
14  */

15 public class VariableElement implements TemplateElementIF {
16   /////////////////////////////////////////////////////////////////////////////////////////
17
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
18
/////////////////////////////////////////////////////////////////////////////////////////
19

20   /** The name of this variable element. */
21   private String JavaDoc _theName;
22   
23   private boolean _throwEx;
24
25   /////////////////////////////////////////////////////////////////////////////////////////
26
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
27
/////////////////////////////////////////////////////////////////////////////////////////
28

29   /**
30    * Creates a new VariableElement with the argument passed in.
31    *
32    * @param aName The name of this variable element.
33    */

34   public VariableElement(String JavaDoc aName, boolean throwEx) {
35     _throwEx = throwEx;
36     _theName = aName;
37   }
38
39   /////////////////////////////////////////////////////////////////////////////////////////
40
////////////////////////////////// ACCESSOR METHODS ///////////////////////////////////
41
/////////////////////////////////////////////////////////////////////////////////////////
42

43   /**
44    * Returns the name of this variable element.
45    *
46    * @return The name of this variable element.
47    */

48   public String JavaDoc getName() {
49     return _theName;
50   }
51
52   /////////////////////////////////////////////////////////////////////////////////////////
53
/////////////////////////////// INTERACE IMPLEMENTATION ///////////////////////////////
54
/////////////////////////////////////////////////////////////////////////////////////////
55

56   /**
57    * Renders this template element using the template context passed in and
58    * returns the result in a new string.
59    *
60    * @param aContext The template context to use in the rendering process.
61    * @return The result of the rendering operation as a new string.
62    * @exception TemplateException If an error occurs rendering this template element.
63    */

64   public String JavaDoc render(TemplateContextIF aContext) throws TemplateException {
65     Object JavaDoc aValue = aContext.getValue(_theName);
66
67     if (aValue == null) {
68       if(_throwEx)
69         throw new TemplateException("No value found for: " + _theName);
70       else
71         aValue = _theName;
72     } else if (!(aValue instanceof String JavaDoc)) {
73       throw new TemplateException("The value for " + _theName +
74         " is not a string : " + aValue);
75     }
76
77     return (String JavaDoc) aValue;
78   }
79
80   /**
81    * Renders this template element using the template context passed in and
82    * appending the result in the string buffer passed in.
83    *
84    * @param aContext The template context to use in the rendering process.
85    * @exception TemplateException If an error occurs rendering this template element.
86    */

87   public void render(TemplateContextIF aContext, StringBuffer JavaDoc aBuffer)
88     throws TemplateException {
89     Object JavaDoc aValue = aContext.getValue(_theName);
90
91     if (aValue == null) {
92       if(_throwEx)
93         throw new TemplateException("No value found for: " + _theName);
94       else
95         aValue = _theName;
96     } else if (!(aValue instanceof String JavaDoc)) {
97       throw new TemplateException("The value for " + _theName +
98         " is not a string : " + aValue);
99     }
100
101     aBuffer.append(aValue);
102   }
103 }
104
Popular Tags