1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.parser;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.Reader;
27 import java.io.StringReader;
28 import java.util.Collection;
29 import java.util.LinkedList;
30 import java.util.PriorityQueue;
31 import java.util.Properties;
32
33 import org.apache.maven.doxia.macro.Macro;
34 import org.apache.maven.doxia.macro.MacroExecutionException;
35 import org.apache.maven.doxia.macro.MacroRequest;
36 import org.apache.maven.doxia.macro.manager.MacroManager;
37 import org.apache.maven.doxia.macro.manager.MacroNotFoundException;
38 import org.apache.maven.doxia.sink.Sink;
39 import org.apache.maven.doxia.sink.impl.CreateAnchorsForIndexEntriesFactory;
40 import org.apache.maven.doxia.sink.impl.SinkWrapperFactory;
41 import org.apache.maven.doxia.sink.impl.SinkWrapperFactoryComparator;
42
43
44
45
46
47
48
49
50 public abstract class AbstractParser implements Parser {
51
52 private boolean secondParsing = false;
53
54 @Inject
55 private MacroManager macroManager;
56
57 @Inject
58 private Collection<SinkWrapperFactory> automaticallyRegisteredSinkWrapperFactories;
59
60 private final Collection<SinkWrapperFactory> manuallyRegisteredSinkWrapperFactories = new LinkedList<>();
61
62
63
64
65 private boolean emitComments = true;
66
67 private boolean emitAnchors = false;
68
69 private static final String DOXIA_VERSION;
70
71 static {
72 final Properties props = new Properties();
73 final InputStream is = AbstractParser.class.getResourceAsStream(
74 "/META-INF/maven/org.apache.maven.doxia/doxia-core/pom.properties");
75
76 if (is == null) {
77 props.setProperty("version", "unknown");
78 } else {
79 try {
80 props.load(is);
81 } catch (IOException ex) {
82 props.setProperty("version", "unknown");
83 } finally {
84 try {
85 is.close();
86 } catch (IOException ex) {
87
88 }
89 }
90 }
91
92 DOXIA_VERSION = props.getProperty("version");
93 }
94
95
96
97
98
99
100 public int getType() {
101 return UNKNOWN_TYPE;
102 }
103
104
105 public void setEmitComments(boolean emitComments) {
106 this.emitComments = emitComments;
107 }
108
109
110
111
112
113
114 public boolean isEmitComments() {
115 return emitComments;
116 }
117
118 @Override
119 public boolean isEmitAnchorsForIndexableEntries() {
120 return emitAnchors;
121 }
122
123 @Override
124 public void setEmitAnchorsForIndexableEntries(boolean emitAnchors) {
125 this.emitAnchors = emitAnchors;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 public void executeMacro(String macroId, MacroRequest request, Sink sink)
140 throws MacroExecutionException, MacroNotFoundException {
141 Macro macro = getMacroManager().getMacro(macroId);
142
143 macro.execute(sink, request);
144 }
145
146
147
148
149
150
151
152 protected File getBasedir() {
153
154
155
156 String basedir = System.getProperty("basedir");
157
158 if (basedir != null) {
159 return new File(basedir);
160 }
161
162 return new File(new File("").getAbsolutePath());
163 }
164
165
166
167
168
169
170
171
172
173 public void parse(String string, Sink sink) throws ParseException {
174 this.parse(string, sink, null);
175 }
176
177
178
179
180
181
182
183
184
185
186 public void parse(String string, Sink sink, String reference) throws ParseException {
187 parse(new StringReader(string), sink, reference);
188 }
189
190
191 @Override
192 public void parse(Reader source, Sink sink) throws ParseException {
193 parse(source, sink, null);
194 }
195
196
197
198
199
200
201
202 protected Sink getWrappedSink(Sink sink) {
203
204 if (secondParsing) {
205 return sink;
206 }
207 Sink currentSink = sink;
208 for (SinkWrapperFactory factory : getSinkWrapperFactories()) {
209 currentSink = factory.createWrapper(currentSink);
210 }
211 return currentSink;
212 }
213
214
215
216
217
218
219 public void setSecondParsing(boolean second) {
220 this.secondParsing = second;
221 }
222
223
224
225
226
227
228
229 protected boolean isSecondParsing() {
230 return secondParsing;
231 }
232
233 @Override
234 public void addSinkWrapperFactory(SinkWrapperFactory factory) {
235 manuallyRegisteredSinkWrapperFactories.add(factory);
236 }
237
238 @Override
239 public Collection<SinkWrapperFactory> getSinkWrapperFactories() {
240 PriorityQueue<SinkWrapperFactory> effectiveSinkWrapperFactories =
241 new PriorityQueue<>(new SinkWrapperFactoryComparator());
242 if (automaticallyRegisteredSinkWrapperFactories != null) {
243 effectiveSinkWrapperFactories.addAll(automaticallyRegisteredSinkWrapperFactories);
244 }
245 effectiveSinkWrapperFactories.addAll(manuallyRegisteredSinkWrapperFactories);
246 if (emitAnchors) {
247 effectiveSinkWrapperFactories.add(new CreateAnchorsForIndexEntriesFactory());
248 }
249 return effectiveSinkWrapperFactories;
250 }
251
252
253
254
255
256
257
258 protected MacroManager getMacroManager() {
259 return macroManager;
260 }
261
262
263
264
265
266
267
268
269 protected void init() {
270
271 }
272
273
274
275
276
277
278
279 protected static String doxiaVersion() {
280 return DOXIA_VERSION;
281 }
282 }