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.SinkEventAttributeSet;
26  
27  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
28  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29  
30  /**
31   * Parse Fml questions and answers, these may contain arbitrary xdoc elements.
32   *
33   * @author ltheussl
34   * @version $Id: FmlContentParser.java 807169 2009-08-24 12:05:48Z vsiveton $
35   * @since 1.0
36   */
37  public class FmlContentParser
38      extends XhtmlBaseParser
39      implements FmlMarkup
40  {
41      /** Empty elements don't write a closing tag. */
42      private boolean isEmptyElement;
43  
44      /** {@inheritDoc} */
45      protected void handleStartTag( XmlPullParser parser, Sink sink )
46          throws XmlPullParserException, MacroExecutionException
47      {
48          isEmptyElement = parser.isEmptyElementTag();
49  
50          if ( parser.getName().equals( QUESTION_TAG.toString() )
51                  || parser.getName().equals( TITLE.toString() )
52              || parser.getName().equals( ANSWER_TAG.toString() ) )
53          {
54              // ignore
55              return;
56          }
57          else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
58          {
59              verbatim();
60  
61              sink.verbatim( SinkEventAttributeSet.BOXED );
62          }
63          else if ( !baseStartTag( parser, sink ) )
64          {
65              if ( isEmptyElement )
66              {
67                  handleUnknown( parser, sink, TAG_TYPE_SIMPLE );
68              }
69              else
70              {
71                  handleUnknown( parser, sink, TAG_TYPE_START );
72              }
73  
74              if ( getLog().isDebugEnabled() )
75              {
76                  String position = "[" + parser.getLineNumber() + ":"
77                      + parser.getColumnNumber() + "]";
78                  String tag = "<" + parser.getName() + ">";
79  
80                  getLog().debug( "Unrecognized fml tag: " + tag + " at " + position );
81              }
82          }
83      }
84  
85      /** {@inheritDoc} */
86      protected void handleEndTag( XmlPullParser parser, Sink sink )
87          throws XmlPullParserException, MacroExecutionException
88      {
89          if ( parser.getName().equals( QUESTION_TAG.toString() )
90                  || parser.getName().equals( TITLE.toString() )
91              || parser.getName().equals( ANSWER_TAG.toString() ) )
92          {
93              // ignore
94              return;
95          }
96          else if ( parser.getName().equals( SOURCE_TAG.toString() ) )
97          {
98              verbatim_();
99  
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 }