diff --git a/maven-model-transform/src/main/java/org/apache/maven/model/transform/BuildToRawPomXMLFilterFactory.java b/maven-model-transform/src/main/java/org/apache/maven/model/transform/BuildToRawPomXMLFilterFactory.java index 3d1f1a2ef8..dba9026b12 100644 --- a/maven-model-transform/src/main/java/org/apache/maven/model/transform/BuildToRawPomXMLFilterFactory.java +++ b/maven-model-transform/src/main/java/org/apache/maven/model/transform/BuildToRawPomXMLFilterFactory.java @@ -53,7 +53,9 @@ public class BuildToRawPomXMLFilterFactory public final XmlPullParser get( XmlPullParser orgParser, Path projectFile ) { - XmlPullParser parser = orgParser; + // Ensure that xs:any elements aren't touched by next filters + XmlPullParser parser = orgParser instanceof FastForwardFilter + ? orgParser : new FastForwardFilter( orgParser ); if ( getDependencyKeyToVersionMapper() != null ) { diff --git a/maven-model-transform/src/main/java/org/apache/maven/model/transform/FastForwardFilter.java b/maven-model-transform/src/main/java/org/apache/maven/model/transform/FastForwardFilter.java index 91977c0138..c6647ba33e 100644 --- a/maven-model-transform/src/main/java/org/apache/maven/model/transform/FastForwardFilter.java +++ b/maven-model-transform/src/main/java/org/apache/maven/model/transform/FastForwardFilter.java @@ -28,8 +28,9 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParser; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; /** - * This filter will skip all following filters and write directly to the output. - * Should be used in case of a DOM that should not be effected by other filters, even though the elements match. + * This filter will bypass all following filters and write directly to the output. + * Should be used in case of a DOM that should not be effected by other filters, + * even though the elements match. * * @author Robert Scholte * @author Guillaume Nodet @@ -59,7 +60,22 @@ class FastForwardFilter extends BufferingParser } @Override - protected boolean accept() throws XmlPullParserException, IOException + public int next() throws XmlPullParserException, IOException + { + int event = super.next(); + filter(); + return event; + } + + @Override + public int nextToken() throws XmlPullParserException, IOException + { + int event = super.nextToken(); + filter(); + return event; + } + + protected void filter() throws XmlPullParserException, IOException { if ( xmlPullParser.getEventType() == START_TAG ) { @@ -70,7 +86,7 @@ class FastForwardFilter extends BufferingParser } else { - final String key = state.peek() + '/' + localName; + final String key = state.peekLast() + '/' + localName; switch ( key ) { case "execution/configuration": @@ -79,8 +95,11 @@ class FastForwardFilter extends BufferingParser case "profile/reports": case "project/reports": case "reportSet/configuration": + if ( domDepth == 0 ) + { + bypass( true ); + } domDepth++; - disable(); break; default: break; @@ -90,14 +109,20 @@ class FastForwardFilter extends BufferingParser } else if ( xmlPullParser.getEventType() == END_TAG ) { - domDepth--; - if ( domDepth == 0 ) + if ( domDepth > 0 ) { - enable(); + if ( --domDepth == 0 ) + { + bypass( false ); + } } - state.pop(); + state.removeLast(); } - return true; } + @Override + public void bypass( boolean bypass ) + { + this.bypass = bypass; + } } \ No newline at end of file diff --git a/maven-model-transform/src/main/java/org/apache/maven/model/transform/RawToConsumerPomXMLFilterFactory.java b/maven-model-transform/src/main/java/org/apache/maven/model/transform/RawToConsumerPomXMLFilterFactory.java index e36342b686..ec8bd4bdae 100644 --- a/maven-model-transform/src/main/java/org/apache/maven/model/transform/RawToConsumerPomXMLFilterFactory.java +++ b/maven-model-transform/src/main/java/org/apache/maven/model/transform/RawToConsumerPomXMLFilterFactory.java @@ -39,13 +39,12 @@ public class RawToConsumerPomXMLFilterFactory public final XmlPullParser get( XmlPullParser orgParser, Path projectPath ) { - XmlPullParser parser = orgParser; + // Ensure that xs:any elements aren't touched by next filters + XmlPullParser parser = orgParser instanceof FastForwardFilter + ? orgParser : new FastForwardFilter( orgParser ); parser = buildPomXMLFilterFactory.get( parser, projectPath ); - // Ensure that xs:any elements aren't touched by next filters - parser = new FastForwardFilter( parser ); - // Strip modules parser = new ModulesXMLFilter( parser ); // Adjust relativePath diff --git a/maven-model-transform/src/main/java/org/apache/maven/model/transform/pull/BufferingParser.java b/maven-model-transform/src/main/java/org/apache/maven/model/transform/pull/BufferingParser.java index f8235d80c1..e3662686ab 100644 --- a/maven-model-transform/src/main/java/org/apache/maven/model/transform/pull/BufferingParser.java +++ b/maven-model-transform/src/main/java/org/apache/maven/model/transform/pull/BufferingParser.java @@ -41,7 +41,7 @@ public class BufferingParser implements XmlPullParser protected XmlPullParser xmlPullParser; protected Deque events; protected Event current; - protected boolean disabled; + protected boolean bypass; @SuppressWarnings( "checkstyle:VisibilityModifier" ) public static class Event @@ -404,7 +404,7 @@ public class BufferingParser implements XmlPullParser throw new XmlPullParserException( "already reached end of XML input", this, null ); } int currentEvent = xmlPullParser.next(); - if ( disabled || accept() ) + if ( bypass() || accept() ) { return currentEvent; } @@ -430,7 +430,7 @@ public class BufferingParser implements XmlPullParser throw new XmlPullParserException( "already reached end of XML input", this, null ); } int currentEvent = xmlPullParser.nextToken(); - if ( accept() ) + if ( bypass() || accept() ) { return currentEvent; } @@ -531,22 +531,20 @@ public class BufferingParser implements XmlPullParser return true; } - protected void enable() + public void bypass( boolean bypass ) { - disabled = false; - } - - protected void disable() - { - if ( events != null && !events.isEmpty() ) + if ( bypass && events != null && !events.isEmpty() ) { throw new IllegalStateException( "Can not disable filter while processing" ); } - disabled = true; - if ( xmlPullParser instanceof BufferingParser ) - { - ( ( BufferingParser ) xmlPullParser ).disable(); - } + this.bypass = bypass; + } + + public boolean bypass() + { + return bypass + || ( xmlPullParser instanceof BufferingParser + && ( (BufferingParser) xmlPullParser ).bypass() ); } protected static String nullSafeAppend( String originalValue, String charSegment ) diff --git a/maven-model-transform/src/test/java/org/apache/maven/model/transform/ParentXMLFilterTest.java b/maven-model-transform/src/test/java/org/apache/maven/model/transform/ParentXMLFilterTest.java index 5c66e2f75d..98191cb1fc 100644 --- a/maven-model-transform/src/test/java/org/apache/maven/model/transform/ParentXMLFilterTest.java +++ b/maven-model-transform/src/test/java/org/apache/maven/model/transform/ParentXMLFilterTest.java @@ -33,7 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class ParentXMLFilterTest extends AbstractXMLFilterTests { - private Function filterCreator; + private Function filterCreator; @BeforeEach void reset() { @@ -41,22 +41,93 @@ public class ParentXMLFilterTest } @Override - protected ParentXMLFilter getFilter( XmlPullParser parser ) + protected XmlPullParser getFilter( XmlPullParser parser ) { - Function filterCreator = + Function filterCreator = (this.filterCreator != null ? this.filterCreator : this::createFilter); return filterCreator.apply(parser); } - protected ParentXMLFilter createFilter( XmlPullParser parser ) { + protected XmlPullParser createFilter( XmlPullParser parser ) { return createFilter( parser, x -> Optional.of(new RelativeProject("GROUPID", "ARTIFACTID", "1.0.0")), Paths.get( "pom.xml").toAbsolutePath() ); } - protected ParentXMLFilter createFilter( XmlPullParser parser, Function> pathMapper, Path projectPath ) { - ParentXMLFilter filter = new ParentXMLFilter( parser, pathMapper, projectPath ); - return filter; + protected XmlPullParser createFilter( XmlPullParser parser, Function> pathMapper, Path projectPath ) { + return new ParentXMLFilter( new FastForwardFilter( parser ), pathMapper, projectPath ); + } + + @Test + public void testWithFastForward() + throws Exception + { + String input = "" + + "" + + "" + + "" + + "" + + "" + + "GROUPID" + + "ARTIFACTID" + + "" + + "" + + "" + + "" + + "" + + ""; + String expected = input; + + String actual = transform( input ); + + assertEquals( expected, actual ); + } + + @Test + public void testWithFastForwardAfterByPass() + throws Exception + { + String input = "" + + "" + + "" + + "" + + "" + + "" + + "GROUPID" + + "ARTIFACTID" + + "" + + "" + + "" + + "" + + "" + + "" + + "GROUPID" + + "ARTIFACTID" + + "" + + ""; + String expected = "" + + "" + + "" + + "" + + "" + + "" + + "GROUPID" + + "ARTIFACTID" + + "" + + "" + + "" + + "" + + "" + + "" + + "GROUPID" + + "ARTIFACTID" + + "1.0.0" + + "" + + ""; + + String actual = transform( input ); + + assertEquals( expected, actual ); } @Test