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 java.io.Reader;
022
023import org.apache.maven.doxia.index.IndexingSink;
024import org.apache.maven.doxia.macro.MacroExecutor;
025import org.apache.maven.doxia.sink.Sink;
026import org.apache.maven.doxia.sink.impl.SinkWrapperFactory;
027
028/**
029 * A Parser is responsible for parsing any document in a supported front-end
030 * format, and emitting the standard Doxia events, which can then be consumed
031 * by any Doxia Sink.
032 *
033 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
034 * @since 1.0
035 */
036public interface Parser {
037
038    /** Unknown parser type */
039    int UNKNOWN_TYPE = 0;
040
041    /** Text parser type */
042    int TXT_TYPE = 1;
043
044    /** XML parser type */
045    int XML_TYPE = 2;
046
047    /**
048     * Parses the given source model and emits Doxia events into the given sink.
049     * Shortcut for {@link #parse(Reader, Sink, String)} with last argument being {@code null}.
050     *
051     * @param source not null reader that provides the source document.
052     * @param sink A sink that consumes the Doxia events.
053     * @throws org.apache.maven.doxia.parser.ParseException if the model could not be parsed.
054     *
055     */
056    void parse(Reader source, Sink sink) throws ParseException;
057
058    /**
059     * Parses the given source model and emits Doxia events into the given sink.
060     *
061     * @param source not null reader that provides the source document.
062     * @param sink A sink that consumes the Doxia events.
063     * @param reference a string identifying the source (for file based documents the source file path)
064     * @throws org.apache.maven.doxia.parser.ParseException if the model could not be parsed.
065     */
066    void parse(Reader source, Sink sink, String reference) throws ParseException;
067
068    /**
069     * The parser type value could be {@link #UNKNOWN_TYPE}, {@link #TXT_TYPE} or
070     * {@link #XML_TYPE}.
071     *
072     * @return the type of Parser
073     */
074    int getType();
075
076    /**
077     * When comments are found in source markup, emit comment Doxia events or just ignore?
078     *
079     * @param emitComments <code>true</code> (default value) to emit comment Doxia events
080     */
081    void setEmitComments(boolean emitComments);
082
083    /**
084     * Does the parser emit Doxia comments event when comments found in source?
085     *
086     * @return <code>true</code> (default value) if comment Doxia events are emitted
087     */
088    boolean isEmitComments();
089
090    /**
091     * Registers a given {@link SinkWrapperFactory} with the parser used in subsequent calls of {@code parse(...)}
092     * @param factory the factory to create the sink wrapper
093     * @since 2.0.0
094     */
095    void addSinkWrapperFactory(SinkWrapperFactory factory);
096
097    /**
098     * Determines whether to automatically generate anchors for each index entry found by {@link IndexingSink} or not.
099     * By default no anchors are generated.
100     *
101     * @param emitAnchors {@code true} to emit anchors otherwise {@code false} (the default)
102     * @since 2.0.0
103     */
104    void setEmitAnchorsForIndexableEntries(boolean emitAnchors);
105
106    /**
107     * Returns whether anchors are automatically generated for each index entry found by {@link IndexingSink} or not.
108     * @return  {@code true} if anchors are emitted otherwise {@code false}
109     * @since 2.0.0
110     */
111    boolean isEmitAnchorsForIndexableEntries();
112
113    /**
114     * Delegates macro execution to the given {@link MacroExecutor}.
115     * @param macroExecutor (may be {@code null} to use the built-in macro executor, which resolves all macros to {@link Sink} methods)}
116     * @since 2.1.0
117     */
118    void setMacroExecutor(MacroExecutor macroExecutor);
119}