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.DitContentRuleDescriptionSchemaParser;
029import org.apache.directory.api.util.Strings;
030
031
032/**
033 * A SyntaxChecker which verifies that a value follows the
034 * DIT content rule descripton syntax according to RFC 4512, par 4.2.6:
035 * 
036 * <pre>
037 * DITContentRuleDescription = 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 "AUX" SP oids ]       ; auxiliary object classes
043 *    [ SP "MUST" SP oids ]      ; attribute types
044 *    [ SP "MAY" SP oids ]       ; attribute types
045 *    [ SP "NOT" SP oids ]       ; attribute types
046 *    extensions WSP RPAREN      ; extensions
047 * </pre>
048 * 
049 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
050 */
051@SuppressWarnings("serial")
052public final class DitContentRuleDescriptionSyntaxChecker extends SyntaxChecker
053{
054    /** The schema parser used to parse the DITContentRuleDescription Syntax */
055    private transient DitContentRuleDescriptionSchemaParser schemaParser = new DitContentRuleDescriptionSchemaParser();
056    
057    /**
058     * A static instance of DitContentRuleDescriptionSyntaxChecker
059     */
060    public static final DitContentRuleDescriptionSyntaxChecker INSTANCE = 
061        new DitContentRuleDescriptionSyntaxChecker( SchemaConstants.DIT_CONTENT_RULE_SYNTAX );
062    
063    /**
064     * A static Builder for this class
065     */
066    public static final class Builder extends SCBuilder<DitContentRuleDescriptionSyntaxChecker>
067    {
068        /**
069         * The Builder constructor
070         */
071        private Builder()
072        {
073            super( SchemaConstants.DIT_CONTENT_RULE_SYNTAX );
074        }
075        
076        
077        /**
078         * Create a new instance of DitContentRuleDescriptionSyntaxChecker
079         * @return A new instance of DitContentRuleDescriptionSyntaxChecker
080         */
081        @Override
082        public DitContentRuleDescriptionSyntaxChecker build()
083        {
084            return new DitContentRuleDescriptionSyntaxChecker( oid );
085        }
086    }
087
088    
089    /**
090     * Creates a new instance of DITContentRuleDescriptionSyntaxChecker.
091     * @param oid The OID to use for this SyntaxChecker
092     */
093    private DitContentRuleDescriptionSyntaxChecker( 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.parseDITContentRuleDescription( strValue );
142
143            if ( LOG.isDebugEnabled() )
144            {
145                LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
146            }
147            
148            return true;
149        }
150        catch ( ParseException pe )
151        {
152            if ( LOG.isDebugEnabled() )
153            {
154                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
155            }
156            
157            return false;
158        }
159    }
160}