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