001package org.apache.maven.doxia.module.fml;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.doxia.macro.MacroExecutionException;
023import org.apache.maven.doxia.parser.Xhtml5BaseParser;
024import org.apache.maven.doxia.sink.Sink;
025import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
026import org.codehaus.plexus.util.xml.pull.XmlPullParser;
027import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
028
029/**
030 * Parse Fml questions and answers, these may contain arbitrary xdoc elements.
031 *
032 * @author ltheussl
033 * @since 1.0
034 */
035public class FmlContentParser
036    extends Xhtml5BaseParser
037    implements FmlMarkup
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    {
046        isEmptyElement = parser.isEmptyElementTag();
047
048        if ( parser.getName().equals( QUESTION_TAG.toString() )
049                || parser.getName().equals( TITLE.toString() )
050            || parser.getName().equals( ANSWER_TAG.toString() ) )
051        {
052            // ignore
053            return;
054        }
055        else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
056        {
057            verbatim();
058
059            sink.verbatim( SinkEventAttributeSet.BOXED );
060        }
061        else if ( !baseStartTag( parser, sink ) )
062        {
063            if ( isEmptyElement )
064            {
065                handleUnknown( parser, sink, TAG_TYPE_SIMPLE );
066            }
067            else
068            {
069                handleUnknown( parser, sink, TAG_TYPE_START );
070            }
071
072            if ( getLog().isDebugEnabled() )
073            {
074                String position = "[" + parser.getLineNumber() + ":"
075                    + parser.getColumnNumber() + "]";
076                String tag = "<" + parser.getName() + ">";
077
078                getLog().debug( "Unrecognized fml tag: " + tag + " at " + position );
079            }
080        }
081    }
082
083    /** {@inheritDoc} */
084    protected void handleEndTag( XmlPullParser parser, Sink sink )
085        throws XmlPullParserException, MacroExecutionException
086    {
087        if ( parser.getName().equals( QUESTION_TAG.toString() )
088                || parser.getName().equals( TITLE.toString() )
089            || parser.getName().equals( ANSWER_TAG.toString() ) )
090        {
091            // ignore
092            return;
093        }
094        else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
095        {
096            verbatim_();
097
098            sink.verbatim_();
099        }
100        else if ( !baseEndTag( parser, sink ) )
101        {
102            if ( !isEmptyElement )
103            {
104                handleUnknown( parser, sink, TAG_TYPE_END );
105            }
106        }
107
108        isEmptyElement = false;
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    protected void init()
115    {
116        super.init();
117
118        this.isEmptyElement = false;
119    }
120}