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