View Javadoc
1   package org.apache.maven.doxia.module.fml;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.doxia.macro.MacroExecutionException;
23  import org.apache.maven.doxia.parser.Xhtml5BaseParser;
24  import org.apache.maven.doxia.sink.Sink;
25  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
26  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
27  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28  
29  /**
30   * Parse Fml questions and answers, these may contain arbitrary xdoc elements.
31   *
32   * @author ltheussl
33   * @since 1.0
34   */
35  public class FmlContentParser
36      extends Xhtml5BaseParser
37      implements FmlMarkup
38  {
39      /** Empty elements don't write a closing tag. */
40      private boolean isEmptyElement;
41  
42      /** {@inheritDoc} */
43      protected void handleStartTag( XmlPullParser parser, Sink sink )
44          throws XmlPullParserException, MacroExecutionException
45      {
46          isEmptyElement = parser.isEmptyElementTag();
47  
48          if ( parser.getName().equals( QUESTION_TAG.toString() )
49                  || parser.getName().equals( TITLE.toString() )
50              || parser.getName().equals( ANSWER_TAG.toString() ) )
51          {
52              // ignore
53              return;
54          }
55          else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
56          {
57              verbatim();
58  
59              sink.verbatim( SinkEventAttributeSet.BOXED );
60          }
61          else if ( !baseStartTag( parser, sink ) )
62          {
63              if ( isEmptyElement )
64              {
65                  handleUnknown( parser, sink, TAG_TYPE_SIMPLE );
66              }
67              else
68              {
69                  handleUnknown( parser, sink, TAG_TYPE_START );
70              }
71  
72              if ( getLog().isDebugEnabled() )
73              {
74                  String position = "[" + parser.getLineNumber() + ":"
75                      + parser.getColumnNumber() + "]";
76                  String tag = "<" + parser.getName() + ">";
77  
78                  getLog().debug( "Unrecognized fml tag: " + tag + " at " + position );
79              }
80          }
81      }
82  
83      /** {@inheritDoc} */
84      protected void handleEndTag( XmlPullParser parser, Sink sink )
85          throws XmlPullParserException, MacroExecutionException
86      {
87          if ( parser.getName().equals( QUESTION_TAG.toString() )
88                  || parser.getName().equals( TITLE.toString() )
89              || parser.getName().equals( ANSWER_TAG.toString() ) )
90          {
91              // ignore
92              return;
93          }
94          else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
95          {
96              verbatim_();
97  
98              sink.verbatim_();
99          }
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 }