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     */
020    package org.apache.directory.shared.ldap.schema.syntaxes;
021    
022    
023    import java.text.ParseException;
024    
025    import org.apache.directory.shared.ldap.constants.SchemaConstants;
026    import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
027    import org.apache.directory.shared.ldap.schema.parsers.DITStructureRuleDescriptionSchemaParser;
028    import org.apache.directory.shared.ldap.util.StringTools;
029    
030    
031    /**
032     * A SyntaxChecker which verifies that a value follows the
033     * DIT structure rule descripton syntax according to RFC 4512, par 4.2.7.1:
034     * 
035     * <pre>
036     * DITStructureRuleDescription = LPAREN WSP
037     *   ruleid                     ; rule identifier
038     *   [ SP "NAME" SP qdescrs ]   ; short names (descriptors)
039     *   [ SP "DESC" SP qdstring ]  ; description
040     *   [ SP "OBSOLETE" ]          ; not active
041     *   SP "FORM" SP oid           ; NameForm
042     *   [ SP "SUP" ruleids ]       ; superior rules
043     *   extensions WSP RPAREN      ; extensions
044     *
045     * ruleids = ruleid / ( LPAREN WSP ruleidlist WSP RPAREN )
046     * ruleidlist = ruleid *( SP ruleid )
047     * ruleid = numbers
048     * </pre>
049     * 
050     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
051     * @version $Rev$
052     */
053    public class DITStructureRuleDescriptionSyntaxChecker extends AbstractSyntaxChecker
054    {
055        /** The schema parser used to parse the DITContentRuleDescription Syntax */
056        private DITStructureRuleDescriptionSchemaParser schemaParser = new DITStructureRuleDescriptionSchemaParser();
057    
058    
059        /**
060         * 
061         * Creates a new instance of DITContentRuleDescriptionSyntaxChecker.
062         *
063         */
064        public DITStructureRuleDescriptionSyntaxChecker()
065        {
066            super( SchemaConstants.DIT_STRUCTURE_RULE_SYNTAX );
067        }
068    
069        /**
070         * 
071         * Creates a new instance of DITStructureRuleDescriptionSyntaxChecker.
072         * 
073         * @param oid the oid to associate with this new SyntaxChecker
074         *
075         */
076        protected DITStructureRuleDescriptionSyntaxChecker( String oid )
077        {
078            super( oid );
079        }
080        
081        /* (non-Javadoc)
082         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
083         */
084        public boolean isValidSyntax( Object value )
085        {
086            String strValue = null;
087    
088            if ( value == null )
089            {
090                return false;
091            }
092    
093            if ( value instanceof String )
094            {
095                strValue = ( String ) value;
096            }
097            else if ( value instanceof byte[] )
098            {
099                strValue = StringTools.utf8ToString( ( byte[] ) value );
100            }
101            else
102            {
103                strValue = value.toString();
104            }
105    
106            try
107            {
108                schemaParser.parseDITStructureRuleDescription( strValue );
109                return true;
110            }
111            catch ( ParseException pe )
112            {
113                return false;
114            }
115        }
116    }