001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ExtensionNamespaceSupport.java 468637 2006-10-28 06:51:02Z minchau $
020     */
021    package org.apache.xalan.extensions;
022    
023    import java.lang.reflect.Constructor;
024    
025    import javax.xml.transform.TransformerException;
026    
027    /**
028     * During styleseet composition, an ExtensionNamespaceSupport object is created for each extension 
029     * namespace the stylesheet uses. At the beginning of a transformation, TransformerImpl generates
030     * an ExtensionHandler for each of these objects and adds an entry to the ExtensionsTable hashtable.
031     */
032    public class ExtensionNamespaceSupport
033    {
034      // Namespace, ExtensionHandler class name, constructor signature 
035      // and arguments.
036      String m_namespace = null;
037      String m_handlerClass = null;
038      Class [] m_sig = null;  
039      Object [] m_args = null;
040     
041      public ExtensionNamespaceSupport(String namespace, 
042                                       String handlerClass, 
043                                       Object[] constructorArgs)
044      {
045        m_namespace = namespace;
046        m_handlerClass = handlerClass;
047        m_args = constructorArgs;
048        // Create the constructor signature.
049        m_sig = new Class[m_args.length];
050        for (int i = 0; i < m_args.length; i++)
051        {
052          if (m_args[i] != null)
053            m_sig[i] = m_args[i].getClass();//System.out.println("arg class " + i + " " +m_sig[i]);
054          else // If an arguments is null, pick the constructor later.
055          {
056            m_sig = null;
057            break;
058          }
059        }
060      }
061      
062      public String getNamespace()
063      {
064        return m_namespace;
065      }
066      
067      /**
068       * Launch the ExtensionHandler that this ExtensionNamespaceSupport object defines.
069       */
070      public ExtensionHandler launch()
071        throws TransformerException
072      {
073        ExtensionHandler handler = null;
074        try
075        {
076          Class cl = ExtensionHandler.getClassForName(m_handlerClass);
077          Constructor con = null;
078          //System.out.println("class " + cl + " " + m_args + " " + m_args.length + " " + m_sig);
079          if (m_sig != null)
080            con = cl.getConstructor(m_sig);
081          else // Pick the constructor based on number of args.
082          {
083            Constructor[] cons = cl.getConstructors();
084            for (int i = 0; i < cons.length; i ++)
085            {
086              if (cons[i].getParameterTypes().length == m_args.length)
087              {
088                con = cons[i];
089                break;
090              }
091            }
092          }
093          // System.out.println("constructor " + con);
094          if (con != null)
095            handler = (ExtensionHandler)con.newInstance(m_args);
096          else
097            throw new TransformerException("ExtensionHandler constructor not found");
098        }
099        catch (Exception e)
100        {
101          throw new TransformerException(e);
102        }
103        return handler;
104      }
105    
106    }