KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > query > jqlc > JDOQLParameterDeclarationParser


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jdo.spi.persistence.support.sqlstore.query.jqlc;
25
26 import java.util.*;
27
28 import persistence.antlr.ANTLRException;
29 import persistence.antlr.collections.AST;
30
31 import com.sun.enterprise.deployment.interfaces.QueryParser;
32 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
33 import com.sun.jdo.spi.persistence.utility.I18NHelper;
34
35 /** Helper class to support parsing of JDOQL parameter declarations.
36  *
37  * Created on October 16, 2002
38  * @author Michael Bouschen
39  */

40 public class JDOQLParameterDeclarationParser
41     implements QueryParser
42 {
43     /**
44      * I18N support
45      */

46     protected final static ResourceBundle messages = I18NHelper.loadBundle(
47         JDOQLParameterDeclarationParser.class);
48
49     /**
50      * Returns an iterator over the parameter types of the specified JDOQL
51      * parameter declartion. The types are represented by their name, thus
52      * the Iterator's next method returns Strings.
53      * @param text the JDOQL parameter declaration
54      * @return an iterator over parameter types
55      * @exception JDOQueryException indicates a parse error
56      */

57     public Iterator parameterTypeIterator(String JavaDoc text)
58     {
59         return new ParameterTypeIterator(parse(text));
60     }
61     
62     /**
63      * Internal method parsing the JDOQL parameter declaration.
64      * @param text the JDOQL parameter declaration
65      * @return an AST representing the parameter declarations
66      */

67     private AST parse(String JavaDoc text)
68     {
69         if (text == null) {
70             return null;
71         }
72
73         // return value
74
AST paramsAST = null;
75
76         // error message helper
77
ErrorMsg errorMsg = new ErrorMsg();
78
79         // create parser
80
JQLParser parser = JQLC.createStringParser(text, errorMsg);
81
82         try {
83             // start parsing
84
parser.parseParameters();
85             // get the AST representation
86
paramsAST = parser.getAST();
87         }
88         catch (ANTLRException ex) {
89             // handle any exceptions thrown by lexer or parser
90
JQLParser.handleANTLRException(ex, errorMsg);
91         }
92
93         return paramsAST;
94     }
95     
96    /**
97     * Iterator over the parameter types. The next method returns the type
98     * of the next parameter represented as String.
99     */

100     private static class ParameterTypeIterator
101         implements Iterator
102     {
103         // current parameter declaration node
104
private AST current;
105
106         /**
107          * The constructor takes the parameter declarations AST. A
108          * parameter declaration node must have a PARAMETER_DEF node as
109          * root. The first child is the type, the next child is the name of
110          * the parameter. All subsequent parameter declarations as siblings
111          * of the specified ast node.
112          * @param ast the list of parameter declarations nodes
113          */

114         ParameterTypeIterator(AST ast)
115         {
116             current = ast;
117         }
118
119         /**
120          * Returns <code>true</code> if the iteration has more elements.
121          * @return <code>true</code> if the iterator has more elements.
122          */

123         public boolean hasNext()
124         {
125             return (current != null);
126         }
127         
128         /**
129          * Returns the next element in the iteration. For this Iterator it
130          * returns the String representation of the type of the next
131          * parameter declaration.
132          * @return the type of the next parameter declaration.
133          * @exception NoSuchElementException iteration has no more elements.
134          */

135         public Object JavaDoc next()
136         {
137             // check whether iteration has no more elements
138
if (current == null)
139                 throw new NoSuchElementException();
140
141             // Check whether the current node has the token type
142
// PARAMETER_DEF => throw exception if not.
143
if (current.getType() != JQLParser.PARAMETER_DEF)
144                 throw new JDOFatalInternalException(I18NHelper.getMessage(
145                     messages,
146                     "jqlc.jdoqlparameterdeclarationparser.next.wrongtoken", //NOI18N
147
current.getType()));
148
149             // get string repr of parameter type node
150
String JavaDoc typeRepr = getTypeRepr(current.getFirstChild());
151
152             // advance current ast node to next parameter declaration node
153
current = current.getNextSibling();
154
155             return typeRepr;
156         }
157         
158         /**
159          * Not supported.
160          * @exception UnsupportedOperationException remove is not supported
161          * by this Iterator
162          */

163         public void remove() { throw new UnsupportedOperationException JavaDoc(); }
164
165         /**
166          * Internal method to calculate the string representation of a type
167          * node.
168          * @param ast the type node
169          * @return the string representation
170          */

171         private String JavaDoc getTypeRepr(AST ast)
172         {
173             if (ast == null)
174                 return "";
175
176             // Check for DOT nodes #(DOT left right)
177
// They are represented as left.right
178
if (ast.getType() == JQLParser.DOT) {
179                 StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
180                 AST left = ast.getFirstChild();
181                 AST right = left.getNextSibling();
182                 return getTypeRepr(left) + "." + getTypeRepr(right);
183             }
184
185             // For all other nodes return the token text
186
return ast.getText();
187         }
188     }
189     
190     /**
191      * Method main for testing purposes. Parameter args is expected to a
192      * an array of parameter declaration String. One parameter declaration
193      * may declare multiple parameters according to the JDOQL parameter
194      * declaration syntax. Calling
195      * java com...jqlc.ParameterDeclarationHelper "int a, String b"
196      * will print
197      * <br><code>
198      * Parameter types for >int a, String b<
199      * <br>
200      * int
201      * <br>
202      * String</code>
203      * <br>
204      */

205     public static void main(String JavaDoc[] args)
206     {
207         QueryParser helper = new JDOQLParameterDeclarationParser();
208         for (int i = 0; i < args.length; i++) {
209             String JavaDoc text = args[i];
210             System.out.println("Parameter types for >" + text + "<");
211             for (Iterator types = helper.parameterTypeIterator(text);
212                  types.hasNext();) {
213                 System.out.println(" " + types.next());
214             }
215         }
216     }
217
218 }
219
Popular Tags