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