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