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.Strings;
027
028
029/**
030 * A SyntaxChecker which verifies that a value is a Number according to RFC 4512.
031 * <p>
032 * From RFC 4512 :
033 * <pre>
034 * number  = DIGIT | ( LDIGIT 1*DIGIT )
035 * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
036 * LDIGIT  = %x31-39             ; "1"-"9"
037 * </pre>
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041@SuppressWarnings("serial")
042public final class NumberSyntaxChecker extends SyntaxChecker
043{
044    /**
045     * A static instance of NumberSyntaxChecker
046     */
047    public static final NumberSyntaxChecker INSTANCE = new NumberSyntaxChecker( SchemaConstants.NUMBER_SYNTAX );
048    
049    /**
050     * A static Builder for this class
051     */
052    public static final class Builder extends SCBuilder<NumberSyntaxChecker>
053    {
054        /**
055         * The Builder constructor
056         */
057        private Builder()
058        {
059            super( SchemaConstants.NUMBER_SYNTAX );
060        }
061        
062        
063        /**
064         * Create a new instance of NumberSyntaxChecker
065         * @return A new instance of NumberSyntaxChecker
066         */
067        @Override
068        public NumberSyntaxChecker build()
069        {
070            return new NumberSyntaxChecker( oid );
071        }
072    }
073
074    
075    /**
076     * Creates a new instance of NumberSyntaxChecker.
077     * 
078     * @param oid The OID to use for this SyntaxChecker
079     */
080    private NumberSyntaxChecker( String oid )
081    {
082        super( oid );
083    }
084
085    
086    /**
087     * @return An instance of the Builder for this class
088     */
089    public static Builder builder()
090    {
091        return new Builder();
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public boolean isValidSyntax( Object value )
100    {
101        String strValue;
102
103        if ( value == null )
104        {
105            if ( LOG.isDebugEnabled() )
106            {
107                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
108            }
109            
110            return false;
111        }
112
113        if ( value instanceof String )
114        {
115            strValue = ( String ) value;
116        }
117        else if ( value instanceof byte[] )
118        {
119            strValue = Strings.utf8ToString( ( byte[] ) value );
120        }
121        else
122        {
123            strValue = value.toString();
124        }
125
126        // We should have at least one char
127        if ( strValue.length() == 0 )
128        {
129            if ( LOG.isDebugEnabled() )
130            {
131                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
132            }
133            
134            return false;
135        }
136
137        // Check that each char is either a digit or a space
138        for ( int i = 0; i < strValue.length(); i++ )
139        {
140            switch ( strValue.charAt( i ) )
141            {
142                case '0':
143                case '1':
144                case '2':
145                case '3':
146                case '4':
147                case '5':
148                case '6':
149                case '7':
150                case '8':
151                case '9':
152                    continue;
153
154                default:
155                    if ( LOG.isDebugEnabled() )
156                    {
157                        LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
158                    }
159                    
160                    return false;
161            }
162        }
163
164        if ( ( strValue.charAt( 0 ) == '0' ) && strValue.length() > 1 )
165        {
166            // A number can't start with a '0' unless it's the only
167            // number
168            if ( LOG.isDebugEnabled() )
169            {
170            LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
171            }
172            
173            return false;
174        }
175
176        if ( LOG.isDebugEnabled() )
177        {
178            LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
179        }
180        
181        return true;
182    }
183}