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     */
020    package org.apache.directory.shared.ldap.schema.syntaxes;
021    
022    
023    import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
024    import org.apache.directory.shared.ldap.util.StringTools;
025    
026    
027    /**
028     * A SyntaxChecker which verifies that a value is a Number according to RFC 4512.
029     * 
030     * From RFC 4512 :
031     * number  = DIGIT | ( LDIGIT 1*DIGIT )
032     * DIGIT   = %x30 | LDIGIT       ; "0"-"9"
033     * LDIGIT  = %x31-39             ; "1"-"9"
034     * 
035     *
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev$
038     */
039    public class NumberSyntaxChecker extends AbstractSyntaxChecker
040    {
041        /** The Syntax OID, according to RFC 4517, par. 3.3.23 */
042        private static final String SC_OID = "1.3.6.1.4.1.18060.0.4.0.0.4";
043        
044        /**
045         * 
046         * Creates a new instance of NumberSyntaxChecker.
047         *
048         */
049        public NumberSyntaxChecker()
050        {
051            super( SC_OID );
052        }
053        
054        
055        /**
056         * 
057         * Creates a new instance of NumberSyntaxChecker.
058         * 
059         * @param oid the oid to associate with this new SyntaxChecker
060         *
061         */
062        protected NumberSyntaxChecker( String oid )
063        {
064            super( oid );
065        }
066        
067        /* (non-Javadoc)
068         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
069         */
070        public boolean isValidSyntax( Object value )
071        {
072            String strValue = null;
073    
074            if ( value == null )
075            {
076                return false;
077            }
078            
079            if ( value instanceof String )
080            {
081                strValue = ( String ) value;
082            }
083            else if ( value instanceof byte[] )
084            {
085                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
086            }
087            else
088            {
089                strValue = value.toString();
090            }
091    
092            // We should have at least one char
093            if ( strValue.length() == 0 )
094            {
095                return false;
096            }
097            
098            // Check that each char is either a digit or a space
099            for ( int i = 0; i < strValue.length(); i++ )
100            {
101                switch ( strValue.charAt( i ) )
102                {
103                    case '0': 
104                    case '1' :
105                    case '2' :
106                    case '3' :
107                    case '4' :
108                    case '5' :
109                    case '6' :
110                    case '7' :
111                    case '8' :
112                    case '9' :
113                        continue;
114                        
115                    default : 
116                        return false;
117                }
118            }
119            
120            if ( ( strValue.charAt( 0 ) == '0' ) && strValue.length() > 1 )
121            {
122                // A number can't start with a '0' unless it's the only
123                // number
124                return false;
125            }
126    
127            return true;
128        }
129    }