View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.normalizers;
21  
22  
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.apache.directory.api.ldap.model.schema.Normalizer;
27  import org.apache.directory.api.ldap.model.schema.PrepareString;
28  
29  
30  /**
31   * A Normalizer that uses Perl5 based regular expressions to normalize values.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  @SuppressWarnings("serial")
36  public class RegexNormalizer extends Normalizer
37  {
38      /** the perl 5 regex engine */
39      private final Pattern[] regexes;
40  
41      /** the set of regular expressions used to transform values */
42      private final transient Matcher[] matchers;
43  
44  
45      /**
46       * Creates a Perl5 regular expression based normalizer.
47       * 
48       * @param oid The MR OID to use for this Normalizer
49       * @param regexes the set of regular expressions used to transform values
50       */
51      public RegexNormalizer( String oid, Pattern[] regexes )
52      {
53          super( oid );
54          if ( regexes != null )
55          {
56              this.regexes = new Pattern[regexes.length];
57              System.arraycopy( regexes, 0, this.regexes, 0, regexes.length );
58  
59              matchers = new Matcher[regexes.length];
60  
61              for ( int i = 0; i < regexes.length; i++ )
62              {
63                  matchers[i] = regexes[i].matcher( "" );
64              }
65          }
66          else
67          {
68              this.regexes = null;
69              matchers = new Matcher[0];
70          }
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public String normalize( String value )
79      {
80          return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public String normalize( String value, PrepareString.AssertionType assertionType )
89      {
90          if ( value == null )
91          {
92              return null;
93          }
94  
95          String str = value;
96  
97          for ( int i = 0; i < matchers.length; i++ )
98          {
99              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 }