KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > extensions > ExtensionNamespaceSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ExtensionNamespaceSupport.java,v 1.6 2004/02/11 05:26:23 minchau Exp $
18  */

19 package org.apache.xalan.extensions;
20
21 import java.lang.reflect.Constructor JavaDoc;
22
23 import javax.xml.transform.TransformerException JavaDoc;
24
25 /**
26  * During styleseet composition, an ExtensionNamespaceSupport object is created for each extension
27  * namespace the stylesheet uses. At the beginning of a transformation, TransformerImpl generates
28  * an ExtensionHandler for each of these objects and adds an entry to the ExtensionsTable hashtable.
29  */

30 public class ExtensionNamespaceSupport
31 {
32   // Namespace, ExtensionHandler class name, constructor signature
33
// and arguments.
34
String JavaDoc m_namespace = null;
35   String JavaDoc m_handlerClass = null;
36   Class JavaDoc [] m_sig = null;
37   Object JavaDoc [] m_args = null;
38  
39   public ExtensionNamespaceSupport(String JavaDoc namespace,
40                                    String JavaDoc handlerClass,
41                                    Object JavaDoc[] constructorArgs)
42   {
43     m_namespace = namespace;
44     m_handlerClass = handlerClass;
45     m_args = constructorArgs;
46     // Create the constructor signature.
47
m_sig = new Class JavaDoc[m_args.length];
48     for (int i = 0; i < m_args.length; i++)
49     {
50       if (m_args[i] != null)
51         m_sig[i] = m_args[i].getClass();//System.out.println("arg class " + i + " " +m_sig[i]);
52
else // If an arguments is null, pick the constructor later.
53
{
54         m_sig = null;
55         break;
56       }
57     }
58   }
59   
60   public String JavaDoc getNamespace()
61   {
62     return m_namespace;
63   }
64   
65   /**
66    * Launch the ExtensionHandler that this ExtensionNamespaceSupport object defines.
67    */

68   public ExtensionHandler launch()
69     throws TransformerException JavaDoc
70   {
71     ExtensionHandler handler = null;
72     try
73     {
74       Class JavaDoc cl = ExtensionHandler.getClassForName(m_handlerClass);
75       Constructor JavaDoc con = null;
76       //System.out.println("class " + cl + " " + m_args + " " + m_args.length + " " + m_sig);
77
if (m_sig != null)
78         con = cl.getConstructor(m_sig);
79       else // Pick the constructor based on number of args.
80
{
81         Constructor JavaDoc[] cons = cl.getConstructors();
82         for (int i = 0; i < cons.length; i ++)
83         {
84           if (cons[i].getParameterTypes().length == m_args.length)
85           {
86             con = cons[i];
87             break;
88           }
89         }
90       }
91       // System.out.println("constructor " + con);
92
if (con != null)
93         handler = (ExtensionHandler)con.newInstance(m_args);
94       else
95         throw new TransformerException JavaDoc("ExtensionHandler constructor not found");
96     }
97     catch (Exception JavaDoc e)
98     {
99       throw new TransformerException JavaDoc(e);
100     }
101     return handler;
102   }
103
104 }
105
Popular Tags