1 17 package org.apache.ldap.server.db; 18 19 20 import org.apache.ldap.common.filter.BranchNode; 21 import org.apache.ldap.common.filter.ExprNode; 22 import org.apache.ldap.server.schema.AttributeTypeRegistry; 23 import org.apache.ldap.server.schema.OidRegistry; 24 25 import javax.naming.NamingException ; 26 import java.util.Iterator ; 27 28 29 35 public class ExpressionEvaluator implements Evaluator 36 { 37 38 private LeafEvaluator leafEvaluator; 39 40 41 45 46 52 public ExpressionEvaluator( LeafEvaluator leafEvaluator ) 53 { 54 this.leafEvaluator = leafEvaluator; 55 } 56 57 58 66 public ExpressionEvaluator( Database db, 67 OidRegistry oidRegistry, 68 AttributeTypeRegistry attributeTypeRegistry ) 69 { 70 ScopeEvaluator scopeEvaluator = null; 71 SubstringEvaluator substringEvaluator = null; 72 73 scopeEvaluator = new ScopeEvaluator( db ); 74 substringEvaluator = new SubstringEvaluator( db, oidRegistry, 75 attributeTypeRegistry ); 76 leafEvaluator = new LeafEvaluator( db, oidRegistry, 77 attributeTypeRegistry, scopeEvaluator, substringEvaluator ); 78 } 79 80 81 86 public LeafEvaluator getLeafEvaluator() 87 { 88 return leafEvaluator; 89 } 90 91 92 96 97 100 public boolean evaluate( ExprNode node, IndexRecord record ) 101 throws NamingException 102 { 103 if ( node.isLeaf() ) 104 { 105 return leafEvaluator.evaluate( node, record ); 106 } 107 108 BranchNode bnode = ( BranchNode ) node; 109 110 switch( bnode.getOperator() ) 111 { 112 case( BranchNode.OR ): 113 Iterator children = bnode.getChildren().iterator(); 114 115 while ( children.hasNext() ) 116 { 117 ExprNode child = ( ExprNode ) children.next(); 118 119 if ( evaluate( child, record ) ) 120 { 121 return true; 122 } 123 } 124 125 return false; 126 case( BranchNode.AND ): 127 children = bnode.getChildren().iterator(); 128 while ( children.hasNext() ) 129 { 130 ExprNode child = ( ExprNode ) children.next(); 131 132 if ( ! evaluate( child, record ) ) 133 { 134 return false; 135 } 136 } 137 138 return true; 139 case( BranchNode.NOT ): 140 if ( null != bnode.getChild() ) 141 { 142 return ! evaluate( bnode.getChild(), record ); 143 } 144 145 throw new NamingException ( "Negation has no child: " + node ); 146 default: 147 throw new NamingException ( "Unrecognized branch node operator: " 148 + bnode.getOperator() ); 149 } 150 } 151 } 152 | Popular Tags |