1 33 34 35 package bsh; 36 37 import java.util.*; 38 39 import java.io.InputStream ; 40 import java.io.BufferedReader ; 41 import java.io.InputStreamReader ; 42 import java.io.IOException ; 43 44 import java.lang.reflect.Method ; 45 import java.lang.reflect.Field ; 46 47 62 66 public class NameSpace 67 implements java.io.Serializable , BshClassManager.Listener, 68 NameSource 69 { 70 public static final NameSpace JAVACODE = 71 new NameSpace((BshClassManager)null, "Called from compiled Java code."); 72 static { 73 JAVACODE.isMethod = true; 74 } 75 76 79 84 private String nsName; 85 private NameSpace parent; 86 private Hashtable variables; 87 private Hashtable methods; 88 89 protected Hashtable importedClasses; 90 private Vector importedPackages; 91 private Vector importedCommands; 92 private Vector importedObjects; 93 private Vector importedStatic; 94 private String packageName; 95 96 transient private BshClassManager classManager; 97 98 private This thisReference; 100 101 102 private Hashtable names; 103 104 106 SimpleNode callerInfoNode; 107 108 112 boolean isMethod; 113 117 121 boolean isClass; 122 Class classStatic; 123 Object classInstance; 124 125 void setClassStatic( Class clas ) { 126 this.classStatic = clas; 127 importStatic( clas ); 128 } 129 void setClassInstance( Object instance ) { 130 this.classInstance = instance; 131 importObject( instance ); 132 } 133 Object getClassInstance() 134 throws UtilEvalError 135 { 136 if ( classInstance != null ) 137 return classInstance; 138 139 if ( classStatic != null 140 ) 142 throw new UtilEvalError( 143 "Can't refer to class instance from static context."); 144 else 145 throw new InterpreterError( 146 "Can't resolve class instance 'this' in: "+this); 147 } 148 149 150 156 transient private Hashtable classCache; 157 158 160 162 167 public NameSpace( NameSpace parent, String name ) 168 { 169 this( parent, null, name ); 171 } 172 173 public NameSpace( BshClassManager classManager, String name ) 174 { 175 this( null, classManager, name ); 176 } 177 178 public NameSpace( 179 NameSpace parent, BshClassManager classManager, String name ) 180 { 181 186 setName(name); 187 setParent(parent); 188 setClassManager( classManager ); 189 190 if ( classManager != null ) 192 classManager.addListener(this); 193 } 194 195 197 public void setName( String name ) { 198 this.nsName = name; 199 } 200 201 206 public String getName() { 207 return this.nsName; 208 } 209 210 215 void setNode( SimpleNode node ) { 216 callerInfoNode = node; 217 } 218 219 221 SimpleNode getNode() 222 { 223 if ( callerInfoNode != null ) 224 return callerInfoNode; 225 if ( parent != null ) 226 return parent.getNode(); 227 else 228 return null; 229 } 230 231 234 public Object get( String name, Interpreter interpreter ) 235 throws UtilEvalError 236 { 237 CallStack callstack = new CallStack( this ); 238 return getNameResolver( name ).toObject( callstack, interpreter ); 239 } 240 241 256 public void setVariable( String name, Object value, boolean strictJava ) 257 throws UtilEvalError 258 { 259 boolean recurse = Interpreter.LOCALSCOPING ? strictJava : true; 261 setVariable( name, value, strictJava, recurse ); 262 } 263 264 267 void setLocalVariable( 268 String name, Object value, boolean strictJava ) 269 throws UtilEvalError 270 { 271 setVariable( name, value, strictJava, false ); 272 } 273 274 297 void setVariable( 298 String name, Object value, boolean strictJava, boolean recurse ) 299 throws UtilEvalError 300 { 301 if ( variables == null ) 302 variables = new Hashtable(); 303 304 309 if ( value == null ) { 310 unsetVariable(name); 312 return; 313 } 314 315 Variable existing = getVariableImpl( name, recurse ); 318 319 if ( existing != null ) 321 { 322 try { 323 existing.setValue( value, Variable.ASSIGNMENT ); 324 } catch ( UtilEvalError e ) { 325 throw new UtilEvalError( 326 "Variable assignment: " + name + ": " + e.getMessage()); 327 } 328 } else 329 { 331 if ( strictJava ) 332 throw new UtilEvalError( 333 "(Strict Java mode) Assignment to undeclared variable: " 334 +name ); 335 336 NameSpace varScope = this; 340 341 varScope.variables.put( 342 name, new Variable( name, value, null ) ); 343 344 nameSpaceChanged(); 346 } 347 } 348 349 352 public void unsetVariable( String name ) 353 { 354 if ( variables != null ) 355 { 356 variables.remove( name ); 357 nameSpaceChanged(); 358 } 359 } 360 361 365 public String [] getVariableNames() { 366 if ( variables == null ) 367 return new String [0]; 368 else 369 return enumerationToStringArray( variables.keys() ); 370 } 371 372 376 public String [] getMethodNames() 377 { 378 if ( methods == null ) 379 return new String [0]; 380 else 381 return enumerationToStringArray( methods.keys() ); 382 } 383 384 389 public BshMethod [] getMethods() 390 { 391 if ( methods == null ) 392 return new BshMethod [0]; 393 else 394 return flattenMethodCollection( methods.elements() ); 395 } 396 397 private String [] enumerationToStringArray( Enumeration e ) { 398 Vector v = new Vector(); 399 while ( e.hasMoreElements() ) 400 v.addElement( e.nextElement() ); 401 String [] sa = new String [ v.size() ]; 402 v.copyInto( sa ); 403 return sa; 404 } 405 406 410 private BshMethod [] flattenMethodCollection( Enumeration e ) { 411 Vector v = new Vector(); 412 while ( e.hasMoreElements() ) { 413 Object o = e.nextElement(); 414 if ( o instanceof BshMethod ) 415 v.addElement( o ); 416 else { 417 Vector ov = (Vector)o; 418 for(int i=0; i<ov.size(); i++) 419 v.addElement( ov.elementAt( i ) ); 420 } 421 } 422 BshMethod [] bma = new BshMethod [ v.size() ]; 423 v.copyInto( bma ); 424 return bma; 425 } 426 427 432 public NameSpace getParent() { 433 return parent; 434 } 435 436 440 public This getSuper( Interpreter declaringInterpreter ) 441 { 442 if ( parent != null ) 443 return parent.getThis( declaringInterpreter ); 444 else 445 return getThis( declaringInterpreter ); 446 } 447 448 453 public This getGlobal( Interpreter declaringInterpreter ) 454 { 455 if ( parent != null ) 456 return parent.getGlobal( declaringInterpreter ); 457 else 458 return getThis( declaringInterpreter ); 459 } 460 461 462 473 491 This getThis( Interpreter declaringInterpreter ) 492 { 493 if ( thisReference == null ) 494 thisReference = This.getThis( this, declaringInterpreter ); 495 496 return thisReference; 497 } 498 499 public BshClassManager getClassManager() 500 { 501 if ( classManager != null ) 502 return classManager; 503 if ( parent != null && parent != JAVACODE ) 504 return parent.getClassManager(); 505 506 System.out.println("experiment: creating class manager"); 507 classManager = BshClassManager.createClassManager( null ); 508 509 return classManager; 511 } 512 513 void setClassManager( BshClassManager classManager ) { 514 this.classManager = classManager; 515 } 516 517 520 public void prune() 521 { 522 527 if ( this.classManager == null ) 528 setClassManager( 531 BshClassManager.createClassManager( null ) ); 532 533 setParent( null ); 534 } 535 536 public void setParent( NameSpace parent ) 537 { 538 this.parent = parent; 539 540 if ( parent == null ) 542 loadDefaultImports(); 543 } 544 545 555 public Object getVariable( String name ) 556 throws UtilEvalError 557 { 558 return getVariable( name, true ); 559 } 560 561 573 public Object getVariable( String name, boolean recurse ) 574 throws UtilEvalError 575 { 576 Variable var = getVariableImpl( name, recurse ); 577 return unwrapVariable( var ); 578 } 579 580 588 protected Variable getVariableImpl( String name, boolean recurse ) 589 throws UtilEvalError 590 { 591 Variable var = null; 592 593 if ( var == null && isClass ) 596 var = getImportedVar( name ); 597 598 if ( var == null && variables != null ) 599 var = (Variable)variables.get(name); 600 601 if ( var == null && !isClass ) 603 var = getImportedVar( name ); 604 605 if ( recurse && (var == null) && (parent != null) ) 607 var = parent.getVariableImpl( name, recurse ); 608 609 return var; 610 } 611 612 615 public Variable [] getDeclaredVariables() 616 { 617 if ( variables == null ) 618 return new Variable[0]; 619 Variable [] vars = new Variable [ variables.size() ]; 620 int i=0; 621 for( Enumeration e = variables.elements(); e.hasMoreElements(); ) 622 vars[i++] = (Variable)e.nextElement(); 623 return vars; 624 } 625 626 631 protected Object unwrapVariable( Variable var ) 632 throws UtilEvalError 633 { 634 return (var == null) ? Primitive.VOID : var.getValue(); 635 } 636 637 640 public void setTypedVariable( 641 String name, Class type, Object value, boolean isFinal ) 642 throws UtilEvalError 643 { 644 Modifiers modifiers = new Modifiers(); 645 if ( isFinal ) 646 modifiers.addModifier( Modifiers.FIELD, "final" ); 647 setTypedVariable( name, type, value, modifiers ); 648 } 649 650 669 public void setTypedVariable( 670 String name, Class type, Object value, Modifiers modifiers ) 671 throws UtilEvalError 672 { 673 675 if ( variables == null ) 676 variables = new Hashtable(); 677 678 Variable existing = getVariableImpl( name, false ); 680 681 682 689 690 if ( existing != null ) 692 { 693 if ( existing.getType() != null ) 695 { 696 if ( existing.getType() != type ) 700 { 701 throw new UtilEvalError( "Typed variable: "+name 702 +" was previously declared with type: " 703 + existing.getType() ); 704 } else 705 { 706 existing.setValue( value, Variable.DECLARATION ); 708 return; 709 } 710 } 711 } 714 715 variables.put( name, new Variable( name, type, value, modifiers ) ); 717 } 718 719 730 731 736 public void setMethod( String name, BshMethod method ) 737 throws UtilEvalError 738 { 739 741 if ( methods == null ) 742 methods = new Hashtable(); 743 744 Object m = methods.get(name); 745 746 if (m == null) 748 methods.put(name, method); 749 else if (m instanceof BshMethod) 750 { 751 if (Arrays.equals(((BshMethod)m).getParameterTypes(), 753 method.getParameterTypes())) 754 { 755 methods.put(name, method); 756 } 757 else 758 { 759 Vector v = new Vector(); 760 v.addElement( m ); 761 v.addElement( method ); 762 methods.put( name, v ); 763 } 764 } 765 else 766 { 767 Vector _methods = (Vector) m; 768 for (int i = 0; i < _methods.size(); i++) 769 { 770 BshMethod _old_m = (BshMethod) _methods.get(i); 773 if (Arrays.equals(_old_m.getParameterTypes(), 774 method.getParameterTypes())) 775 { 776 _methods.remove(i); 777 break; 778 } 779 } 780 _methods.addElement( method ); 781 } 782 784 } 797 798 802 public BshMethod getMethod( String name, Class [] sig ) 803 throws UtilEvalError 804 { 805 return getMethod( name, sig, false ); 806 } 807 808 821 public BshMethod getMethod( 822 String name, Class [] sig, boolean declaredOnly ) 823 throws UtilEvalError 824 { 825 BshMethod method = null; 826 827 if ( method == null && isClass && !declaredOnly ) 830 method = getImportedMethod( name, sig ); 831 832 Object m = null; 833 if ( method == null && methods != null ) 834 { 835 m = methods.get(name); 836 837 if ( m != null ) 839 { 840 BshMethod [] ma; 842 if ( m instanceof Vector ) 843 { 844 Vector vm = (Vector)m; 845 ma = new BshMethod[ vm.size() ]; 846 vm.copyInto( ma ); 847 } else 848 ma = new BshMethod[] { (BshMethod)m }; 849 850 Class [][] candidates = new Class [ ma.length ][]; 852 for( int i=0; i< ma.length; i++ ) 853 candidates[i] = ma[i].getParameterTypes(); 854 855 int match = 856 Reflect.findMostSpecificSignature( sig, candidates ); 857 if ( match != -1 ) 858 method = ma[match]; 859 } 860 } 861 862 if ( method == null && !isClass && !declaredOnly ) 863 method = getImportedMethod( name, sig ); 864 865 if ( !declaredOnly && (method == null) && (parent != null) ) 867 return parent.getMethod( name, sig ); 868 869 return method; 870 } 871 872 876 public void importClass(String name) 877 { 878 if ( importedClasses == null ) 879 importedClasses = new Hashtable(); 880 881 importedClasses.put( Name.suffix(name, 1), name ); 882 nameSpaceChanged(); 883 } 884 885 888 public void importPackage(String name) 889 { 890 if(importedPackages == null) 891 importedPackages = new Vector(); 892 893 if ( importedPackages.contains( name ) ) 895 importedPackages.remove( name ); 896 897 importedPackages.addElement(name); 898 nameSpaceChanged(); 899 } 900 901 908 public void importCommands( String name ) 909 { 910 if ( importedCommands == null ) 911 importedCommands = new Vector(); 912 913 name = name.replace('.','/'); 915 if ( !name.startsWith("/") ) 917 name = "/"+name; 918 if ( name.length() > 1 && name.endsWith("/") ) 920 name = name.substring( 0, name.length()-1 ); 921 922 if ( importedCommands.contains( name ) ) 924 importedCommands.remove( name ); 925 926 importedCommands.addElement(name); 927 nameSpaceChanged(); 928 } 929 930 957 public Object getCommand( 959 String name, Class [] argTypes, Interpreter interpreter ) 960 throws UtilEvalError 961 { 962 if (Interpreter.DEBUG) Interpreter.debug("getCommand: "+name); 963 BshClassManager bcm = interpreter.getClassManager(); 964 965 InputStream in = getCommand( name ); 966 967 if ( in != null ) 968 return loadScriptedCommand( 969 in, name, argTypes, name, interpreter ); 970 971 981 982 if ( parent != null ) 983 return parent.getCommand( name, argTypes, interpreter ); 984 else 985 return null; 986 } 987 988 989 1037 protected BshMethod getImportedMethod( String name, Class [] sig ) 1039 throws UtilEvalError 1040 { 1041 if ( importedObjects != null ) 1043 for(int i=0; i<importedObjects.size(); i++) 1044 { 1045 Object object = importedObjects.elementAt(i); 1046 Class clas = object.getClass(); 1047 Method method = Reflect.resolveJavaMethod( 1048 getClassManager(), clas, name, sig, false ); 1049 if ( method != null ) 1050 return new BshMethod( method, object ); 1051 } 1052 1053 if ( importedStatic!= null ) 1055 for(int i=0; i<importedStatic.size(); i++) 1056 { 1057 Class clas = (Class )importedStatic.elementAt(i); 1058 Method method = Reflect.resolveJavaMethod( 1059 getClassManager(), clas, name, sig, true ); 1060 if ( method != null ) 1061 return new BshMethod( method, null ); 1062 } 1063 1064 return null; 1065 } 1066 1067 protected Variable getImportedVar( String name ) 1068 throws UtilEvalError 1069 { 1070 if ( importedObjects != null ) 1072 for(int i=0; i<importedObjects.size(); i++) 1073 { 1074 Object object = importedObjects.elementAt(i); 1075 Class clas = object.getClass(); 1076 Field field = Reflect.resolveJavaField( 1077 clas, name, false ); 1078 if ( field != null ) 1079 return new Variable( 1080 name, field.getType(), new LHS( object, field ) ); 1081 } 1082 1083 if ( importedStatic!= null ) 1085 for(int i=0; i<importedStatic.size(); i++) 1086 { 1087 Class clas = (Class )importedStatic.elementAt(i); 1088 Field field = Reflect.resolveJavaField( 1089 clas, name, true ); 1090 if ( field != null ) 1091 return new Variable( name, field.getType(), new LHS( field ) ); 1092 } 1093 1094 return null; 1095 } 1096 1097 1103 1107 private BshMethod loadScriptedCommand( 1108 InputStream in, String name, Class [] argTypes, String resourcePath, 1109 Interpreter interpreter ) 1110 throws UtilEvalError 1111 { 1112 try { 1113 interpreter.eval( 1114 new InputStreamReader (in), this, resourcePath ); 1115 } catch ( EvalError e ) { 1116 1121 Interpreter.debug( e.toString() ); 1122 throw new UtilEvalError( 1123 "Error loading script: "+ e.getMessage()); 1124 } 1125 1126 BshMethod meth = getMethod( name, argTypes ); 1128 1133 1134 return meth; 1135 } 1136 1137 1140 void cacheClass( String name, Class c ) { 1141 if ( classCache == null ) { 1142 classCache = new Hashtable(); 1143 } 1145 1146 classCache.put(name, c); 1147 } 1148 1149 1156 public Class getClass( String name ) 1157 throws UtilEvalError 1158 { 1159 Class c = getClassImpl(name); 1160 if ( c != null ) 1161 return c; 1162 else 1163 if ( parent != null ) 1165 return parent.getClass( name ); 1166 else 1167 return null; 1168 } 1169 1170 1187 private Class getClassImpl( String name ) 1188 throws UtilEvalError 1189 { 1190 Class c = null; 1191 1192 if (classCache != null) { 1194 c = (Class )classCache.get(name); 1195 1196 if ( c != null ) 1197 return c; 1198 } 1199 1200 boolean unqualifiedName = !Name.isCompound(name); 1202 1203 if ( unqualifiedName ) 1205 { 1206 if ( c == null ) 1208 c = getImportedClassImpl( name ); 1209 1210 if ( c != null ) { 1212 cacheClass( name, c ); 1213 return c; 1214 } 1215 } 1216 1217 c = classForName( name ); 1219 if ( c != null ) { 1220 if ( unqualifiedName ) 1222 cacheClass( name, c ); 1223 return c; 1224 } 1225 1226 if ( Interpreter.DEBUG ) 1228 Interpreter.debug("getClass(): " + name + " not found in "+this); 1229 return null; 1230 } 1231 1232 1237 private Class getImportedClassImpl( String name ) 1238 throws UtilEvalError 1239 { 1240 String fullname = null; 1242 if ( importedClasses != null ) 1243 fullname = (String )importedClasses.get(name); 1244 1245 1248 if ( fullname != null ) 1249 { 1250 1253 Class clas=classForName(fullname); 1255 1256 if ( clas == null ) 1258 { 1259 1263 if ( Name.isCompound( fullname ) ) 1264 try { 1265 clas = getNameResolver( fullname ).toClass(); 1266 } catch ( ClassNotFoundException e ) { } 1267 else 1268 if ( Interpreter.DEBUG ) Interpreter.debug( 1269 "imported unpackaged name not found:" +fullname); 1270 1271 if ( clas != null ) { 1273 getClassManager().cacheClassInfo( fullname, clas ); 1275 return clas; 1276 } 1277 } else 1278 return clas; 1279 1280 return null; 1283 } 1284 1285 1290 if ( importedPackages != null ) 1291 for(int i=importedPackages.size()-1; i>=0; i--) 1292 { 1293 String s = ((String )importedPackages.elementAt(i)) + "." + name; 1294 Class c=classForName(s); 1295 if ( c != null ) 1296 return c; 1297 } 1298 1299 BshClassManager bcm = getClassManager(); 1300 1306 if ( bcm.hasSuperImport() ) 1307 { 1308 String s = bcm.getClassNameByUnqName( name ); 1309 if ( s != null ) 1310 return classForName( s ); 1311 } 1312 1313 return null; 1314 } 1315 1316 private Class classForName( String name ) 1317 { 1318 return getClassManager().classForName( name ); 1319 } 1320 1321 1326 public String [] getAllNames() 1327 { 1328 Vector vec = new Vector(); 1329 getAllNamesAux( vec ); 1330 String [] names = new String [ vec.size() ]; 1331 vec.copyInto( names ); 1332 return names; 1333 } 1334 1335 1338 protected void getAllNamesAux( Vector vec ) 1339 { 1340 Enumeration varNames = variables.keys(); 1341 while( varNames.hasMoreElements() ) 1342 vec.addElement( varNames.nextElement() ); 1343 1344 Enumeration methodNames = methods.keys(); 1345 while( methodNames.hasMoreElements() ) 1346 vec.addElement( methodNames.nextElement() ); 1347 1348 if ( parent != null ) 1349 parent.getAllNamesAux( vec ); 1350 } 1351 1352 Vector nameSourceListeners; 1353 1357 public void addNameSourceListener( NameSource.Listener listener ) { 1358 if ( nameSourceListeners == null ) 1359 nameSourceListeners = new Vector(); 1360 nameSourceListeners.addElement( listener ); 1361 } 1362 1363 1367 public void doSuperImport() 1368 throws UtilEvalError 1369 { 1370 getClassManager().doSuperImport(); 1371 } 1372 1373 1374 public String toString() { 1375 return "NameSpace: " 1376 + ( nsName==null 1377 ? super.toString() 1378 : nsName + " (" + super.toString() +")" ) 1379 + ( isClass ? " (isClass) " : "" ) 1380 + ( isMethod ? " (method) " : "" ) 1381 + ( classStatic != null ? " (class static) " : "" ) 1382 + ( classInstance != null ? " (class instance) " : "" ); 1383 } 1384 1385 1389 private synchronized void writeObject(java.io.ObjectOutputStream s) 1390 throws IOException 1391 { 1392 names = null; 1394 1395 s.defaultWriteObject(); 1396 } 1397 1398 1408 public Object invokeMethod( 1409 String methodName, Object [] args, Interpreter interpreter ) 1410 throws EvalError 1411 { 1412 return invokeMethod( 1413 methodName, args, interpreter, null, null ); 1414 } 1415 1416 1423 public Object invokeMethod( 1424 String methodName, Object [] args, Interpreter interpreter, 1425 CallStack callstack, SimpleNode callerInfo ) 1426 throws EvalError 1427 { 1428 return getThis( interpreter ).invokeMethod( 1429 methodName, args, interpreter, callstack, callerInfo, 1430 false ); 1431 } 1432 1433 1436 public void classLoaderChanged() { 1437 nameSpaceChanged(); 1438 } 1439 1440 1443 public void nameSpaceChanged() { 1444 classCache = null; 1445 names = null; 1446 } 1447 1448 1464 public void loadDefaultImports() 1465 { 1466 1471 importClass("bsh.EvalError"); 1472 importClass("bsh.Interpreter"); 1473 importPackage("javax.swing.event"); 1474 importPackage("javax.swing"); 1475 importPackage("java.awt.event"); 1476 importPackage("java.awt"); 1477 importPackage("java.net"); 1478 importPackage("java.util"); 1479 importPackage("java.io"); 1480 importPackage("java.lang"); 1481 addCommandPath("/bsh/commands",getClass()); 1484 } 1486 1487 1508 Name getNameResolver( String ambigname ) 1509 { 1510 if ( names == null ) 1511 names = new Hashtable(); 1512 1513 Name name = (Name)names.get( ambigname ); 1514 1515 if ( name == null ) { 1516 name = new Name( this, ambigname ); 1517 names.put( ambigname, name ); 1518 } 1519 1520 return name; 1521 } 1522 1523 public int getInvocationLine() { 1524 SimpleNode node = getNode(); 1525 if ( node != null ) 1526 return node.getLineNumber(); 1527 else 1528 return -1; 1529 } 1530 public String getInvocationText() { 1531 SimpleNode node = getNode(); 1532 if ( node != null ) 1533 return node.getText(); 1534 else 1535 return "<invoked from Java code>"; 1536 } 1537 1538 1547 public static Class identifierToClass( ClassIdentifier ci ) 1548 { 1549 return ci.getTargetClass(); 1550 } 1551 1552 1553 1559 public void clear() 1560 { 1561 variables = null; 1562 methods = null; 1563 importedClasses = null; 1564 importedPackages = null; 1565 importedCommands = null; 1566 importedObjects = null; 1567 if ( parent == null ) 1568 loadDefaultImports(); 1569 classCache = null; 1570 names = null; 1571 } 1572 1573 1581 1585 public void importObject( Object obj ) 1586 { 1587 if ( importedObjects == null ) 1588 importedObjects = new Vector(); 1589 1590 if ( importedObjects.contains( obj ) ) 1592 importedObjects.remove( obj ); 1593 1594 importedObjects.addElement( obj ); 1595 nameSpaceChanged(); 1596 1597 } 1598 1599 1601 public void importStatic( Class clas ) 1602 { 1603 if ( importedStatic == null ) 1604 importedStatic = new Vector(); 1605 1606 if ( importedStatic.contains( clas ) ) 1608 importedStatic.remove( clas ); 1609 1610 importedStatic.addElement( clas ); 1611 nameSpaceChanged(); 1612 } 1613 1614 1618 void setPackage( String packageName ) 1619 { 1620 this.packageName = packageName; 1621 } 1622 1623 String getPackage() 1624 { 1625 if ( packageName != null ) 1626 return packageName; 1627 1628 if ( parent != null ) 1629 return parent.getPackage(); 1630 1631 return null; 1632 } 1633 1634 public void setVariable(String name, Object value) throws UtilEvalError 1636 { 1637 setVariable(name,value,false); 1638 } 1639 1640 1643 public void addCommandPath(String path, Class clas) 1644 { 1645 if(importedCommands == null) 1646 importedCommands = new Vector(); 1647 1648 if(!path.endsWith("/")) 1649 path += '/'; 1650 importedCommands.addElement(new CommandPathEntry(path,clas)); 1651 } 1652 1653 1656 public void removeCommandPath(String path, Class clas) 1657 { 1658 if(importedCommands == null) 1659 return; 1660 1661 for(int i = 0; i < importedCommands.size(); i++) 1662 { 1663 CommandPathEntry entry = (CommandPathEntry)importedCommands 1664 .elementAt(i); 1665 if(entry.path.equals(path) && entry.clas == clas) 1666 { 1667 importedCommands.removeElementAt(i); 1668 return; 1669 } 1670 } 1671 } 1672 1673 1676 public InputStream getCommand(String name) 1677 { 1678 if(importedCommands != null) 1679 { 1680 String extName = name + ".bsh"; 1681 for(int i = importedCommands.size() - 1; i >= 0; i--) 1682 { 1683 CommandPathEntry entry = (CommandPathEntry)importedCommands 1684 .elementAt(i); 1685 InputStream in = entry.clas.getResourceAsStream(entry.path + extName); 1686 if(in != null) 1687 return in; 1688 } 1689 } 1690 1691 if(parent == null) 1692 return null; 1693 else 1694 return parent.getCommand(name); 1695 } 1696 1697 static class CommandPathEntry 1698 { 1699 final String path; 1700 final Class clas; 1701 1702 CommandPathEntry(String path, Class clas) 1703 { 1704 this.path = path; 1705 this.clas = clas; 1706 } 1707 } 1708 1709 } 1711 1712 | Popular Tags |