001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.schema.syntaxes;
021
022 import org.apache.directory.shared.ldap.constants.SchemaConstants;
023 import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
024 import org.apache.directory.shared.ldap.util.StringTools;
025
026
027 /**
028 * A SyntaxChecker which verifies that a value is a valid Java primitive int or
029 * the Integer wrapper. Essentially this constrains the min and max values of
030 * the Integer.
031 *
032 * From RFC 4517 :
033 *
034 * Integer = ( HYPHEN LDIGIT *DIGIT ) | number
035 *
036 * From RFC 4512 :
037 * number = DIGIT | ( LDIGIT 1*DIGIT )
038 * DIGIT = %x30 | LDIGIT ; "0"-"9"
039 * LDIGIT = %x31-39 ; "1"-"9"
040 * HYPHEN = %x2D ; hyphen ("-")
041 *
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 * @version $Rev$
045 */
046 public class JavaIntegerSyntaxChecker extends AbstractSyntaxChecker
047 {
048 /**
049 *
050 * Creates a new instance of IntegerSyntaxChecker.
051 *
052 */
053 public JavaIntegerSyntaxChecker()
054 {
055 super( SchemaConstants.JAVA_INT_SYNTAX );
056 }
057
058
059 /**
060 *
061 * Creates a new instance of IntegerSyntaxChecker.
062 *
063 * @param oid the oid to associate with this new SyntaxChecker
064 *
065 */
066 protected JavaIntegerSyntaxChecker( String oid )
067 {
068 super( oid );
069 }
070
071 /* (non-Javadoc)
072 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
073 */
074 public boolean isValidSyntax( Object value )
075 {
076 String strValue =null;
077
078 if ( value == null )
079 {
080 return false;
081 }
082
083 if ( value instanceof String )
084 {
085 strValue = ( String ) value;
086 }
087 else if ( value instanceof byte[] )
088 {
089 strValue = StringTools.utf8ToString( ( byte[] ) value );
090 }
091 else
092 {
093 strValue = value.toString();
094 }
095
096 if ( strValue.length() == 0 )
097 {
098 return false;
099 }
100
101 // The first char must be either a '-' or in [0..9].
102 // If it's a '0', then there should be any other char after
103 int pos = 0;
104 char c = strValue.charAt( pos );
105
106 if ( c == '-' )
107 {
108 pos = 1;
109 }
110 else if ( !StringTools.isDigit( c ) )
111 {
112 return false;
113 }
114 else if ( c == '0' )
115 {
116 return strValue.length() <= 1;
117 }
118
119 // We must have at least a digit which is not '0'
120 if ( !StringTools.isDigit( strValue, pos ) )
121 {
122 return false;
123 }
124 else if ( StringTools.isCharASCII( strValue, pos, '0' ) )
125 {
126 return false;
127 }
128 else
129 {
130 pos++;
131 }
132
133 while ( StringTools.isDigit( strValue, pos) )
134 {
135 pos++;
136 }
137
138 if ( pos != strValue.length() )
139 {
140 return false;
141 }
142
143 try
144 {
145 Integer.valueOf( strValue );
146 return true;
147 }
148 catch ( NumberFormatException e )
149 {
150 return false;
151 }
152 }
153 }