1 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 40 public class JDOQLParameterDeclarationParser 41 implements QueryParser 42 { 43 46 protected final static ResourceBundle messages = I18NHelper.loadBundle( 47 JDOQLParameterDeclarationParser.class); 48 49 57 public Iterator parameterTypeIterator(String text) 58 { 59 return new ParameterTypeIterator(parse(text)); 60 } 61 62 67 private AST parse(String text) 68 { 69 if (text == null) { 70 return null; 71 } 72 73 AST paramsAST = null; 75 76 ErrorMsg errorMsg = new ErrorMsg(); 78 79 JQLParser parser = JQLC.createStringParser(text, errorMsg); 81 82 try { 83 parser.parseParameters(); 85 paramsAST = parser.getAST(); 87 } 88 catch (ANTLRException ex) { 89 JQLParser.handleANTLRException(ex, errorMsg); 91 } 92 93 return paramsAST; 94 } 95 96 100 private static class ParameterTypeIterator 101 implements Iterator 102 { 103 private AST current; 105 106 114 ParameterTypeIterator(AST ast) 115 { 116 current = ast; 117 } 118 119 123 public boolean hasNext() 124 { 125 return (current != null); 126 } 127 128 135 public Object next() 136 { 137 if (current == null) 139 throw new NoSuchElementException(); 140 141 if (current.getType() != JQLParser.PARAMETER_DEF) 144 throw new JDOFatalInternalException(I18NHelper.getMessage( 145 messages, 146 "jqlc.jdoqlparameterdeclarationparser.next.wrongtoken", current.getType())); 148 149 String typeRepr = getTypeRepr(current.getFirstChild()); 151 152 current = current.getNextSibling(); 154 155 return typeRepr; 156 } 157 158 163 public void remove() { throw new UnsupportedOperationException (); } 164 165 171 private String getTypeRepr(AST ast) 172 { 173 if (ast == null) 174 return ""; 175 176 if (ast.getType() == JQLParser.DOT) { 179 StringBuffer tmp = new StringBuffer (); 180 AST left = ast.getFirstChild(); 181 AST right = left.getNextSibling(); 182 return getTypeRepr(left) + "." + getTypeRepr(right); 183 } 184 185 return ast.getText(); 187 } 188 } 189 190 205 public static void main(String [] args) 206 { 207 QueryParser helper = new JDOQLParameterDeclarationParser(); 208 for (int i = 0; i < args.length; i++) { 209 String 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 |