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.DitStructureRule;
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 structure rule descriptons
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class DitStructureRuleDescriptionSchemaParser extends AbstractSchemaParser
40  {
41      /** The LoggerFactory used by this class */
42      protected static final Logger LOG = LoggerFactory.getLogger( DitStructureRuleDescriptionSchemaParser.class );
43  
44  
45      /**
46       * Creates a schema parser instance.
47       */
48      public DitStructureRuleDescriptionSchemaParser()
49      {
50          super();
51      }
52  
53  
54      /**
55       * Parses a DIT structure rule description according to RFC 4512:
56       * 
57       * <pre>
58       * DITStructureRuleDescription = LPAREN WSP
59       *   ruleid                     ; rule identifier
60       *   [ SP "NAME" SP qdescrs ]   ; short names (descriptors)
61       *   [ SP "DESC" SP qdstring ]  ; description
62       *   [ SP "OBSOLETE" ]          ; not active
63       *   SP "FORM" SP oid           ; NameForm
64       *   [ SP "SUP" ruleids ]       ; superior rules
65       *   extensions WSP RPAREN      ; extensions
66       *
67       * ruleids = ruleid / ( LPAREN WSP ruleidlist WSP RPAREN )
68       * ruleidlist = ruleid *( SP ruleid )
69       * ruleid = numbers
70       * </pre>
71       * 
72       * @param ditStructureRuleDescription the DIT structure rule description to be parsed
73       * @return the parsed DITStructureRuleDescription bean
74       * @throws ParseException if there are any recognition errors (bad syntax)
75       */
76      public synchronized DitStructureRule parseDITStructureRuleDescription( String ditStructureRuleDescription )
77          throws ParseException
78      {
79          LOG.debug( "Parsing a DitStructureRule : {}", ditStructureRuleDescription );
80  
81          if ( ditStructureRuleDescription == null )
82          {
83              LOG.error( I18n.err( I18n.ERR_04233 ) );
84              throw new ParseException( "Null", 0 );
85          }
86  
87          reset( ditStructureRuleDescription ); // reset and initialize the parser / lexer pair
88  
89          try
90          {
91              DitStructureRule ditStructureRule = parser.ditStructureRuleDescription();
92  
93              // Update the schemaName
94              updateSchemaName( ditStructureRule );
95  
96              return ditStructureRule;
97          }
98          catch ( RecognitionException re )
99          {
100             String msg = I18n.err( I18n.ERR_04234, ditStructureRuleDescription, re.getMessage(), re.getColumn() );
101             LOG.error( msg );
102             throw new ParseException( msg, re.getColumn() );
103         }
104         catch ( TokenStreamException tse )
105         {
106             String msg = I18n.err( I18n.ERR_04235, ditStructureRuleDescription, tse.getMessage() );
107             LOG.error( msg );
108             throw new ParseException( msg, 0 );
109         }
110 
111     }
112 
113 
114     /**
115      * Parses a DitStructureRule description.
116      * 
117      * @param schemaDescription The DitStructureRule description to parse
118      * @return An instance of DitStructureRule
119      * @throws ParseException {@inheritDoc}
120      */
121     public DitStructureRule parse( String schemaDescription ) throws ParseException
122     {
123         return parseDITStructureRuleDescription( schemaDescription );
124     }
125 }