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