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