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   *    http://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.entry.StringValue;
27  import org.apache.directory.api.ldap.model.entry.Value;
28  import org.apache.directory.api.ldap.model.schema.Normalizer;
29  
30  
31  /**
32   * A Normalizer that uses Perl5 based regular expressions to normalize values.
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  @SuppressWarnings("serial")
37  public class RegexNormalizer extends Normalizer
38  {
39      /** the perl 5 regex engine */
40      private final Pattern[] regexes;
41  
42      /** the set of regular expressions used to transform values */
43      private final Matcher[] matchers;
44  
45  
46      /**
47       * Creates a Perl5 regular expression based normalizer.
48       * 
49       * @param oid The MR OID to use for this Normalizer
50       * @param regexes the set of regular expressions used to transform values
51       */
52      public RegexNormalizer( String oid, Pattern[] regexes )
53      {
54          super( oid );
55          if ( regexes != null )
56          {
57              this.regexes = new Pattern[regexes.length];
58              System.arraycopy( regexes, 0, this.regexes, 0, regexes.length );
59  
60              matchers = new Matcher[regexes.length];
61  
62              for ( int i = 0; i < regexes.length; i++ )
63              {
64                  matchers[i] = regexes[i].matcher( "" );
65              }
66          }
67          else
68          {
69              this.regexes = null;
70              matchers = new Matcher[0];
71          }
72      }
73  
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public Value<?> normalize( final Value<?> value )
80      {
81          if ( value == null )
82          {
83              return null;
84          }
85  
86          if ( value.isHumanReadable() )
87          {
88              String str = value.getString();
89  
90              for ( int i = 0; i < matchers.length; i++ )
91              {
92  
93                  str = matchers[i].replaceAll( str );
94              }
95  
96              return new StringValue( str );
97          }
98  
99          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 }