This is an automated email from the ASF dual-hosted git repository. rfscholte pushed a commit to branch maven-xml in repository https://gitbox.apache.org/repos/asf/maven-studies.git
commit fa5060f6d50695dee9eb78fb1d31a828e0fefbf8 Author: rfscholte <rfscho...@apache.org> AuthorDate: Sat Oct 27 14:28:58 2018 +0200 Improve ParentXMLFilter --- .../maven/xml/filters/ParentVersionResolver.java | 42 ----- .../apache/maven/xml/filters/ParentXMLFilter.java | 209 ++++++++++----------- .../apache/maven/xml/filters/ModelFilterTest.java | 6 +- .../maven/xml/filters/ParentXMLFilterTest.java | 9 +- 4 files changed, 111 insertions(+), 155 deletions(-) diff --git a/src/main/java/org/apache/maven/xml/filters/ParentVersionResolver.java b/src/main/java/org/apache/maven/xml/filters/ParentVersionResolver.java deleted file mode 100644 index 74ec4b0..0000000 --- a/src/main/java/org/apache/maven/xml/filters/ParentVersionResolver.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.apache.maven.xml.filters; - -/* - * 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. - */ - -/** - * - * @author Robert Scholte - * @since 4.0.0 - */ -@FunctionalInterface -public interface ParentVersionResolver -{ - /** - * Resolve the version based on the relative path. - * The groupId and artifactId can be used to confirm the expected parent. - * Return {@code null} if the version could not be resolved, so the xml will - * stay as it is and the ModelBuilder will make it fail. - * - * @param relativePath the relativePath - * @param groupId the groupId - * @param artifactId the artifactId - * @return the resolved version, otherwise {@code null} - */ - String resolve( String relativePath, String groupId, String artifactId ); -} diff --git a/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java b/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java index 1c6bb4b..7bebb54 100644 --- a/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java +++ b/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java @@ -19,8 +19,14 @@ package org.apache.maven.xml.filters; * under the License. */ +import java.nio.file.Path; +import java.nio.file.Paths; + import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; import org.apache.maven.xml.SAXEvent; import org.apache.maven.xml.SAXEventFactory; @@ -49,25 +55,27 @@ public class ParentXMLFilter private String groupId; private String artifactId; + + private String relativePath; - /** - * If parent has no version-element, rewrite relativePath to version.<br> - * If parent has version-element, then remove relativePath.<br> - * Order of elements must stay the same. - */ private boolean hasVersion; - private String resolvedVersion; + private Optional<RelativeProject> resolvedParent; private char[] linebreak; private List<SAXEvent> saxEvents = new ArrayList<>(); private SAXEventFactory eventFactory; + + private final Function<Path, Optional<RelativeProject>> relativePathMapper; - private final ParentVersionResolver relativePathMapper; - - public ParentXMLFilter( ParentVersionResolver relativePathMapper ) + /** + * + * + * @param relativePathMapper + */ + public ParentXMLFilter( Function<Path, Optional<RelativeProject>> relativePathMapper ) { this.relativePathMapper = relativePathMapper; } @@ -90,7 +98,7 @@ public class ParentXMLFilter saxEvents.add( () -> { - if ( !( "relativePath".equals( eventState ) && hasVersion ) ) + if ( !( "relativePath".equals( eventState ) && resolvedParent.isPresent() ) ) { event.execute(); } @@ -115,36 +123,10 @@ public class ParentXMLFilter { state = localName; - switch ( localName ) - { - case "relativePath": - processEvent( () -> - { - if ( resolvedVersion != null ) - { - String versionQName = SAXEventUtils.renameQName( qName, "version" ); - - getEventFactory().startElement( uri, "version", versionQName, null ).execute(); - } - else - { - getEventFactory().startElement( uri, localName, qName, atts ).execute(); - } - } ); - break; - case "version": - hasVersion = true; - - // fall through - default: - processEvent( getEventFactory().startElement( uri, localName, qName, atts ) ); - break; - } - } - else - { - super.startElement( uri, localName, qName, atts ); + hasVersion |= "version".equals( localName ); } + + processEvent( getEventFactory().startElement( uri, localName, qName, atts ) ); } @Override @@ -172,22 +154,8 @@ public class ParentXMLFilter System.arraycopy( ch, start, linebreak, 0, l ); break; case "relativePath": - String relativePath = new String( ch, start, length ); - resolvedVersion = relativePathToVersion( relativePath ); - - processEvent( () -> - { - if ( resolvedVersion != null ) - { - getEventFactory().characters( resolvedVersion.toCharArray(), 0, - resolvedVersion.length() ).execute(); - } - else - { - getEventFactory().characters( ch, start, length ).execute(); - } - } ); - return; + relativePath = new String( ch, start, length ); + break; case "groupId": groupId = new String( ch, start, length ); break; @@ -197,12 +165,9 @@ public class ParentXMLFilter default: break; } - processEvent( getEventFactory().characters( ch, start, length ) ); - } - else - { - super.characters( ch, start, length ); } + + processEvent( getEventFactory().characters( ch, start, length ) ); } @Override @@ -216,54 +181,15 @@ public class ParentXMLFilter public void endElement( String uri, final String localName, String qName ) throws SAXException { - if ( !parsingParent ) - { - super.endElement( uri, localName, qName ); - } - else + if ( parsingParent ) { switch ( localName ) { - case "relativePath": - processEvent( () -> - { - if ( resolvedVersion != null ) - { - String versionQName = SAXEventUtils.renameQName( qName, "version" ); - getEventFactory().endElement( uri, "version", versionQName ).execute(); - } - else - { - getEventFactory().endElement( uri, localName, qName ).execute(); - } - } ); - break; case "parent": - if ( !hasVersion && resolvedVersion == null && groupId != null && artifactId != null ) + if ( !hasVersion || relativePath != null ) { - resolvedVersion = relativePathToVersion( "../pom.xml" ); - - if ( resolvedVersion != null ) - { - processEvent( () -> - { - String versionQName = SAXEventUtils.renameQName( qName, "version" ); - -// getEventFactory().characters( indent, 0, indent.length ).execute(); - - getEventFactory().startElement( uri, "version", versionQName, null ).execute(); - - getEventFactory().characters( resolvedVersion.toCharArray(), 0, - resolvedVersion.length() ).execute(); - - getEventFactory().endElement( uri, "version", versionQName ).execute(); - - if ( linebreak != null ) - { - getEventFactory().characters( linebreak, 0, linebreak.length ).execute(); - } - } ); - } + resolvedParent = + resolveRelativePath( Paths.get( Objects.toString( relativePath, "../pom.xml" ) ) ); } // not with streams due to checked SAXException @@ -271,15 +197,37 @@ public class ParentXMLFilter { saxEvent.execute(); } - parsingParent = false; - // fall through + if ( !hasVersion && resolvedParent.isPresent() ) + { + String versionQName = SAXEventUtils.renameQName( qName, "version" ); + + getEventFactory().startElement( uri, "version", versionQName, null ).execute(); + + String resolvedParentVersion = resolvedParent.get().getVersion(); + + getEventFactory().characters( resolvedParentVersion.toCharArray(), 0, + resolvedParentVersion.length() ).execute(); + + getEventFactory().endElement( uri, "version", versionQName ).execute(); + + if ( linebreak != null ) + { + getEventFactory().characters( linebreak, 0, linebreak.length ).execute(); + } + } + + parsingParent = false; + break; default: - processEvent( getEventFactory().endElement( uri, localName, qName ) ); break; } - } + + processEvent( getEventFactory().endElement( uri, localName, qName ) ); + + // for this simple structure resetting to parent it sufficient + state = "parent"; } @Override @@ -338,8 +286,51 @@ public class ParentXMLFilter processEvent( getEventFactory().startPrefixMapping( prefix, uri ) ); } - protected String relativePathToVersion( String relativePath ) + protected Optional<RelativeProject> resolveRelativePath( Path relativePath ) + { + Optional<RelativeProject> mappedProject = relativePathMapper.apply( relativePath ); + + if ( mappedProject.isPresent() ) + { + RelativeProject project = mappedProject.get(); + + if ( Objects.equals( groupId, project.getGroupId() ) + && Objects.equals( artifactId, project.getArtifactId() ) ) + { + return mappedProject; + } + } + return Optional.empty(); + } + + static class RelativeProject { - return relativePathMapper.resolve( relativePath, groupId, artifactId ); + private String groupId; + + private String artifactId; + + private String version; + + RelativeProject( String groupId, String artifactId, String version ) + { + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + } + + public String getGroupId() + { + return groupId; + } + + public String getArtifactId() + { + return artifactId; + } + + public String getVersion() + { + return version; + } } } diff --git a/src/test/java/org/apache/maven/xml/filters/ModelFilterTest.java b/src/test/java/org/apache/maven/xml/filters/ModelFilterTest.java index 4c40baf..5d08009 100644 --- a/src/test/java/org/apache/maven/xml/filters/ModelFilterTest.java +++ b/src/test/java/org/apache/maven/xml/filters/ModelFilterTest.java @@ -21,6 +21,8 @@ package org.apache.maven.xml.filters; import static org.junit.Assert.assertEquals; +import java.util.Optional; + import org.junit.Before; import org.junit.Test; @@ -40,7 +42,9 @@ public class ModelFilterTest extends AbstractXMLFilterTests // order matters!! filter.addFilter( new ThisXMLFilter( x -> "1.0.0" ) ); filter.addFilter( new FastForwardFilter() ); - filter.addFilter( new ParentXMLFilter( ( r, g, a ) -> "1.0.0" ) ); + filter.addFilter( new ParentXMLFilter( x -> Optional.of( new ParentXMLFilter.RelativeProject( "GROUPID", + "ARTIFACTID", + "1.0.0" ) ) ) ); filter.addFilter( new ReactorDependencyXMLFilter( x -> "2.0.0" ) ); } diff --git a/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java b/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java index 060b320..24229fb 100644 --- a/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java +++ b/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java @@ -21,6 +21,8 @@ package org.apache.maven.xml.filters; import static org.junit.Assert.assertEquals; +import java.util.Optional; + import org.junit.Before; import org.junit.Test; import org.xml.sax.XMLFilter; @@ -32,7 +34,9 @@ public class ParentXMLFilterTest extends AbstractXMLFilterTests @Before public void setUp() { - filter = new ParentXMLFilter( ( r, g, a ) -> "1.0.0" ); + filter = new ParentXMLFilter( x -> Optional.of( new ParentXMLFilter.RelativeProject( "GROUPID", + "ARTIFACTID", + "1.0.0" ) ) ); } @Test @@ -99,7 +103,7 @@ public class ParentXMLFilterTest extends AbstractXMLFilterTests @Test public void testInvalidRelativePath() throws Exception { - XMLFilter filter = new ParentXMLFilter( ( r, g, a ) -> null ); + XMLFilter filter = new ParentXMLFilter( x -> Optional.ofNullable( null ) ); String input = "<parent>" + "<groupId>GROUPID</groupId>" @@ -140,7 +144,6 @@ public class ParentXMLFilterTest extends AbstractXMLFilterTests + "<relativePath:groupId>GROUPID</relativePath:groupId>" + "<relativePath:artifactId>ARTIFACTID</relativePath:artifactId>" + "<relativePath:relativePath>RELATIVEPATH</relativePath:relativePath>" - + "<relativePath:version>1.0.0</relativePath:version>" + "</relativePath:parent>"; String expected = "<relativePath:parent xmlns:relativePath=\"relativePath\">" + "<relativePath:groupId>GROUPID</relativePath:groupId>"