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.DitContentRule;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import antlr.RecognitionException;
31  import antlr.TokenStreamException;
32  
33  
34  /**
35   * A parser for RFC 4512 DIT content rule descriptons
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class DitContentRuleDescriptionSchemaParser extends AbstractSchemaParser
40  {
41      /** The LoggerFactory used by this class */
42      protected static final Logger LOG = LoggerFactory.getLogger( DitContentRuleDescriptionSchemaParser.class );
43  
44  
45      /**
46       * Creates a schema parser instance.
47       */
48      public DitContentRuleDescriptionSchemaParser()
49      {
50      }
51  
52  
53      /**
54       * Parses a DIT content rule description according to RFC 4512:
55       * 
56       * <pre>
57       * DITContentRuleDescription = LPAREN WSP
58       *    numericoid                 ; object identifier
59       *    [ SP "NAME" SP qdescrs ]   ; short names (descriptors)
60       *    [ SP "DESC" SP qdstring ]  ; description
61       *    [ SP "OBSOLETE" ]          ; not active
62       *    [ SP "AUX" SP oids ]       ; auxiliary object classes
63       *    [ SP "MUST" SP oids ]      ; attribute types
64       *    [ SP "MAY" SP oids ]       ; attribute types
65       *    [ SP "NOT" SP oids ]       ; attribute types
66       *    extensions WSP RPAREN      ; extensions
67       * </pre>
68       * 
69       * @param ditContentRuleDescription the DIT content rule description to be parsed
70       * @return the parsed DITContentRuleDescription bean
71       * @throws ParseException if there are any recognition errors (bad syntax)
72       */
73      public synchronized DitContentRule parseDITContentRuleDescription( String ditContentRuleDescription )
74          throws ParseException
75      {
76          LOG.debug( "Parsing a DitContentRule : {}", ditContentRuleDescription );
77  
78          if ( ditContentRuleDescription == null )
79          {
80              LOG.error( I18n.err( I18n.ERR_04230 ) );
81              throw new ParseException( "Null", 0 );
82          }
83  
84          reset( ditContentRuleDescription ); // reset and initialize the parser / lexer pair
85  
86          try
87          {
88              DitContentRule ditContentRule = parser.ditContentRuleDescription();
89  
90              // Update the schemaName
91              updateSchemaName( ditContentRule );
92  
93              return ditContentRule;
94          }
95          catch ( RecognitionException re )
96          {
97              String msg = I18n.err( I18n.ERR_04231, ditContentRuleDescription, re.getMessage(), re.getColumn() );
98              LOG.error( msg );
99              throw new ParseException( msg, re.getColumn() );
100         }
101         catch ( TokenStreamException tse )
102         {
103             String msg = I18n.err( I18n.ERR_04232, ditContentRuleDescription, tse.getMessage() );
104             LOG.error( msg );
105             throw new ParseException( msg, 0 );
106         }
107 
108     }
109 
110 
111     /**
112      * Parses a DitContentRule description.
113      * 
114      * @param schemaDescription The DitContentRule description to parse
115      * @return An instance of DitContentRule
116      * @throws ParseException {@inheritDoc}
117      */
118     public DitContentRule parse( String schemaDescription ) throws ParseException
119     {
120         return parseDITContentRuleDescription( schemaDescription );
121     }
122 }