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.Chars;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * A SyntaxChecker which verifies that a value is a valid Java primitive int or
32   * the Integer wrapper.  Essentially this constrains the min and max values of
33   * the Integer.
34   * <p>
35   * From RFC 4517 :
36   * <pre>
37   * Integer = ( HYPHEN LDIGIT *DIGIT ) | number
38   *
39   * From RFC 4512 :
40   * number  = DIGIT | ( LDIGIT 1*DIGIT )
41   * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
42   * LDIGIT  = %x31-39             ; "1"-"9"
43   * HYPHEN  = %x2D                ; hyphen ("-")
44   * </pre>
45   *
46   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
47   */
48  @SuppressWarnings("serial")
49  public final class JavaIntegerSyntaxChecker extends SyntaxChecker
50  {
51      /**
52       * A static instance of JavaIntegerSyntaxChecker
53       */
54      public static final JavaIntegerSyntaxChecker INSTANCE = 
55          new JavaIntegerSyntaxChecker( SchemaConstants.JAVA_INT_SYNTAX );
56      
57      /**
58       * A static Builder for this class
59       */
60      public static final class Builder extends SCBuilder<JavaIntegerSyntaxChecker>
61      {
62          /**
63           * The Builder constructor
64           */
65          private Builder()
66          {
67              super( SchemaConstants.JAVA_INT_SYNTAX );
68          }
69          
70          
71          /**
72           * Create a new instance of JavaIntegerSyntaxChecker
73           * @return A new instance of JavaIntegerSyntaxChecker
74           */
75          @Override
76          public JavaIntegerSyntaxChecker build()
77          {
78              return new JavaIntegerSyntaxChecker( oid );
79          }
80      }
81  
82      
83      /**
84       * Creates a new instance of JavaIntegerSyntaxChecker.
85       * 
86       * @param oid The OID to use for this SyntaxChecker
87       */
88      private JavaIntegerSyntaxChecker( String oid )
89      {
90          super( oid );
91      }
92  
93      
94      /**
95       * @return An instance of the Builder for this class
96       */
97      public static Builder builder()
98      {
99          return new Builder();
100     }
101 
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public boolean isValidSyntax( Object value )
108     {
109         String strValue;
110 
111         if ( value == null )
112         {
113             if ( LOG.isDebugEnabled() )
114             {
115                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
116             }
117             
118             return false;
119         }
120 
121         if ( value instanceof String )
122         {
123             strValue = ( String ) value;
124         }
125         else if ( value instanceof byte[] )
126         {
127             strValue = Strings.utf8ToString( ( byte[] ) value );
128         }
129         else
130         {
131             strValue = value.toString();
132         }
133 
134         if ( strValue.length() == 0 )
135         {
136             if ( LOG.isDebugEnabled() )
137             {
138                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
139             }
140             
141             return false;
142         }
143 
144         // The first char must be either a '-' or in [0..9].
145         // If it's a '0', then there should be any other char after
146         int pos = 0;
147         char c = strValue.charAt( pos );
148 
149         if ( c == '-' )
150         {
151             pos = 1;
152         }
153         else if ( !Chars.isDigit( c ) )
154         {
155             if ( LOG.isDebugEnabled() )
156             {
157                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
158             }
159             
160             return false;
161         }
162         else if ( c == '0' )
163         {
164             boolean result = strValue.length() <= 1;
165 
166             if ( LOG.isDebugEnabled() )
167             {
168                 if ( result )
169                 {
170                     LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
171                 }
172                 else
173                 {
174                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
175                 }
176             }
177 
178             return result;
179         }
180 
181         // We must have at least a digit which is not '0'
182         if ( !Chars.isDigit( strValue, pos ) || Strings.isCharASCII( strValue, pos, '0' ) )
183         {
184             if ( LOG.isDebugEnabled() )
185             {
186                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
187             }
188             
189             return false;
190         }
191         else
192         {
193             pos++;
194         }
195 
196         while ( Chars.isDigit( strValue, pos ) )
197         {
198             pos++;
199         }
200 
201         if ( pos != strValue.length() )
202         {
203             if ( LOG.isDebugEnabled() )
204             {
205                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
206             }
207             
208             return false;
209         }
210 
211         try
212         {
213             Integer.valueOf( strValue );
214 
215             if ( LOG.isDebugEnabled() )
216             {
217                 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
218             }
219             
220             return true;
221         }
222         catch ( NumberFormatException e )
223         {
224             if ( LOG.isDebugEnabled() )
225             {
226                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
227             }
228             
229             return false;
230         }
231     }
232 }