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    public Value<?> normalize( final Value<?> value )
079    {
080        if ( value == null )
081        {
082            return null;
083        }
084
085        if ( value.isHumanReadable() )
086        {
087            String str = value.getString();
088
089            for ( int i = 0; i < matchers.length; i++ )
090            {
091
092                str = matchers[i].replaceAll( str );
093            }
094
095            return new StringValue( str );
096        }
097
098        return value;
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    public String normalize( String value )
106    {
107        if ( value == null )
108        {
109            return null;
110        }
111
112        String str = value;
113
114        for ( int i = 0; i < matchers.length; i++ )
115        {
116
117            str = matchers[i].replaceAll( str );
118        }
119
120        return str;
121    }
122
123
124    /**
125     * @see java.lang.Object#toString()
126     */
127    public String toString()
128    {
129        StringBuffer buf = new StringBuffer();
130        buf.append( "RegexNormalizer( " );
131
132        for ( int i = 0; i < regexes.length; i++ )
133        {
134            buf.append( regexes[i] );
135
136            if ( i < regexes.length - 1 )
137            {
138                buf.append( ", " );
139            }
140        }
141
142        buf.append( " )" );
143        return buf.toString();
144    }
145}