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