1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.mitosis.configuration;
21
22
23 import java.beans.PropertyEditor;
24 import java.beans.PropertyEditorSupport;
25 import java.util.regex.Pattern;
26
27 import org.apache.directory.shared.ldap.util.StringTools;
28
29
30
31
32
33
34
35
36
37 public class ReplicaIdPropertyEditor extends PropertyEditorSupport
38 {
39
40 private static final Pattern REPLICA_ID_PATTERN = Pattern.compile( "[-_A-Z0-9]{1,16}" );
41
42 public ReplicaIdPropertyEditor()
43 {
44 super();
45 }
46
47
48 public ReplicaIdPropertyEditor( Object source )
49 {
50 super( source );
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public static boolean check( String id )
66 {
67 if ( StringTools.isEmpty( id ) )
68 {
69 throw new IllegalArgumentException( "Empty ID: " + id );
70 }
71
72 String tmpId = id.trim().toUpperCase();
73
74 if ( !REPLICA_ID_PATTERN.matcher( tmpId ).matches() )
75 {
76 return false;
77 }
78
79 return true;
80 }
81
82
83 public String getAsText()
84 {
85 Object val = getValue();
86
87 if ( val == null )
88 {
89 return "";
90 }
91 else
92 {
93 return val.toString();
94 }
95 }
96
97
98 public void setAsText( String text ) throws IllegalArgumentException
99 {
100 text = text.trim();
101
102 if ( text.length() == 0 )
103 {
104 setValue( null );
105 }
106 else
107 {
108 if ( check( text ) )
109 {
110 setValue( text );
111 }
112 else
113 {
114 setValue( null );
115 }
116 }
117 }
118 }