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.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import antlr.RecognitionException;
30  import antlr.TokenStreamException;
31  
32  
33  /**
34   * A parser for ApacheDS normalizer descriptions.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class NormalizerDescriptionSchemaParser extends AbstractSchemaParser
39  {
40      /** The LoggerFactory used by this class */
41      protected static final Logger LOG = LoggerFactory.getLogger( NormalizerDescriptionSchemaParser.class );
42  
43  
44      /**
45       * Creates a schema parser instance.
46       */
47      public NormalizerDescriptionSchemaParser()
48      {
49          super();
50      }
51  
52  
53      /**
54       * Parses a normalizer description:
55       * 
56       * <pre>
57       * NormalizerDescription = LPAREN WSP
58       *     numericoid                           ; object identifier
59       *     [ SP "DESC" SP qdstring ]            ; description
60       *     SP "FQCN" SP fqcn                    ; fully qualified class name
61       *     [ SP "BYTECODE" SP base64 ]          ; optional base64 encoded bytecode
62       *     extensions WSP RPAREN                ; extensions
63       * 
64       * base64          = *(4base64-char)
65       * base64-char     = ALPHA / DIGIT / "+" / "/"
66       * fqcn = fqcnComponent 1*( DOT fqcnComponent )
67       * fqcnComponent = ???
68       * 
69       * extensions = *( SP xstring SP qdstrings )
70       * xstring = "X" HYPHEN 1*( ALPHA / HYPHEN / USCORE ) 
71       * </pre>
72       * 
73       * @param normalizerDescription the normalizer description to be parsed
74       * @return the parsed NormalizerDescription bean
75       * @throws ParseException if there are any recognition errors (bad syntax)
76       */
77      public synchronized NormalizerDescription parseNormalizerDescription( String normalizerDescription )
78          throws ParseException
79      {
80          LOG.debug( "Parsing a Normalizer : {}", normalizerDescription );
81  
82          if ( normalizerDescription == null )
83          {
84              LOG.error( I18n.err( I18n.ERR_04251 ) );
85              throw new ParseException( "Null", 0 );
86          }
87  
88          reset( normalizerDescription ); // reset and initialize the parser / lexer pair
89  
90          try
91          {
92              NormalizerDescription normalizer = parser.normalizerDescription();
93  
94              // Update the schemaName
95              updateSchemaName( normalizer );
96  
97              return normalizer;
98          }
99          catch ( RecognitionException re )
100         {
101             String msg = I18n.err( I18n.ERR_04252, normalizerDescription, re.getMessage(), re.getColumn() );
102             LOG.error( msg );
103             throw new ParseException( msg, re.getColumn() );
104         }
105         catch ( TokenStreamException tse )
106         {
107             String msg = I18n.err( I18n.ERR_04253, normalizerDescription, tse.getMessage() );
108             LOG.error( msg );
109             throw new ParseException( msg, 0 );
110         }
111 
112     }
113 
114 
115     /**
116      * Parses a Normalizer description.
117      * 
118      * @param schemaDescription The Normalizer description to parse
119      * @return An instance of NormalizerDescription
120      * @throws ParseException {@inheritDoc}
121      */
122     public NormalizerDescription parse( String schemaDescription ) throws ParseException
123     {
124         return parseNormalizerDescription( schemaDescription );
125     }
126 }