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