1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.api.ldap.model.subtree;
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.NormalizerMappingResolver;
29 import org.apache.directory.api.ldap.model.schema.SchemaManager;
30
31 import antlr.RecognitionException;
32 import antlr.TokenStreamException;
33
34
35
36
37
38
39
40
41
42
43
44 public class SubtreeSpecificationParser
45 {
46
47 private ReusableAntlrSubtreeSpecificationParser parser;
48
49
50 private ReusableAntlrSubtreeSpecificationLexer lexer;
51
52 private final boolean isNormalizing;
53
54
55
56
57
58
59
60 public SubtreeSpecificationParser( SchemaManager schemaManager )
61 {
62
63 StringReader in = new StringReader( "" );
64 this.lexer = new ReusableAntlrSubtreeSpecificationLexer( in );
65 this.parser = new ReusableAntlrSubtreeSpecificationParser( lexer );
66
67
68 this.parser.init( schemaManager );
69 this.isNormalizing = false;
70 }
71
72
73
74
75
76
77
78
79 public SubtreeSpecificationParser( @SuppressWarnings("rawtypes") NormalizerMappingResolver resolver,
80 SchemaManager schemaManager )
81 {
82
83 StringReader in = new StringReader( "" );
84 this.lexer = new ReusableAntlrSubtreeSpecificationLexer( in );
85 this.parser = new ReusableAntlrSubtreeSpecificationParser( lexer );
86 this.parser.setNormalizerMappingResolver( resolver );
87
88
89 this.parser.init( schemaManager );
90 this.isNormalizing = true;
91 }
92
93
94
95
96
97
98 private synchronized void reset( String spec )
99 {
100
101 StringReader in = new StringReader( spec + "end" );
102 this.lexer.prepareNextInput( in );
103 this.parser.resetState();
104 }
105
106
107
108
109
110
111
112
113
114
115
116 public synchronized SubtreeSpecification parse( String spec ) throws ParseException
117 {
118 SubtreeSpecification ss;
119
120 if ( ( spec == null ) || ( spec.trim().length() == 0 ) )
121 {
122 return null;
123 }
124
125
126 reset( spec );
127
128 try
129 {
130 ss = this.parser.wrapperEntryPoint();
131 }
132 catch ( TokenStreamException | RecognitionException e )
133 {
134 String msg = I18n.err( I18n.ERR_04329, spec, e.getLocalizedMessage() );
135 throw new ParseException( msg, 0 );
136 }
137
138 return ss;
139 }
140
141
142
143
144
145
146
147 public boolean isNormizing()
148 {
149 return this.isNormalizing;
150 }
151 }