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.Chars; 027import org.apache.directory.api.util.Strings; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031 032/** 033 * A SyntaxChecker which verifies that a value is an oid according to RFC 4512. 034 * 035 * From RFC 4512 : 036 * 037 * oid = descr | numericoid 038 * descr = keystring 039 * keystring = leadkeychar *keychar 040 * leadkeychar = ALPHA 041 * keychar = ALPHA | DIGIT | HYPHEN 042 * number = DIGIT | ( LDIGIT 1*DIGIT ) 043 * ALPHA = %x41-5A | %x61-7A ; "A"-"Z" | "a"-"z" 044 * DIGIT = %x30 | LDIGIT ; "0"-"9" 045 * LDIGIT = %x31-39 ; "1"-"9" 046 * HYPHEN = %x2D ; hyphen ("-") 047 * numericoid = number 1*( DOT number ) 048 * DOT = %x2E ; period (".") 049 * 050 * 051 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 052 */ 053@SuppressWarnings("serial") 054public class OidSyntaxChecker extends SyntaxChecker 055{ 056 /** A logger for this class */ 057 private static final Logger LOG = LoggerFactory.getLogger( OidSyntaxChecker.class ); 058 059 060 /** 061 * Creates a new instance of OidSyntaxChecker. 062 */ 063 public OidSyntaxChecker() 064 { 065 super( SchemaConstants.OID_SYNTAX ); 066 } 067 068 069 /** 070 * {@inheritDoc} 071 */ 072 public boolean isValidSyntax( Object value ) 073 { 074 String strValue = null; 075 076 if ( value == null ) 077 { 078 LOG.debug( "Syntax invalid for 'null'" ); 079 return false; 080 } 081 082 if ( value instanceof String ) 083 { 084 strValue = ( String ) value; 085 } 086 else if ( value instanceof byte[] ) 087 { 088 strValue = Strings.utf8ToString( ( byte[] ) value ); 089 } 090 else 091 { 092 strValue = value.toString(); 093 } 094 095 if ( strValue.length() == 0 ) 096 { 097 LOG.debug( "Syntax invalid for '{}'", value ); 098 return false; 099 } 100 101 // if the first character is a digit it's an attempt at an OID and must be 102 // checked to make sure there are no other chars except '.' and digits. 103 if ( Chars.isDigit( strValue.charAt( 0 ) ) ) 104 { 105 if ( !Oid.isOid( strValue ) ) 106 { 107 LOG.debug( "Syntax invalid for '{}'", value ); 108 return false; 109 } 110 else 111 { 112 LOG.debug( "Syntax valid for '{}'", value ); 113 return true; 114 } 115 } 116 117 // here we just need to make sure that we have the right characters in the 118 // string and that it starts with a letter. 119 if ( Chars.isAlphaASCII( strValue, 0 ) ) 120 { 121 for ( int index = 0; index < strValue.length(); index++ ) 122 { 123 if ( !Chars.isAlphaDigitMinus( strValue, index ) ) 124 { 125 LOG.debug( "Syntax invalid for '{}'", value ); 126 return false; 127 } 128 } 129 130 LOG.debug( "Syntax valid for '{}'", value ); 131 return true; 132 } 133 else 134 { 135 LOG.debug( "Syntax invalid for '{}'", value ); 136 return false; 137 } 138 } 139}