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