View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.parsers;
21  
22  
23  import java.text.ParseException;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.schema.AttributeType;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import antlr.RecognitionException;
31  import antlr.TokenStreamException;
32  import antlr.TokenStreamRecognitionException;
33  
34  
35  /**
36   * A parser for RFC 4512 attribute type descriptions.
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class AttributeTypeDescriptionSchemaParser extends AbstractSchemaParser
41  {
42      /** The LoggerFactory used by this class */
43      protected static final Logger LOG = LoggerFactory.getLogger( AttributeTypeDescriptionSchemaParser.class );
44  
45  
46      /**
47       * Creates a schema parser instance.
48       */
49      public AttributeTypeDescriptionSchemaParser()
50      {
51      }
52  
53  
54      /**
55       * Parses a attribute type description according to RFC 4512:
56       * 
57       * <pre>
58       * AttributeTypeDescription = LPAREN WSP
59       *     numericoid                    ; object identifier
60       *     [ SP "NAME" SP qdescrs ]      ; short names (descriptors)
61       *     [ SP "DESC" SP qdstring ]     ; description
62       *     [ SP "OBSOLETE" ]             ; not active
63       *     [ SP "SUP" SP oid ]           ; supertype
64       *     [ SP "EQUALITY" SP oid ]      ; equality matching rule
65       *     [ SP "ORDERING" SP oid ]      ; ordering matching rule
66       *     [ SP "SUBSTR" SP oid ]        ; substrings matching rule
67       *     [ SP "SYNTAX" SP noidlen ]    ; value syntax
68       *     [ SP "SINGLE-VALUE" ]         ; single-value
69       *     [ SP "COLLECTIVE" ]           ; collective
70       *     [ SP "NO-USER-MODIFICATION" ] ; not user modifiable
71       *     [ SP "USAGE" SP usage ]       ; usage
72       *     extensions WSP RPAREN         ; extensions
73       * 
74       * usage = "userApplications"     /  ; user
75       *         "directoryOperation"   /  ; directory operational
76       *         "distributedOperation" /  ; DSA-shared operational
77       *         "dSAOperation"            ; DSA-specific operational     
78       * 
79       * extensions = *( SP xstring SP qdstrings )
80       * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
81       * </pre>
82       * 
83       * @param attributeTypeDescription the attribute type description to be parsed
84       * @return the parsed AttributeTypeDescription bean
85       * @throws ParseException if there are any recognition errors (bad syntax)
86       */
87      public synchronized AttributeType parseAttributeTypeDescription( String attributeTypeDescription )
88          throws ParseException
89      {
90  
91          LOG.debug( "Parsing an AttributeType : {}", attributeTypeDescription );
92  
93          if ( attributeTypeDescription == null )
94          {
95              LOG.error( I18n.err( I18n.ERR_04227 ) );
96              throw new ParseException( "Null", 0 );
97          }
98  
99          reset( attributeTypeDescription ); // reset and initialize the parser / lexer pair
100 
101         try
102         {
103             AttributeType attributeType = parser.attributeTypeDescription();
104 
105             // Update the schemaName
106             updateSchemaName( attributeType );
107 
108             return attributeType;
109         }
110         catch ( RecognitionException re )
111         {
112             String msg = I18n.err( I18n.ERR_04228, attributeTypeDescription, re.getMessage(), re.getColumn() );
113             LOG.error( msg );
114             throw new ParseException( msg, re.getColumn() );
115         }
116         catch ( TokenStreamRecognitionException tsre )
117         {
118             String msg = I18n.err( I18n.ERR_04229, attributeTypeDescription, tsre.getMessage() );
119             LOG.error( msg );
120             throw new ParseException( msg, 0 );
121         }
122         catch ( TokenStreamException tse )
123         {
124             String msg = I18n.err( I18n.ERR_04229, attributeTypeDescription, tse.getMessage() );
125             LOG.error( msg );
126             throw new ParseException( msg, 0 );
127         }
128 
129     }
130 
131 
132     /**
133      * Parses a AttributeType description.
134      * 
135      * @param schemaDescription The AttributeType description to parse
136      * @return An instance of AttributeType
137      * @throws ParseException {@inheritDoc}
138      */
139     public AttributeType parse( String schemaDescription ) throws ParseException
140     {
141         return parseAttributeTypeDescription( schemaDescription );
142     }
143 }