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
48 public abstract class AbstractParser
49 implements Parser
50 {
51
52 private boolean secondParsing = false;
53
54 @Requirement
55 private MacroManager macroManager;
56
57
58 private Log logger;
59
60
61
62
63 private boolean emitComments = true;
64
65 private static final String DOXIA_VERSION;
66
67 static
68 {
69 final Properties props = new Properties();
70 final InputStream is = AbstractParser.class.getResourceAsStream( "/build-info.properties" );
71
72 if ( is == null )
73 {
74 props.setProperty( "version", "unknown" );
75 }
76 else
77 {
78 try
79 {
80 props.load( is );
81 }
82 catch ( IOException ex )
83 {
84 props.setProperty( "version", "unknown" );
85 }
86 finally
87 {
88 try
89 {
90 is.close();
91 }
92 catch ( IOException ex )
93 {
94
95 }
96 }
97 }
98
99 DOXIA_VERSION = props.getProperty( "version" );
100 }
101
102
103 public int getType()
104 {
105 return UNKNOWN_TYPE;
106 }
107
108 public void setEmitComments( boolean emitComments )
109 {
110 this.emitComments = emitComments;
111 }
112
113 public boolean isEmitComments()
114 {
115 return emitComments;
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129 public void executeMacro( String macroId, MacroRequest request, Sink sink )
130 throws MacroExecutionException, MacroNotFoundException
131 {
132 Macro macro = getMacroManager().getMacro( macroId );
133
134 macro.enableLogging( getLog() );
135
136 macro.execute( sink, request );
137 }
138
139
140
141
142
143
144
145
146 protected File getBasedir()
147 {
148
149
150
151 String basedir = System.getProperty( "basedir" );
152
153 if ( basedir != null )
154 {
155 return new File( basedir );
156 }
157
158 return new File( new File( "" ).getAbsolutePath() );
159 }
160
161
162
163
164
165
166
167
168
169 public void parse( String string, Sink sink )
170 throws ParseException
171 {
172 parse( new StringReader( string ), sink );
173 }
174
175 @Override
176 public void parse( Reader source, Sink sink, String reference )
177 throws ParseException
178 {
179 parse( source, sink );
180 }
181
182
183
184
185
186
187 public void setSecondParsing( boolean second )
188 {
189 this.secondParsing = second;
190 }
191
192
193
194
195
196
197
198 protected boolean isSecondParsing()
199 {
200 return secondParsing;
201 }
202
203
204 public void enableLogging( Log log )
205 {
206 this.logger = log;
207 }
208
209
210
211
212
213
214
215
216 protected Log getLog()
217 {
218 if ( logger == null )
219 {
220 logger = new SystemStreamLog();
221 }
222
223 return logger;
224 }
225
226
227
228
229
230
231
232 protected MacroManager getMacroManager()
233 {
234 return macroManager;
235 }
236
237
238
239
240
241
242
243
244 protected void init()
245 {
246
247 }
248
249
250
251
252
253
254
255
256 protected static String doxiaVersion()
257 {
258 return DOXIA_VERSION;
259 }
260 }