This project has retired. For details please refer to its Attic page.
AbstractPredicateWalker xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   *
19   * Contributors:
20   *     Florent Guillaume, Nuxeo
21   */
22  package org.apache.chemistry.opencmis.server.support.query;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.antlr.runtime.tree.Tree;
28  import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
29  
30  /**
31   * Basic implementation walking a predicate in lexical order.
32   * <p>
33   * The {@code walkXYZ} methods can be overridden to change the walking order.
34   */
35  public abstract class AbstractPredicateWalker implements PredicateWalker {
36  
37      public Boolean walkPredicate(Tree node) {
38          switch (node.getType()) {
39          case CmisQlStrictLexer.NOT:
40              return walkNot(node, node.getChild(0));
41          case CmisQlStrictLexer.AND:
42              return walkAnd(node, node.getChild(0), node.getChild(1));
43          case CmisQlStrictLexer.OR:
44              return walkOr(node, node.getChild(0), node.getChild(1));
45          case CmisQlStrictLexer.EQ:
46              return walkEquals(node, node.getChild(0), node.getChild(1));
47          case CmisQlStrictLexer.NEQ:
48              return walkNotEquals(node, node.getChild(0), node.getChild(1));
49          case CmisQlStrictLexer.GT:
50              return walkGreaterThan(node, node.getChild(0), node.getChild(1));
51          case CmisQlStrictLexer.GTEQ:
52              return walkGreaterOrEquals(node, node.getChild(0), node.getChild(1));
53          case CmisQlStrictLexer.LT:
54              return walkLessThan(node, node.getChild(0), node.getChild(1));
55          case CmisQlStrictLexer.LTEQ:
56              return walkLessOrEquals(node, node.getChild(0), node.getChild(1));
57          case CmisQlStrictLexer.IN:
58              return walkIn(node, node.getChild(0), node.getChild(1));
59          case CmisQlStrictLexer.NOT_IN:
60              return walkNotIn(node, node.getChild(0), node.getChild(1));
61          case CmisQlStrictLexer.IN_ANY:
62              return walkInAny(node, node.getChild(0), node.getChild(1));
63          case CmisQlStrictLexer.NOT_IN_ANY:
64              return walkNotInAny(node, node.getChild(0), node.getChild(1));
65          case CmisQlStrictLexer.EQ_ANY:
66              return walkEqAny(node, node.getChild(0), node.getChild(1));
67          case CmisQlStrictLexer.IS_NULL:
68              return walkIsNull(node, node.getChild(0));
69          case CmisQlStrictLexer.IS_NOT_NULL:
70              return walkIsNotNull(node, node.getChild(0));
71          case CmisQlStrictLexer.LIKE:
72              return walkLike(node, node.getChild(0), node.getChild(1));
73          case CmisQlStrictLexer.NOT_LIKE:
74              return walkNotLike(node, node.getChild(0), node.getChild(1));
75          case CmisQlStrictLexer.CONTAINS:
76              if (node.getChildCount() == 1) {
77                  return walkContains(node, null, node.getChild(0));
78              } else {
79                  return walkContains(node, node.getChild(0), node.getChild(1));
80              }
81          case CmisQlStrictLexer.IN_FOLDER:
82              if (node.getChildCount() == 1) {
83                  return walkInFolder(node, null, node.getChild(0));
84              } else {
85                  return walkInFolder(node, node.getChild(0), node.getChild(1));
86              }
87          case CmisQlStrictLexer.IN_TREE:
88              if (node.getChildCount() == 1) {
89                  return walkInTree(node, null, node.getChild(0));
90              } else {
91                  return walkInTree(node, node.getChild(0), node.getChild(1));
92              }
93          default:
94              return walkOtherPredicate(node);
95          }
96      }
97  
98      /** For extensibility. */
99      protected Boolean walkOtherPredicate(Tree node) {
100         throw new CmisRuntimeException("Unknown node type: " + node.getType() + " (" + node.getText() + ")");
101     }
102 
103     public Boolean walkNot(Tree opNode, Tree node) {
104         walkPredicate(node);
105         return false;
106     }
107 
108     public Boolean walkAnd(Tree opNode, Tree leftNode, Tree rightNode) {
109         walkPredicate(leftNode);
110         walkPredicate(rightNode);
111         return false;
112     }
113 
114     public Boolean walkOr(Tree opNode, Tree leftNode, Tree rightNode) {
115         walkPredicate(leftNode);
116         walkPredicate(rightNode);
117         return false;
118     }
119 
120     public Object walkExpr(Tree node) {
121         switch (node.getType()) {
122         case CmisQlStrictLexer.BOOL_LIT:
123             return walkBoolean(node);
124         case CmisQlStrictLexer.NUM_LIT:
125             return walkNumber(node);
126         case CmisQlStrictLexer.STRING_LIT:
127             return walkString(node);
128         case CmisQlStrictLexer.TIME_LIT:
129             return walkTimestamp(node);
130         case CmisQlStrictLexer.IN_LIST:
131             return walkList(node);
132         case CmisQlStrictLexer.COL:
133             return walkCol(node);
134         case CmisQlStrictLexer.ID:
135             return walkId(node);
136         default:
137             return walkOtherExpr(node);
138         }
139     }
140 
141     public Boolean walkSearchExpr(Tree node) {
142         switch (node.getType()) {
143         case TextSearchLexer.TEXT_AND:
144             return walkTextAnd(node);
145         case TextSearchLexer.TEXT_OR:
146             return walkTextOr(node);
147         case TextSearchLexer.TEXT_MINUS:
148             return walkTextMinus(node);
149         case TextSearchLexer.TEXT_SEARCH_WORD_LIT:
150             return walkTextWord(node);
151         case TextSearchLexer.TEXT_SEARCH_PHRASE_STRING_LIT:
152             return walkTextPhrase(node);
153         default:
154             walkOtherExpr(node);
155             return null;
156         }
157     }
158 
159     /** For extensibility. */
160     protected Object walkOtherExpr(Tree node) {
161         throw new CmisRuntimeException("Unknown node type: " + node.getType() + " (" + node.getText() + ")");
162     }
163 
164     public Boolean walkEquals(Tree opNode, Tree leftNode, Tree rightNode) {
165         walkExpr(leftNode);
166         walkExpr(rightNode);
167         return false;
168     }
169 
170     public Boolean walkNotEquals(Tree opNode, Tree leftNode, Tree rightNode) {
171         walkExpr(leftNode);
172         walkExpr(rightNode);
173         return false;
174     }
175 
176     public Boolean walkGreaterThan(Tree opNode, Tree leftNode, Tree rightNode) {
177         walkExpr(leftNode);
178         walkExpr(rightNode);
179         return false;
180     }
181 
182     public Boolean walkGreaterOrEquals(Tree opNode, Tree leftNode, Tree rightNode) {
183         walkExpr(leftNode);
184         walkExpr(rightNode);
185         return false;
186     }
187 
188     public Boolean walkLessThan(Tree opNode, Tree leftNode, Tree rightNode) {
189         walkExpr(leftNode);
190         walkExpr(rightNode);
191         return false;
192     }
193 
194     public Boolean walkLessOrEquals(Tree opNode, Tree leftNode, Tree rightNode) {
195         walkExpr(leftNode);
196         walkExpr(rightNode);
197         return false;
198     }
199 
200     public Boolean walkIn(Tree opNode, Tree colNode, Tree listNode) {
201         walkExpr(colNode);
202         walkExpr(listNode);
203         return false;
204     }
205 
206     public Boolean walkNotIn(Tree opNode, Tree colNode, Tree listNode) {
207         walkExpr(colNode);
208         walkExpr(listNode);
209         return false;
210     }
211 
212     public Boolean walkInAny(Tree opNode, Tree colNode, Tree listNode) {
213         walkExpr(colNode);
214         walkExpr(listNode);
215         return false;
216     }
217 
218     public Boolean walkNotInAny(Tree opNode, Tree colNode, Tree listNode) {
219         walkExpr(colNode);
220         walkExpr(listNode);
221         return false;
222     }
223 
224     public Boolean walkEqAny(Tree opNode, Tree literalNode, Tree colNode) {
225         walkExpr(literalNode);
226         walkExpr(colNode);
227         return false;
228     }
229 
230     public Boolean walkIsNull(Tree opNode, Tree colNode) {
231         walkExpr(colNode);
232         return false;
233     }
234 
235     public Boolean walkIsNotNull(Tree opNode, Tree colNode) {
236         walkExpr(colNode);
237         return false;
238     }
239 
240     public Boolean walkLike(Tree opNode, Tree colNode, Tree stringNode) {
241         walkExpr(colNode);
242         walkExpr(stringNode);
243         return false;
244     }
245 
246     public Boolean walkNotLike(Tree opNode, Tree colNode, Tree stringNode) {
247         walkExpr(colNode);
248         walkExpr(stringNode);
249         return false;
250     }
251 
252     public Boolean walkContains(Tree opNode, Tree qualNode, Tree queryNode) {
253         if (qualNode != null) {
254             return walkSearchExpr(qualNode);
255         }
256         return walkSearchExpr(queryNode);
257     }
258 
259     public Boolean walkInFolder(Tree opNode, Tree qualNode, Tree paramNode) {
260         if (qualNode != null) {
261             walkExpr(qualNode);
262         }
263         walkExpr(paramNode);
264         return false;
265     }
266 
267     public Boolean walkInTree(Tree opNode, Tree qualNode, Tree paramNode) {
268         if (qualNode != null) {
269             walkExpr(qualNode);
270         }
271         walkExpr(paramNode);
272         return false;
273     }
274 
275     public Object walkList(Tree node) {
276         int n = node.getChildCount();
277         List<Object> res = new ArrayList<Object>(n);
278         for (int i = 0; i < n; i++) {
279             res.add(walkExpr(node.getChild(i)));
280         }
281         return res;
282     }
283 
284     public Object walkBoolean(Tree node) {
285         String s = node.getText();
286         return Boolean.valueOf(s);
287     }
288 
289     public Object walkNumber(Tree node) {
290         String s = node.getText();
291         if (s.contains(".") || s.contains("e") || s.contains("E")) {
292             return Double.valueOf(s);
293         } else {
294             return Long.valueOf(s);
295         }
296     }
297 
298     public Object walkString(Tree node) {
299         String s = node.getText();
300         s = s.substring(1, s.length() - 1);
301         s = s.replace("''", "'"); // unescape quotes
302         return s;
303     }
304 
305     public Object walkTimestamp(Tree node) {
306         String s = node.getText();
307         s = s.substring(s.indexOf('\'') + 1, s.length() - 1);
308         return CalendarHelper.fromString(s);
309     }
310 
311     public Object walkCol(Tree node) {
312         return null;
313     }
314 
315     public Object walkId(Tree node) {
316         return null;
317     }
318     
319     protected Boolean walkTextAnd(Tree node) {
320         return null;
321     }
322     
323     protected Boolean walkTextOr(Tree node) {
324         return null;
325     }
326     
327     protected Boolean walkTextMinus(Tree node) {
328         return null;
329     }
330     
331     protected Boolean walkTextWord(Tree node) {
332         return null;
333     }
334     
335     protected Boolean walkTextPhrase(Tree node) {
336         return null;
337     }
338 }