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 */
020 package org.apache.directory.shared.ldap.schema.normalizers;
021
022
023 import java.util.regex.Matcher;
024 import java.util.regex.Pattern;
025
026 import org.apache.directory.shared.ldap.entry.Value;
027 import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
028 import org.apache.directory.shared.ldap.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 * @version $Rev: 798550 $
036 */
037 public class RegexNormalizer implements Normalizer
038 {
039 // The serial UID
040 private static final long serialVersionUID = 1L;
041
042 /** the perl 5 regex engine */
043 private final Pattern[] regexes;
044
045 /** the set of regular expressions used to transform values */
046 private final Matcher[] matchers;
047
048
049 /**
050 * Creates a Perl5 regular expression based normalizer.
051 *
052 * @param regexes
053 * the set of regular expressions used to transform values
054 */
055 public RegexNormalizer( Pattern[] regexes )
056 {
057 if ( regexes != null )
058 {
059 this.regexes = new Pattern[ regexes.length ];
060 System.arraycopy( regexes, 0, this.regexes, 0, regexes.length );
061
062 matchers = new Matcher[regexes.length];
063
064 for ( int i = 0; i < regexes.length; i++ )
065 {
066 matchers[i] = regexes[i].matcher( "" );
067 }
068 }
069 else
070 {
071 this.regexes = null;
072 matchers = new Matcher[0];
073 }
074 }
075
076
077 /**
078 * {@inheritDoc}
079 */
080 public Value<?> normalize( final Value<?> value )
081 {
082 if ( value == null )
083 {
084 return null;
085 }
086
087 if ( !value.isBinary() )
088 {
089 String str = value.getString();
090
091 for ( int i = 0; i < matchers.length; i++ )
092 {
093
094 str = matchers[i].replaceAll( str );
095 }
096
097 return new ClientStringValue( str );
098 }
099
100 return value;
101 }
102
103
104
105
106 /**
107 * {@inheritDoc}
108 */
109 public String normalize( String value )
110 {
111 if ( value == null )
112 {
113 return null;
114 }
115
116 String str = value;
117
118 for ( int i = 0; i < matchers.length; i++ )
119 {
120
121 str = matchers[i].replaceAll( str );
122 }
123
124 return str;
125 }
126
127
128 /**
129 * @see java.lang.Object#toString()
130 */
131 public String toString()
132 {
133 StringBuffer buf = new StringBuffer();
134 buf.append( "RegexNormalizer( " );
135
136 for ( int i = 0; i < regexes.length; i++ )
137 {
138 buf.append( regexes[i] );
139
140 if ( i < regexes.length - 1 )
141 {
142 buf.append( ", " );
143 }
144 }
145
146 buf.append( " )" );
147 return buf.toString();
148 }
149 }