001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.doxia.parser;
020
021import javax.inject.Inject;
022
023import java.io.File;
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.Reader;
027import java.io.StringReader;
028import java.util.ArrayList;
029import java.util.Collection;
030import java.util.Collections;
031import java.util.LinkedList;
032import java.util.List;
033import java.util.Properties;
034
035import org.apache.maven.doxia.macro.Macro;
036import org.apache.maven.doxia.macro.MacroExecutionException;
037import org.apache.maven.doxia.macro.MacroExecutor;
038import org.apache.maven.doxia.macro.MacroRequest;
039import org.apache.maven.doxia.macro.manager.MacroManager;
040import org.apache.maven.doxia.macro.manager.MacroNotFoundException;
041import org.apache.maven.doxia.sink.Sink;
042import org.apache.maven.doxia.sink.impl.CreateAnchorsForIndexEntriesFactory;
043import org.apache.maven.doxia.sink.impl.SinkWrapperFactory;
044import org.apache.maven.doxia.sink.impl.SinkWrapperFactoryComparator;
045
046/**
047 * An abstract base class that defines some convenience methods for parsers.
048 * Provides a macro mechanism to give dynamic functionalities for the parsing.
049 *
050 * @author Jason van Zyl
051 * @since 1.0
052 */
053public abstract class AbstractParser implements Parser, MacroExecutor {
054    /** Indicates that a second parsing is required. */
055    private boolean secondParsing = false;
056
057    @Inject
058    private MacroManager macroManager;
059
060    @Inject
061    private Collection<SinkWrapperFactory> automaticallyRegisteredSinkWrapperFactories;
062
063    private final Collection<SinkWrapperFactory> manuallyRegisteredSinkWrapperFactories = new LinkedList<>();
064
065    /**
066     * Emit Doxia comment events when parsing comments?
067     */
068    private boolean emitComments = true;
069
070    private boolean emitAnchors = false;
071
072    private MacroExecutor macroExecutor = null;
073
074    private static final String DOXIA_VERSION;
075
076    static {
077        final Properties props = new Properties();
078        final InputStream is = AbstractParser.class.getResourceAsStream(
079                "/META-INF/maven/org.apache.maven.doxia/doxia-core/pom.properties");
080
081        if (is == null) {
082            props.setProperty("version", "unknown"); // should not happen
083        } else {
084            try {
085                props.load(is);
086            } catch (IOException ex) {
087                props.setProperty("version", "unknown"); // should not happen
088            } finally {
089                try {
090                    is.close();
091                } catch (IOException ex) {
092                    // oh well...
093                }
094            }
095        }
096
097        DOXIA_VERSION = props.getProperty("version");
098    }
099
100    /**
101     * {@inheritDoc}
102     *
103     * @return a int
104     */
105    public int getType() {
106        return UNKNOWN_TYPE;
107    }
108
109    public void setEmitComments(boolean emitComments) {
110        this.emitComments = emitComments;
111    }
112
113    /**
114     * <p>isEmitComments.</p>
115     *
116     * @return a boolean
117     */
118    public boolean isEmitComments() {
119        return emitComments;
120    }
121
122    @Override
123    public boolean isEmitAnchorsForIndexableEntries() {
124        return emitAnchors;
125    }
126
127    @Override
128    public void setEmitAnchorsForIndexableEntries(boolean emitAnchors) {
129        this.emitAnchors = emitAnchors;
130    }
131
132    @Override
133    public void setMacroExecutor(MacroExecutor macroExecutor) {
134        this.macroExecutor = macroExecutor;
135    }
136    /**
137     * Execute a macro on the given sink.
138     *
139     * @param macroId an id to lookup the macro
140     * @param request the corresponding MacroRequest
141     * @param sink the sink to receive the events
142     * @throws org.apache.maven.doxia.macro.MacroExecutionException if an error occurred during execution
143     * @throws org.apache.maven.doxia.macro.manager.MacroNotFoundException if the macro could not be found
144     */
145    @Override
146    public void executeMacro(String macroId, MacroRequest request, Sink sink)
147            throws MacroExecutionException, MacroNotFoundException {
148        if (macroExecutor != null) {
149            macroExecutor.executeMacro(macroId, request, sink);
150        } else {
151            Macro macro = getMacroManager().getMacro(macroId);
152            macro.execute(sink, request);
153        }
154    }
155
156    /**
157     * Returns the current base directory.
158     *
159     * @return the base directory
160     * @deprecated this does not work in multi-module builds, see DOXIA-373
161     */
162    @Deprecated
163    protected File getBasedir() {
164        // TODO: This is baaad, it should come in with the request.
165        // (this is only used for macro requests, see AptParser)
166
167        String basedir = System.getProperty("basedir");
168
169        if (basedir != null) {
170            return new File(basedir);
171        }
172
173        return new File(new File("").getAbsolutePath());
174    }
175
176    /**
177     * Convenience method to parse an arbitrary string and emit events into the given sink.
178     *
179     * @param string a string that provides the source input
180     * @param sink a sink that consumes the Doxia events
181     * @throws org.apache.maven.doxia.parser.ParseException if the string could not be parsed
182     * @since 1.1
183     */
184    public void parse(String string, Sink sink) throws ParseException {
185        this.parse(string, sink, null);
186    }
187
188    /**
189     * Convenience method to parse an arbitrary string and emit events into the given sink.
190     *
191     * @param string a string that provides the source input
192     * @param sink a sink that consumes the Doxia events
193     * @param reference a string containing the reference to the source of the input string (e.g. filename)
194     * @throws org.apache.maven.doxia.parser.ParseException if the string could not be parsed
195     * @since 1.10
196     */
197    public void parse(String string, Sink sink, String reference) throws ParseException {
198        parse(new StringReader(string), sink, reference);
199    }
200
201    @Override
202    public void parse(Reader source, Sink sink) throws ParseException {
203        parse(source, sink, null);
204    }
205
206    /**
207     * Creates a sink pipeline built from all registered {@link SinkWrapperFactory} objects.
208     * For secondary parsers (i.e. ones with {@link #isSecondParsing()} returning {@code true} just the given original sink is returned.
209     * @param sink
210     * @return the Sink pipeline to be used
211     */
212    protected Sink getWrappedSink(Sink sink) {
213        // no wrapping for secondary parsing
214        if (secondParsing) {
215            return sink;
216        }
217        Sink currentSink = sink;
218        for (SinkWrapperFactory factory : getSinkWrapperFactories()) {
219            currentSink = factory.createWrapper(currentSink);
220        }
221        return currentSink;
222    }
223
224    /**
225     * Set <code>secondParsing</code> to true, if this represents a secondary parsing of the same source.
226     *
227     * @param second true for second parsing
228     */
229    public void setSecondParsing(boolean second) {
230        this.secondParsing = second;
231    }
232
233    /**
234     * Indicates if we are currently parsing a second time.
235     *
236     * @return true if we are currently parsing a second time
237     * @since 1.1
238     */
239    protected boolean isSecondParsing() {
240        return secondParsing;
241    }
242
243    @Override
244    public void addSinkWrapperFactory(SinkWrapperFactory factory) {
245        manuallyRegisteredSinkWrapperFactories.add(factory);
246    }
247
248    /**
249     * Returns all sink wrapper factories (both registered automatically and manually). The collection is ordered in a way that
250     * the factories having the lowest priority come first (i.e. in reverse order).
251     * @return all sink wrapper factories in the reverse order
252     * @since 2.0.0
253     */
254    protected List<SinkWrapperFactory> getSinkWrapperFactories() {
255        List<SinkWrapperFactory> effectiveSinkWrapperFactories = new ArrayList<>();
256        if (automaticallyRegisteredSinkWrapperFactories != null) {
257            effectiveSinkWrapperFactories.addAll(automaticallyRegisteredSinkWrapperFactories);
258        }
259        effectiveSinkWrapperFactories.addAll(manuallyRegisteredSinkWrapperFactories);
260        if (emitAnchors) {
261            effectiveSinkWrapperFactories.add(new CreateAnchorsForIndexEntriesFactory());
262        }
263        Collections.sort(effectiveSinkWrapperFactories, Collections.reverseOrder(new SinkWrapperFactoryComparator()));
264        return effectiveSinkWrapperFactories;
265    }
266
267    /**
268     * Gets the current {@link MacroManager}.
269     *
270     * @return the current {@link MacroManager}
271     * @since 1.1
272     */
273    protected MacroManager getMacroManager() {
274        return macroManager;
275    }
276
277    /**
278     * Initialize the parser. This is called first by
279     * {@link #parse(java.io.Reader, org.apache.maven.doxia.sink.Sink)} and can be used
280     * to set the parser into a clear state so it can be re-used.
281     *
282     * @since 1.1.2
283     */
284    protected void init() {
285        // nop
286    }
287
288    /**
289     * The current Doxia version.
290     *
291     * @return the current Doxia version as a String
292     * @since 1.2
293     */
294    protected static String doxiaVersion() {
295        return DOXIA_VERSION;
296    }
297}