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 */ 020package org.apache.directory.api.ldap.model.schema.normalizers; 021 022 023import java.util.regex.Matcher; 024import java.util.regex.Pattern; 025 026import org.apache.directory.api.ldap.model.schema.Normalizer; 027import org.apache.directory.api.ldap.model.schema.PrepareString; 028 029 030/** 031 * A Normalizer that uses Perl5 based regular expressions to normalize values. 032 * 033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 034 */ 035@SuppressWarnings("serial") 036public class RegexNormalizer extends Normalizer 037{ 038 /** the perl 5 regex engine */ 039 private final Pattern[] regexes; 040 041 /** the set of regular expressions used to transform values */ 042 private final transient Matcher[] matchers; 043 044 045 /** 046 * Creates a Perl5 regular expression based normalizer. 047 * 048 * @param oid The MR OID to use for this Normalizer 049 * @param regexes the set of regular expressions used to transform values 050 */ 051 public RegexNormalizer( String oid, Pattern[] regexes ) 052 { 053 super( oid ); 054 if ( regexes != null ) 055 { 056 this.regexes = new Pattern[regexes.length]; 057 System.arraycopy( regexes, 0, this.regexes, 0, regexes.length ); 058 059 matchers = new Matcher[regexes.length]; 060 061 for ( int i = 0; i < regexes.length; i++ ) 062 { 063 matchers[i] = regexes[i].matcher( "" ); 064 } 065 } 066 else 067 { 068 this.regexes = null; 069 matchers = new Matcher[0]; 070 } 071 } 072 073 074 /** 075 * {@inheritDoc} 076 */ 077 @Override 078 public String normalize( String value ) 079 { 080 return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE ); 081 } 082 083 084 /** 085 * {@inheritDoc} 086 */ 087 @Override 088 public String normalize( String value, PrepareString.AssertionType assertionType ) 089 { 090 if ( value == null ) 091 { 092 return null; 093 } 094 095 String str = value; 096 097 for ( int i = 0; i < matchers.length; i++ ) 098 { 099 str = matchers[i].replaceAll( str ); 100 } 101 102 return str; 103 } 104 105 106 /** 107 * @see java.lang.Object#toString() 108 */ 109 @Override 110 public String toString() 111 { 112 StringBuilder buf = new StringBuilder(); 113 buf.append( "RegexNormalizer( " ); 114 115 for ( int i = 0; i < regexes.length; i++ ) 116 { 117 buf.append( regexes[i] ); 118 119 if ( i < regexes.length - 1 ) 120 { 121 buf.append( ", " ); 122 } 123 } 124 125 buf.append( " )" ); 126 return buf.toString(); 127 } 128}