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.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   * A SyntaxChecker which verifies that a value is a Numeric String according to RFC 4517.
32   * 
33   * From RFC 4517 :
34   * 
35   * NumericString = 1*(DIGIT / SPACE)
36   * 
37   * From RFC 4512 :
38   * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
39   * LDIGIT  = %x31-39             ; "1"-"9"
40   * SPACE   = %x20                ; space (" ")
41   * 
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  @SuppressWarnings("serial")
46  public class NumericStringSyntaxChecker extends SyntaxChecker
47  {
48      /** A logger for this class */
49      private static final Logger LOG = LoggerFactory.getLogger( NumericStringSyntaxChecker.class );
50  
51  
52      /**
53       * Creates a new instance of NumericStringSyntaxChecker.
54       */
55      public NumericStringSyntaxChecker()
56      {
57          super( SchemaConstants.NUMERIC_STRING_SYNTAX );
58      }
59  
60  
61      /**
62       * {@inheritDoc}
63       */
64      public boolean isValidSyntax( Object value )
65      {
66          String strValue = null;
67  
68          if ( value == null )
69          {
70              LOG.debug( "Syntax invalid for 'null'" );
71              return false;
72          }
73  
74          if ( value instanceof String )
75          {
76              strValue = ( String ) value;
77          }
78          else if ( value instanceof byte[] )
79          {
80              strValue = Strings.utf8ToString( ( byte[] ) value );
81          }
82          else
83          {
84              strValue = value.toString();
85          }
86  
87          // We should have at least one char
88          if ( strValue.length() == 0 )
89          {
90              LOG.debug( "Syntax invalid for '{}'", value );
91              return false;
92          }
93  
94          // Check that each char is either a digit or a space
95          for ( int i = 0; i < strValue.length(); i++ )
96          {
97              switch ( strValue.charAt( i ) )
98              {
99                  case '0':
100                 case '1':
101                 case '2':
102                 case '3':
103                 case '4':
104                 case '5':
105                 case '6':
106                 case '7':
107                 case '8':
108                 case '9':
109                 case ' ':
110                     continue;
111 
112                 default:
113                     LOG.debug( "Syntax invalid for '{}'", value );
114                     return false;
115             }
116         }
117 
118         LOG.debug( "Syntax valid for '{}'", value );
119         return true;
120     }
121 }