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