View Javadoc

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   */
20  package org.apache.directory.server.xdbm.search.impl;
21  
22  
23  import org.apache.directory.server.schema.registries.Registries;
24  import org.apache.directory.server.xdbm.Store;
25  import org.apache.directory.server.xdbm.search.Evaluator;
26  import org.apache.directory.server.core.entry.ServerEntry;
27  import org.apache.directory.shared.ldap.filter.*;
28  import org.apache.directory.shared.ldap.NotImplementedException;
29  
30  import java.util.List;
31  import java.util.ArrayList;
32  
33  
34  /**
35   * Top level filter expression evaluator builder implemenation.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 659774 $
39   */
40  public class EvaluatorBuilder
41  {
42      private final Store<ServerEntry> db;
43      private final Registries registries;
44  
45  
46      /**
47       * Creates a top level Evaluator where leaves are delegated to a leaf node
48       * evaluator which will be created.
49       *
50       * @param db the database this evaluator operates upon
51       * @param registries the schema registries
52       * @throws Exception failure to access db or lookup schema in registries
53       */
54      public EvaluatorBuilder( Store<ServerEntry> db, Registries registries ) throws Exception
55      {
56          this.db = db;
57          this.registries = registries;
58      }
59  
60  
61      public Evaluator<? extends ExprNode, ServerEntry> build( ExprNode node ) throws Exception
62      {
63          switch ( node.getAssertionType() )
64          {
65              /* ---------- LEAF NODE HANDLING ---------- */
66  
67              case APPROXIMATE:
68                  return new ApproximateEvaluator( ( ApproximateNode ) node, db, registries );
69              case EQUALITY:
70                  return new EqualityEvaluator( ( EqualityNode ) node, db, registries );
71              case GREATEREQ:
72                  return new GreaterEqEvaluator( ( GreaterEqNode ) node, db, registries );
73              case LESSEQ:
74                  return new LessEqEvaluator( ( LessEqNode ) node, db, registries );
75              case PRESENCE:
76                  return new PresenceEvaluator( ( PresenceNode ) node, db, registries );
77              case SCOPE:
78                  if ( ( ( ScopeNode ) node ).getScope() == SearchScope.ONELEVEL )
79                  {
80                      return new OneLevelScopeEvaluator<ServerEntry>( db, ( ScopeNode ) node );
81                  }
82                  else
83                  {
84                      return new SubtreeScopeEvaluator<ServerEntry>( db, ( ScopeNode ) node );
85                  }
86              case SUBSTRING:
87                  return new SubstringEvaluator( ( SubstringNode ) node, db, registries );
88  
89              /* ---------- LOGICAL OPERATORS ---------- */
90  
91              case AND:
92                  return buildAndEvaluator( ( AndNode ) node );
93              case NOT:
94                  return new NotEvaluator( ( NotNode ) node, build( ( ( NotNode ) node).getFirstChild() ) );
95              case OR:
96                  return buildOrEvaluator( ( OrNode ) node );
97  
98              /* ----------  NOT IMPLEMENTED  ---------- */
99  
100             case ASSERTION:
101             case EXTENSIBLE:
102                 throw new NotImplementedException();
103 
104             default:
105                 throw new IllegalStateException( "Unknown assertion type: " + node.getAssertionType() );
106         }
107     }
108 
109 
110     AndEvaluator buildAndEvaluator( AndNode node ) throws Exception
111     {
112         List<ExprNode> children = node.getChildren();
113         List<Evaluator<? extends ExprNode,ServerEntry>> evaluators =
114             new ArrayList<Evaluator<? extends ExprNode,ServerEntry>>( children.size() );
115         for ( ExprNode child : children )
116         {
117             evaluators.add( build( child ) );
118         }
119         return new AndEvaluator( node, evaluators );
120     }
121 
122 
123     OrEvaluator buildOrEvaluator( OrNode node ) throws Exception
124     {
125         List<ExprNode> children = node.getChildren();
126         List<Evaluator<? extends ExprNode,ServerEntry>> evaluators =
127             new ArrayList<Evaluator<? extends ExprNode,ServerEntry>>( children.size() );
128         for ( ExprNode child : children )
129         {
130             evaluators.add( build( child ) );
131         }
132         return new OrEvaluator( node, evaluators );
133     }
134 }