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