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