001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.server.xdbm.search.impl;
021    
022    
023    import org.apache.directory.server.schema.registries.Registries;
024    import org.apache.directory.server.xdbm.Store;
025    import org.apache.directory.server.xdbm.search.Evaluator;
026    import org.apache.directory.server.core.entry.ServerEntry;
027    import org.apache.directory.shared.ldap.filter.*;
028    import org.apache.directory.shared.ldap.NotImplementedException;
029    
030    import java.util.List;
031    import java.util.ArrayList;
032    
033    
034    /**
035     * Top level filter expression evaluator builder implemenation.
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev: 659774 $
039     */
040    public class EvaluatorBuilder
041    {
042        private final Store<ServerEntry> db;
043        private final Registries registries;
044    
045    
046        /**
047         * Creates a top level Evaluator where leaves are delegated to a leaf node
048         * evaluator which will be created.
049         *
050         * @param db the database this evaluator operates upon
051         * @param registries the schema registries
052         * @throws Exception failure to access db or lookup schema in registries
053         */
054        public EvaluatorBuilder( Store<ServerEntry> db, Registries registries ) throws Exception
055        {
056            this.db = db;
057            this.registries = registries;
058        }
059    
060    
061        public Evaluator<? extends ExprNode, ServerEntry> build( ExprNode node ) throws Exception
062        {
063            switch ( node.getAssertionType() )
064            {
065                /* ---------- LEAF NODE HANDLING ---------- */
066    
067                case APPROXIMATE:
068                    return new ApproximateEvaluator( ( ApproximateNode ) node, db, registries );
069                case EQUALITY:
070                    return new EqualityEvaluator( ( EqualityNode ) node, db, registries );
071                case GREATEREQ:
072                    return new GreaterEqEvaluator( ( GreaterEqNode ) node, db, registries );
073                case LESSEQ:
074                    return new LessEqEvaluator( ( LessEqNode ) node, db, registries );
075                case PRESENCE:
076                    return new PresenceEvaluator( ( PresenceNode ) node, db, registries );
077                case SCOPE:
078                    if ( ( ( ScopeNode ) node ).getScope() == SearchScope.ONELEVEL )
079                    {
080                        return new OneLevelScopeEvaluator<ServerEntry>( db, ( ScopeNode ) node );
081                    }
082                    else
083                    {
084                        return new SubtreeScopeEvaluator<ServerEntry>( db, ( ScopeNode ) node );
085                    }
086                case SUBSTRING:
087                    return new SubstringEvaluator( ( SubstringNode ) node, db, registries );
088    
089                /* ---------- LOGICAL OPERATORS ---------- */
090    
091                case AND:
092                    return buildAndEvaluator( ( AndNode ) node );
093                case NOT:
094                    return new NotEvaluator( ( NotNode ) node, build( ( ( NotNode ) node).getFirstChild() ) );
095                case OR:
096                    return buildOrEvaluator( ( OrNode ) node );
097    
098                /* ----------  NOT IMPLEMENTED  ---------- */
099    
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    }