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.XhtmlBaseParser;
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   * @version $Id: FmlContentParser.java 1726411 2016-01-23 16:34:09Z hboutemy $
34   * @since 1.0
35   */
36  public class FmlContentParser
37      extends XhtmlBaseParser
38      implements FmlMarkup
39  {
40      /** Empty elements don't write a closing tag. */
41      private boolean isEmptyElement;
42  
43      /** {@inheritDoc} */
44      protected void handleStartTag( XmlPullParser parser, Sink sink )
45          throws XmlPullParserException, MacroExecutionException
46      {
47          isEmptyElement = parser.isEmptyElementTag();
48  
49          if ( parser.getName().equals( QUESTION_TAG.toString() )
50                  || parser.getName().equals( TITLE.toString() )
51              || parser.getName().equals( ANSWER_TAG.toString() ) )
52          {
53              // ignore
54              return;
55          }
56          else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
57          {
58              verbatim();
59  
60              sink.verbatim( SinkEventAttributeSet.BOXED );
61          }
62          else if ( !baseStartTag( parser, sink ) )
63          {
64              if ( isEmptyElement )
65              {
66                  handleUnknown( parser, sink, TAG_TYPE_SIMPLE );
67              }
68              else
69              {
70                  handleUnknown( parser, sink, TAG_TYPE_START );
71              }
72  
73              if ( getLog().isDebugEnabled() )
74              {
75                  String position = "[" + parser.getLineNumber() + ":"
76                      + parser.getColumnNumber() + "]";
77                  String tag = "<" + parser.getName() + ">";
78  
79                  getLog().debug( "Unrecognized fml tag: " + tag + " at " + position );
80              }
81          }
82      }
83  
84      /** {@inheritDoc} */
85      protected void handleEndTag( XmlPullParser parser, Sink sink )
86          throws XmlPullParserException, MacroExecutionException
87      {
88          if ( parser.getName().equals( QUESTION_TAG.toString() )
89                  || parser.getName().equals( TITLE.toString() )
90              || parser.getName().equals( ANSWER_TAG.toString() ) )
91          {
92              // ignore
93              return;
94          }
95          else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
96          {
97              verbatim_();
98  
99              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 }