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 */ 020package org.apache.directory.api.ldap.model.schema.syntaxCheckers; 021 022 023import org.apache.directory.api.ldap.model.constants.SchemaConstants; 024import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 025import org.apache.directory.api.util.Chars; 026import org.apache.directory.api.util.Strings; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030 031/** 032 * A SyntaxChecker which verifies that a value is a valid Java primitive short or 033 * the Short wrapper. Essentially this constrains the min and max values of 034 * the Integer. 035 * 036 * From RFC 4517 : 037 * 038 * Integer = ( HYPHEN LDIGIT *DIGIT ) | number 039 * 040 * From RFC 4512 : 041 * number = DIGIT | ( LDIGIT 1*DIGIT ) 042 * DIGIT = %x30 | LDIGIT ; "0"-"9" 043 * LDIGIT = %x31-39 ; "1"-"9" 044 * HYPHEN = %x2D ; hyphen ("-") 045 * 046 * 047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 048 */ 049@SuppressWarnings("serial") 050public class JavaByteSyntaxChecker extends SyntaxChecker 051{ 052 /** A logger for this class */ 053 private static final Logger LOG = LoggerFactory.getLogger( JavaByteSyntaxChecker.class ); 054 055 056 /** 057 * Creates a new instance of JavaByteSyntaxChecker. 058 */ 059 public JavaByteSyntaxChecker() 060 { 061 super( SchemaConstants.JAVA_BYTE_SYNTAX ); 062 } 063 064 065 /** 066 * {@inheritDoc} 067 */ 068 public boolean isValidSyntax( Object value ) 069 { 070 String strValue = null; 071 072 if ( value == null ) 073 { 074 LOG.debug( "Syntax invalid for 'null'" ); 075 return false; 076 } 077 078 if ( value instanceof String ) 079 { 080 strValue = ( String ) value; 081 } 082 else if ( value instanceof byte[] ) 083 { 084 strValue = Strings.utf8ToString( ( byte[] ) value ); 085 } 086 else 087 { 088 strValue = value.toString(); 089 } 090 091 if ( strValue.length() == 0 ) 092 { 093 LOG.debug( "Syntax invalid for '{}'", value ); 094 return false; 095 } 096 097 // The first char must be either a '-' or in [0..9]. 098 // If it's a '0', then there should be any other char after 099 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 Byte.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}