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.ldap.model.constants.SchemaConstants;
24 import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
25 import org.apache.directory.api.util.Strings;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34
35
36
37
38
39 @SuppressWarnings("serial")
40 public class PostalAddressSyntaxChecker extends SyntaxChecker
41 {
42
43 private static final Logger LOG = LoggerFactory.getLogger( PostalAddressSyntaxChecker.class );
44
45
46
47
48
49 public PostalAddressSyntaxChecker()
50 {
51 super( SchemaConstants.POSTAL_ADDRESS_SYNTAX );
52 }
53
54
55
56
57
58 public boolean isValidSyntax( Object value )
59 {
60 String strValue = null;
61
62 if ( value == null )
63 {
64 LOG.debug( "Syntax invalid for 'null'" );
65 return false;
66 }
67
68 if ( value instanceof String )
69 {
70 strValue = ( String ) value;
71 }
72 else if ( value instanceof byte[] )
73 {
74 strValue = Strings.utf8ToString( ( byte[] ) value );
75 }
76 else
77 {
78 strValue = value.toString();
79 }
80
81 if ( strValue.length() == 0 )
82 {
83 LOG.debug( "Syntax invalid for '{}'", value );
84 return false;
85 }
86
87
88 int dollar = strValue.indexOf( '$' );
89
90 if ( dollar == -1 )
91 {
92
93 LOG.debug( "Syntax valid for '{}'", value );
94 return true;
95 }
96
97 int pos = 0;
98 do
99 {
100
101 String address = strValue.substring( pos, dollar );
102
103 if ( Strings.isEmpty( address ) )
104 {
105 LOG.debug( "Syntax invalid for '{}'", value );
106 return false;
107 }
108
109 pos = dollar + 1;
110
111 if ( pos == strValue.length() )
112 {
113
114 LOG.debug( "Syntax invalid for '{}'", value );
115 return false;
116 }
117
118 dollar = strValue.indexOf( '$', pos );
119 }
120 while ( dollar > -1 );
121
122 return true;
123 }
124 }