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.StringValue;
027 import org.apache.directory.shared.ldap.entry.Value;
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: 928945 $
036 */
037 public class RegexNormalizer extends Normalizer
038 {
039 /** The serial UID */
040 public 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 oid The MR OID to use for this Normalizer
053 * @param regexes the set of regular expressions used to transform values
054 */
055 public RegexNormalizer( String oid, Pattern[] regexes )
056 {
057 super( oid );
058 if ( regexes != null )
059 {
060 this.regexes = new Pattern[ regexes.length ];
061 System.arraycopy( regexes, 0, this.regexes, 0, regexes.length );
062
063 matchers = new Matcher[regexes.length];
064
065 for ( int i = 0; i < regexes.length; i++ )
066 {
067 matchers[i] = regexes[i].matcher( "" );
068 }
069 }
070 else
071 {
072 this.regexes = null;
073 matchers = new Matcher[0];
074 }
075 }
076
077
078 /**
079 * {@inheritDoc}
080 */
081 public Value<?> normalize( final Value<?> value )
082 {
083 if ( value == null )
084 {
085 return null;
086 }
087
088 if ( !value.isBinary() )
089 {
090 String str = value.getString();
091
092 for ( int i = 0; i < matchers.length; i++ )
093 {
094
095 str = matchers[i].replaceAll( str );
096 }
097
098 return new StringValue( str );
099 }
100
101 return value;
102 }
103
104
105
106
107 /**
108 * {@inheritDoc}
109 */
110 public String normalize( String value )
111 {
112 if ( value == null )
113 {
114 return null;
115 }
116
117 String str = value;
118
119 for ( int i = 0; i < matchers.length; i++ )
120 {
121
122 str = matchers[i].replaceAll( str );
123 }
124
125 return str;
126 }
127
128
129 /**
130 * @see java.lang.Object#toString()
131 */
132 public String toString()
133 {
134 StringBuffer buf = new StringBuffer();
135 buf.append( "RegexNormalizer( " );
136
137 for ( int i = 0; i < regexes.length; i++ )
138 {
139 buf.append( regexes[i] );
140
141 if ( i < regexes.length - 1 )
142 {
143 buf.append( ", " );
144 }
145 }
146
147 buf.append( " )" );
148 return buf.toString();
149 }
150 }