extensionhandlerjavapackage.java

来自「java jdk 1.4的源码」· Java 代码 · 共 508 行 · 第 1/2 页

JAVA
508
字号
    Object[] methodArgs;    Object[][] convertedArgs;    Class[] paramTypes;    try    {      if (funcName.endsWith(".new")) {                   // Handle constructor call        methodArgs = new Object[args.size()];        convertedArgs = new Object[1][];        for (int i = 0; i < methodArgs.length; i++)        {          methodArgs[i] = args.elementAt(i);        }        Constructor c = (Constructor) getFromCache(methodKey, null, methodArgs);        if (c != null)        {          try          {            paramTypes = c.getParameterTypes();            MethodResolver.convertParams(methodArgs, convertedArgs, paramTypes, exprContext);            return c.newInstance(convertedArgs[0]);          }          catch (InvocationTargetException ite)          {            throw ite;          }          catch(Exception e)          {            // Must not have been the right one          }        }        className = m_className + funcName.substring(0, lastDot);        try        {          classObj = getClassForName(className);        }        catch (ClassNotFoundException e)         {          throw new TransformerException(e);        }        c = MethodResolver.getConstructor(classObj,                                           methodArgs,                                          convertedArgs,                                          exprContext);        putToCache(methodKey, null, methodArgs, c);        return c.newInstance(convertedArgs[0]);      }      else if (-1 != lastDot) {                         // Handle static method call        methodArgs = new Object[args.size()];        convertedArgs = new Object[1][];        for (int i = 0; i < methodArgs.length; i++)        {          methodArgs[i] = args.elementAt(i);        }        Method m = (Method) getFromCache(methodKey, null, methodArgs);        if (m != null)        {          try          {            paramTypes = m.getParameterTypes();            MethodResolver.convertParams(methodArgs, convertedArgs, paramTypes, exprContext);            return m.invoke(null, convertedArgs[0]);          }          catch (InvocationTargetException ite)          {            throw ite;          }          catch(Exception e)          {            // Must not have been the right one          }        }        className = m_className + funcName.substring(0, lastDot);        methodName = funcName.substring(lastDot + 1);        try        {          classObj = getClassForName(className);        }        catch (ClassNotFoundException e)         {          throw new TransformerException(e);        }        m = MethodResolver.getMethod(classObj,                                     methodName,                                     methodArgs,                                      convertedArgs,                                     exprContext,                                     MethodResolver.STATIC_ONLY);        putToCache(methodKey, null, methodArgs, m);        return m.invoke(null, convertedArgs[0]);      }      else {                                            // Handle instance method call        if (args.size() < 1)        {          throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_INSTANCE_MTHD_CALL_REQUIRES, new Object[]{funcName })); //"Instance method call to method " + funcName                                    //+ " requires an Object instance as first argument");        }        targetObject = args.elementAt(0);        if (targetObject instanceof XObject)          // Next level down for XObjects          targetObject = ((XObject) targetObject).object();        methodArgs = new Object[args.size() - 1];        convertedArgs = new Object[1][];        for (int i = 0; i < methodArgs.length; i++)        {          methodArgs[i] = args.elementAt(i+1);        }        Method m = (Method) getFromCache(methodKey, targetObject, methodArgs);        if (m != null)        {          try          {            paramTypes = m.getParameterTypes();            MethodResolver.convertParams(methodArgs, convertedArgs, paramTypes, exprContext);            return m.invoke(targetObject, convertedArgs[0]);          }          catch (InvocationTargetException ite)          {            throw ite;          }          catch(Exception e)          {            // Must not have been the right one          }        }        classObj = targetObject.getClass();        m = MethodResolver.getMethod(classObj,                                     funcName,                                     methodArgs,                                      convertedArgs,                                     exprContext,                                     MethodResolver.INSTANCE_ONLY);        putToCache(methodKey, targetObject, methodArgs, m);        return m.invoke(targetObject, convertedArgs[0]);      }    }    catch (InvocationTargetException ite)    {      Throwable resultException = ite;      Throwable targetException = ite.getTargetException();       if (targetException instanceof TransformerException)        throw ((TransformerException)targetException);      else if (targetException != null)        resultException = targetException;                  throw new TransformerException(resultException);    }    catch (Exception e)    {      // e.printStackTrace();      throw new TransformerException(e);    }  }  /**   * Process a call to this extension namespace via an element. As a side   * effect, the results are sent to the TransformerImpl's result tree.   * For this namespace, only static element methods are currently supported.   * If instance methods are needed, please let us know your requirements.   * @param localPart      Element name's local part.   * @param element        The extension element being processed.   * @param transformer      Handle to TransformerImpl.   * @param stylesheetTree The compiled stylesheet tree.   * @param mode           The current mode.   * @param sourceTree     The root of the source tree (but don't assume   *                       it's a Document).   * @param sourceNode     The current context node.   * @param mode           The current mode.   * @param methodKey      A key that uniquely identifies this element call.   * @throws IOException           if loading trouble   * @throws TransformerException          if parsing trouble   */  public void processElement (String localPart,                              ElemTemplateElement element,                              TransformerImpl transformer,                              Stylesheet stylesheetTree,                              Object methodKey)    throws TransformerException, IOException  {    Object result = null;    Class classObj;    Method m = (Method) getFromCache(methodKey, null, null);    if (null == m)    {      try      {        String fullName = m_className + localPart;        int lastDot = fullName.lastIndexOf(".");        if (lastDot < 0)          throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_INVALID_ELEMENT_NAME, new Object[]{fullName })); //"Invalid element name specified " + fullName);        try        {          classObj = getClassForName(fullName.substring(0, lastDot));        }        catch (ClassNotFoundException e)         {          throw new TransformerException(e);        }        localPart = fullName.substring(lastDot + 1);        m = MethodResolver.getElementMethod(classObj, localPart);        if (!Modifier.isStatic(m.getModifiers()))          throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_ELEMENT_NAME_METHOD_STATIC, new Object[]{fullName })); //"Element name method must be static " + fullName);      }      catch (Exception e)      {        // e.printStackTrace ();        throw new TransformerException (e);      }      putToCache(methodKey, null, null, m);    }    XSLProcessorContext xpc = new XSLProcessorContext(transformer,                                                       stylesheetTree);    try    {      result = m.invoke(null, new Object[] {xpc, element});    }    catch (InvocationTargetException ite)    {      Throwable resultException = ite;      Throwable targetException = ite.getTargetException();       if (targetException instanceof TransformerException)        throw ((TransformerException)targetException);      else if (targetException != null)        resultException = targetException;                  throw new TransformerException(resultException);    }    catch (Exception e)    {      // e.printStackTrace ();      throw new TransformerException (e);    }    if (result != null)    {      xpc.outputToResultTree (stylesheetTree, result);    }   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?