2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.jaxen;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Optional;
13 import org.jaxen.expr.AllNodeStep;
14 import org.jaxen.expr.BinaryExpr;
15 import org.jaxen.expr.CommentNodeStep;
16 import org.jaxen.expr.Expr;
17 import org.jaxen.expr.FilterExpr;
18 import org.jaxen.expr.FunctionCallExpr;
19 import org.jaxen.expr.LiteralExpr;
20 import org.jaxen.expr.LocationPath;
21 import org.jaxen.expr.NameStep;
22 import org.jaxen.expr.NumberExpr;
23 import org.jaxen.expr.PathExpr;
24 import org.jaxen.expr.ProcessingInstructionNodeStep;
25 import org.jaxen.expr.TextNodeStep;
26 import org.jaxen.expr.UnaryExpr;
27 import org.jaxen.expr.VariableReferenceExpr;
29 final class ExprWalker {
30 private final ExprListener listener;
32 ExprWalker(final ExprListener listener) {
33 this.listener = requireNonNull(listener);
36 public void walk(final Expr expr) {
37 if (expr instanceof BinaryExpr) {
38 final BinaryExpr binary = (BinaryExpr) expr;
39 listener.enterBinaryExpr(binary);
40 walk(binary.getLHS());
41 listener.visitOperator(Operator.forString(binary.getOperator()));
42 walk(binary.getRHS());
43 listener.exitBinaryExpr(binary);
44 } else if (expr instanceof FilterExpr) {
45 final FilterExpr filter = (FilterExpr) expr;
46 listener.enterFilterExpr(filter);
48 listener.exitFilterExpr(filter);
49 } else if (expr instanceof FunctionCallExpr) {
50 final FunctionCallExpr func = (FunctionCallExpr) expr;
51 listener.enterFunctionCallExpr(func);
53 for (Object arg : func.getParameters()) {
57 listener.exitFunctionCallExpr(func);
58 } else if (expr instanceof LiteralExpr) {
59 listener.visitLiteralExpr((LiteralExpr) expr);
60 } else if (expr instanceof LocationPath) {
61 final LocationPath path = (LocationPath) expr;
62 final Optional<StepListener> maybeListener = listener.enterLocationPath(path);
63 if (maybeListener.isPresent()) {
64 final StepListener l = maybeListener.get();
65 for (Object step : path.getSteps()) {
66 if (step instanceof AllNodeStep) {
67 l.onAll((AllNodeStep) step);
68 } else if (step instanceof CommentNodeStep) {
69 l.onComment((CommentNodeStep) step);
70 } else if (step instanceof NameStep) {
71 l.onName((NameStep) step);
72 } else if (step instanceof ProcessingInstructionNodeStep) {
73 l.onProcessingInstruction((ProcessingInstructionNodeStep) step);
74 } else if (step instanceof TextNodeStep) {
75 l.onTest((TextNodeStep) step);
77 throw new IllegalArgumentException("Unsupported step " + step);
82 listener.exitLocationPath(path);
83 } else if (expr instanceof NumberExpr) {
84 listener.visitNumberExpr((NumberExpr) expr);
85 } else if (expr instanceof PathExpr) {
86 final PathExpr path = (PathExpr) expr;
87 listener.enterPathExpr(path);
88 walk(path.getFilterExpr());
89 walk(path.getLocationPath());
90 listener.exitPathExpr(path);
91 } else if (expr instanceof UnaryExpr) {
92 final UnaryExpr unary = (UnaryExpr) expr;
93 listener.enterNotExpr(unary);
94 walk(unary.getExpr());
95 listener.exitNotExpr(unary);
96 } else if (expr instanceof VariableReferenceExpr) {
97 listener.visitVariableReferenceExpr((VariableReferenceExpr) expr);
99 throw new IllegalArgumentException("Unsupported expression " + expr);