1 package jfun.yan.xml.nut; 2 3 4 import java.net.MalformedURLException ; 5 6 import jfun.yan.Component; 7 import jfun.yan.ParameterBinder; 8 import jfun.yan.PropertyBinder; 9 import jfun.yan.xml.ConfigurationException; 10 import jfun.yan.xml.Location; 11 import jfun.yan.xml.NutEnvironment; 12 import jfun.yan.xml.NutsUtils; 13 14 65 public abstract class Nut implements java.io.Serializable { 66 private Location loc; 67 private NutEnvironment env; 68 private String tagname; 69 private String id; 70 private boolean globallyDefined; 71 private int seq_no; 72 75 public boolean isGloballyDefined(){ 76 return globallyDefined; 77 } 78 86 public void register(Object key, Object val, 87 boolean overridable, boolean overriding){ 88 env.registerDynamic(key, val, overridable, overriding, loc); 89 } 90 94 public int getSequenceNumber(){ 95 return seq_no; 96 } 97 101 public String getId() { 102 return id; 103 } 104 108 public void setId(String id) { 109 this.id = id; 110 } 111 public String toString() { 112 return tagname; 113 } 114 117 public Location getTagLocation(){ 118 return loc; 119 } 120 124 public void initSequenceNumber(int seq){ 125 this.seq_no = seq; 126 } 127 131 public void initGloballyDefined(boolean gd){ 132 this.globallyDefined = gd; 133 } 134 138 public void initTagLocation(Location loc){ 139 this.loc = loc; 140 } 141 145 public void initNutEnvironment(NutEnvironment env){ 146 this.env = env; 147 } 148 151 public ClassLoader getComponentClassLoader() { 152 return env.getComponentClassLoader(); 153 } 154 162 public ClassLoader getNutClassLoader(String classpath){ 163 try{ 164 return NutsUtils.getClassLoader(getClass().getClassLoader(), 165 classpath, 166 env.getBaseDir()); 167 } 168 catch(MalformedURLException e){ 169 throw new ConfigurationException("invalid classpath", 170 loc); 171 } 172 } 173 177 public void initTagName(String name){ 178 this.tagname = name; 179 } 180 183 public NutEnvironment getNutEnvironment() { 184 return env; 185 } 186 187 193 public Object convert(Class target_type, Object v){ 194 return env.convert(target_type, v, loc); 195 } 196 203 public Component cast(final Class target_type, Component c){ 204 return env.cast(target_type, c, this.getTagLocation()); 205 } 206 209 public String getTagName() { 210 return tagname; 211 } 212 213 217 public void registerEagerInstantiation(Component c){ 218 if(!globallyDefined){ 219 raise("only global tags can be registered as eager instantiated"); 220 } 221 225 env.registerEagerInstantiation(seq_no, id, c); 226 } 227 234 public ConfigurationException raise(String msg){ 235 throw new ConfigurationException(tagname+": " + msg, loc); 236 } 237 244 public ConfigurationException raise(Throwable e){ 245 throw new ConfigurationException(tagname+": " + e.getMessage(), e, loc); 246 } 247 248 254 protected void checkMandatory(String attrname, Object v){ 255 if(v==null) 256 raise("missing mandatory attribute <"+attrname +">"); 257 } 258 265 protected void checkMandatory(String name1, Object v1, 266 String name2, Object v2){ 267 if(v1==null&&v2==null) 268 raise("either <"+name1+"> or <"+ name2+ "> has to be specified."); 269 } 270 276 protected void checkDuplicate(String attrname, Object v){ 277 if(v!=null){ 278 raise("attribute <"+attrname+"> already specified."); 279 } 280 } 281 286 290 291 299 public ParameterBinder getParameterWiring(String mode){ 300 return env.getParameterWiringMode(mode, loc); 301 } 302 310 public PropertyBinder getPropertyWiring(String mode){ 311 return env.getPropertyWiringMode(mode, loc); 312 } 313 314 } 316 | Popular Tags |