@Override public Object pre(Object t) { if (!(t instanceof CommonTree)) return t; if (t instanceof CommonErrorNode) { return t; } CommonTree node = (CommonTree) t; if (node instanceof QueryNode) { QueryVariableContext newCurrent = new QueryVariableContext(model, (QueryNode) node); if (root == null) { root = newCurrent; } QueryVariableContext last = stack.peekLast(); if (last != null) { last.addChild(newCurrent); } stack.addLast(newCurrent); } return t; }
private String getTheSonOf(Object... params) { String sonsStr = ""; String sep = ""; for(Object son : params) { String paramTxt = ""; if (son instanceof CommonErrorNode) { paramTxt = ((CommonErrorNode) son).getText(); } else if (son instanceof CommonTree) { CommonTree cTree = ((CommonTree)son); paramTxt = ((CommonTree) son).getText(); if (paramTxt.equals("Number") || paramTxt.equals("MAType") || paramTxt.equals("String")) { paramTxt = cTree.getChild(0).getText(); } else if (paramTxt.equals("StockOperation")) { paramTxt = cTree.getChild(0).getChild(0).getText(); } else { paramTxt = cTree.getText() + ((cTree.getChildren()!= null)?"("+getTheSonOf(cTree.getChildren().toArray(new Object[] {}))+")":""); } } sonsStr = sonsStr + sep + paramTxt; sep = ","; } return sonsStr; }
private int[] whereIsThatSonOf(Object son) { int[] location = new int[2]; if (son instanceof CommonToken) { location[0] = ((CommonToken) son).getLine(); location[1] = ((CommonToken) son).getCharPositionInLine(); } else if (son instanceof CommonErrorNode) { location[0] = ((CommonErrorNode) son).start.getLine(); location[1] = ((CommonErrorNode) son).start.getCharPositionInLine(); } else if (son instanceof CommonTree) { CommonTree cTree = ((CommonTree)son); if (cTree.getText().equals("Number") || cTree.getText().equals("MAType") || cTree.getText().equals("String") ) { location[0] = cTree.getChild(0).getLine(); location[1] = cTree.getChild(0).getCharPositionInLine(); } else if (cTree.getText().equals("StockOperation")) { location[0] = cTree.getChild(0).getChild(0).getLine(); location[1] = cTree.getChild(0).getChild(0).getCharPositionInLine(); } else { location[0] = cTree.getLine(); location[1] = cTree.getCharPositionInLine(); } } return location; }
/** * Takes the "where" parameter and turns it into a Java Object that can be used for querying * * @param whereParam String * @return Query a parsed version of the where clause, represented in Java */ default Query getWhereClause(String whereParam) throws InvalidQueryException { if (whereParam == null) return QueryImpl.EMPTY; try { CommonTree whereTree = WhereCompiler.compileWhereClause(whereParam); if (whereTree instanceof CommonErrorNode) { rpeLogger().debug("Error parsing the WHERE clause " + whereTree); throw new InvalidQueryException(whereTree); } return new QueryImpl(whereTree); } catch (RewriteCardinalityException re) { //Catch any error so it doesn't get thrown up the stack rpeLogger().info("Unhandled Error parsing the WHERE clause: " + re); } catch (RecognitionException e) { whereParam += ", " + WhereCompiler.resolveMessage(e); rpeLogger().info("Error parsing the WHERE clause: " + whereParam); } //Default to throw out an invalid query throw new InvalidQueryException(whereParam); }
@Override public Object pre(Object t) { if (t instanceof CommonErrorNode) { errorNodes.add((CommonErrorNode) t); return t; } return t; }
@Override public Object post(Object t) { if (!(t instanceof CommonTree)) return t; if (t instanceof CommonErrorNode) { return t; } CommonTree node = (CommonTree) t; if (node.token == null) return t; if (node.getType() == JPA2Lexer.DISTINCT || node.getType() == JPA2Lexer.FETCH || node.parent != null && node.parent.getType() == JPA2Lexer.T_SELECTED_ITEM && node.getType() == JPA2Lexer.AS) { sb.appendSpace(); } if (node instanceof TreeToQueryCapable) { return ((TreeToQueryCapable) t).treeToQueryPost(sb, invalidNodes); } return t; }
protected boolean isValid(CommonTree tree) { TreeVisitor visitor = new TreeVisitor(); ErrorNodesFinder errorNodesFinder = new ErrorNodesFinder(); visitor.visit(tree, errorNodesFinder); List<CommonErrorNode> errorNodes = errorNodesFinder.getErrorNodes(); if (!errorNodes.isEmpty()) { System.err.println(errorNodes); } return errorNodes.isEmpty(); }
/** Changes this tree node into an error node. */ void setErrorNode(Token start, Token stop, RecognitionException e) { this.delegate = new CommonErrorNode(this.tokenStream, start, stop, e); }
private CommonErrorNode getDelegate() { return this.delegate; }
/** * Gets the clause specificed in paramName * * @param param * @param paramName * @return bean property names potentially using JSON Pointer syntax */ default List<String> getClause(String param, String paramName) { if (param == null) return Collections.emptyList(); try { CommonTree selectedPropsTree = WhereCompiler.compileSelectClause(param); if (selectedPropsTree instanceof CommonErrorNode) { rpeLogger().debug("Error parsing the " + paramName + " clause " + selectedPropsTree); throw new InvalidSelectException(paramName, selectedPropsTree); } if (selectedPropsTree.getChildCount() == 0 && !selectedPropsTree.getText().isEmpty()) { return Arrays.asList(selectedPropsTree.getText()); } List<Tree> children = (List<Tree>) selectedPropsTree.getChildren(); if (children != null && !children.isEmpty()) { List<String> properties = new ArrayList<String>(children.size()); for (Tree child : children) { properties.add(child.getText()); } return properties; } } catch (RewriteCardinalityException re) { //Catch any error so it doesn't get thrown up the stack rpeLogger().debug("Unhandled Error parsing the " + paramName + " clause: " + re); } catch (RecognitionException e) { rpeLogger().debug("Error parsing the \"+paramName+\" clause: " + param); } catch (InvalidQueryException iqe) { throw new InvalidSelectException(paramName, iqe.getQueryParam()); } //Default to throw out an invalid query throw new InvalidSelectException(paramName, param); }
public GrammarASTErrorNode(TokenStream input, Token start, Token stop, org.antlr.runtime.RecognitionException e) { delegate = new CommonErrorNode(input,start,stop,e); }
public void identifyVariableEntity(DomainModel model, Deque<QueryVariableContext> stack, List<ErrorRec> invalidNodes) { String variableName = getVariableName(); if (variableName == null) { invalidNodes.add(new ErrorRec(this, "No variable name found")); return; } List children = getChildren(); if (children == null || children.size() == 0) { invalidNodes.add(new ErrorRec(this, "No children found")); return; } CommonTree child0 = (CommonTree) children.get(0); if (child0 instanceof CommonErrorNode) { invalidNodes.add(new ErrorRec(this, "Child 0 is an error node")); return; } QueryVariableContext queryVC = stack.peekLast(); if (child0 instanceof PathNode) { PathNode pathNode = (PathNode) child0; Pointer pointer = pathNode.resolvePointer(model, queryVC); if (pointer instanceof NoPointer) { invalidNodes.add(new ErrorRec(this, "Cannot resolve joined entity")); } else if (pointer instanceof SimpleAttributePointer) { invalidNodes.add(new ErrorRec(this, "Joined entity resolved to a non-entity attribute")); } else if (pointer instanceof EntityPointer) { queryVC.addEntityVariable(variableName, ((EntityPointer) pointer).getEntity()); } else if (pointer instanceof CollectionPointer) { queryVC.addEntityVariable(variableName, ((CollectionPointer) pointer).getEntity()); } else { invalidNodes.add(new ErrorRec(this, "Unexpected pointer variable type: " + pointer.getClass()) ); } } else {//this special case is for "join X on X.a = Y.b" query. Entity name would be just text in the child node try { queryVC.addEntityVariable(variableName, model.getEntityByName(child0.getText())); } catch (UnknownEntityNameException e) { invalidNodes.add(new ErrorRec(this, "Could not find entity for name " + child0.getText()) ); } } }
public List<CommonErrorNode> getErrorNodes() { return Collections.unmodifiableList(errorNodes); }
@Override public Object pre(Object t) { if (!(t instanceof CommonTree)) { return t; } if (t instanceof CommonErrorNode) { invalidNodes.add(new ErrorRec((CommonErrorNode) t, "Error node")); return t; } CommonTree node = (CommonTree) t; if (node.token == null) return t; if (node.getType() == JPA2Lexer.HAVING || node.parent != null && node.parent.getType() == JPA2Lexer.T_SIMPLE_CONDITION && !parentNodeHasPreviousLparen(node) || node.parent != null && node.parent.getType() == JPA2Lexer.T_GROUP_BY || node.parent != null && node.parent.getType() == JPA2Lexer.T_ORDER_BY && node.getType() != JPA2Lexer.T_ORDER_BY_FIELD || node.parent != null && node.parent.getType() == JPA2Lexer.T_CONDITION && node.getType() == JPA2Lexer.LPAREN && (node.childIndex == 0 || node.parent.getChild(node.childIndex - 1).getType() != JPA2Lexer.LPAREN) || node.getType() == JPA2Lexer.AND || node.parent != null && node.parent.getType() == JPA2Lexer.T_ORDER_BY_FIELD || node.parent != null && node.parent.getType() == JPA2Lexer.T_SELECTED_ITEM && node.getType() == JPA2Lexer.AS || node.getType() == JPA2Lexer.OR || node.getType() == JPA2Lexer.NOT || node.getType() == JPA2Lexer.DISTINCT && node.childIndex == 0 || node.getType() == JPA2Lexer.JOIN || node.getType() == JPA2Lexer.LEFT || node.getType() == JPA2Lexer.OUTER || node.getType() == JPA2Lexer.INNER || node.getType() == JPA2Lexer.FETCH ) { sb.appendSpace(); } if (node.getType() == JPA2Lexer.T_ORDER_BY_FIELD && node.childIndex > 0 && node.parent.getChild(node.childIndex - 1).getType() == JPA2Lexer.T_ORDER_BY_FIELD) { sb.appendString(", "); } if (isGroupByItem(node)) { if (node.childIndex > 0 && isGroupByItem((CommonTree) node.parent.getChild(node.childIndex - 1))) { sb.appendString(", "); } } if (node instanceof TreeToQueryCapable) { return ((TreeToQueryCapable) t).treeToQueryPre(sb, invalidNodes); } if (node.getType() == JPA2Lexer.T_SELECTED_ITEMS) { return t; } if (node.getType() == JPA2Lexer.T_SOURCES) { sb.appendString("from "); return t; } sb.appendString(node.toString()); return t; }
public ASTErrorNode(TokenStream input, Token start, Token stop, RecognitionException e) { delegate = new CommonErrorNode(input, start, stop, e); }
public TNodeError(TokenStream input, Token start, Token stop, RecognitionException e) { delegate = new CommonErrorNode(input,start,stop,e); }