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