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.NameFormDescriptionSchemaParser; 029import org.apache.directory.api.util.Strings; 030 031 032/** 033 * A SyntaxChecker which verifies that a value follows the 034 * name descripton syntax according to RFC 4512, par 4.2.7.2: 035 * 036 * <pre> 037 * NameFormDescription = 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 "OC" SP oid ; structural object class 043 * SP "MUST" SP oids ; attribute types 044 * [ SP "MAY" SP oids ] ; attribute types 045 * extensions WSP RPAREN ; extensions 046 * </pre> 047 * 048 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 049 */ 050@SuppressWarnings("serial") 051public final class NameFormDescriptionSyntaxChecker extends SyntaxChecker 052{ 053 /** The schema parser used to parse the DITContentRuleDescription Syntax */ 054 private transient NameFormDescriptionSchemaParser schemaParser = new NameFormDescriptionSchemaParser(); 055 056 /** 057 * A static instance of NameFormDescriptionSyntaxChecker 058 */ 059 public static final NameFormDescriptionSyntaxChecker INSTANCE = 060 new NameFormDescriptionSyntaxChecker( SchemaConstants.NAME_FORM_DESCRIPTION_SYNTAX ); 061 062 /** 063 * A static Builder for this class 064 */ 065 public static final class Builder extends SCBuilder<NameFormDescriptionSyntaxChecker> 066 { 067 /** 068 * The Builder constructor 069 */ 070 private Builder() 071 { 072 super( SchemaConstants.NAME_FORM_DESCRIPTION_SYNTAX ); 073 } 074 075 076 /** 077 * Create a new instance of NameFormDescriptionSyntaxChecker 078 * @return A new instance of NameFormDescriptionSyntaxChecker 079 */ 080 @Override 081 public NameFormDescriptionSyntaxChecker build() 082 { 083 return new NameFormDescriptionSyntaxChecker( oid ); 084 } 085 } 086 087 088 /** 089 * Creates a new instance of DITContentRuleDescriptionSyntaxChecker. 090 * 091 * @param oid The OID to use for this SyntaxChecker 092 */ 093 private NameFormDescriptionSyntaxChecker( String oid ) 094 { 095 super( oid ); 096 } 097 098 099 /** 100 * @return An instance of the Builder for this class 101 */ 102 public static Builder builder() 103 { 104 return new Builder(); 105 } 106 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override 112 public boolean isValidSyntax( Object value ) 113 { 114 String strValue; 115 116 if ( value == null ) 117 { 118 if ( LOG.isDebugEnabled() ) 119 { 120 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) ); 121 } 122 123 return false; 124 } 125 126 if ( value instanceof String ) 127 { 128 strValue = ( String ) value; 129 } 130 else if ( value instanceof byte[] ) 131 { 132 strValue = Strings.utf8ToString( ( byte[] ) value ); 133 } 134 else 135 { 136 strValue = value.toString(); 137 } 138 139 try 140 { 141 schemaParser.parseNameFormDescription( strValue ); 142 143 return true; 144 } 145 catch ( ParseException pe ) 146 { 147 if ( LOG.isDebugEnabled() ) 148 { 149 LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) ); 150 } 151 152 return false; 153 } 154 } 155}