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 long or
32   * the Long 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 JavaLongSyntaxChecker extends SyntaxChecker
50  {
51      /**
52       * A static instance of JavaLongSyntaxChecker
53       */
54      public static final JavaLongSyntaxChecker INSTANCE = new JavaLongSyntaxChecker( SchemaConstants.JAVA_LONG_SYNTAX );
55      
56      /**
57       * A static Builder for this class
58       */
59      public static final class Builder extends SCBuilder<JavaLongSyntaxChecker>
60      {
61          /**
62           * The Builder constructor
63           */
64          private Builder()
65          {
66              super(  SchemaConstants.JAVA_LONG_SYNTAX );
67          }
68          
69          
70          /**
71           * Create a new instance of JavaLongSyntaxChecker
72           * @return A new instance of JavaLongSyntaxChecker
73           */
74          @Override
75          public JavaLongSyntaxChecker build()
76          {
77              return new JavaLongSyntaxChecker( oid );
78          }
79      }
80  
81      
82      /**
83       * Creates a new instance of JavaLongSyntaxChecker.
84       * 
85       * @param oid The OID to use for this SyntaxChecker
86       */
87      private JavaLongSyntaxChecker( String oid )
88      {
89          super( oid );
90      }
91  
92      
93      /**
94       * @return An instance of the Builder for this class
95       */
96      public static Builder builder()
97      {
98          return new Builder();
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public boolean isValidSyntax( Object value )
107     {
108         String strValue;
109 
110         if ( value == null )
111         {
112             if ( LOG.isDebugEnabled() )
113             {
114                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
115             }
116             
117             return false;
118         }
119 
120         if ( value instanceof String )
121         {
122             strValue = ( String ) value;
123         }
124         else if ( value instanceof byte[] )
125         {
126             strValue = Strings.utf8ToString( ( byte[] ) value );
127         }
128         else
129         {
130             strValue = value.toString();
131         }
132 
133         if ( strValue.length() == 0 )
134         {
135             if ( LOG.isDebugEnabled() )
136             {
137                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
138             }
139             
140             return false;
141         }
142 
143         // The first char must be either a '-' or in [0..9].
144         // If it's a '0', then there should be any other char after
145         int pos = 0;
146         char c = strValue.charAt( pos );
147 
148         if ( c == '-' )
149         {
150             pos = 1;
151         }
152         else if ( !Chars.isDigit( c ) )
153         {
154             if ( LOG.isDebugEnabled() )
155             {
156                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
157             }
158             
159             return false;
160         }
161         else if ( c == '0' )
162         {
163             boolean result = strValue.length() <= 1;
164             
165             if ( LOG.isDebugEnabled() )
166             {
167                 if ( result )
168                 {
169                     LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
170                 }
171                 else
172                 {
173                     LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
174                 }
175             }
176                 
177             return result;
178         }
179 
180         // We must have at least a digit which is not '0'
181         if ( !Chars.isDigit( strValue, pos ) || Strings.isCharASCII( strValue, pos, '0' ) )
182         {
183             if ( LOG.isDebugEnabled() )
184             {
185                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
186             }
187             
188             return false;
189         }
190         else
191         {
192             pos++;
193         }
194 
195         while ( Chars.isDigit( strValue, pos ) )
196         {
197             pos++;
198         }
199 
200         if ( pos != strValue.length() )
201         {
202             if ( LOG.isDebugEnabled() )
203             {
204                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
205             }
206             
207             return false;
208         }
209 
210         // Should get a NumberFormatException for Byte values out of range
211         try
212         {
213             Long.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 }