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.Strings;
27  
28  
29  /**
30   * A SyntaxChecker which verifies that a value is a Number according to RFC 4512.
31   * <p>
32   * From RFC 4512 :
33   * <pre>
34   * number  = DIGIT | ( LDIGIT 1*DIGIT )
35   * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
36   * LDIGIT  = %x31-39             ; "1"-"9"
37   * </pre>
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  @SuppressWarnings("serial")
42  public final class NumberSyntaxChecker extends SyntaxChecker
43  {
44      /**
45       * A static instance of NumberSyntaxChecker
46       */
47      public static final NumberSyntaxChecker INSTANCE = new NumberSyntaxChecker( SchemaConstants.NUMBER_SYNTAX );
48      
49      /**
50       * A static Builder for this class
51       */
52      public static final class Builder extends SCBuilder<NumberSyntaxChecker>
53      {
54          /**
55           * The Builder constructor
56           */
57          private Builder()
58          {
59              super( SchemaConstants.NUMBER_SYNTAX );
60          }
61          
62          
63          /**
64           * Create a new instance of NumberSyntaxChecker
65           * @return A new instance of NumberSyntaxChecker
66           */
67          @Override
68          public NumberSyntaxChecker build()
69          {
70              return new NumberSyntaxChecker( oid );
71          }
72      }
73  
74      
75      /**
76       * Creates a new instance of NumberSyntaxChecker.
77       * 
78       * @param oid The OID to use for this SyntaxChecker
79       */
80      private NumberSyntaxChecker( String oid )
81      {
82          super( oid );
83      }
84  
85      
86      /**
87       * @return An instance of the Builder for this class
88       */
89      public static Builder builder()
90      {
91          return new Builder();
92      }
93  
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public boolean isValidSyntax( Object value )
100     {
101         String strValue;
102 
103         if ( value == null )
104         {
105             if ( LOG.isDebugEnabled() )
106             {
107                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
108             }
109             
110             return false;
111         }
112 
113         if ( value instanceof String )
114         {
115             strValue = ( String ) value;
116         }
117         else if ( value instanceof byte[] )
118         {
119             strValue = Strings.utf8ToString( ( byte[] ) value );
120         }
121         else
122         {
123             strValue = value.toString();
124         }
125 
126         // We should have at least one char
127         if ( strValue.length() == 0 )
128         {
129             if ( LOG.isDebugEnabled() )
130             {
131                 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
132             }
133             
134             return false;
135         }
136 
137         // Check that each char is either a digit or a space
138         for ( int i = 0; i < strValue.length(); i++ )
139         {
140             switch ( strValue.charAt( i ) )
141             {
142                 case '0':
143                 case '1':
144                 case '2':
145                 case '3':
146                 case '4':
147                 case '5':
148                 case '6':
149                 case '7':
150                 case '8':
151                 case '9':
152                     continue;
153 
154                 default:
155                     if ( LOG.isDebugEnabled() )
156                     {
157                         LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
158                     }
159                     
160                     return false;
161             }
162         }
163 
164         if ( ( strValue.charAt( 0 ) == '0' ) && strValue.length() > 1 )
165         {
166             // A number can't start with a '0' unless it's the only
167             // number
168             if ( LOG.isDebugEnabled() )
169             {
170             LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
171             }
172             
173             return false;
174         }
175 
176         if ( LOG.isDebugEnabled() )
177         {
178             LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
179         }
180         
181         return true;
182     }
183 }