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.syntaxCheckers;
21
22
23 import org.apache.directory.api.i18n.I18n;
24 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26 import org.apache.directory.api.util.Strings;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 @SuppressWarnings("serial")
41 public final class DirectoryStringSyntaxChecker extends SyntaxChecker
42 {
43
44
45
46 public static final DirectoryStringSyntaxChecker INSTANCE =
47 new DirectoryStringSyntaxChecker( SchemaConstants.DIRECTORY_STRING_SYNTAX );
48
49
50
51
52 public static final class Builder extends SCBuilder<DirectoryStringSyntaxChecker>
53 {
54
55
56
57 private Builder()
58 {
59 super( SchemaConstants.DIRECTORY_STRING_SYNTAX );
60 }
61
62
63
64
65
66
67 @Override
68 public DirectoryStringSyntaxChecker build()
69 {
70 return new DirectoryStringSyntaxChecker( oid );
71 }
72 }
73
74
75
76
77
78
79
80 private DirectoryStringSyntaxChecker( String oid )
81 {
82 super( oid );
83 }
84
85
86
87
88
89 public static Builder builder()
90 {
91 return new Builder();
92 }
93
94
95
96
97
98 @Override
99 public boolean isValidSyntax( Object value )
100 {
101 String strValue;
102
103 if ( value == null )
104 {
105 if ( LOG.isDebugEnabled() )
106 {
107 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
108 }
109
110 return false;
111 }
112
113 if ( value instanceof String )
114 {
115 strValue = ( String ) value;
116 }
117 else if ( value instanceof byte[] )
118 {
119 strValue = Strings.utf8ToString( ( byte[] ) value );
120 }
121 else
122 {
123 strValue = value.toString();
124 }
125
126
127
128
129 if ( strValue.length() == 0 )
130 {
131 if ( LOG.isDebugEnabled() )
132 {
133 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
134 }
135
136 return false;
137 }
138
139
140
141 for ( char c : strValue.toCharArray() )
142 {
143 if ( c == 0xFFFD )
144 {
145 if ( LOG.isDebugEnabled() )
146 {
147 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
148 }
149
150 return false;
151 }
152 }
153
154 if ( LOG.isDebugEnabled() )
155 {
156 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
157 }
158
159 return true;
160 }
161 }