diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java index c0ad57d37830..33bbe1a54385 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/ComparableVersion.java @@ -150,6 +150,30 @@ public int compareTo( Item item ) } } + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + IntItem intItem = (IntItem) o; + + return value == intItem.value; + + } + + @Override + public int hashCode() + { + return value; + } + @Override public String toString() { @@ -211,6 +235,30 @@ public int compareTo( Item item ) } } + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + LongItem longItem = (LongItem) o; + + return value == longItem.value; + + } + + @Override + public int hashCode() + { + return (int) ( value ^ ( value >>> 32 ) ); + } + @Override public String toString() { @@ -272,6 +320,29 @@ public int compareTo( Item item ) } @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + BigIntegerItem that = (BigIntegerItem) o; + + return value.equals( that.value ); + + } + + @Override + public int hashCode() + { + return value.hashCode(); + } + public String toString() { return value.toString(); @@ -384,6 +455,29 @@ public int compareTo( Item item ) } @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( o == null || getClass() != o.getClass() ) + { + return false; + } + + StringItem that = (StringItem) o; + + return value.equals( that.value ); + + } + + @Override + public int hashCode() + { + return value.hashCode(); + } + public String toString() { return value; @@ -583,8 +677,6 @@ else if ( Character.isDigit( c ) ) list = (ListItem) stack.pop(); list.normalize(); } - - canonical = items.toString(); } private static Item parseItem( boolean isDigit, String buf ) @@ -631,19 +723,23 @@ public String toString() public String getCanonical() { + if ( canonical == null ) + { + canonical = items.toString(); + } return canonical; } @Override public boolean equals( Object o ) { - return ( o instanceof ComparableVersion ) && canonical.equals( ( (ComparableVersion) o ).canonical ); + return ( o instanceof ComparableVersion ) && items.equals( ( (ComparableVersion) o ).items ); } @Override public int hashCode() { - return canonical.hashCode(); + return items.hashCode(); } // CHECKSTYLE_OFF: LineLength diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java index 81a52b8ae801..0739098c3cc2 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/versioning/DefaultArtifactVersion.java @@ -20,8 +20,8 @@ */ import java.util.StringTokenizer; -import java.util.regex.Pattern; -import java.util.NoSuchElementException; + +import static org.apache.commons.lang3.math.NumberUtils.isDigits; /** * Default implementation of artifact versioning. @@ -128,18 +128,15 @@ public final void parseVersion( String version ) if ( part2 != null ) { - try + if ( part2.length() == 1 || !part2.startsWith( "0" ) ) { - if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) ) - { - buildNumber = Integer.valueOf( part2 ); - } - else + buildNumber = tryParseInt( part2 ); + if ( buildNumber == null ) { qualifier = part2; } } - catch ( NumberFormatException e ) + else { qualifier = part2; } @@ -147,11 +144,8 @@ public final void parseVersion( String version ) if ( ( !part1.contains( "." ) ) && !part1.startsWith( "0" ) ) { - try - { - majorVersion = Integer.valueOf( part1 ); - } - catch ( NumberFormatException e ) + majorVersion = tryParseInt( part1 ); + if ( majorVersion == null ) { // qualifier is the whole version, including "-" qualifier = version; @@ -163,30 +157,42 @@ public final void parseVersion( String version ) boolean fallback = false; StringTokenizer tok = new StringTokenizer( part1, "." ); - try + if ( tok.hasMoreTokens() ) { majorVersion = getNextIntegerToken( tok ); - if ( tok.hasMoreTokens() ) - { - minorVersion = getNextIntegerToken( tok ); - } - if ( tok.hasMoreTokens() ) + if ( majorVersion == null ) { - incrementalVersion = getNextIntegerToken( tok ); + fallback = true; } - if ( tok.hasMoreTokens() ) + } + else + { + fallback = true; + } + if ( tok.hasMoreTokens() ) + { + minorVersion = getNextIntegerToken( tok ); + if ( minorVersion == null ) { - qualifier = tok.nextToken(); - fallback = Pattern.compile( "\\d+" ).matcher( qualifier ).matches(); + fallback = true; } - - // string tokenizer won't detect these and ignores them - if ( part1.contains( ".." ) || part1.startsWith( "." ) || part1.endsWith( "." ) ) + } + if ( tok.hasMoreTokens() ) + { + incrementalVersion = getNextIntegerToken( tok ); + if ( incrementalVersion == null ) { fallback = true; } } - catch ( NumberFormatException e ) + if ( tok.hasMoreTokens() ) + { + qualifier = tok.nextToken(); + fallback = isDigits( qualifier ); + } + + // string tokenizer won't detect these and ignores them + if ( part1.contains( ".." ) || part1.startsWith( "." ) || part1.endsWith( "." ) ) { fallback = true; } @@ -204,19 +210,33 @@ public final void parseVersion( String version ) } private static Integer getNextIntegerToken( StringTokenizer tok ) + { + String s = tok.nextToken(); + if ( ( s.length() > 1 ) && s.startsWith( "0" ) ) + { + return null; + } + return tryParseInt( s ); + } + + private static Integer tryParseInt( String s ) { try { - String s = tok.nextToken(); - if ( ( s.length() > 1 ) && s.startsWith( "0" ) ) + if ( !isDigits( s ) ) + { + return null; + } + long longValue = Long.parseLong( s ); + if ( longValue > Integer.MAX_VALUE ) { - throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" ); + return null; } - return Integer.valueOf( s ); + return (int) longValue; } - catch ( NoSuchElementException e ) + catch ( NumberFormatException e ) { - throw new NumberFormatException( "Number is invalid" ); + return null; } } diff --git a/maven-core/src/main/java/org/apache/maven/artifact/handler/manager/DefaultArtifactHandlerManager.java b/maven-core/src/main/java/org/apache/maven/artifact/handler/manager/DefaultArtifactHandlerManager.java index 9394132e21db..a4b2f4473990 100644 --- a/maven-core/src/main/java/org/apache/maven/artifact/handler/manager/DefaultArtifactHandlerManager.java +++ b/maven-core/src/main/java/org/apache/maven/artifact/handler/manager/DefaultArtifactHandlerManager.java @@ -39,11 +39,11 @@ public class DefaultArtifactHandlerManager @Requirement( role = ArtifactHandler.class ) private Map artifactHandlers; - private Map unmanagedHandlers = new ConcurrentHashMap<>(); + private Map allHandlers = new ConcurrentHashMap<>(); public ArtifactHandler getArtifactHandler( String type ) { - ArtifactHandler handler = unmanagedHandlers.get( type ); + ArtifactHandler handler = allHandlers.get( type ); if ( handler == null ) { @@ -53,6 +53,10 @@ public ArtifactHandler getArtifactHandler( String type ) { handler = new DefaultArtifactHandler( type ); } + else + { + allHandlers.put( type, handler ); + } } return handler; @@ -61,7 +65,7 @@ public ArtifactHandler getArtifactHandler( String type ) public void addHandlers( Map handlers ) { // legacy support for maven-gpg-plugin:1.0 - unmanagedHandlers.putAll( handlers ); + allHandlers.putAll( handlers ); } @Deprecated diff --git a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java index f58d03af2815..749dd2f69c1e 100644 --- a/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java +++ b/maven-model-builder/src/main/java/org/apache/maven/model/validation/DefaultModelValidator.java @@ -73,10 +73,6 @@ public class DefaultModelValidator AbstractStringBasedModelInterpolator.CHANGELIST_PROPERTY, AbstractStringBasedModelInterpolator.SHA1_PROPERTY ); - private static final Pattern ID_REGEX = Pattern.compile( "[A-Za-z0-9_\\-.]+" ); - - private static final Pattern ID_WITH_WILDCARDS_REGEX = Pattern.compile( "[A-Za-z0-9_\\-.?*]+" ); - private static final String ILLEGAL_FS_CHARS = "\\/:\"<>|?*"; private static final String ILLEGAL_VERSION_CHARS = ILLEGAL_FS_CHARS; @@ -829,14 +825,33 @@ private boolean validateId( String fieldName, ModelProblemCollector problems, Se } else { - boolean match = ID_REGEX.matcher( id ).matches(); - if ( !match ) + if ( !isValidId( id ) ) { addViolation( problems, severity, version, fieldName, sourceHint, "with value '" + id + "' does not match a valid id pattern.", tracker ); + return false; + } + return true; + } + } + + private boolean isValidId( String id ) + { + for ( int i = 0; i < id.length(); i++ ) + { + char c = id.charAt( i ); + if ( !isValidIdCharacter( c ) ) + { + return false; } - return match; } + return true; + } + + + private boolean isValidIdCharacter( char c ) + { + return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.'; } private boolean validateIdWithWildcards( String fieldName, ModelProblemCollector problems, Severity severity, @@ -849,16 +864,34 @@ private boolean validateIdWithWildcards( String fieldName, ModelProblemCollector } else { - boolean match = ID_WITH_WILDCARDS_REGEX.matcher( id ).matches(); - if ( !match ) + if ( !isValidIdWithWildCards( id ) ) { addViolation( problems, severity, version, fieldName, sourceHint, "with value '" + id + "' does not match a valid id pattern.", tracker ); + return false; } - return match; + return true; } } + private boolean isValidIdWithWildCards( String id ) + { + for ( int i = 0; i < id.length(); i++ ) + { + char c = id.charAt( i ); + if ( !isValidIdWithWildCardCharacter( c ) ) + { + return false; + } + } + return true; + } + + private boolean isValidIdWithWildCardCharacter( char c ) + { + return isValidIdCharacter( c ) || c == '?' || c == '*'; + } + private boolean validateStringNoExpression( String fieldName, ModelProblemCollector problems, Severity severity, Version version, String string, InputLocationTracker tracker ) {