Skip to content

Commit

Permalink
Add combined expression evaluator to file activation property
Browse files Browse the repository at this point in the history
  • Loading branch information
lbndev authored and Loïc Bardon committed Jul 27, 2018
1 parent 7fb467a commit 2fc9d54
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.maven.model.Model;
import org.apache.maven.model.composition.DefaultDependencyManagementImporter;
import org.apache.maven.model.composition.DependencyManagementImporter;
import org.apache.maven.model.condition.CombinedValueEvaluator;
import org.apache.maven.model.condition.DefaultCombinedValueEvaluator;
import org.apache.maven.model.inheritance.DefaultInheritanceAssembler;
import org.apache.maven.model.inheritance.InheritanceAssembler;
import org.apache.maven.model.interpolation.ModelInterpolator;
Expand Down Expand Up @@ -111,7 +113,9 @@ protected ProfileActivator[] newProfileActivators()
return new ProfileActivator[]
{
new JdkVersionProfileActivator(), new OperatingSystemProfileActivator(),
new PropertyProfileActivator(), new FileProfileActivator().setPathTranslator( newPathTranslator() )
new PropertyProfileActivator(),
new FileProfileActivator().setPathTranslator( newPathTranslator() )
.setCombinedValueEvaluator( newCombinedValueEvaluator() )
};
}

Expand All @@ -125,6 +129,11 @@ protected PathTranslator newPathTranslator()
return new DefaultPathTranslator();
}

protected CombinedValueEvaluator newCombinedValueEvaluator()
{
return new DefaultCombinedValueEvaluator();
}

protected ModelInterpolator newModelInterpolator()
{
UrlNormalizer normalizer = newUrlNormalizer();
Expand Down Expand Up @@ -237,7 +246,9 @@ private static class StubLifecycleBindingsInjector
{

@Override
public void injectLifecycleBindings( Model model, ModelBuildingRequest request, ModelProblemCollector problems )
public void injectLifecycleBindings( Model model,
ModelBuildingRequest request,
ModelProblemCollector problems )
{
}

Expand Down
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 );

}
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 );
}
}
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 );
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
import org.apache.maven.model.Activation;
import org.apache.maven.model.ActivationFile;
import org.apache.maven.model.Profile;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.building.ModelProblem.Severity;
import org.apache.maven.model.building.ModelProblem.Version;
import org.apache.maven.model.building.ModelProblemCollector;
import org.apache.maven.model.building.ModelProblemCollectorRequest;
import org.apache.maven.model.condition.CombinedValueEvaluator;
import org.apache.maven.model.path.PathTranslator;
import org.apache.maven.model.profile.ProfileActivationContext;
import org.codehaus.plexus.component.annotations.Component;
Expand All @@ -53,16 +54,24 @@
public class FileProfileActivator
implements ProfileActivator
{

@Requirement
private PathTranslator pathTranslator;

@Requirement
private CombinedValueEvaluator combinedValueEvaluator;

public FileProfileActivator setPathTranslator( PathTranslator pathTranslator )
{
this.pathTranslator = pathTranslator;
return this;
}

public FileProfileActivator setCombinedValueEvaluator( CombinedValueEvaluator combinedValueEvaluator )
{
this.combinedValueEvaluator = combinedValueEvaluator;
return this;
}

@Override
public boolean isActive( Profile profile, ProfileActivationContext context, ModelProblemCollector problems )
{
Expand Down Expand Up @@ -146,9 +155,8 @@ else if ( path.contains( "${basedir}" ) )
return false;
}

path = pathTranslator.alignToBaseDirectory( path, basedir );

// replace activation value with interpolated value
// (expression is still present & paths are still relative at this step)
if ( missing )
{
file.setMissing( path );
Expand All @@ -158,16 +166,7 @@ else if ( path.contains( "${basedir}" ) )
file.setExists( path );
}

File f = new File( path );

if ( !f.isAbsolute() )
{
return false;
}

boolean fileExists = f.exists();

return missing ? !fileExists : fileExists;
return combinedValueEvaluator.evaluate( path, new FileExistenceEvaluator( missing, pathTranslator, basedir ) );
}

@Override
Expand Down
Loading

0 comments on commit 2fc9d54

Please sign in to comment.