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