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.i18n.I18n;
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
026import org.apache.directory.api.util.Chars;
027import org.apache.directory.api.util.Strings;
028
029
030/**
031 * A SyntaxChecker which verifies that a value is a valid Java primitive Short or
032 * the Short wrapper.  Essentially this constrains the min and max values of
033 * the Short.
034 * <p>
035 * From RFC 4517 :
036 * <pre>
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 * </pre>
045 *
046 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047 */
048@SuppressWarnings("serial")
049public final class JavaShortSyntaxChecker extends SyntaxChecker
050{
051    /**
052     * A static instance of JavaShortSyntaxChecker
053     */
054    public static final JavaShortSyntaxChecker INSTANCE = new JavaShortSyntaxChecker( SchemaConstants.JAVA_SHORT_SYNTAX );
055    
056    /**
057     * A static Builder for this class
058     */
059    public static final class Builder extends SCBuilder<JavaShortSyntaxChecker>
060    {
061        /**
062         * The Builder constructor
063         */
064        private Builder()
065        {
066            super( SchemaConstants.JAVA_SHORT_SYNTAX );
067        }
068        
069        
070        /**
071         * Create a new instance of JavaShortSyntaxChecker
072         * @return A new instance of JavaShortSyntaxChecker
073         */
074        @Override
075        public JavaShortSyntaxChecker build()
076        {
077            return new JavaShortSyntaxChecker( oid );
078        }
079    }
080
081    
082    /**
083     * Creates a new instance of JavaShortSyntaxChecker.
084     * 
085     * @param oid The OID to use for this SyntaxChecker
086     */
087    private JavaShortSyntaxChecker( String oid )
088    {
089        super( oid );
090    }
091
092    
093    /**
094     * @return An instance of the Builder for this class
095     */
096    public static Builder builder()
097    {
098        return new Builder();
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public boolean isValidSyntax( Object value )
107    {
108        String strValue;
109
110        if ( value == null )
111        {
112            if ( LOG.isDebugEnabled() )
113            {
114                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
115            }
116            
117            return false;
118        }
119
120        if ( value instanceof String )
121        {
122            strValue = ( String ) value;
123        }
124        else if ( value instanceof byte[] )
125        {
126            strValue = Strings.utf8ToString( ( byte[] ) value );
127        }
128        else
129        {
130            strValue = value.toString();
131        }
132
133        if ( strValue.length() == 0 )
134        {
135            if ( LOG.isDebugEnabled() )
136            {
137                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
138            }
139            
140            return false;
141        }
142
143        // The first char must be either a '-' or in [0..9].
144        // If it's a '0', then there should be any other char after
145        int pos = 0;
146        char c = strValue.charAt( pos );
147
148        if ( c == '-' )
149        {
150            pos = 1;
151        }
152        else if ( !Chars.isDigit( c ) )
153        {
154            if ( LOG.isDebugEnabled() )
155            {
156                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
157            }
158            
159            return false;
160        }
161        else if ( c == '0' )
162        {
163            boolean result = strValue.length() <= 1;
164            
165            if ( LOG.isDebugEnabled() )
166            {
167                if ( result )
168                {
169                    LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
170                    return false;
171                }
172                else
173                {
174                    LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
175                    return true;
176                }
177            }
178            
179            return result;
180        }
181
182        // We must have at least a digit which is not '0'
183        if ( !Chars.isDigit( strValue, pos ) || Strings.isCharASCII( strValue, pos, '0' ) )
184        {
185            if ( LOG.isDebugEnabled() )
186            {
187                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
188            }
189            
190            return false;
191        }
192        else
193        {
194            pos++;
195        }
196
197        while ( Chars.isDigit( strValue, pos ) )
198        {
199            pos++;
200        }
201
202        if ( pos != strValue.length() )
203        {
204            if ( LOG.isDebugEnabled() )
205            {
206                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
207            }
208            
209            return false;
210        }
211
212        // Should get a NumberFormatException for Byte values out of range
213        try
214        {
215            Short.valueOf( strValue );
216
217            if ( LOG.isDebugEnabled() )
218            {
219                LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
220            }
221            
222            return true;
223        }
224        catch ( NumberFormatException e )
225        {
226            if ( LOG.isDebugEnabled() )
227            {
228                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
229            }
230            
231            return false;
232        }
233    }
234}