Home | History | Annotate | Download | only in extensions
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: ExtensionNamespaceSupport.java 468637 2006-10-28 06:51:02Z minchau $
     20  */
     21 package org.apache.xalan.extensions;
     22 
     23 import java.lang.reflect.Constructor;
     24 
     25 import javax.xml.transform.TransformerException;
     26 
     27 /**
     28  * During styleseet composition, an ExtensionNamespaceSupport object is created for each extension
     29  * namespace the stylesheet uses. At the beginning of a transformation, TransformerImpl generates
     30  * an ExtensionHandler for each of these objects and adds an entry to the ExtensionsTable hashtable.
     31  */
     32 public class ExtensionNamespaceSupport
     33 {
     34   // Namespace, ExtensionHandler class name, constructor signature
     35   // and arguments.
     36   String m_namespace = null;
     37   String m_handlerClass = null;
     38   Class [] m_sig = null;
     39   Object [] m_args = null;
     40 
     41   public ExtensionNamespaceSupport(String namespace,
     42                                    String handlerClass,
     43                                    Object[] constructorArgs)
     44   {
     45     m_namespace = namespace;
     46     m_handlerClass = handlerClass;
     47     m_args = constructorArgs;
     48     // Create the constructor signature.
     49     m_sig = new Class[m_args.length];
     50     for (int i = 0; i < m_args.length; i++)
     51     {
     52       if (m_args[i] != null)
     53         m_sig[i] = m_args[i].getClass();//System.out.println("arg class " + i + " " +m_sig[i]);
     54       else // If an arguments is null, pick the constructor later.
     55       {
     56         m_sig = null;
     57         break;
     58       }
     59     }
     60   }
     61 
     62   public String getNamespace()
     63   {
     64     return m_namespace;
     65   }
     66 
     67   /**
     68    * Launch the ExtensionHandler that this ExtensionNamespaceSupport object defines.
     69    */
     70   public ExtensionHandler launch()
     71     throws TransformerException
     72   {
     73     ExtensionHandler handler = null;
     74     try
     75     {
     76       Class cl = ExtensionHandler.getClassForName(m_handlerClass);
     77       Constructor con = null;
     78       //System.out.println("class " + cl + " " + m_args + " " + m_args.length + " " + m_sig);
     79       if (m_sig != null)
     80         con = cl.getConstructor(m_sig);
     81       else // Pick the constructor based on number of args.
     82       {
     83         Constructor[] cons = cl.getConstructors();
     84         for (int i = 0; i < cons.length; i ++)
     85         {
     86           if (cons[i].getParameterTypes().length == m_args.length)
     87           {
     88             con = cons[i];
     89             break;
     90           }
     91         }
     92       }
     93       // System.out.println("constructor " + con);
     94       if (con != null)
     95         handler = (ExtensionHandler)con.newInstance(m_args);
     96       else
     97         throw new TransformerException("ExtensionHandler constructor not found");
     98     }
     99     catch (Exception e)
    100     {
    101       throw new TransformerException(e);
    102     }
    103     return handler;
    104   }
    105 
    106 }
    107