1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
35 @SuppressWarnings("serial")
36 public class RegexNormalizer extends Normalizer
37 {
38
39 private final Pattern[] regexes;
40
41
42 private final transient Matcher[] matchers;
43
44
45
46
47
48
49
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
76
77 @Override
78 public String normalize( String value )
79 {
80 return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
81 }
82
83
84
85
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
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 }