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    protected void handleStartTag(XmlPullParser parser, Sink sink)
043            throws XmlPullParserException, MacroExecutionException {
044        isEmptyElement = parser.isEmptyElementTag();
045
046        if (parser.getName().equals(QUESTION_TAG.toString())
047                || parser.getName().equals(TITLE.toString())
048                || parser.getName().equals(ANSWER_TAG.toString())) {
049            // ignore
050            return;
051        } else if (parser.getName().equals(SOURCE_TAG.toString())) {
052            verbatim();
053
054            sink.verbatim(SinkEventAttributeSet.SOURCE);
055        } else if (!baseStartTag(parser, sink)) {
056            if (isEmptyElement) {
057                handleUnknown(parser, sink, TAG_TYPE_SIMPLE);
058            } else {
059                handleUnknown(parser, sink, TAG_TYPE_START);
060            }
061
062            LOGGER.warn(
063                    "Unrecognized fml tag <{}> at [{}:{}]",
064                    parser.getName(),
065                    parser.getLineNumber(),
066                    parser.getColumnNumber());
067        }
068    }
069
070    protected void handleEndTag(XmlPullParser parser, Sink sink)
071            throws XmlPullParserException, MacroExecutionException {
072        if (parser.getName().equals(QUESTION_TAG.toString())
073                || parser.getName().equals(TITLE.toString())
074                || parser.getName().equals(ANSWER_TAG.toString())) {
075            // ignore
076            return;
077        } else if (parser.getName().equals(SOURCE_TAG.toString())) {
078            verbatim_();
079
080            sink.verbatim_();
081        } else if (!baseEndTag(parser, sink)) {
082            if (!isEmptyElement) {
083                handleUnknown(parser, sink, TAG_TYPE_END);
084            }
085        }
086
087        isEmptyElement = false;
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    protected void init() {
094        super.init();
095
096        this.isEmptyElement = false;
097    }
098}