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.MatchingRule;
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 matching rule descriptions.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class MatchingRuleDescriptionSchemaParser extends AbstractSchemaParser
40  {
41      /** The LoggerFactory used by this class */
42      protected static final Logger LOG = LoggerFactory.getLogger( MatchingRuleDescriptionSchemaParser.class );
43  
44  
45      /**
46       * Creates a schema parser instance.
47       */
48      public MatchingRuleDescriptionSchemaParser()
49      {
50      }
51  
52  
53      /**
54       * Parses a matching rule description according to RFC 4512:
55       * 
56       * <pre>
57       * MatchingRuleDescription = 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 "SYNTAX" SP numericoid  ; assertion syntax
63       *    extensions WSP RPAREN      ; extensions
64       * 
65       * extensions = *( SP xstring SP qdstrings )
66       * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
67       * </pre>
68       * 
69       * @param matchingRuleDescription the matching rule description to be parsed
70       * @return the parsed MatchingRuleDescription bean
71       * @throws ParseException if there are any recognition errors (bad syntax)
72       */
73      public synchronized MatchingRule parseMatchingRuleDescription( String matchingRuleDescription )
74          throws ParseException
75      {
76          LOG.debug( "Parsing a MatchingRule : {}", matchingRuleDescription );
77  
78          if ( matchingRuleDescription == null )
79          {
80              LOG.error( I18n.err( I18n.ERR_04242 ) );
81              throw new ParseException( "Null", 0 );
82          }
83  
84          reset( matchingRuleDescription ); // reset and initialize the parser / lexer pair
85  
86          try
87          {
88              MatchingRule matchingRule = parser.matchingRuleDescription();
89  
90              // Update the schemaName
91              updateSchemaName( matchingRule );
92  
93              return matchingRule;
94          }
95          catch ( RecognitionException re )
96          {
97              String msg = I18n.err( I18n.ERR_04243, matchingRuleDescription, 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_04244, matchingRuleDescription, tse.getMessage() );
104             LOG.error( msg );
105             throw new ParseException( msg, 0 );
106         }
107     }
108 
109 
110     /**
111      * Parses a MatchingRule description.
112      * 
113      * @param schemaDescription The MatchingRule description to parse
114      * @return An instance of MatchingRule
115      * @throws ParseException {@inheritDoc}
116      */
117     public MatchingRule parse( String schemaDescription ) throws ParseException
118     {
119         return parseMatchingRuleDescription( schemaDescription );
120     }
121 }