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