1 package jfun.yan.xml; 2 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.lang.reflect.Array ; 7 import java.util.HashMap ; 8 import java.util.HashSet ; 9 import java.util.List ; 10 import java.util.Locale ; 11 import java.util.Set ; 12 13 import jfun.util.Misc; 14 import jfun.util.dict.Dict; 15 import jfun.yan.Component; 16 import jfun.yan.Components; 17 import jfun.yan.Creator; 18 import jfun.yan.ParameterBinder; 19 import jfun.yan.PropertyBinder; 20 import jfun.yan.function.Signature; 21 import jfun.yan.util.Utils; 22 import jfun.yan.util.resource.ResourceLoader; 23 24 25 import org.xml.sax.Locator ; 26 27 class MyUtil { 28 static boolean isRefName(String name){ 29 if(name.length()>0){ 30 if(name.charAt(0)!='$') return false; 31 if(name.length()>1){ 32 final char c1 = name.charAt(1); 33 return c1!='$' && Character.isJavaIdentifierPart(c1); 34 } 35 else return true; 36 } 37 return false; 38 } 39 static Location toLocation(Locator loc){ 40 return new Location(loc.getSystemId(), loc.getLineNumber(), loc.getColumnNumber()); 41 } 42 static Set getNameSet(String [] names){ 43 final HashSet result = new HashSet (names.length); 44 for(int i=0; i<names.length; i++){ 45 result.add(names[i]); 46 } 47 return result; 48 } 49 private static final HashMap primitives = getPrimitiveTypes(); 50 private static HashMap getPrimitiveTypes(){ 51 final HashMap types = new HashMap (); 52 types.put("int", int.class); 53 types.put("short", short.class); 54 types.put("byte", byte.class); 55 types.put("char", char.class); 56 types.put("boolean", boolean.class); 57 types.put("long", long.class); 58 types.put("float", float.class); 59 types.put("double", double.class); 60 return types; 61 } 62 static Class getArrayType(Class type){ 63 return Misc.getArrayType(type); 64 } 65 static void assertAttributes(Tag tag, Set table){ 66 final Attributes attrs = tag.getAttributes(); 67 for(int i=0;i<attrs.size();i++){ 68 final String name = attrs.getKey(i); 69 if(!table.contains(name)){ 70 throw new ConfigurationException("attribute "+name+" not supported for " 71 +tag.getName(), 72 tag.getLocation()); 73 } 74 } 75 } 76 static Class getClass(ClassLoader cloader, String typename) 77 throws ClassNotFoundException { 78 if(typename.endsWith("[]")){ 79 return getArrayType( 80 getClass(cloader, typename.substring(0, typename.length()-2)) 81 ); 82 } 83 Class type = (Class )primitives.get(typename); 84 if(type!=null) return type; 85 return cloader.loadClass(typename); 86 } 87 private static Class getCastType(ClassLoader cloader, Attributes attrs, Location loc){ 88 final String typename = attrs.getVal(Constants.TYPE); 89 if(typename != null){ 90 try{ 91 final Class type = getClass(cloader, typename.trim()); 92 return type; 93 } 94 catch(ClassNotFoundException e){ 95 throw new ConfigurationException("class " + typename+" not found", 96 loc); 97 } 98 } 99 return null; 100 } 101 static SingletonMode getSingletonStrategy( 102 String singleton, Location loc, SingletonMode def){ 103 if(singleton==null){ 104 return def; 105 } 106 else{ 107 singleton = singleton.trim().toLowerCase(Locale.US); 108 if(singleton.equals("thread")){ 109 return Modes.thread_local_singleton; 110 } 111 else{ 112 Boolean yesno = NutsUtils.toBoolean(singleton); 113 if(yesno==null){ 114 throw new ConfigurationException("unrecognized value for singleton: "+singleton, 115 loc); 116 } 117 if(yesno.booleanValue()){ 118 return Modes.simple_singleton; 119 } 120 else return null; 121 } 122 } 123 } 124 130 145 private static Set wirenames = getNameSet(new String []{ 146 Constants.BYNAME, Constants.BYTYPE, Constants.BYQUALIFIEDNAME, 147 Constants.TRUE, Constants.FALSE, Constants.YES, Constants.NO, 148 Constants.ON, Constants.OFF, Constants.AUTODETECT, 149 }); 150 private static Set propmodes = getNameSet(new String []{ 151 Constants.BYNAME, Constants.BYQUALIFIEDNAME 152 }); 153 static ParameterBinder getParamWiring(String autowire, AutoWiringMap custom_wirings, 154 Location loc, ParameterBinder def){ 155 if(autowire!=null){ 156 autowire = autowire.trim().toLowerCase(Locale.US); 157 ParameterBinder custom = custom_wirings.getParameterWiringMode(autowire); 158 if(custom!=null) return custom; 159 if(propmodes.contains(autowire)){ 160 return Modes.params_bytype; 162 } 163 if(Constants.BYTYPE.equals(autowire) || Constants.AUTODETECT.equals(autowire)){ 164 return Modes.params_bytype; 165 } 166 else{ 167 final Boolean tmp = NutsUtils.toBoolean(autowire); 168 if(tmp!=null){ 169 if(tmp.booleanValue()){ 170 return Modes.params_bytype; 171 } 172 else return null; 173 } 174 else if(custom_wirings.getPropertyWiringMode(autowire)==null){ 175 throw new ConfigurationException("unrecognized autowire mode: "+autowire, 176 loc); 177 } 178 } 179 } 180 return def; 181 } 182 private static final HashMap auto_resolutions = getResolutionNames(); 183 private static final HashMap manual_names = getManualNames(); 184 private static HashMap getManualNames(){ 185 final HashMap result = new HashMap (); 186 result.put(Constants.OFF, Boolean.valueOf(false)); 187 result.put(Constants.FALSE, Boolean.valueOf(false)); 188 result.put(Constants.NO, Boolean.valueOf(false)); 189 result.put(Constants.NONE, Boolean.valueOf(false)); 190 return result; 191 } 192 193 private static HashMap getResolutionNames(){ 194 final HashMap result = new HashMap (); 195 final PropertyBinder bytype = Modes.props_bytype; 196 result.put(Constants.BYNAME, Modes.props_byname); 197 result.put(Constants.BYTYPE, bytype); 198 result.put(Constants.AUTODETECT, Modes.props_autodetect); 199 result.put(Constants.BYQUALIFIEDNAME, Modes.props_byqualifiedname); 203 return result; 204 } 205 public static PropertyBinder getPropWiring(String autowire, 206 AutoWiringMap custom_wirings, Location loc, PropertyBinder def){ 207 if(autowire!=null){ 208 autowire = autowire.trim().toLowerCase(Locale.US); 209 PropertyBinder custom = custom_wirings.getPropertyWiringMode(autowire); 210 if(custom!=null) return custom; 211 212 if(manual_names.containsKey(autowire)){ 213 return null; 214 } 215 final PropertyBinder result = (PropertyBinder)auto_resolutions.get(autowire); 216 if(result != null) return result; 217 else if(custom_wirings.getParameterWiringMode(autowire)!=null){ 218 throw new ConfigurationException("unrecognized autowire mode: "+autowire, 219 loc); 220 } 221 } 222 return def; 223 } 224 static ParameterBinder autocast(final ParameterBinder binder, 225 final Location loc, final Converter conv){ 226 if(binder==null) return null; 227 return new ParameterBinder(){ 228 public String toString(){ 229 return binder.toString(); 230 } 231 public Creator bind(Signature src, int ind, Class type) { 232 return cast(type, Components.adapt(binder.bind(src, ind, type)), 233 loc, conv); 234 } 235 }; 236 } 237 static PropertyBinder autocast(final PropertyBinder binder, 238 final Location loc, final Converter conv){ 239 if(binder==null) return null; 240 return new PropertyBinder(){ 241 public String toString(){ 242 return binder.toString(); 243 } 244 public Creator bind(Class component_type, Object key, Class type) { 245 return cast(type, Components.adapt(binder.bind(component_type, key, type)), 246 loc, conv); 247 } 248 }; 249 } 250 static ComponentDecorator getComponentDecorator( 251 Runtime runtime, 252 final Location loc, 253 final Class castto, final ComponentDecorator singleton, 254 final ParameterBinder wiring, final boolean sync, 255 final Converter conv){ 256 270 return new SingletonMode(){ 271 276 public Component decorate(Component c){ 277 if(sync){ 278 c = c.synchronize(); 279 } 280 if(castto!=null) 281 c = cast(castto, c, loc, conv); if(wiring!=null){ 283 c = c.bindArguments(autocast(wiring, loc, conv)); 284 } 285 if(singleton!=null){ 286 c = singleton.decorate(c); 287 } 288 316 return c; 317 } 318 }; 319 } 320 static Component wrapComponent(Component c, Runtime runtime, 321 final Location loc, Class casttype, ComponentDecorator singleton, 322 ParameterBinder autowire, boolean sync, 323 Converter conv){ 324 return getComponentDecorator(runtime, loc, casttype, singleton, autowire, sync, 325 conv) 326 .decorate(c).label(loc); 327 } 328 static InputStream readResource(ResourceLoader loader, String resourcename) 329 throws IOException { 330 final InputStream in = loader.getResourceAsStream(resourcename); 331 if(in == null){ 332 throw new IllegalArgumentException ("cannot find resuorce: "+resourcename); 333 } 334 return in; 335 } 336 static String getMandatory(Tag tag, String name){ 337 final String result = tag.getAttribute(name); 338 if(result==null) 339 throw new ConfigurationException("<"+tag.getName()+"> - missing mandatory attribute: " 340 + name 341 ,tag.getLocation()); 342 return result; 343 } 344 static String getAttribute(Tag tag, String name1, String name2){ 345 final String val1 = tag.getAttribute(name1); 346 final String val2 = tag.getAttribute(name2); 347 if(val1!=null && val2!=null){ 348 throw new ConfigurationException("<"+tag.getName()+"> - " 349 +name1 + " and " + name2 + " are both specified, while only one is expected." 350 ,tag.getLocation()); 351 } 352 return val1==null?val2:val1; 353 } 354 static String getEagerMode(Tag tag){ 355 return getAttribute(tag, Constants.EAGER_INSTANTIATED, 356 Constants.EAGER_INSTANTIATED2); 357 } 358 405 static Stmt typedValue(final Class type, final Object val, final Location loc, 406 final Converter converter){ 407 return new Stmt(){ 408 public Object run(Dict frame, Runtime runtime){ 409 return converter.convert(type, val, loc); 410 } 411 public Class getType(){ 412 return type; 413 } 414 public Location getLocation(){ 415 return loc; 416 } 417 public String toString(){return ""+val;} 418 }; 419 } 420 static Stmt value(final Object v, final Location loc){ 421 final Class type = v==null?Object .class:v.getClass(); 422 return new Stmt(){ 423 public Object run(Dict frame, Runtime runtime){ 424 return v; 425 } 426 public Class getType(){ 427 return type; 428 } 429 public Location getLocation(){ 430 return loc; 431 } 432 public String toString(){return ""+v;} 433 }; 434 } 435 static ContinuationEscapeException getEscapeException(Throwable e){ 436 for(Throwable cur = e;;){ 437 if(cur instanceof ContinuationEscapeException){ 438 return (ContinuationEscapeException)cur; 439 } 440 final Throwable cause = cur.getCause(); 441 if(cause==null || cause==cur) return null; 442 cur = cause; 443 } 444 } 445 private static Component convert(final Class target_type, Component c, 446 final Location loc, final Converter converter){ 447 return c.map(new jfun.yan.Map(){ 448 public Object map(Object v){ 449 return converter.convert(target_type, v, loc); 450 } 451 }).cast(target_type); 452 } 453 static Component cast(final Class target_type, Component c, 454 final Location loc, final Converter converter){ 455 final Class type = c.getType(); 456 if(type==null || Literal.class.isAssignableFrom(type)){ 457 return convert(target_type, c, loc, converter); 458 } 459 else if(String .class.equals(type)){ 460 if(target_type.isAssignableFrom(type)){ 461 return c; 462 } 463 else{ 464 return convert(target_type, c, loc, converter); 465 } 466 } 467 else return c.cast(target_type); 468 } 469 479 public static Set toSet(Class impltype, Object obj) 480 throws IllegalAccessException , InstantiationException { 481 final Set set = Utils.createSet(impltype, 1); 482 set.add(obj); 483 return set; 484 } 485 495 public static List toList(Class impltype, Object obj) 496 throws IllegalAccessException , InstantiationException { 497 final List list = Utils.createList(impltype, 1); 498 list.add(obj); 499 return list; 500 } 501 507 public static Object toArray(Class elem_type, Object obj){ 508 final Object arr = Array.newInstance(elem_type, 1); 509 Array.set(arr, 0, obj); 510 return arr; 511 } 512 } 513 | Popular Tags |