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.ldap.model.constants.SchemaConstants;
26 import org.apache.directory.api.ldap.model.schema.SchemaManager;
27 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28 import org.apache.directory.api.ldap.model.subtree.SubtreeSpecificationChecker;
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 @SuppressWarnings("serial")
42 public class SubtreeSpecificationSyntaxChecker extends SyntaxChecker
43 {
44
45 private static final Logger LOG = LoggerFactory.getLogger( SubtreeSpecificationSyntaxChecker.class );
46
47
48 private SubtreeSpecificationChecker subtreeSpecificationChecker;
49
50
51
52
53
54 public SubtreeSpecificationSyntaxChecker()
55 {
56 super( SchemaConstants.SUBTREE_SPECIFICATION_SYNTAX );
57 }
58
59
60
61
62
63 public boolean isValidSyntax( Object value )
64 {
65 String strValue = null;
66
67 if ( value == null )
68 {
69 LOG.debug( "Syntax invalid for 'null'" );
70 return false;
71 }
72
73 if ( value instanceof String )
74 {
75 strValue = ( String ) value;
76 }
77 else if ( value instanceof byte[] )
78 {
79 strValue = Strings.utf8ToString( ( byte[] ) value );
80 }
81 else
82 {
83 strValue = value.toString();
84 }
85
86 if ( strValue.length() == 0 )
87 {
88 LOG.debug( "Syntax invalid for '{}'", value );
89 return false;
90 }
91
92 try
93 {
94 synchronized ( subtreeSpecificationChecker )
95 {
96 subtreeSpecificationChecker.parse( strValue );
97 }
98
99 LOG.debug( "Syntax valid for '{}'", value );
100 return true;
101 }
102 catch ( ParseException pe )
103 {
104 LOG.debug( "Syntax invalid for '{}'", value );
105 return false;
106 }
107 }
108
109
110
111
112
113 public void setSchemaManager( SchemaManager schemaManager )
114 {
115 subtreeSpecificationChecker = new SubtreeSpecificationChecker( schemaManager );
116 }
117 }