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.LdapSyntax;
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 LDAP syntx descriptions.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class LdapSyntaxDescriptionSchemaParser extends AbstractSchemaParser
40  {
41      /** The LoggerFactory used by this class */
42      protected static final Logger LOG = LoggerFactory.getLogger( LdapSyntaxDescriptionSchemaParser.class );
43  
44  
45      /**
46       * Creates a schema parser instance.
47       */
48      public LdapSyntaxDescriptionSchemaParser()
49      {
50      }
51  
52  
53      /**
54       * Parses a LDAP syntax description according to RFC 4512:
55       * 
56       * <pre>
57       * SyntaxDescription = LPAREN WSP
58       *    numericoid                 ; object identifier
59       *    [ SP "DESC" SP qdstring ]  ; description
60       *    extensions WSP RPAREN      ; extensions
61       * </pre>
62       * 
63       * @param ldapSyntaxDescription the LDAP syntax description to be parsed
64       * @return the parsed LdapSyntax bean
65       * @throws ParseException if there are any recognition errors (bad syntax)
66       */
67      public synchronized LdapSyntax parseLdapSyntaxDescription( String ldapSyntaxDescription )
68          throws ParseException
69      {
70          LOG.debug( "Parsing a LdapSyntax : {}", ldapSyntaxDescription );
71  
72          if ( ldapSyntaxDescription == null )
73          {
74              LOG.error( I18n.err( I18n.ERR_04239 ) );
75              throw new ParseException( "Null", 0 );
76          }
77  
78          reset( ldapSyntaxDescription ); // reset and initialize the parser / lexer pair
79  
80          try
81          {
82              LdapSyntax ldapSyntax = parser.ldapSyntaxDescription();
83              ldapSyntax.setSpecification( ldapSyntaxDescription );
84              
85              // Update the schemaName
86              updateSchemaName( ldapSyntax );
87  
88              return ldapSyntax;
89          }
90          catch ( RecognitionException re )
91          {
92              String msg = I18n.err( I18n.ERR_04240, ldapSyntaxDescription, re.getMessage(), re.getColumn() );
93              LOG.error( msg );
94              throw new ParseException( msg, re.getColumn() );
95          }
96          catch ( TokenStreamException tse )
97          {
98              String msg = I18n.err( I18n.ERR_04241, ldapSyntaxDescription, tse.getMessage() );
99              LOG.error( msg );
100             throw new ParseException( msg, 0 );
101         }
102     }
103 
104 
105     /**
106      * Parses a LdapSyntax description.
107      * 
108      * @param schemaDescription The LdapSyntax description to parse
109      * @return An instance of LdapSyntax
110      * @throws ParseException {@inheritDoc}
111      */
112     public LdapSyntax parse( String schemaDescription ) throws ParseException
113     {
114         return parseLdapSyntaxDescription( schemaDescription );
115     }
116 }