1 17 18 27 package org.apache.ldap.server.db; 28 29 30 import org.apache.ldap.common.filter.ExprNode; 31 import org.apache.ldap.common.filter.SubstringNode; 32 import org.apache.ldap.common.schema.AttributeType; 33 import org.apache.ldap.common.schema.Normalizer; 34 import org.apache.ldap.server.schema.AttributeTypeRegistry; 35 import org.apache.regexp.RE; 36 import org.apache.regexp.RESyntaxException; 37 38 import javax.naming.NamingEnumeration ; 39 import javax.naming.NamingException ; 40 41 42 49 public class SubstringEnumerator implements Enumerator 50 { 51 52 private final Database db; 53 54 private final SubstringEvaluator evaluator; 55 56 private final AttributeTypeRegistry attributeTypeRegistry; 57 58 59 65 public SubstringEnumerator( Database db, 66 AttributeTypeRegistry attributeTypeRegistry, 67 SubstringEvaluator evaluator ) 68 { 69 this.db = db; 70 this.evaluator = evaluator; 71 this.attributeTypeRegistry = attributeTypeRegistry; 72 } 73 74 75 79 80 84 public NamingEnumeration enumerate( final ExprNode node ) 85 throws NamingException 86 { 87 RE regex = null; 88 Index idx = null; 89 final SubstringNode snode = ( SubstringNode ) node; 90 AttributeType type = attributeTypeRegistry.lookup( snode.getAttribute() ); 91 Normalizer normalizer = type.getSubstr().getNormalizer(); 92 93 if ( db.hasUserIndexOn( snode.getAttribute() ) ) 94 { 95 99 try 100 { 101 regex = snode.getRegex( normalizer ); 102 } 103 catch ( RESyntaxException e ) 104 { 105 NamingException ne = new NamingException ( "SubstringNode '" 106 + node + "' had incorrect syntax" ); 107 ne.setRootCause( e ); 108 throw ne; 109 } 110 111 116 idx = db.getUserIndex( snode.getAttribute() ); 117 if ( null == snode.getInitial() ) 118 { 119 return idx.listIndices( regex ); 120 } 121 else 122 { 123 return idx.listIndices( regex, snode.getInitial() ); 124 } 125 } 126 127 136 NamingEnumeration underlying = db.getNdnIndex().listIndices(); 137 IndexAssertion assertion = new IndexAssertion() 138 { 139 public boolean assertCandidate( final IndexRecord record ) throws NamingException 140 { 141 return evaluator.evaluate( node, record ); 142 } 143 }; 144 145 return new IndexAssertionEnumeration( underlying, assertion ); 146 } 147 } | Popular Tags |