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      protected void handleStartTag(XmlPullParser parser, Sink sink)
43              throws XmlPullParserException, MacroExecutionException {
44          isEmptyElement = parser.isEmptyElementTag();
45  
46          if (parser.getName().equals(QUESTION_TAG.toString())
47                  || parser.getName().equals(TITLE.toString())
48                  || parser.getName().equals(ANSWER_TAG.toString())) {
49              // ignore
50              return;
51          } else if (parser.getName().equals(SOURCE_TAG.toString())) {
52              verbatim();
53  
54              sink.verbatim(SinkEventAttributeSet.SOURCE);
55          } else if (!baseStartTag(parser, sink)) {
56              if (isEmptyElement) {
57                  handleUnknown(parser, sink, TAG_TYPE_SIMPLE);
58              } else {
59                  handleUnknown(parser, sink, TAG_TYPE_START);
60              }
61  
62              LOGGER.warn(
63                      "Unrecognized fml tag <{}> at [{}:{}]",
64                      parser.getName(),
65                      parser.getLineNumber(),
66                      parser.getColumnNumber());
67          }
68      }
69  
70      protected void handleEndTag(XmlPullParser parser, Sink sink)
71              throws XmlPullParserException, MacroExecutionException {
72          if (parser.getName().equals(QUESTION_TAG.toString())
73                  || parser.getName().equals(TITLE.toString())
74                  || parser.getName().equals(ANSWER_TAG.toString())) {
75              // ignore
76              return;
77          } else if (parser.getName().equals(SOURCE_TAG.toString())) {
78              verbatim_();
79  
80              sink.verbatim_();
81          } else if (!baseEndTag(parser, sink)) {
82              if (!isEmptyElement) {
83                  handleUnknown(parser, sink, TAG_TYPE_END);
84              }
85          }
86  
87          isEmptyElement = false;
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      protected void init() {
94          super.init();
95  
96          this.isEmptyElement = false;
97      }
98  }