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.shared.ldap.model.schema.syntaxCheckers; 021 022 023 024import org.apache.directory.shared.asn1.util.Oid; 025import org.apache.directory.shared.ldap.model.constants.SchemaConstants; 026import org.apache.directory.shared.ldap.model.schema.SyntaxChecker; 027import org.apache.directory.shared.util.Strings; 028import org.slf4j.Logger; 029import 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 */ 047@SuppressWarnings("serial") 048public class NumericOidSyntaxChecker extends SyntaxChecker 049{ 050 /** A logger for this class */ 051 private static final Logger LOG = LoggerFactory.getLogger( NumericOidSyntaxChecker.class ); 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}