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 23 public class Constant implements ObjectCreationCallback, ContextAware { 24 25 private String _name; 26 private RenderContext _context; 27 28 public void setName(String name) { 29 _name = name; 30 } 31 32 public void handleContext(RenderContext ctx) { 33 _context = ctx; 34 } 35 36 39 public Object 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 className = _name.substring(0, i); 49 Class 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 e) { 56 throw new ConfigurationException("Unkown constant: " 57 + _name.substring(i + 1) + " for class " + className); 58 } catch(IllegalAccessException e) { 59 throw new ConfigurationException("Constant not public: " 60 + _name.substring(i + 1) + " for class " + className); 61 } 62 } 63 64 } 65 | Popular Tags |