View Javadoc

1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: SelectorASTFactory.java,v 1.1 2005/11/12 13:47:56 tanderson Exp $
44   */
45  package org.exolab.jms.selector.parser;
46  
47  import java.lang.reflect.Constructor;
48  import java.util.Hashtable;
49  
50  import antlr.ASTFactory;
51  import antlr.Token;
52  import antlr.collections.AST;
53  
54  
55  /***
56   * This class was created to override the 4 methods in antlr.ASTFactory that use
57   * {@link Class#forName}. In the case of interaction with OpenJMS,
58   * the ClassLoader for the Factory class must not be higher in the tree than
59   * the OpenJMS classes.  In the case of Weblogic 8.1 SP3, some antlr classes,
60   * including ASTFactory are encapsulated in the weblogic.jar, the ClassLoader on
61   * the antlr.ASTFactory class is the System ClassLoader.  If you have
62   * openjms.jar in your EAR or WAR files, the antlr.ASTFactory class will not be
63   * able to find the OpenJMS classes when it is doing
64   * a Class.forName to load them.
65   *
66   * @author jason.michael
67   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
68   * @version $Revision: 1.1 $ $Date: 2005/11/12 13:47:56 $
69   */
70  public class SelectorASTFactory extends ASTFactory {
71  
72      /***
73       * Specify an "override" for the Java AST object created for a
74       * specific token.
75       *
76       * @since 2.7.2
77       */
78      public void setTokenTypeASTNodeType(int tokenType, String className)
79              throws IllegalArgumentException {
80          if (tokenTypeToASTClassMap == null) {
81              tokenTypeToASTClassMap = new Hashtable();
82          }
83          if (className == null) {
84              tokenTypeToASTClassMap.remove(new Integer(tokenType));
85              return;
86          }
87          Class c = null;
88          try {
89              c = Class.forName(className);
90              tokenTypeToASTClassMap.put(new Integer(tokenType), c);
91          } catch (Exception e) {
92              throw new IllegalArgumentException("Invalid class, " + className);
93          }
94      }
95  
96      /***
97       * @since 2.7.2
98       */
99      protected AST create(String className) {
100         Class c = null;
101         try {
102             c = Class.forName(className);
103         } catch (Exception e) {
104             throw new IllegalArgumentException("Invalid class, " + className);
105         }
106         return create(c);
107     }
108 
109     /***
110      * @since 2.7.2
111      */
112     protected AST createUsingCtor(Token token, String className) {
113         Class c = null;
114         AST t = null;
115         try {
116             c = Class.forName(className);
117             Class[] tokenArgType = new Class[]{antlr.Token.class};
118             Constructor ctor = c.getConstructor(tokenArgType);
119             if (ctor != null) {
120                 t = (AST) ctor.newInstance(new Object[]{token}); // make a new one
121             } else {
122                 // just do the regular thing if you can't find the ctor
123                 // Your AST must have default ctor to use this.
124                 t = create(c);
125             }
126         } catch (Exception e) {
127             throw new IllegalArgumentException(
128                     "Invalid class or can't make instance, " + className);
129         }
130         return t;
131     }
132 
133     public void setASTNodeClass(String t) {
134         theASTNodeType = t;
135         try {
136             theASTNodeTypeClass = Class.forName(t); // get class def
137         } catch (Exception e) {
138             // either class not found,
139             // class is interface/abstract, or
140             // class or initializer is not accessible.
141             error("Can't find/access AST Node type" + t);
142         }
143     }
144 
145 }