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.core.subtree;
021
022
023 import org.apache.directory.server.i18n.I18n;
024 import org.apache.directory.shared.ldap.constants.SchemaConstants;
025 import org.apache.directory.shared.ldap.entry.EntryAttribute;
026 import org.apache.directory.shared.ldap.exception.LdapException;
027 import org.apache.directory.shared.ldap.filter.AndNode;
028 import org.apache.directory.shared.ldap.filter.BranchNode;
029 import org.apache.directory.shared.ldap.filter.ExprNode;
030 import org.apache.directory.shared.ldap.filter.NotNode;
031 import org.apache.directory.shared.ldap.filter.OrNode;
032 import org.apache.directory.shared.ldap.filter.SimpleNode;
033
034
035 /**
036 * The top level evaluation node for a refinement.
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 * @version $Rev: 927839 $
040 */
041 public class RefinementEvaluator
042 {
043 /** Leaf Evaluator flyweight use for leaf filter assertions */
044 private RefinementLeafEvaluator leafEvaluator;
045
046
047 // ------------------------------------------------------------------------
048 // C O N S T R U C T O R S
049 // ------------------------------------------------------------------------
050
051 public RefinementEvaluator(RefinementLeafEvaluator leafEvaluator)
052 {
053 this.leafEvaluator = leafEvaluator;
054 }
055
056
057 public boolean evaluate( ExprNode node, EntryAttribute objectClasses ) throws LdapException
058 {
059 if ( node == null )
060 {
061 throw new IllegalArgumentException( I18n.err( I18n.ERR_295 ) );
062 }
063
064 if ( objectClasses == null )
065 {
066 throw new IllegalArgumentException( I18n.err( I18n.ERR_296 ) );
067 }
068
069 if ( !objectClasses.instanceOf( SchemaConstants.OBJECT_CLASS_AT ) )
070 {
071 throw new IllegalArgumentException( I18n.err( I18n.ERR_297 ) );
072 }
073
074 if ( node.isLeaf() )
075 {
076 return leafEvaluator.evaluate( ( SimpleNode ) node, objectClasses );
077 }
078
079 BranchNode bnode = ( BranchNode ) node;
080
081 if ( node instanceof OrNode )
082 {
083 for ( ExprNode child:bnode.getChildren() )
084 {
085 if ( evaluate( child, objectClasses ) )
086 {
087 return true;
088 }
089 }
090
091 return false;
092 }
093 else if ( node instanceof AndNode )
094 {
095 for ( ExprNode child:bnode.getChildren() )
096 {
097 if ( !evaluate( child, objectClasses ) )
098 {
099 return false;
100 }
101 }
102
103 return true;
104
105 }
106 else if ( node instanceof NotNode )
107 {
108 if ( null != bnode.getFirstChild() )
109 {
110 return !evaluate( bnode.getFirstChild(), objectClasses );
111 }
112
113 throw new IllegalArgumentException( I18n.err( I18n.ERR_243, node ) );
114
115 }
116 else
117 {
118 throw new IllegalArgumentException( I18n.err( I18n.ERR_244, bnode ) );
119 }
120 }
121 }