001: /**********************************************************************
002: Copyright (c) 2007 Erik Bengtson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015:
016: Contributors:
017: ...
018: **********************************************************************/package org.jpox.store.query;
019:
020: import java.text.CharacterIterator;
021:
022: import org.jpox.exceptions.JPOXUserException;
023: import org.jpox.util.Imports;
024:
025: /**
026: * Parser for JPQL queries, extending the basic JDOQL parser for supporting the identifier ?1, ?2 syntax.
027: *
028: * @version $Revision: 1.2 $
029: */
030: public class JPQLParser extends Parser {
031: /**
032: * Constructor.
033: * @param input Input string
034: * @param imports Any imports
035: */
036: public JPQLParser(String input, Imports imports) {
037: super (input, imports);
038: }
039:
040: /**
041: * Override the identifier parse to allow for ?1, ?2 syntax
042: * @return The identifier
043: */
044: public String parseIdentifier() {
045: String identifier = super .parseIdentifier();
046: if (identifier != null) {
047: return identifier;
048: }
049: skipWS();
050: char c = ci.current();
051:
052: if (!Character.isJavaIdentifierStart(c) && !(c == '?')) {
053: // Current character is not a valid identifier char, and isn't a
054: // valid JDOQL parameter start character
055: return null;
056: }
057:
058: StringBuffer id = new StringBuffer();
059: id.append(c);
060: while (Character.isJavaIdentifierPart(c = ci.next())) {
061: id.append(c);
062: }
063:
064: return id.toString();
065: }
066:
067: /**
068: * Parse a Character literal.
069: * @return the Character parsed. null if single quotes is found
070: * @throws JPOXUserException if an invalid character is found or the CharacterIterator is finished
071: */
072: public Character parseCharacterLiteral() {
073: skipWS();
074:
075: if (ci.current() != '\'') {
076: return null;
077: }
078:
079: char c = ci.next();
080:
081: if (c == CharacterIterator.DONE) {
082: throw new JPOXUserException("Invalid character literal: "
083: + input);
084: }
085:
086: if (ci.next() != '\'') {
087: throw new JPOXUserException("Invalid character literal: "
088: + input);
089: }
090:
091: ci.next();
092:
093: return new Character(c);
094: }
095:
096: /**
097: * Parse a String literal.
098: * @return the String parsed. null if single quotes or double quotes is found
099: * @throws JPOXUserException if an invalid character is found or the CharacterIterator is finished
100: */
101: public String parseStringLiteral() {
102: skipWS();
103:
104: // Strings can be surrounded by single or double quotes
105: char quote = ci.current();
106: if (quote != '"' && quote != '\'') {
107: return null;
108: }
109:
110: StringBuffer lit = new StringBuffer();
111: char c;
112:
113: while ((c = ci.next()) != quote) {
114: if (c == CharacterIterator.DONE) {
115: throw new JPOXUserException("Invalid string literal: "
116: + input);
117: }
118:
119: lit.append(c);
120: }
121:
122: ci.next();
123:
124: return lit.toString();
125: }
126: }
|