private void processOpenParan(Stack<ExpressionNode> expStack, String expS, int index) throws ParseException { if (!expStack.isEmpty()) { ExpressionNode top = expStack.peek(); // Top can not be a Label Node. a(.. is not valid. but ((a.. is fine. if (top instanceof LeafExpressionNode && top != LeafExpressionNode.OPEN_PARAN_NODE) { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } else if (top instanceof NonLeafExpressionNode) { // Top is non leaf. // It can be ! node but with out any child nodes. !a(.. is invalid // Other NonLeafExpressionNode , then there should be exactly 1 child. // a&b( is not valid. // a&( is valid though. Also !( is valid NonLeafExpressionNode nlTop = (NonLeafExpressionNode) top; if ((nlTop.getOperator() == Operator.NOT && nlTop.getChildExps().size() != 0) || (nlTop.getOperator() != Operator.NOT && nlTop.getChildExps().size() != 1)) { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } } } expStack.push(LeafExpressionNode.OPEN_PARAN_NODE); }
private void processLabelExpNode(LeafExpressionNode node, Stack<ExpressionNode> expStack, String expS, int index) throws ParseException { if (expStack.isEmpty()) { expStack.push(node); } else { ExpressionNode top = expStack.peek(); if (top == LeafExpressionNode.OPEN_PARAN_NODE) { expStack.push(node); } else if (top instanceof NonLeafExpressionNode) { NonLeafExpressionNode nlTop = (NonLeafExpressionNode) expStack.pop(); nlTop.addChildExp(node); if (nlTop.getOperator() == Operator.NOT && !expStack.isEmpty()) { ExpressionNode secondTop = expStack.peek(); if (secondTop == LeafExpressionNode.OPEN_PARAN_NODE) { expStack.push(nlTop); } else if (secondTop instanceof NonLeafExpressionNode) { ((NonLeafExpressionNode) secondTop).addChildExp(nlTop); } } else { expStack.push(nlTop); } } else { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } } }
private void processANDorOROp(Operator op, Stack<ExpressionNode> expStack, String expS, int index) throws ParseException { if (expStack.isEmpty()) { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } ExpressionNode top = expStack.pop(); if (top.isSingleNode()) { if (top == LeafExpressionNode.OPEN_PARAN_NODE) { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } expStack.push(new NonLeafExpressionNode(op, top)); } else { NonLeafExpressionNode nlTop = (NonLeafExpressionNode) top; if (nlTop.getChildExps().size() != 2) { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } expStack.push(new NonLeafExpressionNode(op, nlTop)); } }
private void processNOTOp(Stack<ExpressionNode> expStack, String expS, int index) throws ParseException { // When ! comes, the stack can be empty or top ( or top can be some exp like // a& // !!.., a!, a&b!, !a! are invalid if (!expStack.isEmpty()) { ExpressionNode top = expStack.peek(); if (top.isSingleNode() && top != LeafExpressionNode.OPEN_PARAN_NODE) { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } if (!top.isSingleNode() && ((NonLeafExpressionNode) top).getChildExps().size() != 1) { throw new ParseException("Error parsing expression " + expS + " at column : " + index); } } expStack.push(new NonLeafExpressionNode(Operator.NOT)); }
public ExpressionNode expand(ExpressionNode src) { if (!src.isSingleNode()) { NonLeafExpressionNode nlExp = (NonLeafExpressionNode) src; List<ExpressionNode> childExps = nlExp.getChildExps(); Operator outerOp = nlExp.getOperator(); if (isToBeExpanded(childExps)) { // Any of the child exp is a non leaf exp with & or | operator NonLeafExpressionNode newNode = new NonLeafExpressionNode(nlExp.getOperator()); for (ExpressionNode exp : childExps) { if (exp.isSingleNode()) { newNode.addChildExp(exp); } else { newNode.addChildExp(expand(exp)); } } nlExp = expandNonLeaf(newNode, outerOp); } return nlExp; } if (src instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) src).getOperator() == Operator.NOT) { // Negate the exp return negate((NonLeafExpressionNode) src); } return src; }
private ExpressionNode negate(NonLeafExpressionNode nlExp) { ExpressionNode notChild = nlExp.getChildExps().get(0); if (notChild instanceof LeafExpressionNode) { return nlExp; } NonLeafExpressionNode nlNotChild = (NonLeafExpressionNode) notChild; if (nlNotChild.getOperator() == Operator.NOT) { // negate the negate return nlNotChild.getChildExps().get(0); } Operator negateOp = nlNotChild.getOperator() == Operator.AND ? Operator.OR : Operator.AND; NonLeafExpressionNode newNode = new NonLeafExpressionNode(negateOp); for (ExpressionNode expNode : nlNotChild.getChildExps()) { NonLeafExpressionNode negateNode = new NonLeafExpressionNode(Operator.NOT); negateNode.addChildExp(expNode.deepClone()); newNode.addChildExp(expand(negateNode)); } return newNode; }
private NonLeafExpressionNode mergeChildNodes(NonLeafExpressionNode newOuterNode, Operator outerOp, ExpressionNode lChild, NonLeafExpressionNode nlChild) { // Merge the single right/left node into the other side if (nlChild.getOperator() == outerOp) { NonLeafExpressionNode leftChildNLEClone = nlChild.deepClone(); leftChildNLEClone.addChildExp(lChild); newOuterNode = leftChildNLEClone; } else if (outerOp == Operator.AND) { assert nlChild.getOperator() == Operator.OR; // outerOp is & here. We need to expand the node here // (a | b) & c -> (a & c) | (b & c) // OR // c & (a | b) -> (c & a) | (c & b) newOuterNode = new NonLeafExpressionNode(Operator.OR); for (ExpressionNode exp : nlChild.getChildExps()) { newOuterNode.addChildExp(new NonLeafExpressionNode(Operator.AND, exp, lChild)); } } return newOuterNode; }
@Override public List<Tag> createVisibilityExpTags(String visExpression, boolean withSerializationFormat, boolean checkAuths) throws IOException { ExpressionNode node = null; try { node = this.expressionParser.parse(visExpression); } catch (ParseException e) { throw new IOException(e); } node = this.expressionExpander.expand(node); List<Tag> tags = new ArrayList<Tag>(); if (withSerializationFormat) { tags.add(STRING_SERIALIZATION_FORMAT_TAG); } if (node instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) node).getOperator() == Operator.OR) { for (ExpressionNode child : ((NonLeafExpressionNode) node).getChildExps()) { tags.add(createTag(child)); } } else { tags.add(createTag(node)); } return tags; }
@Override public List<Tag> createVisibilityExpTags(String visExpression, boolean withSerializationFormat, boolean checkAuths) throws IOException { ExpressionNode node = null; try { node = this.expressionParser.parse(visExpression); } catch (ParseException e) { throw new IOException(e); } node = this.expressionExpander.expand(node); List<Tag> tags = new ArrayList<>(); if (withSerializationFormat) { tags.add(STRING_SERIALIZATION_FORMAT_TAG); } if (node instanceof NonLeafExpressionNode && ((NonLeafExpressionNode) node).getOperator() == Operator.OR) { for (ExpressionNode child : ((NonLeafExpressionNode) node).getChildExps()) { tags.add(createTag(child)); } } else { tags.add(createTag(node)); } return tags; }
private static Operator getOperator(byte op) { switch (op) { case AND: return Operator.AND; case OR: return Operator.OR; case NOT: return Operator.NOT; } return null; }