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.parsers;
021    
022    
023    import java.text.ParseException;
024    
025    import org.apache.directory.shared.i18n.I18n;
026    import org.apache.directory.shared.ldap.schema.NameForm;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    import antlr.RecognitionException;
031    import antlr.TokenStreamException;
032    
033    
034    /**
035     * A parser for RFC 4512 name form descriptions
036     * 
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$, $Date$
039     */
040    public class NameFormDescriptionSchemaParser extends AbstractSchemaParser
041    {
042        /** The LoggerFactory used by this class */
043        protected static final Logger LOG = LoggerFactory.getLogger( NameFormDescriptionSchemaParser.class );
044    
045        /**
046         * Creates a schema parser instance.
047         */
048        public NameFormDescriptionSchemaParser()
049        {
050        }
051    
052    
053        /**
054         * Parses a name form description according to RFC 4512:
055         * 
056         * <pre>
057         * NameFormDescription = LPAREN WSP
058         *    numericoid                 ; object identifier
059         *    [ SP "NAME" SP qdescrs ]   ; short names (descriptors)
060         *    [ SP "DESC" SP qdstring ]  ; description
061         *    [ SP "OBSOLETE" ]          ; not active
062         *    SP "OC" SP oid             ; structural object class
063         *    SP "MUST" SP oids          ; attribute types
064         *    [ SP "MAY" SP oids ]       ; attribute types
065         *    extensions WSP RPAREN      ; extensions
066         * </pre>
067         * 
068         * @param nameFormDescription the name form description to be parsed
069         * @return the parsed NameForm bean
070         * @throws ParseException if there are any recognition errors (bad syntax)
071         */
072        public synchronized NameForm parseNameFormDescription( String nameFormDescription)
073            throws ParseException
074        {
075            LOG.debug( "Parsing a NameForm : {}", nameFormDescription );
076    
077            if ( nameFormDescription == null )
078            {
079                LOG.error( I18n.err( I18n.ERR_04248 ) );
080                throw new ParseException( "Null", 0 );
081            }
082    
083            reset( nameFormDescription ); // reset and initialize the parser / lexer pair
084    
085            try
086            {
087                NameForm nameForm = parser.nameFormDescription();
088                
089                // Update the schemaName
090                setSchemaName( nameForm );
091    
092                return nameForm;
093            }
094            catch ( RecognitionException re )
095            {
096                String msg = I18n.err( I18n.ERR_04249, nameFormDescription, re.getMessage(), re.getColumn() );
097                LOG.error( msg );
098                throw new ParseException( msg, re.getColumn() );
099            }
100            catch ( TokenStreamException tse )
101            {
102                String msg = I18n.err( I18n.ERR_04250, nameFormDescription, tse.getMessage() );
103                LOG.error( msg );
104                throw new ParseException( msg, 0 );
105            }
106        }
107    
108    
109        /**
110         * Parses a NameForm description
111         * 
112         * @param The NameForm description to parse
113         * @return An instance of NameForm
114         */
115        public NameForm parse( String schemaDescription ) throws ParseException
116        {
117            return parseNameFormDescription( schemaDescription );
118        }
119    }