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    
026    import antlr.RecognitionException;
027    import antlr.TokenStreamException;
028    import antlr.TokenStreamRecognitionException;
029    
030    
031    /**
032     * A parser for RFC 4512 attribute type descriptions.
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$, $Date$
036     */
037    public class AttributeTypeDescriptionSchemaParser extends AbstractSchemaParser
038    {
039    
040        /**
041         * Creates a schema parser instance.
042         */
043        public AttributeTypeDescriptionSchemaParser()
044        {
045        }
046        
047    
048        /**
049         * Parses a attribute type description according to RFC 4512:
050         * 
051         * <pre>
052         * AttributeTypeDescription = LPAREN WSP
053         *     numericoid                    ; object identifier
054         *     [ SP "NAME" SP qdescrs ]      ; short names (descriptors)
055         *     [ SP "DESC" SP qdstring ]     ; description
056         *     [ SP "OBSOLETE" ]             ; not active
057         *     [ SP "SUP" SP oid ]           ; supertype
058         *     [ SP "EQUALITY" SP oid ]      ; equality matching rule
059         *     [ SP "ORDERING" SP oid ]      ; ordering matching rule
060         *     [ SP "SUBSTR" SP oid ]        ; substrings matching rule
061         *     [ SP "SYNTAX" SP noidlen ]    ; value syntax
062         *     [ SP "SINGLE-VALUE" ]         ; single-value
063         *     [ SP "COLLECTIVE" ]           ; collective
064         *     [ SP "NO-USER-MODIFICATION" ] ; not user modifiable
065         *     [ SP "USAGE" SP usage ]       ; usage
066         *     extensions WSP RPAREN         ; extensions
067         * 
068         * usage = "userApplications"     /  ; user
069         *         "directoryOperation"   /  ; directory operational
070         *         "distributedOperation" /  ; DSA-shared operational
071         *         "dSAOperation"            ; DSA-specific operational     
072         * 
073         * extensions = *( SP xstring SP qdstrings )
074         * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
075         * </pre>
076         * 
077         * @param attributeTypeDescription the attribute type description to be parsed
078         * @return the parsed AttributeTypeDescription bean
079         * @throws ParseException if there are any recognition errors (bad syntax)
080         */
081        public synchronized AttributeTypeDescription parseAttributeTypeDescription( String attributeTypeDescription )
082            throws ParseException
083        {
084    
085            if ( attributeTypeDescription == null )
086            {
087                throw new ParseException( "Null", 0 );
088            }
089    
090            reset( attributeTypeDescription ); // reset and initialize the parser / lexer pair
091    
092            try
093            {
094                AttributeTypeDescription atd = parser.attributeTypeDescription();
095                return atd;
096            }
097            catch ( RecognitionException re )
098            {
099                String msg = "Parser failure on attribute type description:\n\t" + attributeTypeDescription;
100                msg += "\nAntlr message: " + re.getMessage();
101                msg += "\nAntlr column: " + re.getColumn();
102                throw new ParseException( msg, re.getColumn() );
103            }
104            catch ( TokenStreamRecognitionException tsre )
105            {
106                String msg = "Parser failure on attribute type description:\n\t" + attributeTypeDescription;
107                msg += "\nAntlr message: " + tsre.getMessage();
108                throw new ParseException( msg, 0 );
109            }
110            catch ( TokenStreamException tse )
111            {
112                String msg = "Parser failure on attribute type description:\n\t" + attributeTypeDescription;
113                msg += "\nAntlr message: " + tse.getMessage();
114                throw new ParseException( msg, 0 );
115            }
116    
117        }
118    
119    
120        public AbstractSchemaDescription parse( String schemaDescription ) throws ParseException
121        {
122            return parseAttributeTypeDescription( schemaDescription );
123        }
124    
125    
126    }