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 com.google.common.base.Preconditions;
11 import java.util.Optional;
12 import org.jaxen.expr.AllNodeStep;
13 import org.jaxen.expr.BinaryExpr;
14 import org.jaxen.expr.CommentNodeStep;
15 import org.jaxen.expr.Expr;
16 import org.jaxen.expr.FilterExpr;
17 import org.jaxen.expr.FunctionCallExpr;
18 import org.jaxen.expr.LiteralExpr;
19 import org.jaxen.expr.LocationPath;
20 import org.jaxen.expr.NameStep;
21 import org.jaxen.expr.NumberExpr;
22 import org.jaxen.expr.PathExpr;
23 import org.jaxen.expr.ProcessingInstructionNodeStep;
24 import org.jaxen.expr.TextNodeStep;
25 import org.jaxen.expr.UnaryExpr;
26 import org.jaxen.expr.VariableReferenceExpr;
28 final class ExprWalker {
29 private final ExprListener listener;
31 ExprWalker(final ExprListener listener) {
32 this.listener = Preconditions.checkNotNull(listener);
35 public void walk(final Expr expr) {
36 if (expr instanceof BinaryExpr) {
37 final BinaryExpr binary = (BinaryExpr) expr;
38 listener.enterBinaryExpr(binary);
39 walk(binary.getLHS());
40 listener.visitOperator(Operator.forString(binary.getOperator()));
41 walk(binary.getRHS());
42 listener.exitBinaryExpr(binary);
43 } else if (expr instanceof FilterExpr) {
44 final FilterExpr filter = (FilterExpr) expr;
45 listener.enterFilterExpr(filter);
47 listener.exitFilterExpr(filter);
48 } else if (expr instanceof FunctionCallExpr) {
49 final FunctionCallExpr func = (FunctionCallExpr) expr;
50 listener.enterFunctionCallExpr(func);
52 for (Object arg : func.getParameters()) {
56 listener.exitFunctionCallExpr(func);
57 } else if (expr instanceof LiteralExpr) {
58 listener.visitLiteralExpr((LiteralExpr) expr);
59 } else if (expr instanceof LocationPath) {
60 final LocationPath path = (LocationPath) expr;
61 final Optional<StepListener> maybeListener = listener.enterLocationPath(path);
62 if (maybeListener.isPresent()) {
63 final StepListener l = maybeListener.get();
64 for (Object step : path.getSteps()) {
65 if (step instanceof AllNodeStep) {
66 l.onAll((AllNodeStep) step);
67 } else if (step instanceof CommentNodeStep) {
68 l.onComment((CommentNodeStep) step);
69 } else if (step instanceof NameStep) {
70 l.onName((NameStep) step);
71 } else if (step instanceof ProcessingInstructionNodeStep) {
72 l.onProcessingInstruction((ProcessingInstructionNodeStep) step);
73 } else if (step instanceof TextNodeStep) {
74 l.onTest((TextNodeStep) step);
76 throw new IllegalArgumentException("Unsupported step " + step);
81 listener.exitLocationPath(path);
82 } else if (expr instanceof NumberExpr) {
83 listener.visitNumberExpr((NumberExpr) expr);
84 } else if (expr instanceof PathExpr) {
85 final PathExpr path = (PathExpr) expr;
86 listener.enterPathExpr(path);
87 walk(path.getFilterExpr());
88 walk(path.getLocationPath());
89 listener.exitPathExpr(path);
90 } else if (expr instanceof UnaryExpr) {
91 final UnaryExpr unary = (UnaryExpr) expr;
92 listener.enterNotExpr(unary);
93 walk(unary.getExpr());
94 listener.exitNotExpr(unary);
95 } else if (expr instanceof VariableReferenceExpr) {
96 listener.visitVariableReferenceExpr((VariableReferenceExpr) expr);
98 throw new IllegalArgumentException("Unsupported expression " + expr);