1 17 package org.apache.ldap.server.db; 18 19 20 import org.apache.ldap.common.filter.ExprNode; 21 import org.apache.ldap.common.filter.SubstringNode; 22 import org.apache.ldap.common.schema.AttributeType; 23 import org.apache.ldap.common.schema.Normalizer; 24 import org.apache.ldap.server.schema.AttributeTypeRegistry; 25 import org.apache.ldap.server.schema.OidRegistry; 26 import org.apache.regexp.RE; 27 import org.apache.regexp.RESyntaxException; 28 29 import javax.naming.NamingEnumeration ; 30 import javax.naming.NamingException ; 31 import javax.naming.directory.Attribute ; 32 import javax.naming.directory.Attributes ; 33 34 35 41 public class SubstringEvaluator implements Evaluator 42 { 43 44 private Database db; 45 46 private OidRegistry oidRegistry; 47 48 private AttributeTypeRegistry attributeTypeRegistry; 49 50 51 58 public SubstringEvaluator( Database db, OidRegistry oidRegistry, 59 AttributeTypeRegistry attributeTypeRegistry ) 60 { 61 this.db = db; 62 this.oidRegistry = oidRegistry; 63 this.attributeTypeRegistry = attributeTypeRegistry; 64 } 65 66 67 70 public boolean evaluate( ExprNode node, IndexRecord record ) 71 throws NamingException 72 { 73 RE regex = null; 74 SubstringNode snode = ( SubstringNode ) node; 75 String oid = oidRegistry.getOid( snode.getAttribute() ); 76 AttributeType type = attributeTypeRegistry.lookup( oid ); 77 Normalizer normalizer = type.getSubstr().getNormalizer(); 78 79 if ( db.hasUserIndexOn( snode.getAttribute() ) ) 80 { 81 Index idx = db.getUserIndex( snode.getAttribute() ); 82 83 89 90 NamingEnumeration list = idx.listReverseIndices( record.getEntryId() ); 91 92 try 94 { 95 regex = snode.getRegex( normalizer ); 96 } 97 catch ( RESyntaxException e ) 98 { 99 NamingException ne = new NamingException ( "SubstringNode '" 100 + node + "' had " + "incorrect syntax" ); 101 ne.setRootCause( e ); 102 throw ne; 103 } 104 105 while ( list.hasMore() ) 107 { 108 IndexRecord rec = ( IndexRecord ) list.next(); 109 110 if ( regex.match( ( String ) rec.getIndexKey() ) ) 112 { 113 list.close(); 114 return true; 115 } 116 } 117 118 return false; 120 } 121 122 126 if ( null == record.getAttributes() ) 128 { 129 Attributes attrs = db.lookup( record.getEntryId() ); 130 record.setAttributes( attrs ); 131 } 132 133 Attribute attr = record.getAttributes().get( snode.getAttribute() ); 135 136 if ( null == attr ) 138 { 139 return false; 140 } 141 142 try 144 { 145 regex = snode.getRegex( normalizer ); 146 } 147 catch ( RESyntaxException e ) 148 { 149 NamingException ne = new NamingException ( "SubstringNode '" 150 + node + "' had " + "incorrect syntax" ); 151 ne.setRootCause( e ); 152 throw ne; 153 } 154 155 161 NamingEnumeration list = attr.getAll(); 162 while ( list.hasMore() ) 163 { 164 String value = ( String ) 165 normalizer.normalize( list.next() ); 166 167 if ( regex.match( value ) ) 169 { 170 list.close(); 171 return true; 172 } 173 } 174 175 return false; 177 } 178 } 179 | Popular Tags |