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 023 import org.apache.directory.shared.ldap.constants.SchemaConstants; 024 import org.apache.directory.shared.ldap.schema.SyntaxChecker; 025 import org.apache.directory.shared.ldap.util.StringTools; 026 import org.slf4j.Logger; 027 import 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 * @version $Rev$ 045 */ 046 public class NumericStringSyntaxChecker extends SyntaxChecker 047 { 048 /** A logger for this class */ 049 private static final Logger LOG = LoggerFactory.getLogger( NumericStringSyntaxChecker.class ); 050 051 /** The serialVersionUID */ 052 private static final long serialVersionUID = 1L; 053 054 /** 055 * Creates a new instance of NumericStringSyntaxChecker. 056 */ 057 public NumericStringSyntaxChecker() 058 { 059 super( SchemaConstants.NUMERIC_STRING_SYNTAX ); 060 } 061 062 063 /** 064 * {@inheritDoc} 065 */ 066 public boolean isValidSyntax( Object value ) 067 { 068 String strValue = null; 069 070 if ( value == null ) 071 { 072 LOG.debug( "Syntax invalid for '{}'", value ); 073 return false; 074 } 075 076 if ( value instanceof String ) 077 { 078 strValue = ( String ) value; 079 } 080 else if ( value instanceof byte[] ) 081 { 082 strValue = StringTools.utf8ToString( ( byte[] ) value ); 083 } 084 else 085 { 086 strValue = value.toString(); 087 } 088 089 // We should have at least one char 090 if ( strValue.length() == 0 ) 091 { 092 LOG.debug( "Syntax invalid for '{}'", value ); 093 return false; 094 } 095 096 // Check that each char is either a digit or a space 097 for ( int i = 0; i < strValue.length(); i++ ) 098 { 099 switch ( strValue.charAt( i ) ) 100 { 101 case '0': 102 case '1' : 103 case '2' : 104 case '3' : 105 case '4' : 106 case '5' : 107 case '6' : 108 case '7' : 109 case '8' : 110 case '9' : 111 case ' ' : 112 continue; 113 114 default : 115 LOG.debug( "Syntax invalid for '{}'", value ); 116 return false; 117 } 118 } 119 120 LOG.debug( "Syntax valid for '{}'", value ); 121 return true; 122 } 123 }