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.i18n.I18n; 024import org.apache.directory.api.ldap.model.constants.SchemaConstants; 025import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 026import org.apache.directory.api.util.Chars; 027import org.apache.directory.api.util.Strings; 028 029 030/** 031 * A SyntaxChecker which verifies that a value is an Integer according to RFC 4517. 032 * 033 * From RFC 4517 : 034 * 035 * <pre> 036 * Integer = ( HYPHEN LDIGIT *DIGIT ) | number 037 * 038 * From RFC 4512 : 039 * number = DIGIT | ( LDIGIT 1*DIGIT ) 040 * DIGIT = %x30 | LDIGIT ; "0"-"9" 041 * LDIGIT = %x31-39 ; "1"-"9" 042 * HYPHEN = %x2D ; hyphen ("-") 043 * </pre> 044 * 045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 046 */ 047@SuppressWarnings("serial") 048public final class IntegerSyntaxChecker extends SyntaxChecker 049{ 050 /** 051 * A static instance of IntegerSyntaxChecker 052 */ 053 public static final IntegerSyntaxChecker INSTANCE = new IntegerSyntaxChecker( SchemaConstants.INTEGER_SYNTAX ); 054 055 /** 056 * A static Builder for this class 057 */ 058 public static final class Builder extends SCBuilder<IntegerSyntaxChecker> 059 { 060 /** 061 * The Builder constructor 062 */ 063 private Builder() 064 { 065 super( SchemaConstants.INTEGER_SYNTAX ); 066 } 067 068 069 /** 070 * Create a new instance of IntegerSyntaxChecker 071 * @return A new instance of IntegerSyntaxChecker 072 */ 073 @Override 074 public IntegerSyntaxChecker build() 075 { 076 return new IntegerSyntaxChecker( oid ); 077 } 078 } 079 080 081 /** 082 * Creates a new instance of IntegerSyntaxChecker. 083 * 084 * @param oid The OID to use for this SyntaxChecker 085 */ 086 private IntegerSyntaxChecker( String oid ) 087 { 088 super( oid ); 089 } 090 091 092 /** 093 * @return An instance of the Builder for this class 094 */ 095 public static Builder builder() 096 { 097 return new Builder(); 098 } 099 100 101 /** 102 * {@inheritDoc} 103 */ 104 @Override 105 public boolean isValidSyntax( Object value ) 106 { 107 String strValue; 108 109 if ( value == null ) 110 { 111 if ( LOG.isDebugEnabled() ) 112 { 113 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) ); 114 } 115 116 return false; 117 } 118 119 if ( value instanceof String ) 120 { 121 strValue = ( String ) value; 122 } 123 else if ( value instanceof byte[] ) 124 { 125 strValue = Strings.utf8ToString( ( byte[] ) value ); 126 } 127 else 128 { 129 strValue = value.toString(); 130 } 131 132 if ( strValue.length() == 0 ) 133 { 134 if ( LOG.isDebugEnabled() ) 135 { 136 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 137 } 138 139 return false; 140 } 141 142 // The first char must be either a '-' or in [0..9]. 143 // If it's a '0', then there should be any other char after 144 int pos = 0; 145 char c = strValue.charAt( pos ); 146 147 if ( c == '-' ) 148 { 149 pos = 1; 150 } 151 else if ( !Chars.isDigit( c ) ) 152 { 153 if ( LOG.isDebugEnabled() ) 154 { 155 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 156 } 157 158 return false; 159 } 160 else if ( c == '0' ) 161 { 162 if ( strValue.length() > 1 ) 163 { 164 if ( LOG.isDebugEnabled() ) 165 { 166 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 167 } 168 169 return false; 170 } 171 else 172 { 173 if ( LOG.isDebugEnabled() ) 174 { 175 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) ); 176 } 177 178 return true; 179 } 180 } 181 182 // We must have at least a digit which is not '0' 183 if ( !Chars.isDigit( strValue, pos ) || Strings.isCharASCII( strValue, pos, '0' ) ) 184 { 185 if ( LOG.isDebugEnabled() ) 186 { 187 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 188 } 189 190 return false; 191 } 192 else 193 { 194 pos++; 195 } 196 197 while ( Chars.isDigit( strValue, pos ) ) 198 { 199 pos++; 200 } 201 202 boolean result = pos == strValue.length(); 203 204 if ( LOG.isDebugEnabled() ) 205 { 206 if ( result ) 207 { 208 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) ); 209 } 210 else 211 { 212 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 213 } 214 } 215 216 return result; 217 } 218}