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  public class ModelVersionXMLFilter extends NodeBufferingParser {
28  
29      private static final Pattern S_FILTER = Pattern.compile("\\s+");
30      public static final String NAMESPACE_PREFIX = "http://maven.apache.org/POM/";
31  
32      public ModelVersionXMLFilter(XmlPullParser xmlPullParser) {
33          super(xmlPullParser, "project");
34      }
35  
36      @Override
37      protected void process(List<Event> buffer) {
38          if (buffer.stream().noneMatch(e -> e.event == XmlPullParser.START_TAG && "modelVersion".equals(e.name))) {
39              String namespace = null;
40              for (int pos = 0; pos < buffer.size(); pos++) {
41                  Event e = buffer.get(pos);
42                  if (namespace != null) {
43                      if (e.event == XmlPullParser.START_TAG) {
44                          Event prev = buffer.get(pos - 1);
45                          if (prev.event != TEXT || !S_FILTER.matcher(prev.text).matches()) {
46                              prev = null;
47                          }
48                          Event pmse = new Event();
49                          pmse.event = START_TAG;
50                          pmse.name = "modelVersion";
51                          pmse.namespace = namespace;
52                          buffer.add(pos++, pmse);
53                          Event pmve = new Event();
54                          pmve.event = TEXT;
55                          pmve.text = namespace.substring(NAMESPACE_PREFIX.length());
56                          buffer.add(pos++, pmve);
57                          Event pmee = new Event();
58                          pmee.event = END_TAG;
59                          pmee.name = "modelVersion";
60                          pmee.namespace = namespace;
61                          buffer.add(pos++, pmee);
62                          if (prev != null) {
63                              buffer.add(pos++, prev);
64                          }
65                          break;
66                      }
67                  } else if (e.event == XmlPullParser.START_TAG
68                          && "project".equals(e.name)
69                          && e.namespace != null
70                          && e.namespace.startsWith(NAMESPACE_PREFIX)) {
71                      namespace = e.namespace;
72                  }
73              }
74          }
75          buffer.forEach(this::pushEvent);
76      }
77  }