private Node V() throws ScanException { Node e = E(); Node variable = new Node(Node.Type.VARIABLE, e); Token t = peekAtCurentToken(); if (isDefaultToken(t)) { advanceTokenPointer(); Node def = E(); variable.defaultPart = def; } return variable; }
private Node C() throws ScanException { Node e0 = E(); Token t = peekAtCurentToken(); if (isDefaultToken(t)) { advanceTokenPointer(); Node literal = makeNewLiteralNode(CoreConstants.DEFAULT_VALUE_SEPARATOR); e0.append(literal); Node e1 = E(); e0.append(e1); } return e0; }
private Node T() throws ScanException { Token t = peekAtCurentToken(); switch (t.type) { case LITERAL: advanceTokenPointer(); return makeNewLiteralNode(t.payload); case CURLY_LEFT: advanceTokenPointer(); Node innerNode = C(); Token right = peekAtCurentToken(); expectCurlyRight(right); advanceTokenPointer(); Node curlyLeft = makeNewLiteralNode(CoreConstants.LEFT_ACCOLADE); curlyLeft.append(innerNode); curlyLeft.append(makeNewLiteralNode(CoreConstants.RIGHT_ACCOLADE)); return curlyLeft; case START: advanceTokenPointer(); Node v = V(); Token w = peekAtCurentToken(); expectCurlyRight(w); advanceTokenPointer(); return v; default: return null; } }
public static String substituteVariable(String input, PropertyContainer pc0, PropertyContainer pc1) throws ScanException { Node node = tokenizeAndParseString(input); NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, pc0, pc1); return nodeToStringTransformer.transform(); }
private void compileNode(Node inputNode, StringBuilder stringBuilder, Stack<Node> cycleCheckStack) throws ScanException { Node n = inputNode; while (n != null) { switch (n.type) { case LITERAL: handleLiteral(n, stringBuilder); break; case VARIABLE: handleVariable(n, stringBuilder, cycleCheckStack); break; } n = n.next; } }
@Override public int hashCode() { int result = type != null ? type.hashCode() : 0; result = 31 * result + (payload != null ? payload.hashCode() : 0); result = 31 * result + (defaultPart != null ? defaultPart.hashCode() : 0); result = 31 * result + (next != null ? next.hashCode() : 0); return result; } }
private Node Eopt() throws ScanException { Token next = peekAtCurentToken(); if (next == null) { return null; } else { return E(); } }
private Node makeNewLiteralNode(String s) { return new Node(Node.Type.LITERAL, s); }
void recursive(Node n, StringBuilder sb) { Node c = n; while (c != null) { sb.append(c.toString()).append(" --> "); c = c.next; } sb.append("null "); }
public Node parse() throws ScanException { if(tokenList == null || tokenList.isEmpty()) return null; return E(); }
@Override public int hashCode() { int result = type != null ? type.hashCode() : 0; result = 31 * result + (payload != null ? payload.hashCode() : 0); return result; }
/** * See http://logback.qos.ch/manual/configuration.html#variableSubstitution */ public static String substVars(String input, PropertyContainer pc0, PropertyContainer pc1) { try { return NodeToStringTransformer.substituteVariable(input, pc0, pc1); } catch (ScanException e) { throw new IllegalArgumentException("Failed to parse input [" + input + "]", e); } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Node node = (Node) o; if (type != node.type) return false; if (payload != null ? !payload.equals(node.payload) : node.payload != null) return false; if (defaultPart != null ? !defaultPart.equals(node.defaultPart) : node.defaultPart != null) return false; if (next != null ? !next.equals(node.next) : node.next != null) return false; return true; }
void expectCurlyRight(Token t) throws ScanException { expectNotNull(t, "}"); if (t.type != Token.Type.CURLY_RIGHT) { throw new ScanException("Expecting }"); } }
/** * Determine if a node has already been visited already by checking the cycleDetectionStack * for it's existence. This method is used -- rather than Stack.contains() -- because * we want to ignore the Node's 'next' attribute when comparing for equality. */ private boolean haveVisitedNodeAlready(Node node, Stack<Node> cycleDetectionStack) { for (Node cycleNode : cycleDetectionStack) { if (equalNodes(node, cycleNode)) { return true; } } return false; } private boolean equalNodes(Node node1, Node node2) {
private boolean equalNodes(Node node1, Node node2) { if (node1.type != null && !node1.type.equals(node2.type)) return false; if (node1.payload != null && !node1.payload.equals(node2.payload)) return false; if (node1.defaultPart != null && !node1.defaultPart.equals(node2.defaultPart)) return false; return true; }
private String constructRecursionErrorMessage(Stack<Node> recursionNodes) { StringBuilder errorBuilder = new StringBuilder("Circular variable reference detected while parsing input ["); for (Node stackNode : recursionNodes) { errorBuilder.append("${").append(variableNodeValue(stackNode)).append("}"); if (recursionNodes.lastElement() != stackNode) { errorBuilder.append(" --> "); } } errorBuilder.append("]"); return errorBuilder.toString(); }