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.syntaxCheckers;
021    
022    
023    
024    import org.apache.directory.shared.asn1.primitives.OID;
025    import org.apache.directory.shared.ldap.constants.SchemaConstants;
026    import org.apache.directory.shared.ldap.schema.SyntaxChecker;
027    import org.apache.directory.shared.ldap.util.StringTools;
028    import org.slf4j.Logger;
029    import org.slf4j.LoggerFactory;
030    
031    
032    /**
033     * A SyntaxChecker which verifies that a value is a numeric oid 
034     * according to RFC 4512.
035     * 
036     * From RFC 4512 :
037     * 
038     * numericoid = number 1*( DOT number )
039     * number  = DIGIT | ( LDIGIT 1*DIGIT )
040     * DIGIT   = %x30 | LDIGIT                  ; "0"-"9"
041     * LDIGIT  = %x31-39                        ; "1"-"9"
042     * DOT     = %x2E                           ; period (".")
043    
044     *
045     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046     * @version $Rev$
047     */
048    public class NumericOidSyntaxChecker extends SyntaxChecker
049    {
050        /** A logger for this class */
051        private static final Logger LOG = LoggerFactory.getLogger( NumericOidSyntaxChecker.class );
052    
053        /** The serialVersionUID */
054        private static final long serialVersionUID = 1L;
055    
056        /**
057         * Creates a new instance of NumericOidSyntaxChecker.
058         */
059        public NumericOidSyntaxChecker()
060        {
061            super( SchemaConstants.NUMERIC_OID_SYNTAX );
062        }
063        
064        
065        /**
066         * {@inheritDoc}
067         */
068        public boolean isValidSyntax( Object value )
069        {
070            String strValue = null;
071    
072            if ( value == null )
073            {
074                LOG.debug( "Syntax invalid for '{}'", value );
075                return false;
076            }
077            
078            if ( value instanceof String )
079            {
080                strValue = ( String ) value;
081            }
082            else if ( value instanceof byte[] )
083            {
084                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
085            }
086            else
087            {
088                strValue = value.toString();
089            }
090    
091            if ( strValue.length() == 0 )
092            {
093                LOG.debug( "Syntax invalid for '{}'", value );
094                return false;
095            }
096    
097            // Just check that the value is a valid OID
098            boolean result = ( OID.isOID( strValue ) );
099            
100            if ( result )
101            {
102                LOG.debug( "Syntax valid for '{}'", value );
103            }
104            else
105            {
106                LOG.debug( "Syntax invalid for '{}'", value );
107            }
108            
109            return result;
110        }
111    }