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 java.util.HashSet;
24 import java.util.Set;
25
26 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28 import org.apache.directory.api.util.Chars;
29 import org.apache.directory.api.util.Strings;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 @SuppressWarnings("serial")
48 public class DseTypeSyntaxChecker extends SyntaxChecker
49 {
50
51 private static final Logger LOG = LoggerFactory.getLogger( DseTypeSyntaxChecker.class );
52
53
54 private static final String[] DSE_BITS_STRINGS =
55 {
56 "root", "glue", "cp", "entry", "alias", "subr",
57 "nssr", "supr", "xr", "admPoint", "subentry",
58 "shadow", "zombie", "immSupr", "rhob", "sa"
59 };
60
61
62 private static final Set<String> DSE_BITS = new HashSet<String>();
63
64
65 static
66 {
67 for ( String country : DSE_BITS_STRINGS )
68 {
69 DSE_BITS.add( country );
70 }
71 }
72
73
74
75
76
77
78
79 public DseTypeSyntaxChecker()
80 {
81 super( SchemaConstants.DSE_TYPE_SYNTAX );
82 }
83
84
85
86
87
88 public boolean isValidSyntax( Object value )
89 {
90 String strValue = null;
91
92 if ( value == null )
93 {
94 LOG.debug( "Syntax invalid for 'null'" );
95 return false;
96 }
97
98 if ( value instanceof String )
99 {
100 strValue = ( String ) value;
101 }
102 else if ( value instanceof byte[] )
103 {
104 strValue = Strings.utf8ToString( ( byte[] ) value );
105 }
106 else
107 {
108 strValue = value.toString();
109 }
110
111
112 if ( strValue.length() < 4 )
113 {
114 LOG.debug( "Syntax invalid for '{}'", value );
115 return false;
116 }
117
118
119 if ( ( strValue.charAt( 0 ) != '(' )
120 || ( strValue.charAt( strValue.length() - 1 ) != ')' ) )
121 {
122 LOG.debug( "Syntax invalid for '{}'", value );
123 return false;
124 }
125
126 Set<String> keywords = new HashSet<String>();
127 int len = strValue.length() - 1;
128 boolean needKeyword = true;
129
130
131 for ( int i = 1; i < len; )
132 {
133
134 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
135 {
136 i++;
137 }
138
139 int pos = i;
140
141
142 while ( ( i < len ) && Chars.isAlphaASCII( strValue, pos ) )
143 {
144 pos++;
145 }
146
147 if ( pos == i )
148 {
149
150 LOG.debug( "Syntax invalid for '{}'", value );
151 return false;
152 }
153
154 String keyword = strValue.substring( i, pos );
155 i = pos;
156
157 if ( !DSE_BITS.contains( keyword ) )
158 {
159
160 LOG.debug( "Syntax invalid for '{}'", value );
161 return false;
162 }
163
164
165 if ( keywords.contains( keyword ) )
166 {
167 LOG.debug( "Syntax invalid for '{}'", value );
168 return false;
169 }
170
171 keywords.add( keyword );
172 needKeyword = false;
173
174
175 while ( ( i < len ) && ( strValue.charAt( i ) == ' ' ) )
176 {
177 i++;
178 }
179
180
181 if ( ( i < len ) && ( strValue.charAt( i ) == '$' ) )
182 {
183
184 i++;
185 needKeyword = true;
186 continue;
187 }
188 }
189
190
191 if ( needKeyword )
192 {
193 LOG.debug( "Syntax invalid for '{}'", value );
194 }
195 else
196 {
197 LOG.debug( "Syntax valid for '{}'", value );
198 }
199
200 return !needKeyword;
201 }
202 }