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
21 package org.apache.directory.api.ldap.aci;
22
23
24 import java.io.StringReader;
25 import java.text.ParseException;
26
27 import org.apache.directory.api.i18n.I18n;
28 import org.apache.directory.api.ldap.model.schema.SchemaManager;
29 import org.apache.directory.api.util.StringConstants;
30
31 import antlr.RecognitionException;
32 import antlr.TokenStreamException;
33
34
35 /**
36 * A reusable wrapper around the antlr generated parser for an ACIItem as
37 * defined by X.501. This class enables the reuse of the antlr parser/lexer pair
38 * without having to recreate them every time.
39 *
40 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41 */
42 public class ACIItemChecker
43 {
44 /** the antlr generated parser being wrapped */
45 private ReusableAntlrACIItemParser checker;
46
47 /** the antlr generated lexer being wrapped */
48 private ReusableAntlrACIItemLexer lexer;
49
50
51 /**
52 * Creates a ACIItem parser.
53 *
54 * @param schemaManager the schema manager
55 */
56 public ACIItemChecker( SchemaManager schemaManager )
57 {
58 this.lexer = new ReusableAntlrACIItemLexer( new StringReader( "" ) );
59 this.checker = new ReusableAntlrACIItemParser( lexer );
60 this.checker.init( schemaManager );
61 }
62
63
64 /**
65 * Initializes the plumbing by creating a pipe and coupling the parser/lexer
66 * pair with it. param spec the specification to be parsed
67 */
68 private synchronized void reset( String spec )
69 {
70 StringReader in = new StringReader( spec );
71 this.lexer.prepareNextInput( in );
72 this.checker.resetState();
73 }
74
75
76 /**
77 * Parses an ACIItem without exhausting the parser.
78 *
79 * @param spec
80 * the specification to be parsed
81 * @throws ParseException
82 * if there are any recognition errors (bad syntax)
83 */
84 public synchronized void parse( String spec ) throws ParseException
85 {
86 if ( spec == null || StringConstants.EMPTY .equals( spec.trim() ) )
87 {
88 return;
89 }
90
91 // reset and initialize the parser / lexer pair
92 reset( spec );
93
94 try
95 {
96 this.checker.wrapperEntryPoint();
97 }
98 catch ( TokenStreamException e )
99 {
100 throw new ParseException( I18n
101 .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), 0 );
102 }
103 catch ( RecognitionException e )
104 {
105 throw new ParseException( I18n
106 .err( I18n.ERR_04004_PARSER_FAILURE_ACI_ITEM, spec, e.getLocalizedMessage() ), e.getColumn() );
107 }
108 }
109
110 }