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.XhtmlBaseParser;
024import org.apache.maven.doxia.sink.Sink;
025import org.apache.maven.doxia.sink.SinkEventAttributeSet;
026
027import org.codehaus.plexus.util.xml.pull.XmlPullParser;
028import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
029
030/**
031 * Parse Fml questions and answers, these may contain arbitrary xdoc elements.
032 *
033 * @author ltheussl
034 * @version $Id$
035 * @since 1.0
036 */
037public class FmlContentParser
038    extends XhtmlBaseParser
039    implements FmlMarkup
040{
041    /** Empty elements don't write a closing tag. */
042    private boolean isEmptyElement;
043
044    /** {@inheritDoc} */
045    protected void handleStartTag( XmlPullParser parser, Sink sink )
046        throws XmlPullParserException, MacroExecutionException
047    {
048        isEmptyElement = parser.isEmptyElementTag();
049
050        if ( parser.getName().equals( QUESTION_TAG.toString() )
051                || parser.getName().equals( TITLE.toString() )
052            || parser.getName().equals( ANSWER_TAG.toString() ) )
053        {
054            // ignore
055            return;
056        }
057        else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
058        {
059            verbatim();
060
061            sink.verbatim( SinkEventAttributeSet.BOXED );
062        }
063        else if ( !baseStartTag( parser, sink ) )
064        {
065            if ( isEmptyElement )
066            {
067                handleUnknown( parser, sink, TAG_TYPE_SIMPLE );
068            }
069            else
070            {
071                handleUnknown( parser, sink, TAG_TYPE_START );
072            }
073
074            if ( getLog().isDebugEnabled() )
075            {
076                String position = "[" + parser.getLineNumber() + ":"
077                    + parser.getColumnNumber() + "]";
078                String tag = "<" + parser.getName() + ">";
079
080                getLog().debug( "Unrecognized fml tag: " + tag + " at " + position );
081            }
082        }
083    }
084
085    /** {@inheritDoc} */
086    protected void handleEndTag( XmlPullParser parser, Sink sink )
087        throws XmlPullParserException, MacroExecutionException
088    {
089        if ( parser.getName().equals( QUESTION_TAG.toString() )
090                || parser.getName().equals( TITLE.toString() )
091            || parser.getName().equals( ANSWER_TAG.toString() ) )
092        {
093            // ignore
094            return;
095        }
096        else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
097        {
098            verbatim_();
099
100            sink.verbatim_();
101        }
102        else if ( !baseEndTag( parser, sink ) )
103        {
104            if ( !isEmptyElement )
105            {
106                handleUnknown( parser, sink, TAG_TYPE_END );
107            }
108        }
109
110        isEmptyElement = false;
111    }
112
113    /** {@inheritDoc} */
114    protected void init()
115    {
116        super.init();
117
118        this.isEmptyElement = false;
119    }
120}