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.Chars;
26  import org.apache.directory.api.util.Strings;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A SyntaxChecker which verifies that a value is an Integer according to RFC 4517.
33   * 
34   * From RFC 4517 :
35   * 
36   * Integer = ( HYPHEN LDIGIT *DIGIT ) | number
37   * 
38   * From RFC 4512 :
39   * number  = DIGIT | ( LDIGIT 1*DIGIT )
40   * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
41   * LDIGIT  = %x31-39             ; "1"-"9"
42   * HYPHEN  = %x2D                ; hyphen ("-")
43   * 
44   *
45   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
46   */
47  @SuppressWarnings("serial")
48  public class IntegerSyntaxChecker extends SyntaxChecker
49  {
50      /** A logger for this class */
51      private static final Logger LOG = LoggerFactory.getLogger( IntegerSyntaxChecker.class );
52  
53  
54      /**
55       * Creates a new instance of IntegerSyntaxChecker.
56       */
57      public IntegerSyntaxChecker()
58      {
59          super( SchemaConstants.INTEGER_SYNTAX );
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      public boolean isValidSyntax( Object value )
67      {
68          String strValue = null;
69  
70          if ( value == null )
71          {
72              LOG.debug( "Syntax invalid for 'null'" );
73              return false;
74          }
75  
76          if ( value instanceof String )
77          {
78              strValue = ( String ) value;
79          }
80          else if ( value instanceof byte[] )
81          {
82              strValue = Strings.utf8ToString( ( byte[] ) value );
83          }
84          else
85          {
86              strValue = value.toString();
87          }
88  
89          if ( strValue.length() == 0 )
90          {
91              LOG.debug( "Syntax invalid for '{}'", value );
92              return false;
93          }
94  
95          // The first char must be either a '-' or in [0..9].
96          // If it's a '0', then there should be any other char after
97          int pos = 0;
98          char c = strValue.charAt( pos );
99  
100         if ( c == '-' )
101         {
102             pos = 1;
103         }
104         else if ( !Chars.isDigit( c ) )
105         {
106             LOG.debug( "Syntax invalid for '{}'", value );
107             return false;
108         }
109         else if ( c == '0' )
110         {
111             if ( strValue.length() > 1 )
112             {
113                 LOG.debug( "Syntax invalid for '{}'", value );
114                 return false;
115             }
116             else
117             {
118                 LOG.debug( "Syntax valid for '{}'", value );
119                 return true;
120             }
121         }
122 
123         // We must have at least a digit which is not '0'
124         if ( !Chars.isDigit( strValue, pos ) )
125         {
126             LOG.debug( "Syntax invalid for '{}'", value );
127             return false;
128         }
129         else if ( Strings.isCharASCII( strValue, pos, '0' ) )
130         {
131             LOG.debug( "Syntax invalid for '{}'", value );
132             return false;
133         }
134         else
135         {
136             pos++;
137         }
138 
139         while ( Chars.isDigit( strValue, pos ) )
140         {
141             pos++;
142         }
143 
144         boolean result = ( pos == strValue.length() );
145 
146         if ( result )
147         {
148             LOG.debug( "Syntax valid for '{}'", value );
149         }
150         else
151         {
152             LOG.debug( "Syntax invalid for '{}'", value );
153         }
154 
155         return result;
156     }
157 }