-
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.
Add combined expression evaluator to file activation property
- Loading branch information
Showing
9 changed files
with
421 additions
and
20 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
43 changes: 43 additions & 0 deletions
43
...-model-builder/src/main/java/org/apache/maven/model/condition/CombinedValueEvaluator.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,43 @@ | ||
package org.apache.maven.model.condition; | ||
|
||
/* | ||
* 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 Loïc B. | ||
*/ | ||
public interface CombinedValueEvaluator | ||
{ | ||
|
||
/** | ||
* Parses a string representing a condition, and evaluates its result using simple boolean logic. | ||
* | ||
* @param expression Boolean expression to be evaluated. Format : <br/> | ||
* all(condition1,condition2,...) or and(condition1,condition2,...) : will be true if all conditions are | ||
* true<br/> | ||
* any(condition1,condition2,...) or or(condition1,condition2,...) : will be true if at least one of the | ||
* conditions is true Nested operators are not supported.<br/> | ||
* If there is no condition expressed, the string is simply passed to the evaluator as-is. | ||
* @param evaluator Actual evaluator for a single condition. Will be called once for each condition found in the | ||
* expression | ||
* @return result of evaluation, depending on expression contents | ||
*/ | ||
boolean evaluate( String expression, SingleValueEvaluator evaluator ); | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
...builder/src/main/java/org/apache/maven/model/condition/DefaultCombinedValueEvaluator.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,69 @@ | ||
package org.apache.maven.model.condition; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.codehaus.plexus.component.annotations.Component; | ||
|
||
/** | ||
* @author Loïc B. | ||
*/ | ||
@Component( role = CombinedValueEvaluator.class ) | ||
public class DefaultCombinedValueEvaluator | ||
implements CombinedValueEvaluator | ||
{ | ||
private static final Pattern PATTERN_AND = Pattern.compile( "^(?:all|and)\\((?:([^,]*),)*([^,]*)\\)$" ); | ||
|
||
private static final Pattern PATTERN_OR = Pattern.compile( "^(?:any|or)\\((?:([^,]*),)*([^,]*)\\)$" ); | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.apache.maven.model.profile.activation.CombinedValueEvaluator#evaluate(java.lang.String, | ||
* org.apache.maven.model.profile.activation.SingleValueEvaluator) | ||
*/ | ||
@Override | ||
public boolean evaluate( String expression, SingleValueEvaluator evaluator ) | ||
{ | ||
Matcher andMatcher = PATTERN_AND.matcher( expression ); | ||
if ( andMatcher.matches() ) | ||
{ | ||
boolean result = true; | ||
for ( int i = 1; i <= andMatcher.groupCount(); i++ ) | ||
{ | ||
result &= evaluator.evaluate( andMatcher.group( i ) ); | ||
} | ||
return result; | ||
} | ||
Matcher orMatcher = PATTERN_OR.matcher( expression ); | ||
if ( orMatcher.matches() ) | ||
{ | ||
boolean result = false; | ||
for ( int i = 1; i <= andMatcher.groupCount(); i++ ) | ||
{ | ||
result |= evaluator.evaluate( orMatcher.group( i ) ); | ||
} | ||
return result; | ||
} | ||
// Default | ||
return evaluator.evaluate( expression ); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
maven-model-builder/src/main/java/org/apache/maven/model/condition/SingleValueEvaluator.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,32 @@ | ||
package org.apache.maven.model.condition; | ||
|
||
/* | ||
* 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 Loïc B. | ||
*/ | ||
public interface SingleValueEvaluator | ||
{ | ||
/** | ||
* Evaluiates the expression and returns a boolean result. Expression evaluation and intended result is | ||
* implementation-dependent. | ||
*/ | ||
boolean evaluate( String expression ); | ||
} |
78 changes: 78 additions & 0 deletions
78
...ilder/src/main/java/org/apache/maven/model/profile/activation/FileExistenceEvaluator.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,78 @@ | ||
package org.apache.maven.model.profile.activation; | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
import java.io.File; | ||
|
||
import org.apache.maven.model.condition.SingleValueEvaluator; | ||
import org.apache.maven.model.path.PathTranslator; | ||
|
||
/** | ||
* @author Loïc B. | ||
*/ | ||
public class FileExistenceEvaluator | ||
implements SingleValueEvaluator | ||
{ | ||
|
||
private final boolean missing; | ||
|
||
private final PathTranslator pathTranslator; | ||
|
||
private final File basedir; | ||
|
||
/** | ||
* Creates the FileExistenceEvaluator for missing or existing files. | ||
* | ||
* @param missing true if the file should be missing, false if it should be existing | ||
* @param pathTranslator path translator implementation to be used | ||
*/ | ||
public FileExistenceEvaluator( boolean missing, PathTranslator pathTranslator, File basedir ) | ||
{ | ||
this.missing = missing; | ||
this.pathTranslator = pathTranslator; | ||
this.basedir = basedir; | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see org.apache.maven.model.profile.activation.SingleValueEvaluator#evaluate(java.lang.String) | ||
*/ | ||
@Override | ||
public boolean evaluate( String path ) | ||
{ | ||
boolean reversed = false; | ||
if ( path.startsWith( "!" ) ) | ||
{ | ||
reversed = true; | ||
path = path.substring( 1 ); | ||
} | ||
path = pathTranslator.alignToBaseDirectory( path, basedir ); | ||
File f = new File( path ); | ||
|
||
if ( !f.isAbsolute() ) | ||
{ | ||
return false; | ||
} | ||
|
||
boolean fileExists = f.exists(); | ||
|
||
return missing ^ reversed ? !fileExists : fileExists; | ||
} | ||
|
||
} |
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
Oops, something went wrong.