View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
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  
28  
29  /**
30   * A SyntaxChecker which verifies that a value is a Numeric String according to RFC 4517.
31   * <p>
32   * From RFC 4517 :
33   * <pre>
34   * NumericString = 1*(DIGIT / SPACE)
35   * 
36   * From RFC 4512 :
37   * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
38   * LDIGIT  = %x31-39             ; "1"-"9"
39   * SPACE   = %x20                ; space (" ")
40   * </pre>
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  @SuppressWarnings("serial")
45  public final class NumericStringSyntaxChecker extends SyntaxChecker
46  {
47      /**
48       * A static instance of NumericStringSyntaxChecker
49       */
50      public static final NumericStringSyntaxChecker INSTANCE = 
51          new NumericStringSyntaxChecker( SchemaConstants.NUMERIC_STRING_SYNTAX );
52      
53      /**
54       * A static Builder for this class
55       */
56      public static final class Builder extends SCBuilder<NumericStringSyntaxChecker>
57      {
58          /**
59           * The Builder constructor
60           */
61          private Builder()
62          {
63              super( SchemaConstants.NUMERIC_STRING_SYNTAX );
64          }
65          
66          
67          /**
68           * Create a new instance of NumericStringSyntaxChecker
69           * @return A new instance of NumericStringSyntaxChecker
70           */
71          @Override
72          public NumericStringSyntaxChecker build()
73          {
74              return new NumericStringSyntaxChecker( oid );
75          }
76      }
77  
78      
79      /**
80       * Creates a new instance of NumericStringSyntaxChecker.
81       * 
82       * @param oid The OID to use for this SyntaxChecker
83       */
84      private NumericStringSyntaxChecker( String oid )
85      {
86          super( oid );
87      }
88  
89      
90      /**
91       * @return An instance of the Builder for this class
92       */
93      public static Builder builder()
94      {
95          return new Builder();
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public boolean isValidSyntax( Object value )
104     {
105         String strValue;
106 
107         if ( value == null )
108         {
109             if ( LOG.isDebugEnabled() )
110             {
111                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
112             }
113             
114             return false;
115         }
116 
117         if ( value instanceof String )
118         {
119             strValue = ( String ) value;
120         }
121         else if ( value instanceof byte[] )
122         {
123             strValue = Strings.utf8ToString( ( byte[] ) value );
124         }
125         else
126         {
127             strValue = value.toString();
128         }
129 
130         // We should have at least one char
131         if ( strValue.length() == 0 )
132         {
133             if ( LOG.isDebugEnabled() )
134             {
135                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
136             }
137             
138             return false;
139         }
140 
141         // Check that each char is either a digit or a space
142         for ( int i = 0; i < strValue.length(); i++ )
143         {
144             switch ( strValue.charAt( i ) )
145             {
146                 case '0':
147                 case '1':
148                 case '2':
149                 case '3':
150                 case '4':
151                 case '5':
152                 case '6':
153                 case '7':
154                 case '8':
155                 case '9':
156                 case ' ':
157                     continue;
158 
159                 default:
160                     if ( LOG.isDebugEnabled() )
161                     {
162                         LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
163                     }
164                     
165                     return false;
166             }
167         }
168 
169         if ( LOG.isDebugEnabled() )
170         {
171             LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
172         }
173         
174         return true;
175     }
176 }