aboutsummaryrefslogtreecommitdiffstats
path: root/api/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2014-05-13 16:15:09 +0200
committerSven Gothel <[email protected]>2014-05-13 23:05:54 +0200
commitc4930f9d23ae7515434942836f628f767411876c (patch)
tree04902832a17e49e5023726a8f52e56e82527a4ce /api/src
parent3f680fcda4007d7d00931846839eaf37a244cfaa (diff)
Solve issues w/ 'simplifyRegularExpression(..)': Allow passing regexp directly (Comparer, ..) and hence skip production of regexp
In jogamp, we like to exclude 'jogamp/**' packages, however 'simplifyRegularExpression(..)' allows this to match 'com/jogamp/lala/Lala'. This is enforced by tests in 'DifferenceAccumulatingHandlerTest' and hence we have to consider this to be desired !? (IMHO this is a bug). However, to not break backward capability, this patch allows passing a flag for the includes and excludes whether they are regexp already and hence we can skip 'simplifyRegularExpression(..)' and avoid this dilema.
Diffstat (limited to 'api/src')
-rwxr-xr-xapi/src/main/java/org/semver/Comparer.java20
-rwxr-xr-xapi/src/main/java/org/semver/jardiff/DifferenceAccumulatingHandler.java67
2 files changed, 56 insertions, 31 deletions
diff --git a/api/src/main/java/org/semver/Comparer.java b/api/src/main/java/org/semver/Comparer.java
index d682994..9d6ec06 100755
--- a/api/src/main/java/org/semver/Comparer.java
+++ b/api/src/main/java/org/semver/Comparer.java
@@ -24,13 +24,12 @@ import java.util.Set;
import org.osjava.jardiff.DiffCriteria;
import org.osjava.jardiff.DiffException;
import org.osjava.jardiff.JarDiff;
-import org.osjava.jardiff.SimpleDiffCriteria;
import org.semver.jardiff.DifferenceAccumulatingHandler;
/**
- *
+ *
* Allows to compare content of JARs.
- *
+ *
*/
@NotThreadSafe
public class Comparer {
@@ -39,10 +38,17 @@ public class Comparer {
private final File previousJAR;
private final File currentJAR;
private final Set<String> includes;
+ private final boolean includesAreRegExp;
private final Set<String> excludes;
-
+ private final boolean excludesAreRegExp;
+
public Comparer(final DiffCriteria diffCriteria, final File previousJAR, final File currentJAR,
final Set<String> includes, final Set<String> excludes) {
+ this(diffCriteria, previousJAR, currentJAR, includes, false, excludes, false);
+ }
+
+ public Comparer(final DiffCriteria diffCriteria, final File previousJAR, final File currentJAR,
+ final Set<String> includes, final boolean includesAreRegExp, final Set<String> excludes, final boolean excludesAreRegExp) {
if (!previousJAR.isFile()) {
throw new IllegalArgumentException("<"+previousJAR+"> is not a valid file");
}
@@ -54,19 +60,21 @@ public class Comparer {
this.previousJAR = previousJAR;
this.currentJAR = currentJAR;
this.includes = includes;
+ this.includesAreRegExp = includesAreRegExp;
this.excludes = excludes;
+ this.excludesAreRegExp = excludesAreRegExp;
}
/**
* @return all {@link Difference} between both JARs
* @throws IOException
*/
- public final Delta diff() throws IOException {
+ public final Delta diff() throws IOException {
try {
final JarDiff jarDiff = new JarDiff();
jarDiff.loadOldClasses(this.previousJAR);
jarDiff.loadNewClasses(this.currentJAR);
- final DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler(this.includes, this.excludes);
+ final DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler(this.includes, this.includesAreRegExp, this.excludes, this.excludesAreRegExp);
jarDiff.diff(handler, diffCriteria);
return handler.getDelta();
} catch (DiffException e) {
diff --git a/api/src/main/java/org/semver/jardiff/DifferenceAccumulatingHandler.java b/api/src/main/java/org/semver/jardiff/DifferenceAccumulatingHandler.java
index 53b2c54..edfedbb 100755
--- a/api/src/main/java/org/semver/jardiff/DifferenceAccumulatingHandler.java
+++ b/api/src/main/java/org/semver/jardiff/DifferenceAccumulatingHandler.java
@@ -45,7 +45,9 @@ public final class DifferenceAccumulatingHandler extends AbstractDiffHandler {
private static final boolean DEBUG = false;
private String currentClassName;
private final Set<String> includes;
+ private final boolean includesAreRegExp;
private final Set<String> excludes;
+ private final boolean excludesAreRegExp;
private final Set<Difference> differences = new HashSet<Difference>();
public DifferenceAccumulatingHandler() {
@@ -53,14 +55,20 @@ public final class DifferenceAccumulatingHandler extends AbstractDiffHandler {
}
public DifferenceAccumulatingHandler(@Nonnull final Set<String> includes, @Nonnull final Set<String> excludes) {
+ this(includes, false, excludes, false);
+ }
+ public DifferenceAccumulatingHandler(@Nonnull final Set<String> includes, final boolean includesAreRegExp,
+ @Nonnull final Set<String> excludes, final boolean excludesAreRegExp) {
this.includes = includes;
+ this.includesAreRegExp = includesAreRegExp;
this.excludes = excludes;
+ this.excludesAreRegExp = excludesAreRegExp;
}
public String getCurrentClassName() {
return this.currentClassName;
}
-
+
@Override
public void startDiff(final String previous, final String current) throws DiffException {
}
@@ -252,46 +260,55 @@ public final class DifferenceAccumulatingHandler extends AbstractDiffHandler {
}
protected boolean isClassConsideredImpl( final String className ) {
for ( String exclude : this.excludes ) {
- if ( exclude.contains( "/**/" ) ) {
- exclude = exclude.replaceAll( "/\\*\\*/", "{0,1}**/" );
- }
- if ( exclude.contains( "/*/" ) ) {
- exclude = exclude.replaceAll( "/\\*/", "{0,1}*/{0,1}" );
+ final Pattern excludePattern;
+ if( !excludesAreRegExp ) {
+ if ( exclude.contains( "/**/" ) ) {
+ exclude = exclude.replaceAll( "/\\*\\*/", "{0,1}**/" );
+ }
+ if ( exclude.contains( "/*/" ) ) {
+ exclude = exclude.replaceAll( "/\\*/", "{0,1}*/{0,1}" );
+ }
+ excludePattern = simplifyRegularExpression( exclude, false );
+ } else {
+ excludePattern = Pattern.compile( exclude );
}
- Pattern excludePattern = simplifyRegularExpression( exclude, false );
-
- Matcher excludeMatcher = excludePattern.matcher( className );
-
+ final Matcher excludeMatcher = excludePattern.matcher( className );
+
while ( excludeMatcher.find() ) {
return false;
- }
- }
+ }
+ }
if ( !this.includes.isEmpty() ) {
for ( String include : this.includes ) {
- if ( include.contains( "/**/" ) ) {
- include = include.replaceAll( "/\\*\\*/", "{0,1}**/" );
- }
- if ( include.contains( "/*/" ) ) {
- include = include.replaceAll( "/\\*/", "{0,1}*/{0,1}" );
+ final Pattern includePattern;
+ if( !includesAreRegExp ) {
+ if ( include.contains( "/**/" ) ) {
+ include = include.replaceAll( "/\\*\\*/", "{0,1}**/" );
+ }
+ if ( include.contains( "/*/" ) ) {
+ include = include.replaceAll( "/\\*/", "{0,1}*/{0,1}" );
+ }
+ includePattern = simplifyRegularExpression( include, false );
+ } else {
+ includePattern = Pattern.compile( include );
}
- Pattern includePattern = simplifyRegularExpression( include, false );
- Matcher includeMatcher = includePattern.matcher( className );
-
+ final Matcher includeMatcher = includePattern.matcher( className );
+
while ( includeMatcher.find() ) {
return false;
- }
+ }
}
}
return true;
}
-
+
/**
- *
+ *
* Simplifies the given regular expression by the following pattern:<br>
* All substrings not containing "{0,1}", "*" and "?" get surrounded by "\\Q" and "\\E". Then all occurrences of
* "**" are replaced by ".*", "*" with "[^/]*" and all occurrences of "?" are replaced by "." In the end a "$" will
* be appended.
- *
+ *
* @param regEx the regular expression which is in a simple form.
* @return the simple regular expression converted to a normal regular expression.
*/
@@ -299,7 +316,7 @@ public final class DifferenceAccumulatingHandler extends AbstractDiffHandler {
final StringBuilder strBuild = new StringBuilder();
final Pattern p = Pattern.compile( "\\{0,1\\}|\\*|\\?|[[^*^?^{^}]|^]+", Pattern.CASE_INSENSITIVE );
final Matcher m = p.matcher( regEx );
-
+
while ( m.find() ) {
final String token = m.group();
if ( token.equals( "*" ) || token.equals( "?" ) ) { //$NON-NLS-1$ //$NON-NLS-2$