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