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.Strings; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029 030/** 031 * A SyntaxChecker which verifies that a value is a Numeric String according to RFC 4517. 032 * 033 * From RFC 4517 : 034 * 035 * NumericString = 1*(DIGIT / SPACE) 036 * 037 * From RFC 4512 : 038 * DIGIT = %x30 | LDIGIT ; "0"-"9" 039 * LDIGIT = %x31-39 ; "1"-"9" 040 * SPACE = %x20 ; space (" ") 041 * 042 * 043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 044 */ 045@SuppressWarnings("serial") 046public class NumericStringSyntaxChecker extends SyntaxChecker 047{ 048 /** A logger for this class */ 049 private static final Logger LOG = LoggerFactory.getLogger( NumericStringSyntaxChecker.class ); 050 051 052 /** 053 * Creates a new instance of NumericStringSyntaxChecker. 054 */ 055 public NumericStringSyntaxChecker() 056 { 057 super( SchemaConstants.NUMERIC_STRING_SYNTAX ); 058 } 059 060 061 /** 062 * {@inheritDoc} 063 */ 064 public boolean isValidSyntax( Object value ) 065 { 066 String strValue = null; 067 068 if ( value == null ) 069 { 070 LOG.debug( "Syntax invalid for 'null'" ); 071 return false; 072 } 073 074 if ( value instanceof String ) 075 { 076 strValue = ( String ) value; 077 } 078 else if ( value instanceof byte[] ) 079 { 080 strValue = Strings.utf8ToString( ( byte[] ) value ); 081 } 082 else 083 { 084 strValue = value.toString(); 085 } 086 087 // We should have at least one char 088 if ( strValue.length() == 0 ) 089 { 090 LOG.debug( "Syntax invalid for '{}'", value ); 091 return false; 092 } 093 094 // Check that each char is either a digit or a space 095 for ( int i = 0; i < strValue.length(); i++ ) 096 { 097 switch ( strValue.charAt( i ) ) 098 { 099 case '0': 100 case '1': 101 case '2': 102 case '3': 103 case '4': 104 case '5': 105 case '6': 106 case '7': 107 case '8': 108 case '9': 109 case ' ': 110 continue; 111 112 default: 113 LOG.debug( "Syntax invalid for '{}'", value ); 114 return false; 115 } 116 } 117 118 LOG.debug( "Syntax valid for '{}'", value ); 119 return true; 120 } 121}