KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > parsing > ejbql > EJBQLParserBase


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.parsing.ejbql;
23
24 import java.util.Vector JavaDoc;
25
26 import oracle.toplink.essentials.internal.parsing.*;
27 import oracle.toplink.essentials.exceptions.*;
28
29 // Third party (ANLTR) stuff
30
import persistence.antlr.LLkParser;
31 import persistence.antlr.ANTLRException;
32 import persistence.antlr.MismatchedTokenException;
33 import persistence.antlr.MismatchedCharException;
34 import persistence.antlr.NoViableAltException;
35 import persistence.antlr.NoViableAltForCharException;
36 import persistence.antlr.TokenStreamRecognitionException;
37 import persistence.antlr.RecognitionException;
38 import persistence.antlr.ParserSharedInputState;
39 import persistence.antlr.TokenStream;
40 import persistence.antlr.Token;
41
42 /**
43  * INTERNAL
44  * <p><b>Purpose</b>: The super class of the EJBQLParser
45  * <p><b>Responsibilities</b>:<ul>
46  * <li> Respond to messages from the parser appropriately
47  * <li> Build a parse tree
48  * <li> Answer any errors ocurring during parsing
49  * <li> Answer any parameters found during parsing
50  * <li> Maintain the EJBQL string
51  * </ul>
52  * @author Jon Driscoll and Joel Lucuik
53  * @since TopLink 4.0
54  */

