1 package org.apache.ojb.jdo.jdoql; 2 3 17 18 import java.io.StringReader ; 19 import java.util.*; 20 21 import javax.jdo.JDOUserException; 22 23 import antlr.RecognitionException; 24 import antlr.TokenStreamException; 25 26 31 public class QueryParsingHelper 32 { 33 38 public Map parseParameters(String declaration) throws JDOUserException 39 { 40 JDOQLLexer lexer = new JDOQLLexer(new StringReader (declaration)); 41 JDOQLParser parser = new JDOQLParser(lexer); 42 JDOQLTreeParser treeParser = new JDOQLTreeParser(); 43 44 parser.setASTNodeClass(ASTWithPositionInfo.class.getName()); 45 try 46 { 47 parser.declareParameters(); 48 return treeParser.declareParameters(parser.getAST()); 49 } 50 catch (RecognitionException ex) 51 { 52 throw new JDOUserException("Error in parameter declaration at line "+ex.getLine()+", column "+ex.getColumn()); 53 } 54 catch (TokenStreamException ex) 55 { 56 throw new JDOUserException("Could not parse the parameter declaration"); 57 } 58 } 59 60 65 public Map parseVariables(String declaration) throws JDOUserException 66 { 67 JDOQLLexer lexer = new JDOQLLexer(new StringReader (declaration)); 68 JDOQLParser parser = new JDOQLParser(lexer); 69 JDOQLTreeParser treeParser = new JDOQLTreeParser(); 70 71 parser.setASTNodeClass(ASTWithPositionInfo.class.getName()); 72 try 73 { 74 parser.declareVariables(); 75 return treeParser.declareVariables(parser.getAST()); 76 } 77 catch (RecognitionException ex) 78 { 79 throw new JDOUserException("Error in variable declaration at line "+ex.getLine()+", column "+ex.getColumn()); 80 } 81 catch (TokenStreamException ex) 82 { 83 throw new JDOUserException("Could not parse the variable declaration"); 84 } 85 } 86 87 92 public List parseImports(String declaration) throws JDOUserException 93 { 94 JDOQLLexer lexer = new JDOQLLexer(new StringReader (declaration)); 95 JDOQLParser parser = new JDOQLParser(lexer); 96 JDOQLTreeParser treeParser = new JDOQLTreeParser(); 97 98 parser.setASTNodeClass(ASTWithPositionInfo.class.getName()); 99 try 100 { 101 parser.declareImports(); 102 return treeParser.declareImports(parser.getAST()); 103 } 104 catch (RecognitionException ex) 105 { 106 throw new JDOUserException("Error in import specification at line "+ex.getLine()+", column "+ex.getColumn()); 107 } 108 catch (TokenStreamException ex) 109 { 110 throw new JDOUserException("Could not parse the import specification"); 111 } 112 } 113 114 119 public List parseOrderings(String declaration) throws JDOUserException 120 { 121 JDOQLLexer lexer = new JDOQLLexer(new StringReader (declaration)); 122 JDOQLParser parser = new JDOQLParser(lexer); 123 JDOQLTreeParser treeParser = new JDOQLTreeParser(); 124 125 parser.setASTNodeClass(ASTWithPositionInfo.class.getName()); 126 try 127 { 128 parser.setOrdering(); 129 return treeParser.setOrdering(parser.getAST()); 130 } 131 catch (RecognitionException ex) 132 { 133 throw new JDOUserException("Error in ordering specification at line "+ex.getLine()+", column "+ex.getColumn()); 134 } 135 catch (TokenStreamException ex) 136 { 137 throw new JDOUserException("Could not parse the ordering specification"); 138 } 139 } 140 141 146 public Expression parseFilter(String expression) throws JDOUserException 147 { 148 JDOQLLexer lexer = new JDOQLLexer(new StringReader (expression)); 149 JDOQLParser parser = new JDOQLParser(lexer); 150 JDOQLTreeParser treeParser = new JDOQLTreeParser(); 151 152 parser.setASTNodeClass(ASTWithPositionInfo.class.getName()); 153 try 154 { 155 parser.expression(); 156 return treeParser.expression(parser.getAST()); 157 } 158 catch (RecognitionException ex) 159 { 160 throw new JDOUserException("Error in filter expression at line "+ex.getLine()+", column "+ex.getColumn()); 161 } 162 catch (TokenStreamException ex) 163 { 164 throw new JDOUserException("Could not parse the filter expression"); 165 } 166 } 167 168 public static void main(String [] args) throws JDOUserException 169 { 170 if (args.length < 2) 171 { 172 System.out.println("Usage:\n\njava "+QueryParsingHelper.class.getName()+" [type] [text]\n"); 173 System.out.println("where type specifies what kind of text to parse"); 174 System.out.println("(imports, parameters, variables, orderings, filter)"); 175 System.out.println("and text gives the actual text to parse"); 176 return; 177 } 178 179 QueryParsingHelper helper = new QueryParsingHelper(); 180 181 if ("imports".equals(args[0])) 182 { 183 List imports = helper.parseImports(args[1]); 184 185 for (int idx = 0; idx < imports.size(); idx++) 186 { 187 System.out.println("("+idx+"): "+imports.get(idx).toString()); 188 } 189 } 190 else if ("parameters".equals(args[0])) 191 { 192 Map params = helper.parseParameters(args[1]); 193 194 for (Iterator it = params.values().iterator(); it.hasNext();) 195 { 196 System.out.println(it.toString()); 197 } 198 } 199 else if ("variables".equals(args[0])) 200 { 201 Map vars = helper.parseVariables(args[1]); 202 203 for (Iterator it = vars.values().iterator(); it.hasNext();) 204 { 205 System.out.println(it.toString()); 206 } 207 } 208 else if ("orderings".equals(args[0])) 209 { 210 List orderings = helper.parseOrderings(args[1]); 211 212 for (int idx = 0; idx < orderings.size(); idx++) 213 { 214 System.out.println("("+idx+"): "+orderings.get(idx).toString()); 215 } 216 } 217 else if ("filter".equals(args[0])) 218 { 219 Expression filterExpr = helper.parseFilter(args[1]); 220 221 System.out.println(filterExpr.toString()); 222 } 223 else 224 { 225 System.out.println("Unknown kind of text; allowed are: imports, parameters, variables, orderings, filter"); 226 } 227 } 228 } 229 | Popular Tags |