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.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
33
34
35
36 @SuppressWarnings("serial")
37 public class RegexNormalizer extends Normalizer
38 {
39
40 private final Pattern[] regexes;
41
42
43 private final Matcher[] matchers;
44
45
46
47
48
49
50
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
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
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
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 }