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 * A SyntaxChecker which verifies that a value is a valid Java primitive short or
028 * the Short wrapper. Essentially this constrains the min and max values of
029 * the Integer.
030 *
031 * From RFC 4517 :
032 *
033 * Integer = ( HYPHEN LDIGIT *DIGIT ) | number
034 *
035 * From RFC 4512 :
036 * number = DIGIT | ( LDIGIT 1*DIGIT )
037 * DIGIT = %x30 | LDIGIT ; "0"-"9"
038 * LDIGIT = %x31-39 ; "1"-"9"
039 * HYPHEN = %x2D ; hyphen ("-")
040 *
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 * @version $Rev$
044 */
045 public class JavaByteSyntaxChecker extends AbstractSyntaxChecker
046 {
047 /**
048 *
049 * Creates a new instance of JavaByteSyntaxChecker.
050 *
051 */
052 public JavaByteSyntaxChecker()
053 {
054 super( SchemaConstants.JAVA_BYTE_SYNTAX );
055 }
056
057
058 /**
059 *
060 * Creates a new instance of JavaByteSyntaxChecker.
061 *
062 * @param oid the oid to associate with this new SyntaxChecker
063 *
064 */
065 protected JavaByteSyntaxChecker( String oid )
066 {
067 super( oid );
068 }
069
070 /* (non-Javadoc)
071 * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
072 */
073 public boolean isValidSyntax( Object value )
074 {
075 String strValue = null;
076
077 if ( value == null )
078 {
079 return false;
080 }
081
082 if ( value instanceof String )
083 {
084 strValue = ( String ) value;
085 }
086 else if ( value instanceof byte[] )
087 {
088 strValue = StringTools.utf8ToString( ( byte[] ) value );
089 }
090 else
091 {
092 strValue = value.toString();
093 }
094
095 if ( strValue.length() == 0 )
096 {
097 return false;
098 }
099
100 // The first char must be either a '-' or in [0..9].
101 // If it's a '0', then there should be any other char after
102 int pos = 0;
103 char c = strValue.charAt( pos );
104
105 if ( c == '-' )
106 {
107 pos = 1;
108 }
109 else if ( !StringTools.isDigit( c ) )
110 {
111 return false;
112 }
113 else if ( c == '0' )
114 {
115 if ( strValue.length() > 1 )
116 {
117 return false;
118 }
119 else
120 {
121 return true;
122 }
123 }
124
125 // We must have at least a digit which is not '0'
126 if ( !StringTools.isDigit( strValue, pos ) )
127 {
128 return false;
129 }
130 else if ( StringTools.isCharASCII( strValue, pos, '0' ) )
131 {
132 return false;
133 }
134 else
135 {
136 pos++;
137 }
138
139 while ( StringTools.isDigit( strValue, pos) )
140 {
141 pos++;
142 }
143
144 if ( pos != strValue.length() )
145 {
146 return false;
147 }
148
149 // Should get a NumberFormatException for Byte values out of range
150 try
151 {
152 Byte.valueOf( strValue );
153 return true;
154 }
155 catch ( NumberFormatException e )
156 {
157 return false;
158 }
159 }
160 }