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.Chars;
026import org.apache.directory.api.util.Strings;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * A SyntaxChecker which verifies that a value is an Integer according to RFC 4517.
033 * 
034 * From RFC 4517 :
035 * 
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 * 
044 *
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047@SuppressWarnings("serial")
048public class IntegerSyntaxChecker extends SyntaxChecker
049{
050    /** A logger for this class */
051    private static final Logger LOG = LoggerFactory.getLogger( IntegerSyntaxChecker.class );
052
053
054    /**
055     * Creates a new instance of IntegerSyntaxChecker.
056     */
057    public IntegerSyntaxChecker()
058    {
059        super( SchemaConstants.INTEGER_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 'null'" );
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 = Strings.utf8ToString( ( byte[] ) value );
083        }
084        else
085        {
086            strValue = value.toString();
087        }
088
089        if ( strValue.length() == 0 )
090        {
091            LOG.debug( "Syntax invalid for '{}'", value );
092            return false;
093        }
094
095        // The first char must be either a '-' or in [0..9].
096        // If it's a '0', then there should be any other char after
097        int pos = 0;
098        char c = strValue.charAt( pos );
099
100        if ( c == '-' )
101        {
102            pos = 1;
103        }
104        else if ( !Chars.isDigit( c ) )
105        {
106            LOG.debug( "Syntax invalid for '{}'", value );
107            return false;
108        }
109        else if ( c == '0' )
110        {
111            if ( strValue.length() > 1 )
112            {
113                LOG.debug( "Syntax invalid for '{}'", value );
114                return false;
115            }
116            else
117            {
118                LOG.debug( "Syntax valid for '{}'", value );
119                return true;
120            }
121        }
122
123        // We must have at least a digit which is not '0'
124        if ( !Chars.isDigit( strValue, pos ) )
125        {
126            LOG.debug( "Syntax invalid for '{}'", value );
127            return false;
128        }
129        else if ( Strings.isCharASCII( strValue, pos, '0' ) )
130        {
131            LOG.debug( "Syntax invalid for '{}'", value );
132            return false;
133        }
134        else
135        {
136            pos++;
137        }
138
139        while ( Chars.isDigit( strValue, pos ) )
140        {
141            pos++;
142        }
143
144        boolean result = ( pos == strValue.length() );
145
146        if ( result )
147        {
148            LOG.debug( "Syntax valid for '{}'", value );
149        }
150        else
151        {
152            LOG.debug( "Syntax invalid for '{}'", value );
153        }
154
155        return result;
156    }
157}