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