Skip to content

Commit

Permalink
[MNG-7805] Make the modelVersion optional when using build pom (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 19, 2023
1 parent fb612f5 commit c5f9b74
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public final XmlPullParser get(XmlPullParser orgParser, Path projectFile) {
getSha1().ifPresent(ciFriendlyFilter::setSha1);
parser = ciFriendlyFilter;

parser = new ModelVersionXMLFilter(parser);

return parser;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.model.transform;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.maven.model.transform.pull.NodeBufferingParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParser;

public class ModelVersionXMLFilter extends NodeBufferingParser {

private static final Pattern S_FILTER = Pattern.compile("\\s+");
public static final String NAMESPACE_PREFIX = "http://maven.apache.org/POM/";

public ModelVersionXMLFilter(XmlPullParser xmlPullParser) {
super(xmlPullParser, "project");
}

@Override
protected void process(List<Event> buffer) {
if (buffer.stream().noneMatch(e -> e.event == XmlPullParser.START_TAG && "modelVersion".equals(e.name))) {
String namespace = null;
for (int pos = 0; pos < buffer.size(); pos++) {
Event e = buffer.get(pos);
if (namespace != null) {
if (e.event == XmlPullParser.START_TAG) {
Event prev = buffer.get(pos - 1);
if (prev.event != TEXT || !S_FILTER.matcher(prev.text).matches()) {
prev = null;
}
Event pmse = new Event();
pmse.event = START_TAG;
pmse.name = "modelVersion";
pmse.namespace = namespace;
buffer.add(pos++, pmse);
Event pmve = new Event();
pmve.event = TEXT;
pmve.text = namespace.substring(NAMESPACE_PREFIX.length());
buffer.add(pos++, pmve);
Event pmee = new Event();
pmee.event = END_TAG;
pmee.name = "modelVersion";
pmee.namespace = namespace;
buffer.add(pos++, pmee);
if (prev != null) {
buffer.add(pos++, prev);
}
break;
}
} else if (e.event == XmlPullParser.START_TAG
&& "project".equals(e.name)
&& e.namespace != null
&& e.namespace.startsWith(NAMESPACE_PREFIX)) {
namespace = e.namespace;
}
}
}
buffer.forEach(this::pushEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.Objects;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -54,6 +55,55 @@ public static class Event {
public String text;
public Attribute[] attributes;
public Namespace[] namespaces;
public int columnNumber;
public int lineNumber;

public String positionDescription() {
return " " + TYPES[event] + " @" + lineNumber + ":" + columnNumber;
}

@Override
public String toString() {
switch (event) {
case START_DOCUMENT:
case END_DOCUMENT:
return "Event{event=" + TYPES[event] + "'}";
case PROCESSING_INSTRUCTION:
case TEXT:
case CDSECT:
case ENTITY_REF:
case COMMENT:
case IGNORABLE_WHITESPACE:
return "Event{event=" + TYPES[event] + ", text='" + text + "'}";
case START_TAG:
return "Event{" + "event=START_TAG"
+ ", name='"
+ name + '\'' + ", prefix='"
+ prefix + '\'' + ", namespace='"
+ namespace + '\'' + ", empty="
+ empty + ", attributes="
+ Arrays.toString(attributes) + ", namespaces="
+ Arrays.toString(namespaces) + '}';
case END_TAG:
return "Event{" + "event=END_TAG"
+ ", name='"
+ name + '\'' + ", prefix='"
+ prefix + '\'' + ", namespace='"
+ namespace + '\'' + ", empty="
+ empty + ", namespaces="
+ Arrays.toString(namespaces) + '}';
default:
return "Event{" + "event="
+ TYPES[event] + ", name='"
+ name + '\'' + ", prefix='"
+ prefix + '\'' + ", namespace='"
+ namespace + '\'' + ", empty="
+ empty + ", text='"
+ text + '\'' + ", attributes="
+ Arrays.toString(attributes) + ", namespaces="
+ Arrays.toString(namespaces) + '}';
}
}
}

@SuppressWarnings("checkstyle:VisibilityModifier")
Expand Down Expand Up @@ -149,23 +199,23 @@ public int getDepth() {
@Override
public String getPositionDescription() {
if (current != null) {
throw new IllegalStateException("Not supported during events replay");
return current.positionDescription();
}
return xmlPullParser.getPositionDescription();
}

@Override
public int getLineNumber() {
if (current != null) {
throw new IllegalStateException("Not supported during events replay");
return current.lineNumber;
}
return xmlPullParser.getLineNumber();
}

@Override
public int getColumnNumber() {
if (current != null) {
throw new IllegalStateException("Not supported during events replay");
return current.columnNumber;
}
return xmlPullParser.getColumnNumber();
}
Expand Down Expand Up @@ -385,6 +435,8 @@ protected Event bufferEvent() throws XmlPullParserException {
Event event = new Event();
XmlPullParser pp = xmlPullParser;
event.event = xmlPullParser.getEventType();
event.columnNumber = xmlPullParser.getColumnNumber();
event.lineNumber = xmlPullParser.getLineNumber();
switch (event.event) {
case START_DOCUMENT:
case END_DOCUMENT:
Expand Down Expand Up @@ -415,6 +467,8 @@ protected Event bufferEvent() throws XmlPullParserException {
case TEXT:
case COMMENT:
case IGNORABLE_WHITESPACE:
case CDSECT:
case ENTITY_REF:
event.text = pp.getText();
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@ protected boolean accept() throws XmlPullParserException, IOException {
return true;
}

@Override
public boolean bypass() {
return !buffering && super.bypass();
}

protected abstract void process(List<Event> buffer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.model.transform;

import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ModelVersionXMLFilterTest extends AbstractXMLFilterTests {
@Override
protected XmlPullParser getFilter(XmlPullParser parser) {
return new ModelVersionXMLFilter(parser);
}

@Test
void modelVersionWithDefaultPrefix() throws Exception {
String input = "<project xmlns='http://maven.apache.org/POM/4.0.0'>"
+ " <groupId>GROUPID</groupId>"
+ " <artifactId>ARTIFACTID</artifactId>"
+ " <version>VERSION</version>"
+ "</project>";
String expected = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\">"
+ " <modelVersion>4.0.0</modelVersion>"
+ " <groupId>GROUPID</groupId>"
+ " <artifactId>ARTIFACTID</artifactId>"
+ " <version>VERSION</version>"
+ "</project>";

// Check that the modelVersion is added
assertEquals(expected, transform(input));
// Check that the transformed POM is stable (modelVersion not added twice)
assertEquals(expected, transform(expected));
}

@Test
void modelVersionWithPrefix() throws Exception {
String input = "<maven:project xmlns:maven='http://maven.apache.org/POM/4.0.0'>"
+ " <maven:groupId>GROUPID</maven:groupId>"
+ " <maven:artifactId>ARTIFACTID</maven:artifactId>"
+ " <maven:version>VERSION</maven:version>"
+ "</maven:project>";
String expected = "<maven:project xmlns:maven=\"http://maven.apache.org/POM/4.0.0\">"
+ " <maven:modelVersion>4.0.0</maven:modelVersion>"
+ " <maven:groupId>GROUPID</maven:groupId>"
+ " <maven:artifactId>ARTIFACTID</maven:artifactId>"
+ " <maven:version>VERSION</maven:version>"
+ "</maven:project>";

// Check that the modelVersion is added
assertEquals(expected, transform(input));
// Check that the transformed POM is stable (modelVersion not added twice)
assertEquals(expected, transform(expected));
}
}

0 comments on commit c5f9b74

Please sign in to comment.