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 023import java.text.ParseException; 024 025import org.apache.directory.shared.ldap.model.constants.SchemaConstants; 026import org.apache.directory.shared.ldap.model.schema.SyntaxChecker; 027import org.apache.directory.shared.ldap.model.schema.parsers.AttributeTypeDescriptionSchemaParser; 028import org.apache.directory.shared.util.Strings; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032 033/** 034 * A SyntaxChecker which verifies that a value follows the 035 * attribute type descripton syntax according to RFC 4512, par 4.2.2: 036 * 037* <pre> 038 * AttributeTypeDescription = LPAREN WSP 039 * numericoid ; object identifier 040 * [ SP "NAME" SP qdescrs ] ; short names (descriptors) 041 * [ SP "DESC" SP qdstring ] ; description 042 * [ SP "OBSOLETE" ] ; not active 043 * [ SP "SUP" SP oid ] ; supertype 044 * [ SP "EQUALITY" SP oid ] ; equality matching rule 045 * [ SP "ORDERING" SP oid ] ; ordering matching rule 046 * [ SP "SUBSTR" SP oid ] ; substrings matching rule 047 * [ SP "SYNTAX" SP noidlen ] ; value syntax 048 * [ SP "SINGLE-VALUE" ] ; single-value 049 * [ SP "COLLECTIVE" ] ; collective 050 * [ SP "NO-USER-MODIFICATION" ] ; not user modifiable 051 * [ SP "USAGE" SP usage ] ; usage 052 * extensions WSP RPAREN ; extensions 053 * 054 * usage = "userApplications" / ; user 055 * "directoryOperation" / ; directory operational 056 * "distributedOperation" / ; DSA-shared operational 057 * "dSAOperation" ; DSA-specific operational 058 * 059 * extensions = *( SP xstring SP qdstrings ) 060 * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 061 * 062 * Each attribute type description must contain at least one of the SUP 063 * or SYNTAX fields. 064 * 065 * COLLECTIVE requires usage userApplications. 066 * 067 * NO-USER-MODIFICATION requires an operational usage. 068 * 069 * 070 * </pre> 071 * 072 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 073 */ 074@SuppressWarnings("serial") 075public class AttributeTypeDescriptionSyntaxChecker extends SyntaxChecker 076{ 077 /** A logger for this class */ 078 private static final Logger LOG = LoggerFactory.getLogger( AttributeTypeDescriptionSyntaxChecker.class ); 079 080 /** The schema parser used to parse the AttributeTypeDescription Syntax */ 081 private AttributeTypeDescriptionSchemaParser schemaParser = new AttributeTypeDescriptionSchemaParser(); 082 083 /** 084 * 085 * Creates a new instance of AttributeTypeDescriptionSchemaParser. 086 * 087 */ 088 public AttributeTypeDescriptionSyntaxChecker() 089 { 090 super( SchemaConstants.ATTRIBUTE_TYPE_DESCRIPTION_SYNTAX ); 091 } 092 093 /** 094 * {@inheritDoc} 095 */ 096 public boolean isValidSyntax( Object value ) 097 { 098 String strValue = null; 099 100 if ( value == null ) 101 { 102 LOG.debug( "Syntax invalid for 'null'" ); 103 return false; 104 } 105 106 if ( value instanceof String ) 107 { 108 strValue = ( String ) value; 109 } 110 else if ( value instanceof byte[] ) 111 { 112 strValue = Strings.utf8ToString((byte[]) value); 113 } 114 else 115 { 116 strValue = value.toString(); 117 } 118 119 try 120 { 121 schemaParser.parseAttributeTypeDescription( strValue ); 122 LOG.debug( "Syntax valid for '{}'", value ); 123 return true; 124 } 125 catch ( ParseException pe ) 126 { 127 LOG.debug( "Syntax invalid for '{}'", value ); 128 return false; 129 } 130 } 131}