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      public Value<?> normalize( final Value<?> value )
79      {
80          if ( value == null )
81          {
82              return null;
83          }
84  
85          if ( value.isHumanReadable() )
86          {
87              String str = value.getString();
88  
89              for ( int i = 0; i < matchers.length; i++ )
90              {
91  
92                  str = matchers[i].replaceAll( str );
93              }
94  
95              return new StringValue( str );
96          }
97  
98          return value;
99      }
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 }