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