-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...model-transform/src/main/java/org/apache/maven/model/transform/ModelVersionXMLFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...l-transform/src/test/java/org/apache/maven/model/transform/ModelVersionXMLFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |