KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > gumby > tags > Constant


1 package org.sapia.gumby.tags;
2
3 import org.sapia.gumby.RenderContext;
4 import org.sapia.gumby.factory.ContextAware;
5 import org.sapia.util.xml.confix.ConfigurationException;
6 import org.sapia.util.xml.confix.ObjectCreationCallback;
7
8 /**
9  * @author Yanick Duchesne
10  *
11  * <dl>
12  * <dt><b>Copyright: </b>
13  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
14  * Source Software </a>. All Rights Reserved.</dd>
15  * </dt>
16  * <dt><b>License: </b>
17  * <dd>Read the license.txt file of the jar or visit the <a
18  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
19  * OSS web site</dd>
20  * </dt>
21  * </dl>
22  */

23 public class Constant implements ObjectCreationCallback, ContextAware {
24
25   private String JavaDoc _name;
26   private RenderContext _context;
27
28   public void setName(String JavaDoc name) {
29     _name = name;
30   }
31
32   public void handleContext(RenderContext ctx) {
33     _context = ctx;
34   }
35
36   /**
37    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
38    */

39   public Object JavaDoc onCreate() throws ConfigurationException {
40     if(_name == null) {
41       throw new ConfigurationException("Constant name not specified");
42     }
43     int i = _name.lastIndexOf('.');
44     if(i < 0) {
45       throw new ConfigurationException("Invalid name for constant: " + _name
46           + "; expecting format: some.class.Name.Constant");
47     }
48     String JavaDoc className = _name.substring(0, i);
49     Class JavaDoc clazz = _context.getSettings().resolveClass(className);
50     if(clazz == null) {
51       throw new ConfigurationException("Could not resolve class: " + className);
52     }
53     try {
54       return clazz.getField(_name.substring(i + 1)).get(null);
55     } catch(NoSuchFieldException JavaDoc e) {
56       throw new ConfigurationException("Unkown constant: "
57           + _name.substring(i + 1) + " for class " + className);
58     } catch(IllegalAccessException JavaDoc e) {
59       throw new ConfigurationException("Constant not public: "
60           + _name.substring(i + 1) + " for class " + className);
61     }
62   }
63
64 }
65
Popular Tags