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