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