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