From: Jakub Skoczen Date: Wed, 15 Oct 2014 14:51:08 +0000 (+0200) Subject: Add classes for visitor traversal X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=6e0bb6cfb1bc2fefdbbe1b88a097e7dc29cc88f9;p=cql-java-moved-to-github.git Add classes for visitor traversal --- diff --git a/src/main/java/org/z3950/zing/cql/CQLAndNode.java b/src/main/java/org/z3950/zing/cql/CQLAndNode.java index 7e39669..6b95630 100644 --- a/src/main/java/org/z3950/zing/cql/CQLAndNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLAndNode.java @@ -15,6 +15,13 @@ public class CQLAndNode extends CQLBooleanNode { super(left, right, ms); } + @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onAndNode(this); + super.traverse(visitor); + } + + // ### Too much code duplication here with OR and NOT @Override byte[] opType1() { diff --git a/src/main/java/org/z3950/zing/cql/CQLBooleanNode.java b/src/main/java/org/z3950/zing/cql/CQLBooleanNode.java index 18c0bff..1b73ee7 100644 --- a/src/main/java/org/z3950/zing/cql/CQLBooleanNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLBooleanNode.java @@ -43,6 +43,14 @@ public abstract class CQLBooleanNode extends CQLNode { } @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onBooleanNode(this); + left.traverse(visitor); + right.traverse(visitor); + } + + + @Override void toXCQLInternal(XCQLBuilder b, int level, List prefixes, List sortkeys) { b.indent(level).append("\n"); diff --git a/src/main/java/org/z3950/zing/cql/CQLDefaultNodeVisitor.java b/src/main/java/org/z3950/zing/cql/CQLDefaultNodeVisitor.java new file mode 100644 index 0000000..d77dafb --- /dev/null +++ b/src/main/java/org/z3950/zing/cql/CQLDefaultNodeVisitor.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 1995-2014, Index Data + * All rights reserved. + * See the file LICENSE for details. + */ + +package org.z3950.zing.cql; + +/** + * Query tree visitor with default (no-op) implementation + * @author jakub + */ +public class CQLDefaultNodeVisitor implements CQLNodeVisitor { + + @Override + public void onSortNode(CQLSortNode node) { + } + + @Override + public void onPrfixNode(CQLPrefixNode node) { + } + + @Override + public void onBooleanNode(CQLBooleanNode node) { + } + + @Override + public void onProxNode(CQLProxNode node) { + } + + @Override + public void onAndNode(CQLAndNode node) { + } + + @Override + public void onOrNode(CQLOrNode node) { + } + + @Override + public void onNotNode(CQLNotNode node) { + } + + @Override + public void onTermNode(CQLTermNode node) { + } + + @Override + public void onRelation(CQLRelation relation) { + } + +} diff --git a/src/main/java/org/z3950/zing/cql/CQLNode.java b/src/main/java/org/z3950/zing/cql/CQLNode.java index a4e171d..221da44 100644 --- a/src/main/java/org/z3950/zing/cql/CQLNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLNode.java @@ -12,6 +12,8 @@ import java.util.Properties; * */ public abstract class CQLNode { + + public abstract void traverse(CQLNodeVisitor visitor); /** * Returns the name of the result-set to which this query is a diff --git a/src/main/java/org/z3950/zing/cql/CQLNodeVisitor.java b/src/main/java/org/z3950/zing/cql/CQLNodeVisitor.java new file mode 100644 index 0000000..0d120d8 --- /dev/null +++ b/src/main/java/org/z3950/zing/cql/CQLNodeVisitor.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 1995-2014, Index Data + * All rights reserved. + * See the file LICENSE for details. + */ + +package org.z3950.zing.cql; + +/** + * Allows to visit different types of nodes in the query tree. + * @author jakub + */ +public interface CQLNodeVisitor { + + public void onSortNode(CQLSortNode node); + + public void onPrfixNode(CQLPrefixNode node); + + public void onBooleanNode(CQLBooleanNode node); + + public void onProxNode(CQLProxNode node); + + public void onAndNode(CQLAndNode node); + + public void onOrNode(CQLOrNode node); + + public void onNotNode(CQLNotNode node); + + public void onTermNode(CQLTermNode node); + + public void onRelation(CQLRelation relation); + +} diff --git a/src/main/java/org/z3950/zing/cql/CQLNotNode.java b/src/main/java/org/z3950/zing/cql/CQLNotNode.java index 4f3814c..c32d427 100644 --- a/src/main/java/org/z3950/zing/cql/CQLNotNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLNotNode.java @@ -16,6 +16,12 @@ public class CQLNotNode extends CQLBooleanNode { } @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onNotNode(this); + super.traverse(visitor); + } + + @Override byte[] opType1() { byte[] op = new byte[5]; putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator diff --git a/src/main/java/org/z3950/zing/cql/CQLOrNode.java b/src/main/java/org/z3950/zing/cql/CQLOrNode.java index 92c9dfa..52fd899 100644 --- a/src/main/java/org/z3950/zing/cql/CQLOrNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLOrNode.java @@ -16,6 +16,12 @@ public class CQLOrNode extends CQLBooleanNode { } @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onOrNode(this); + super.traverse(visitor); + } + + @Override byte[] opType1() { byte[] op = new byte[5]; putTag(CONTEXT, 46, CONSTRUCTED, op, 0); // Operator diff --git a/src/main/java/org/z3950/zing/cql/CQLPrefixNode.java b/src/main/java/org/z3950/zing/cql/CQLPrefixNode.java index d3f6b74..2b23a6d 100644 --- a/src/main/java/org/z3950/zing/cql/CQLPrefixNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLPrefixNode.java @@ -41,6 +41,12 @@ public class CQLPrefixNode extends CQLNode { } @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onPrfixNode(this); + subtree.traverse(visitor); + } + + @Override void toXCQLInternal(XCQLBuilder b, int level, List prefixes, List sortkeys) { List tmp = (prefixes == null ? diff --git a/src/main/java/org/z3950/zing/cql/CQLProxNode.java b/src/main/java/org/z3950/zing/cql/CQLProxNode.java index 35f1781..c33eade 100644 --- a/src/main/java/org/z3950/zing/cql/CQLProxNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLProxNode.java @@ -18,6 +18,11 @@ public class CQLProxNode extends CQLBooleanNode { super(left, right, ms); } + @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onProxNode(this); + } + /* * proximity ::= exclusion distance ordered relation which-code unit-code. * exclusion ::= '1' | '0' | 'void'. diff --git a/src/main/java/org/z3950/zing/cql/CQLRelation.java b/src/main/java/org/z3950/zing/cql/CQLRelation.java index 716a1e9..81fd38c 100644 --- a/src/main/java/org/z3950/zing/cql/CQLRelation.java +++ b/src/main/java/org/z3950/zing/cql/CQLRelation.java @@ -42,6 +42,11 @@ public class CQLRelation extends CQLNode { } @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onRelation(this); + } + + @Override void toXCQLInternal(XCQLBuilder b, int level, List prefixes, List sortkeys) { if (sortkeys != null) diff --git a/src/main/java/org/z3950/zing/cql/CQLSortNode.java b/src/main/java/org/z3950/zing/cql/CQLSortNode.java index 96a93e8..efc190b 100644 --- a/src/main/java/org/z3950/zing/cql/CQLSortNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLSortNode.java @@ -41,6 +41,12 @@ public class CQLSortNode extends CQLNode { } @Override + public void traverse(CQLNodeVisitor visitor) { + visitor.onSortNode(this); + subtree.traverse(visitor); + } + + @Override void toXCQLInternal(XCQLBuilder b, int level, List prefixes, List sortkeys) { if (sortkeys != null) diff --git a/src/main/java/org/z3950/zing/cql/CQLTermNode.java b/src/main/java/org/z3950/zing/cql/CQLTermNode.java index a6e1772..2ad9188 100644 --- a/src/main/java/org/z3950/zing/cql/CQLTermNode.java +++ b/src/main/java/org/z3950/zing/cql/CQLTermNode.java @@ -42,6 +42,13 @@ public class CQLTermNode extends CQLNode { } @Override + public void traverse(CQLNodeVisitor visitor) { + //we visit relation first to allow filtering on relation type in the visitor + relation.traverse(visitor); + visitor.onTermNode(this); + } + + @Override public String getResultSetName() { if (isResultSetIndex(index)) return term;