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.module.fml;
020
021import org.apache.maven.doxia.macro.MacroExecutionException;
022import org.apache.maven.doxia.parser.Xhtml5BaseParser;
023import org.apache.maven.doxia.sink.Sink;
024import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
025import org.codehaus.plexus.util.xml.pull.XmlPullParser;
026import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * Parse Fml questions and answers, these may contain arbitrary xdoc elements.
032 *
033 * @author ltheussl
034 * @since 1.0
035 */
036public class FmlContentParser extends Xhtml5BaseParser implements FmlMarkup {
037    private static final Logger LOGGER = LoggerFactory.getLogger(FmlContentParser.class);
038
039    /** Empty elements don't write a closing tag. */
040    private boolean isEmptyElement;
041
042    /** {@inheritDoc} */
043    protected void handleStartTag(XmlPullParser parser, Sink sink)
044            throws XmlPullParserException, MacroExecutionException {
045        isEmptyElement = parser.isEmptyElementTag();
046
047        if (parser.getName().equals(QUESTION_TAG.toString())
048                || parser.getName().equals(TITLE.toString())
049                || parser.getName().equals(ANSWER_TAG.toString())) {
050            // ignore
051            return;
052        } else if (parser.getName().equals(SOURCE_TAG.toString())) {
053            verbatim();
054
055            sink.verbatim(SinkEventAttributeSet.SOURCE);
056        } else if (!baseStartTag(parser, sink)) {
057            if (isEmptyElement) {
058                handleUnknown(parser, sink, TAG_TYPE_SIMPLE);
059            } else {
060                handleUnknown(parser, sink, TAG_TYPE_START);
061            }
062
063            LOGGER.warn(
064                    "Unrecognized fml tag <{}> at [{}:{}]",
065                    parser.getName(),
066                    parser.getLineNumber(),
067                    parser.getColumnNumber());
068        }
069    }
070
071    /** {@inheritDoc} */
072    protected void handleEndTag(XmlPullParser parser, Sink sink)
073            throws XmlPullParserException, MacroExecutionException {
074        if (parser.getName().equals(QUESTION_TAG.toString())
075                || parser.getName().equals(TITLE.toString())
076                || parser.getName().equals(ANSWER_TAG.toString())) {
077            // ignore
078            return;
079        } else if (parser.getName().equals(SOURCE_TAG.toString())) {
080            verbatim_();
081
082            sink.verbatim_();
083        } else if (!baseEndTag(parser, sink)) {
084            if (!isEmptyElement) {
085                handleUnknown(parser, sink, TAG_TYPE_END);
086            }
087        }
088
089        isEmptyElement = false;
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    protected void init() {
096        super.init();
097
098        this.isEmptyElement = false;
099    }
100}