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.asn1.util.Oid;
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 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 @SuppressWarnings("serial")
51 public class OidLenSyntaxChecker extends SyntaxChecker
52 {
53
54 private static final Logger LOG = LoggerFactory.getLogger( OidLenSyntaxChecker.class );
55
56
57
58
59
60
61
62 public OidLenSyntaxChecker()
63 {
64 super( SchemaConstants.OID_LEN_SYNTAX );
65 }
66
67
68
69
70
71 public boolean isValidSyntax( Object value )
72 {
73 String strValue = null;
74
75 if ( value == null )
76 {
77 LOG.debug( "Syntax invalid for 'null'" );
78 return false;
79 }
80
81 if ( value instanceof String )
82 {
83 strValue = ( String ) value;
84 }
85 else if ( value instanceof byte[] )
86 {
87 strValue = Strings.utf8ToString( ( byte[] ) value );
88 }
89 else
90 {
91 strValue = value.toString();
92 }
93
94 if ( strValue.length() == 0 )
95 {
96 LOG.debug( "Syntax invalid for '{}'", value );
97 return false;
98 }
99
100
101 int pos = strValue.indexOf( '{' );
102
103 if ( pos < 0 )
104 {
105
106 boolean result = Oid.isOid( strValue );
107
108 if ( result )
109 {
110 LOG.debug( "Syntax valid for '{}'", value );
111 }
112 else
113 {
114 LOG.debug( "Syntax invalid for '{}'", value );
115 }
116
117 return result;
118 }
119 else
120 {
121
122 String oid = strValue.substring( 0, pos );
123
124 if ( !Oid.isOid( oid ) )
125 {
126 LOG.debug( "Syntax invalid for '{}'", value );
127 return false;
128 }
129
130 String len = strValue.substring( pos );
131
132
133 if ( len.charAt( len.length() - 1 ) != '}' )
134 {
135
136 LOG.debug( "Syntax invalid for '{}'", value );
137 return false;
138 }
139
140 for ( int i = 1; i < len.length() - 1; i++ )
141 {
142 switch ( len.charAt( i ) )
143 {
144 case '0':
145 case '1':
146 case '2':
147 case '3':
148 case '4':
149 case '5':
150 case '6':
151 case '7':
152 case '8':
153 case '9':
154 break;
155
156 default:
157 LOG.debug( "Syntax invalid for '{}'", value );
158 return false;
159 }
160 }
161
162 if ( ( len.charAt( 1 ) == '0' ) && len.length() > 3 )
163 {
164
165
166 LOG.debug( "Syntax invalid for '{}'", value );
167 return false;
168 }
169
170 LOG.debug( "Syntax valid for '{}'", value );
171 return true;
172 }
173 }
174 }