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 * A SyntaxChecker which verifies that a value is a valid Java primitive Short or
031 * the Short wrapper.  Essentially this constrains the min and max values of
032 * the Short.
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 JavaShortSyntaxChecker extends SyntaxChecker
049{
050    /** A logger for this class */
051    private static final Logger LOG = LoggerFactory.getLogger( JavaShortSyntaxChecker.class );
052
053    /**
054     * Creates a new instance of JavaShortSyntaxChecker.
055     */
056    public JavaShortSyntaxChecker()
057    {
058        super( SchemaConstants.JAVA_SHORT_SYNTAX );
059    }
060
061
062    /**
063     * {@inheritDoc}
064     */
065    public boolean isValidSyntax( Object value )
066    {
067        String strValue = null;
068
069        if ( value == null )
070        {
071            LOG.debug( "Syntax invalid for 'null'" );
072            return false;
073        }
074
075        if ( value instanceof String )
076        {
077            strValue = ( String ) value;
078        }
079        else if ( value instanceof byte[] )
080        {
081            strValue = Strings.utf8ToString((byte[]) value);
082        }
083        else
084        {
085            strValue = value.toString();
086        }
087
088        if ( strValue.length() == 0 )
089        {
090            LOG.debug( "Syntax invalid for '{}'", value );
091            return false;
092        }
093
094        // The first char must be either a '-' or in [0..9].
095        // If it's a '0', then there should be any other char after
096        int pos = 0;
097        char c = strValue.charAt( pos );
098
099        if ( c == '-' )
100        {
101            pos = 1;
102        }
103        else if ( !Chars.isDigit(c) )
104        {
105            LOG.debug( "Syntax invalid for '{}'", value );
106            return false;
107        }
108        else if ( c == '0' )
109        {
110            if ( strValue.length() > 1 )
111            {
112                LOG.debug( "Syntax invalid for '{}'", value );
113                return false;
114            }
115            else
116            {
117                LOG.debug( "Syntax valid for '{}'", value );
118                return true;
119            }
120        }
121
122        // We must have at least a digit which is not '0'
123        if ( !Chars.isDigit(strValue, pos) )
124        {
125            LOG.debug( "Syntax invalid for '{}'", value );
126            return false;
127        }
128        else if ( Strings.isCharASCII( strValue, pos, '0' ) )
129        {
130            LOG.debug( "Syntax invalid for '{}'", value );
131            return false;
132        }
133        else
134        {
135            pos++;
136        }
137
138        while ( Chars.isDigit(strValue, pos) )
139        {
140            pos++;
141        }
142
143        if ( pos != strValue.length() )
144        {
145            LOG.debug( "Syntax invalid for '{}'", value );
146            return false;
147        }
148
149        // Should get a NumberFormatException for Byte values out of range
150        try
151        {
152            Short.valueOf( strValue );
153            LOG.debug( "Syntax valid for '{}'", value );
154            return true;
155        }
156        catch ( NumberFormatException e )
157        {
158            LOG.debug( "Syntax invalid for '{}'", value );
159            return false;
160        }
161    }
162}