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