1 16 19 20 package org.apache.xalan.extensions; 21 22 import java.io.IOException ; 23 import java.lang.reflect.Constructor ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Modifier ; 27 import java.util.Vector ; 28 29 import javax.xml.transform.TransformerException ; 30 31 import org.apache.xalan.templates.ElemTemplateElement; 32 import org.apache.xalan.templates.Stylesheet; 33 import org.apache.xalan.trace.ExtensionEvent; 34 import org.apache.xalan.transformer.TransformerImpl; 35 import org.apache.xpath.functions.FuncExtFunction; 36 import org.apache.xpath.objects.XObject; 37 38 54 55 public class ExtensionHandlerJavaClass extends ExtensionHandlerJava 56 { 57 58 private Class m_classObj = null; 59 60 64 65 private Object m_defaultInstance = null; 66 67 68 75 public ExtensionHandlerJavaClass(String namespaceUri, 76 String scriptLang, 77 String className) 78 { 79 super(namespaceUri, scriptLang, className); 80 try 81 { 82 m_classObj = getClassForName(className); 83 } 84 catch (ClassNotFoundException e) 85 { 86 } 88 } 89 90 91 100 101 public boolean isFunctionAvailable(String function) 102 { 103 Method [] methods = m_classObj.getMethods(); 104 int nMethods = methods.length; 105 for (int i = 0; i < nMethods; i++) 106 { 107 if (methods[i].getName().equals(function)) 108 return true; 109 } 110 return false; 111 } 112 113 114 121 122 public boolean isElementAvailable(String element) 123 { 124 Method [] methods = m_classObj.getMethods(); 125 int nMethods = methods.length; 126 for (int i = 0; i < nMethods; i++) 127 { 128 if (methods[i].getName().equals(element)) 129 { 130 Class [] paramTypes = methods[i].getParameterTypes(); 131 if ( (paramTypes.length == 2) 132 && paramTypes[0].isAssignableFrom( 133 org.apache.xalan.extensions.XSLProcessorContext.class) 134 && paramTypes[1].isAssignableFrom(org.apache.xalan.templates.ElemExtensionCall.class) ) 135 { 136 return true; 137 } 138 } 139 } 140 return false; 141 } 142 143 179 180 public Object callFunction (String funcName, 181 Vector args, 182 Object methodKey, 183 ExpressionContext exprContext) 184 throws TransformerException 185 { 186 187 Object [] methodArgs; 188 Object [][] convertedArgs; 189 Class [] paramTypes; 190 191 try 192 { 193 if (funcName.equals("new")) { 195 methodArgs = new Object [args.size()]; 196 convertedArgs = new Object [1][]; 197 for (int i = 0; i < methodArgs.length; i++) 198 { 199 methodArgs[i] = args.elementAt(i); 200 } 201 Constructor c = (Constructor ) getFromCache(methodKey, null, methodArgs); 202 if (c != null && !TransformerImpl.S_DEBUG) 203 { 204 try 205 { 206 paramTypes = c.getParameterTypes(); 207 MethodResolver.convertParams(methodArgs, convertedArgs, paramTypes, exprContext); 208 return c.newInstance(convertedArgs[0]); 209 } 210 catch (InvocationTargetException ite) 211 { 212 throw ite; 213 } 214 catch(Exception e) 215 { 216 } 218 } 219 c = MethodResolver.getConstructor(m_classObj, 220 methodArgs, 221 convertedArgs, 222 exprContext); 223 putToCache(methodKey, null, methodArgs, c); 224 if (TransformerImpl.S_DEBUG) { 225 TransformerImpl trans = (TransformerImpl)exprContext.getXPathContext().getOwnerObject(); 226 trans.getTraceManager().fireExtensionEvent(new ExtensionEvent(trans, c, convertedArgs[0])); 227 Object result; 228 try { 229 result = c.newInstance(convertedArgs[0]); 230 } catch (Exception e) { 231 throw e; 232 } finally { 233 trans.getTraceManager().fireExtensionEndEvent(new ExtensionEvent(trans, c, convertedArgs[0])); 234 } 235 return result; 236 } else 237 return c.newInstance(convertedArgs[0]); 238 } 239 240 else 241 { 242 243 int resolveType; 244 Object targetObject = null; 245 methodArgs = new Object [args.size()]; 246 convertedArgs = new Object [1][]; 247 for (int i = 0; i < methodArgs.length; i++) 248 { 249 methodArgs[i] = args.elementAt(i); 250 } 251 Method m = (Method ) getFromCache(methodKey, null, methodArgs); 252 if (m != null && !TransformerImpl.S_DEBUG) 253 { 254 try 255 { 256 paramTypes = m.getParameterTypes(); 257 MethodResolver.convertParams(methodArgs, convertedArgs, paramTypes, exprContext); 258 if (Modifier.isStatic(m.getModifiers())) 259 return m.invoke(null, convertedArgs[0]); 260 else 261 { 262 int nTargetArgs = convertedArgs[0].length; 265 if (ExpressionContext.class.isAssignableFrom(paramTypes[0])) 266 nTargetArgs--; 267 if (methodArgs.length <= nTargetArgs) 268 return m.invoke(m_defaultInstance, convertedArgs[0]); 269 else 270 { 271 targetObject = methodArgs[0]; 272 273 if (targetObject instanceof XObject) 274 targetObject = ((XObject) targetObject).object(); 275 276 return m.invoke(targetObject, convertedArgs[0]); 277 } 278 } 279 } 280 catch (InvocationTargetException ite) 281 { 282 throw ite; 283 } 284 catch(Exception e) 285 { 286 } 288 } 289 290 if (args.size() > 0) 291 { 292 targetObject = methodArgs[0]; 293 294 if (targetObject instanceof XObject) 295 targetObject = ((XObject) targetObject).object(); 296 297 if (m_classObj.isAssignableFrom(targetObject.getClass())) 298 resolveType = MethodResolver.DYNAMIC; 299 else 300 resolveType = MethodResolver.STATIC_AND_INSTANCE; 301 } 302 else 303 { 304 targetObject = null; 305 resolveType = MethodResolver.STATIC_AND_INSTANCE; 306 } 307 308 m = MethodResolver.getMethod(m_classObj, 309 funcName, 310 methodArgs, 311 convertedArgs, 312 exprContext, 313 resolveType); 314 putToCache(methodKey, null, methodArgs, m); 315 316 if (MethodResolver.DYNAMIC == resolveType) { if (TransformerImpl.S_DEBUG) { 318 TransformerImpl trans = (TransformerImpl)exprContext.getXPathContext().getOwnerObject(); 319 trans.getTraceManager().fireExtensionEvent(m, targetObject, convertedArgs[0]); 320 Object result; 321 try { 322 result = m.invoke(targetObject, convertedArgs[0]); 323 } catch (Exception e) { 324 throw e; 325 } finally { 326 trans.getTraceManager().fireExtensionEndEvent(m, targetObject, convertedArgs[0]); 327 } 328 return result; 329 } else 330 return m.invoke(targetObject, convertedArgs[0]); 331 } 332 else { 334 if (Modifier.isStatic(m.getModifiers())) { 335 if (TransformerImpl.S_DEBUG) { 336 TransformerImpl trans = (TransformerImpl)exprContext.getXPathContext().getOwnerObject(); 337 trans.getTraceManager().fireExtensionEvent(m, null, convertedArgs[0]); 338 Object result; 339 try { 340 result = m.invoke(null, convertedArgs[0]); 341 } catch (Exception e) { 342 throw e; 343 } finally { 344 trans.getTraceManager().fireExtensionEndEvent(m, null, convertedArgs[0]); 345 } 346 return result; 347 } else 348 return m.invoke(null, convertedArgs[0]); 349 } 350 else 351 { 352 if (null == m_defaultInstance) 353 { 354 if (TransformerImpl.S_DEBUG) { 355 TransformerImpl trans = (TransformerImpl)exprContext.getXPathContext().getOwnerObject(); 356 trans.getTraceManager().fireExtensionEvent(new ExtensionEvent(trans, m_classObj)); 357 try { 358 m_defaultInstance = m_classObj.newInstance(); 359 } catch (Exception e) { 360 throw e; 361 } finally { 362 trans.getTraceManager().fireExtensionEndEvent(new ExtensionEvent(trans, m_classObj)); 363 } 364 } else 365 m_defaultInstance = m_classObj.newInstance(); 366 } 367 if (TransformerImpl.S_DEBUG) { 368 TransformerImpl trans = (TransformerImpl)exprContext.getXPathContext().getOwnerObject(); 369 trans.getTraceManager().fireExtensionEvent(m, m_defaultInstance, convertedArgs[0]); 370 Object result; 371 try { 372 result = m.invoke(m_defaultInstance, convertedArgs[0]); 373 } catch (Exception e) { 374 throw e; 375 } finally { 376 trans.getTraceManager().fireExtensionEndEvent(m, m_defaultInstance, convertedArgs[0]); 377 } 378 return result; 379 } else 380 return m.invoke(m_defaultInstance, convertedArgs[0]); 381 } 382 } 383 384 } 385 } 386 catch (InvocationTargetException ite) 387 { 388 Throwable resultException = ite; 389 Throwable targetException = ite.getTargetException(); 390 391 if (targetException instanceof TransformerException ) 392 throw ((TransformerException )targetException); 393 else if (targetException != null) 394 resultException = targetException; 395 396 throw new TransformerException (resultException); 397 } 398 catch (Exception e) 399 { 400 throw new TransformerException (e); 402 } 403 } 404 405 414 public Object callFunction(FuncExtFunction extFunction, 415 Vector args, 416 ExpressionContext exprContext) 417 throws TransformerException 418 { 419 return callFunction(extFunction.getFunctionName(), args, 420 extFunction.getMethodKey(), exprContext); 421 } 422 423 442 443 public void processElement(String localPart, 444 ElemTemplateElement element, 445 TransformerImpl transformer, 446 Stylesheet stylesheetTree, 447 Object methodKey) 448 throws TransformerException , IOException 449 { 450 Object result = null; 451 452 Method m = (Method ) getFromCache(methodKey, null, null); 453 if (null == m) 454 { 455 try 456 { 457 m = MethodResolver.getElementMethod(m_classObj, localPart); 458 if ( (null == m_defaultInstance) && !Modifier.isStatic(m.getModifiers()) ) { 459 if (TransformerImpl.S_DEBUG) { 460 transformer.getTraceManager().fireExtensionEvent(new ExtensionEvent(transformer, m_classObj)); 461 try { 462 m_defaultInstance = m_classObj.newInstance(); 463 } catch (Exception e) { 464 throw e; 465 } finally { 466 transformer.getTraceManager().fireExtensionEndEvent(new ExtensionEvent(transformer, m_classObj)); 467 } 468 } else 469 m_defaultInstance = m_classObj.newInstance(); 470 } 471 } 472 catch (Exception e) 473 { 474 throw new TransformerException (e.getMessage (), e); 476 } 477 putToCache(methodKey, null, null, m); 478 } 479 480 XSLProcessorContext xpc = new XSLProcessorContext(transformer, 481 stylesheetTree); 482 483 try 484 { 485 if (TransformerImpl.S_DEBUG) { 486 transformer.getTraceManager().fireExtensionEvent(m, m_defaultInstance, new Object [] {xpc, element}); 487 try { 488 result = m.invoke(m_defaultInstance, new Object [] {xpc, element}); 489 } catch (Exception e) { 490 throw e; 491 } finally { 492 transformer.getTraceManager().fireExtensionEndEvent(m, m_defaultInstance, new Object [] {xpc, element}); 493 } 494 } else 495 result = m.invoke(m_defaultInstance, new Object [] {xpc, element}); 496 } 497 catch (InvocationTargetException e) 498 { 499 Throwable targetException = e.getTargetException(); 500 501 if (targetException instanceof TransformerException ) 502 throw (TransformerException )targetException; 503 else if (targetException != null) 504 throw new TransformerException (targetException.getMessage (), targetException); 505 else 506 throw new TransformerException (e.getMessage (), e); 507 } 508 catch (Exception e) 509 { 510 throw new TransformerException (e.getMessage (), e); 512 } 513 514 if (result != null) 515 { 516 xpc.outputToResultTree (stylesheetTree, result); 517 } 518 519 } 520 521 } 522 | Popular Tags |