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 java.text.ParseException; 024 025import org.apache.directory.api.i18n.I18n; 026import org.apache.directory.api.ldap.model.constants.SchemaConstants; 027import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 028import org.apache.directory.api.ldap.model.schema.parsers.AttributeTypeDescriptionSchemaParser; 029import org.apache.directory.api.util.Strings; 030 031 032/** 033 * A SyntaxChecker which verifies that a value follows the 034 * attribute type descripton syntax according to RFC 4512, par 4.2.2: 035 * 036* <pre> 037 * AttributeTypeDescription = LPAREN WSP 038 * numericoid ; object identifier 039 * [ SP "NAME" SP qdescrs ] ; short names (descriptors) 040 * [ SP "DESC" SP qdstring ] ; description 041 * [ SP "OBSOLETE" ] ; not active 042 * [ SP "SUP" SP oid ] ; supertype 043 * [ SP "EQUALITY" SP oid ] ; equality matching rule 044 * [ SP "ORDERING" SP oid ] ; ordering matching rule 045 * [ SP "SUBSTR" SP oid ] ; substrings matching rule 046 * [ SP "SYNTAX" SP noidlen ] ; value syntax 047 * [ SP "SINGLE-VALUE" ] ; single-value 048 * [ SP "COLLECTIVE" ] ; collective 049 * [ SP "NO-USER-MODIFICATION" ] ; not user modifiable 050 * [ SP "USAGE" SP usage ] ; usage 051 * extensions WSP RPAREN ; extensions 052 * 053 * usage = "userApplications" / ; user 054 * "directoryOperation" / ; directory operational 055 * "distributedOperation" / ; DSA-shared operational 056 * "dSAOperation" ; DSA-specific operational 057 * 058 * extensions = *( SP xstring SP qdstrings ) 059 * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 060 * 061 * Each attribute type description must contain at least one of the SUP 062 * or SYNTAX fields. 063 * 064 * COLLECTIVE requires usage userApplications. 065 * 066 * NO-USER-MODIFICATION requires an operational usage. 067 * </pre> 068 * 069 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 070 */ 071@SuppressWarnings("serial") 072public final class AttributeTypeDescriptionSyntaxChecker extends SyntaxChecker 073{ 074 /** The schema parser used to parse the AttributeTypeDescription Syntax */ 075 private transient AttributeTypeDescriptionSchemaParser schemaParser = new AttributeTypeDescriptionSchemaParser(); 076 077 /** 078 * A static instance of AttributeTypeDescriptionSyntaxChecker 079 */ 080 public static final AttributeTypeDescriptionSyntaxChecker INSTANCE = new AttributeTypeDescriptionSyntaxChecker( 081 SchemaConstants.ATTRIBUTE_TYPE_DESCRIPTION_SYNTAX ); 082 083 /** 084 * A static Builder for this class 085 */ 086 public static final class Builder extends SCBuilder<AttributeTypeDescriptionSyntaxChecker> 087 { 088 /** 089 * The Builder constructor 090 */ 091 private Builder() 092 { 093 super( SchemaConstants.ATTRIBUTE_TYPE_DESCRIPTION_SYNTAX ); 094 } 095 096 097 /** 098 * Create a new instance of AttributeTypeDescriptionSyntaxChecker 099 * @return A new instance of AttributeTypeDescriptionSyntaxChecker 100 */ 101 @Override 102 public AttributeTypeDescriptionSyntaxChecker build() 103 { 104 return new AttributeTypeDescriptionSyntaxChecker( oid ); 105 } 106 } 107 108 109 /** 110 * Creates a new instance of AttributeTypeDescriptionSchemaParser. 111 * 112 * @param oid The OID to use for this SyntaxChecker 113 * 114 */ 115 private AttributeTypeDescriptionSyntaxChecker( String oid ) 116 { 117 super( oid ); 118 } 119 120 121 /** 122 * @return An instance of the Builder for this class 123 */ 124 public static Builder builder() 125 { 126 return new Builder(); 127 } 128 129 130 /** 131 * {@inheritDoc} 132 */ 133 @Override 134 public boolean isValidSyntax( Object value ) 135 { 136 String strValue; 137 138 if ( value == null ) 139 { 140 if ( LOG.isDebugEnabled() ) 141 { 142 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) ); 143 } 144 145 return false; 146 } 147 148 if ( value instanceof String ) 149 { 150 strValue = ( String ) value; 151 } 152 else if ( value instanceof byte[] ) 153 { 154 strValue = Strings.utf8ToString( ( byte[] ) value ); 155 } 156 else 157 { 158 strValue = value.toString(); 159 } 160 161 try 162 { 163 schemaParser.parseAttributeTypeDescription( strValue ); 164 165 if ( LOG.isDebugEnabled() ) 166 { 167 LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) ); 168 } 169 170 return true; 171 } 172 catch ( ParseException pe ) 173 { 174 if ( LOG.isDebugEnabled() ) 175 { 176 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 177 } 178 179 return false; 180 } 181 } 182}