55 public class EJBQLParserBase extends LLkParser {
56     private boolean verbose = false;
57
58     // Removed by JED as part of the re-factoring for EJB 2.1
59
//protected String abstractSchemaName = null;
60
//protected String variableName = null;
61
private Vector JavaDoc errors;
62     private String JavaDoc theEjbql = null;
63
64     /** */
65     protected static final int EOF_CHAR = 65535; // = (char) -1 = EOF
66

67     /** The factory to create parse tree nodes. */
68     protected NodeFactory factory;
69
70     protected EJBQLParserBase(persistence.antlr.TokenBuffer tokenBuf, int k_) {
71         super(tokenBuf, k_);
72         initialize();
73     }
74
75     public EJBQLParserBase(ParserSharedInputState state, int k_) {
76         super(state, k_);
77     }
78
79     protected EJBQLParserBase(TokenStream lexer, int k) {
80         super(lexer, k);
81     }
82
83     /** */
84     public void setNodeFactory(NodeFactory factory) {
85         this.factory = factory;
86     }
87
88     /** */
89     public NodeFactory getNodeFactory() {
90         return factory;
91     }
92
93     /** */
94     public ParseTree getParseTree() {
95         return (ParseTree)getRootNode();
96     }
97     
98     /** */
99     public Object JavaDoc getRootNode() {
100         return null;
101     }
102
103     /**
104      * INTERNAL
105      * Add the passed error to the error collection
106      */

107     public void addError(EJBQLException e) {
108         getErrors().add(e);
109     }
110
111     public void addError(Exception JavaDoc e) {
112         addError(EJBQLException.generalParsingException(getEjbqlString(), e));
113     }
114
115     /**
116      * INTERNAL
117      * Build a parser for the passed ejbql string
118      */

119     public static EJBQLParser buildParserFor(String JavaDoc ejbqlString) {
120         return EJBQLParser.buildParserFor(ejbqlString);
121     }
122
123     /**
124      * Answer an instance of EJBQLParseTree built from parsing EJBQLString.
125      * Throw a DeploymentException if an error occurs
126      */

127     public static ParseTree buildParseTree(String JavaDoc ejbqlString) throws Exception JavaDoc {
128         // parse
129
EJBQLParser parser = buildParserFor(ejbqlString);
130         try {
131             parser.document();
132         } catch (Exception JavaDoc e) {
133             parser.addError(e);
134         }
135         if (parser.hasErrors()) {
136             throw parser.generateException();
137         }
138
139         // return the tree
140
return parser.getParseTree();
141     }
142
143     /**
144      * INTERNAL
145      * Generate an exception which encapsulates all the exceptions generated
146      * by this parser. Special case where the first exception is an EJBQLException.
147      */

148     public Exception JavaDoc generateException() {
149         //Handle exceptions we expect (such as expressionSotSupported)
150
Exception JavaDoc firstException = (Exception JavaDoc)getErrors().elementAt(0);
151         if (firstException instanceof EJBQLException) {
152             return firstException;
153         }
154
155         //Handle general exceptions, such as NPE
156
EJBQLException exception = EJBQLException.generalParsingException(getEjbqlString());
157         exception.setInternalExceptions(getErrors());
158         return exception;
159     }
160
161     /**
162      * INTERNAL
163      * Return the error collection
164      */

165     private Vector JavaDoc getErrors() {
166         return errors;
167     }
168
169     /**
170      * hasErrors(): Answer true if there were errors during the parsing process
171      *
172      */

173     public boolean hasErrors() {
174         return !getErrors().isEmpty();
175     }
176
177     public void initialize() {
178         setErrors(new Vector JavaDoc());
179         setNodeFactory(new NodeFactoryImpl());
180     }
181
182     /**
183      * Insert the method's description here.
184      * Creation date: (1/10/01 7:47:41 AM)
185      * @return boolean
186      */

187     public boolean isVerbose() {
188         return verbose;
189     }
190
191     /**
192      * output: show the output if verbose is on
193      */

194     public void output(String JavaDoc output) {
195         if (isVerbose()) {
196             System.out.println(output);
197         }
198     }
199
200     /*
201      * Show the string being parsed, and where the error occurred
202      */

203     public void reportError(RecognitionException ex) {
204         EJBQLException error = handleANTLRException(ex);
205         addError(error);
206     }
207
208     /** */
209     protected EJBQLException handleANTLRException(ANTLRException ex) {
210         EJBQLException result = null;
211         if (ex instanceof MismatchedCharException) {
212             MismatchedCharException mismatched = (MismatchedCharException)ex;
213             if (mismatched.mismatchType == MismatchedCharException.CHAR) {
214                 if (mismatched.foundChar == EOF_CHAR) {
215                     result = EJBQLException.unexpectedEOF(getEjbqlString());
216                 }
217                 else {
218                     result = EJBQLException.expectedCharFound(
219                         getEjbqlString(),
220                         String.valueOf((char)mismatched.expecting),
221                         String.valueOf((char)mismatched.foundChar));
222                 }
223             }
224         }
225         else if (ex instanceof MismatchedTokenException) {
226             MismatchedTokenException mismatched = (MismatchedTokenException)ex;
227             Token token = mismatched.token;
228             if ((mismatched.mismatchType == MismatchedTokenException.TOKEN) &&
229                 (token != null)) {
230                 if (token.getType() == Token.EOF_TYPE) {
231                     result = EJBQLException.unexpectedEOF(getEjbqlString());
232                 }
233                 else {
234                     result = EJBQLException.syntaxErrorAt(
235                         getEjbqlString(), token.getText());
236                 }
237             }
238         }
239         else if (ex instanceof NoViableAltException) {
240             Token token = ((NoViableAltException)ex).token;
241             if (token != null) {
242                 if (token.getType() == Token.EOF_TYPE) {
243                     result = EJBQLException.unexpectedEOF(getEjbqlString());
244                 }
245                 else {
246                     result = EJBQLException.unexpectedToken(
247                         getEjbqlString(), token.getText());
248                 }
249             }
250         }
251         else if (ex instanceof NoViableAltForCharException) {
252             NoViableAltForCharException noViableAlt = (NoViableAltForCharException)ex;
253             result = EJBQLException.unexpectedChar(
254                 getEjbqlString(), String.valueOf((char)noViableAlt.foundChar));
255         }
256         else if (ex instanceof TokenStreamRecognitionException) {
257             result = handleANTLRException(((TokenStreamRecognitionException)ex).recog);
258         } else {
259             // no special handling from aboves matches the exception if this
260
// line is reached => make it a syntax error
261
result = EJBQLException.syntaxError(getEjbqlString());
262         }
263         return result;
264     }
265
266     /**
267      * INTERNAL
268      * Set the errors vector
269      */

270     private void setErrors(Vector JavaDoc newErrors) {
271         errors = newErrors;
272     }
273
274     /**
275      * INTERNAL
276      * Set the verbose flag
277      */

278     public void setVerbose(boolean newVerbose) {
279         verbose = newVerbose;
280     }
281
282     /**
283      * INTERNAL
284      * Return the DISTINCT state for the parser
285      */

286     public short getDistinctState() {
287         return getParseTree().getDistinctState();
288     }
289
290     /**
291      * INTERNAL
292      * Return the EJBQL String for the parser
293      */

294     public String JavaDoc getEjbqlString() {
295         return theEjbql;
296     }
297
298     /**
299      * INTERNAL
300      * Set the EJBQL String for the parser to the passed value
301      */

302     public void setEjbqlString(String JavaDoc theEjbql) {
303         this.theEjbql = theEjbql;
304     }
305
306     /**
307      * All the query mechanism related things are initialized here.
308      * This method is called on the *clone* of the query with
309      * every execution.
310      */

311     public static EJBQLParser parseEJBQLString(String JavaDoc ejbqlString) throws QueryException {
312         // Parse the ejbql string, generate the where clause and set the query's selection criteria
313
EJBQLParser parser = buildParserFor(ejbqlString);
314         try {
315             parser.document();
316         } catch (EJBQLException e) {
317             parser.addError(e);
318         } catch (ANTLRException e) {
319             parser.addError(parser.handleANTLRException(e));
320         } catch (Exception JavaDoc e) {
321             parser.addError(e);
322         }
323
324         // Handle any errors generated by the Parser
325
if (parser.hasErrors()) {
326             throw (EJBQLException)parser.generateException();
327         }
328         return parser;
329     }
330 }
331
Popular Tags