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.model.transform;
20  
21  import java.io.IOException;
22  import java.util.ArrayDeque;
23  import java.util.Deque;
24  
25  import org.apache.maven.model.transform.pull.BufferingParser;
26  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
27  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28  
29  /**
30   * This filter will bypass all following filters and write directly to the output.
31   * Should be used in case of a DOM that should not be effected by other filters,
32   * even though the elements match.
33   *
34   * @author Robert Scholte
35   * @author Guillaume Nodet
36   * @since 4.0.0
37   */
38  class FastForwardFilter extends BufferingParser {
39      /**
40       * DOM elements of pom
41       *
42       * <ul>
43       *  <li>execution.configuration</li>
44       *  <li>plugin.configuration</li>
45       *  <li>plugin.goals</li>
46       *  <li>profile.reports</li>
47       *  <li>project.reports</li>
48       *  <li>reportSet.configuration</li>
49       * <ul>
50       */
51      private final Deque<String> state = new ArrayDeque<>();
52  
53      private int domDepth = 0;
54  
55      FastForwardFilter(XmlPullParser xmlPullParser) {
56          super(xmlPullParser);
57      }
58  
59      @Override
60      public int next() throws XmlPullParserException, IOException {
61          int event = super.next();
62          filter();
63          return event;
64      }
65  
66      @Override
67      public int nextToken() throws XmlPullParserException, IOException {
68          int event = super.nextToken();
69          filter();
70          return event;
71      }
72  
73      protected void filter() throws XmlPullParserException, IOException {
74          if (xmlPullParser.getEventType() == START_TAG) {
75              String localName = xmlPullParser.getName();
76              if (domDepth > 0) {
77                  domDepth++;
78              } else {
79                  final String key = state.peekLast() + '/' + localName;
80                  switch (key) {
81                      case "execution/configuration":
82                      case "plugin/configuration":
83                      case "plugin/goals":
84                      case "profile/reports":
85                      case "project/reports":
86                      case "reportSet/configuration":
87                          if (domDepth == 0) {
88                              bypass(true);
89                          }
90                          domDepth++;
91                          break;
92                      default:
93                          break;
94                  }
95              }
96              state.add(localName);
97          } else if (xmlPullParser.getEventType() == END_TAG) {
98              if (domDepth > 0) {
99                  if (--domDepth == 0) {
100                     bypass(false);
101                 }
102             }
103             state.removeLast();
104         }
105     }
106 
107     @Override
108     public void bypass(boolean bypass) {
109         this.bypass = bypass;
110     }
111 }