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.util.List;
22  import java.util.regex.Pattern;
23  
24  import org.apache.maven.model.transform.pull.NodeBufferingParser;
25  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
26  
27  /**
28   * Remove relativePath element, has no value for consumer pom
29   *
30   * @author Robert Scholte
31   * @author Guillaume Nodet
32   * @since 4.0.0
33   */
34  public class RelativePathXMLFilter extends NodeBufferingParser {
35  
36      private static final Pattern S_FILTER = Pattern.compile("\\s+");
37  
38      public RelativePathXMLFilter(XmlPullParser xmlPullParser) {
39          super(xmlPullParser, "parent");
40      }
41  
42      protected void process(List<Event> buffer) {
43          boolean skip = false;
44          Event prev = null;
45          for (Event event : buffer) {
46              if (event.event == START_TAG && "relativePath".equals(event.name)) {
47                  skip = true;
48                  if (prev != null
49                          && prev.event == TEXT
50                          && S_FILTER.matcher(prev.text).matches()) {
51                      prev = null;
52                  }
53                  event = null;
54              } else if (event.event == END_TAG && "relativePath".equals(event.name)) {
55                  skip = false;
56                  event = null;
57              } else if (skip) {
58                  event = null;
59              }
60              if (prev != null) {
61                  pushEvent(prev);
62              }
63              prev = event;
64          }
65          pushEvent(prev);
66      }
67  }