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
022import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
023import org.apache.directory.shared.ldap.model.schema.SyntaxChecker;
024import org.apache.directory.shared.util.Chars;
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 valid Java primitive int or
032 * the Integer wrapper.  Essentially this constrains the min and max values of
033 * the Integer.
034 *
035 * From RFC 4517 :
036 *
037 * Integer = ( HYPHEN LDIGIT *DIGIT ) | number
038 *
039 * From RFC 4512 :
040 * number  = DIGIT | ( LDIGIT 1*DIGIT )
041 * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
042 * LDIGIT  = %x31-39             ; "1"-"9"
043 * HYPHEN  = %x2D                ; hyphen ("-")
044 *
045 *
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 */
048@SuppressWarnings("serial")
049public class JavaIntegerSyntaxChecker extends SyntaxChecker
050{
051    /** A logger for this class */
052    private static final Logger LOG = LoggerFactory.getLogger( JavaIntegerSyntaxChecker.class );
053
054    /**
055     * Creates a new instance of JavaIntegerSyntaxChecker.
056     */
057    public JavaIntegerSyntaxChecker()
058    {
059        super( SchemaConstants.JAVA_INT_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            boolean result = strValue.length() <= 1;
112            
113            if ( result )
114            {
115                LOG.debug( "Syntax valid for '{}'", value );
116            }
117            else
118            {
119                LOG.debug( "Syntax invalid for '{}'", value );
120            }
121            
122            return result;
123        }
124
125        // We must have at least a digit which is not '0'
126        if ( !Chars.isDigit(strValue, pos) )
127        {
128            LOG.debug( "Syntax invalid for '{}'", value );
129            return false;
130        }
131        else if ( Strings.isCharASCII( strValue, pos, '0' ) )
132        {
133            LOG.debug( "Syntax invalid for '{}'", value );
134            return false;
135        }
136        else
137        {
138            pos++;
139        }
140
141        while ( Chars.isDigit(strValue, pos) )
142        {
143            pos++;
144        }
145
146        if ( pos != strValue.length() )
147        {
148            LOG.debug( "Syntax invalid for '{}'", value );
149            return false;
150        }
151
152        try
153        {
154            Integer.valueOf( strValue );
155            LOG.debug( "Syntax valid for '{}'", value );
156            return true;
157        }
158        catch ( NumberFormatException e )
159        {
160            LOG.debug( "Syntax invalid for '{}'", value );
161            return false;
162        }
163    }
164}