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 @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
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
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 }