diff options
Diffstat (limited to 'framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation')
7 files changed, 1140 insertions, 0 deletions
diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/AbstractStringBasedModelInterpolator.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/AbstractStringBasedModelInterpolator.java new file mode 100644 index 00000000..dfc3deec --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/AbstractStringBasedModelInterpolator.java @@ -0,0 +1,407 @@ +package org.apache.maven.project.interpolation; + +/* + * 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 org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.apache.maven.project.DefaultProjectBuilderConfiguration; +import org.apache.maven.project.ProjectBuilderConfiguration; +import org.apache.maven.project.path.PathTranslator; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.interpolation.AbstractValueSource; +import org.codehaus.plexus.interpolation.InterpolationException; +import org.codehaus.plexus.interpolation.InterpolationPostProcessor; +import org.codehaus.plexus.interpolation.Interpolator; +import org.codehaus.plexus.interpolation.MapBasedValueSource; +import org.codehaus.plexus.interpolation.ObjectBasedValueSource; +import org.codehaus.plexus.interpolation.PrefixAwareRecursionInterceptor; +import org.codehaus.plexus.interpolation.PrefixedObjectValueSource; +import org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper; +import org.codehaus.plexus.interpolation.RecursionInterceptor; +import org.codehaus.plexus.interpolation.ValueSource; +import org.codehaus.plexus.logging.AbstractLogEnabled; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * Use a regular expression search to find and resolve expressions within the POM. + * + * @author jdcasey Created on Feb 3, 2005 + * @todo Consolidate this logic with the PluginParameterExpressionEvaluator, minus deprecations/bans. + */ +@Deprecated +public abstract class AbstractStringBasedModelInterpolator + extends AbstractLogEnabled + implements ModelInterpolator, Initializable +{ + private static final List<String> PROJECT_PREFIXES = Arrays.asList( "pom.", "project." ); + + private static final List<String> TRANSLATED_PATH_EXPRESSIONS; + + static + { + List<String> translatedPrefixes = new ArrayList<String>(); + + // MNG-1927, MNG-2124, MNG-3355: + // If the build section is present and the project directory is non-null, we should make + // sure interpolation of the directories below uses translated paths. + // Afterward, we'll double back and translate any paths that weren't covered during interpolation via the + // code below... + translatedPrefixes.add( "build.directory" ); + translatedPrefixes.add( "build.outputDirectory" ); + translatedPrefixes.add( "build.testOutputDirectory" ); + translatedPrefixes.add( "build.sourceDirectory" ); + translatedPrefixes.add( "build.testSourceDirectory" ); + translatedPrefixes.add( "build.scriptSourceDirectory" ); + translatedPrefixes.add( "reporting.outputDirectory" ); + + TRANSLATED_PATH_EXPRESSIONS = translatedPrefixes; + } + + @Requirement + private PathTranslator pathTranslator; + + private Interpolator interpolator; + + private RecursionInterceptor recursionInterceptor; + + // for testing. + protected AbstractStringBasedModelInterpolator( PathTranslator pathTranslator ) + { + this.pathTranslator = pathTranslator; + } + + /** + * @todo: Remove the throws clause. + * @throws IOException This exception is not thrown any more, and needs to be removed. + */ + protected AbstractStringBasedModelInterpolator() + { + } + + public Model interpolate( Model model, Map<String, ?> context ) + throws ModelInterpolationException + { + return interpolate( model, context, true ); + } + + /** + * Serialize the inbound Model instance to a StringWriter, perform the regex replacement to resolve + * POM expressions, then re-parse into the resolved Model instance. + * <br/> + * <b>NOTE:</b> This will result in a different instance of Model being returned!!! + * + * @param model The inbound Model instance, to serialize and reference for expression resolution + * @param context The other context map to be used during resolution + * @return The resolved instance of the inbound Model. This is a different instance! + * + * @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead. + */ + public Model interpolate( Model model, Map<String, ?> context, boolean strict ) + throws ModelInterpolationException + { + Properties props = new Properties(); + props.putAll( context ); + + return interpolate( model, + null, + new DefaultProjectBuilderConfiguration().setExecutionProperties( props ), + true ); + } + + public Model interpolate( Model model, + File projectDir, + ProjectBuilderConfiguration config, + boolean debugEnabled ) + throws ModelInterpolationException + { + StringWriter sWriter = new StringWriter( 1024 ); + + MavenXpp3Writer writer = new MavenXpp3Writer(); + try + { + writer.write( sWriter, model ); + } + catch ( IOException e ) + { + throw new ModelInterpolationException( "Cannot serialize project model for interpolation.", e ); + } + + String serializedModel = sWriter.toString(); + serializedModel = interpolate( serializedModel, model, projectDir, config, debugEnabled ); + + StringReader sReader = new StringReader( serializedModel ); + + MavenXpp3Reader modelReader = new MavenXpp3Reader(); + try + { + model = modelReader.read( sReader ); + } + catch ( IOException e ) + { + throw new ModelInterpolationException( + "Cannot read project model from interpolating filter of serialized version.", e ); + } + catch ( XmlPullParserException e ) + { + throw new ModelInterpolationException( + "Cannot read project model from interpolating filter of serialized version.", e ); + } + + return model; + } + + /** + * Interpolates all expressions in the src parameter. + * <p> + * The algorithm used for each expression is: + * <ul> + * <li>If it starts with either "pom." or "project.", the expression is evaluated against the model.</li> + * <li>If the value is null, get the value from the context.</li> + * <li>If the value is null, but the context contains the expression, don't replace the expression string + * with the value, and continue to find other expressions.</li> + * <li>If the value is null, get it from the model properties.</li> + * <li> + * @param overrideContext + * @param outputDebugMessages + */ + public String interpolate( String src, + Model model, + final File projectDir, + ProjectBuilderConfiguration config, + boolean debug ) + throws ModelInterpolationException + { + try + { + List<ValueSource> valueSources = createValueSources( model, projectDir, config ); + List<InterpolationPostProcessor> postProcessors = createPostProcessors( model, projectDir, config ); + + return interpolateInternal( src, valueSources, postProcessors, debug ); + } + finally + { + interpolator.clearAnswers(); + } + } + + protected List<ValueSource> createValueSources( final Model model, final File projectDir, + final ProjectBuilderConfiguration config ) + { + String timestampFormat = DEFAULT_BUILD_TIMESTAMP_FORMAT; + + Properties modelProperties = model.getProperties(); + if ( modelProperties != null ) + { + timestampFormat = modelProperties.getProperty( BUILD_TIMESTAMP_FORMAT_PROPERTY, timestampFormat ); + } + + ValueSource modelValueSource1 = new PrefixedObjectValueSource( PROJECT_PREFIXES, model, false ); + ValueSource modelValueSource2 = new ObjectBasedValueSource( model ); + + ValueSource basedirValueSource = new PrefixedValueSourceWrapper( new AbstractValueSource( false ) + { + public Object getValue( String expression ) + { + if ( projectDir != null && "basedir".equals( expression ) ) + { + return projectDir.getAbsolutePath(); + } + return null; + } + }, PROJECT_PREFIXES, true ); + ValueSource baseUriValueSource = new PrefixedValueSourceWrapper( new AbstractValueSource( false ) + { + public Object getValue( String expression ) + { + if ( projectDir != null && "baseUri".equals( expression ) ) + { + return projectDir.getAbsoluteFile().toURI().toString(); + } + return null; + } + }, PROJECT_PREFIXES, false ); + + List<ValueSource> valueSources = new ArrayList<ValueSource>( 9 ); + + // NOTE: Order counts here! + valueSources.add( basedirValueSource ); + valueSources.add( baseUriValueSource ); + valueSources.add( new BuildTimestampValueSource( config.getBuildStartTime(), timestampFormat ) ); + valueSources.add( modelValueSource1 ); + valueSources.add( new MapBasedValueSource( config.getUserProperties() ) ); + valueSources.add( new MapBasedValueSource( modelProperties ) ); + valueSources.add( new MapBasedValueSource( config.getExecutionProperties() ) ); + valueSources.add( new AbstractValueSource( false ) + { + public Object getValue( String expression ) + { + return config.getExecutionProperties().getProperty( "env." + expression ); + } + } ); + valueSources.add( modelValueSource2 ); + + return valueSources; + } + + protected List<InterpolationPostProcessor> createPostProcessors( final Model model, final File projectDir, + final ProjectBuilderConfiguration config ) + { + return Collections.singletonList( (InterpolationPostProcessor) new PathTranslatingPostProcessor( + PROJECT_PREFIXES, + TRANSLATED_PATH_EXPRESSIONS, + projectDir, + pathTranslator ) ); + } + + @SuppressWarnings( "unchecked" ) + protected String interpolateInternal( String src, List<ValueSource> valueSources, + List<InterpolationPostProcessor> postProcessors, boolean debug ) + throws ModelInterpolationException + { + if ( !src.contains( "${" ) ) + { + return src; + } + + Logger logger = getLogger(); + + String result = src; + synchronized ( this ) + { + + for ( ValueSource vs : valueSources ) + { + interpolator.addValueSource( vs ); + } + + for ( InterpolationPostProcessor postProcessor : postProcessors ) + { + interpolator.addPostProcessor( postProcessor ); + } + + try + { + try + { + result = interpolator.interpolate( result, recursionInterceptor ); + } + catch ( InterpolationException e ) + { + throw new ModelInterpolationException( e.getMessage(), e ); + } + + if ( debug ) + { + List<Object> feedback = interpolator.getFeedback(); + if ( feedback != null && !feedback.isEmpty() ) + { + logger.debug( "Maven encountered the following problems during initial POM interpolation:" ); + + Object last = null; + for ( Object next : feedback ) + { + if ( next instanceof Throwable ) + { + if ( last == null ) + { + logger.debug( "", ( (Throwable) next ) ); + } + else + { + logger.debug( String.valueOf( last ), ( (Throwable) next ) ); + } + } + else + { + if ( last != null ) + { + logger.debug( String.valueOf( last ) ); + } + + last = next; + } + } + + if ( last != null ) + { + logger.debug( String.valueOf( last ) ); + } + } + } + + interpolator.clearFeedback(); + } + finally + { + for ( ValueSource vs : valueSources ) + { + interpolator.removeValuesSource( vs ); + } + + for ( InterpolationPostProcessor postProcessor : postProcessors ) + { + interpolator.removePostProcessor( postProcessor ); + } + } + } + + return result; + } + + protected RecursionInterceptor getRecursionInterceptor() + { + return recursionInterceptor; + } + + protected void setRecursionInterceptor( RecursionInterceptor recursionInterceptor ) + { + this.recursionInterceptor = recursionInterceptor; + } + + protected abstract Interpolator createInterpolator(); + + public void initialize() + throws InitializationException + { + interpolator = createInterpolator(); + recursionInterceptor = new PrefixAwareRecursionInterceptor( PROJECT_PREFIXES ); + } + + protected final Interpolator getInterpolator() + { + return interpolator; + } + +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/BuildTimestampValueSource.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/BuildTimestampValueSource.java new file mode 100644 index 00000000..f6319fa6 --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/BuildTimestampValueSource.java @@ -0,0 +1,63 @@ +package org.apache.maven.project.interpolation; + +/* + * 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.text.SimpleDateFormat; +import java.util.Date; + +import org.codehaus.plexus.interpolation.AbstractValueSource; + +/** + * + */ +@Deprecated +public class BuildTimestampValueSource + extends AbstractValueSource +{ + + private final Date startTime; + + private final String format; + + private String formattedDate; + + public BuildTimestampValueSource( Date startTime, String format ) + { + super( false ); + this.startTime = startTime; + this.format = format; + } + + public Object getValue( String expression ) + { + if ( "build.timestamp".equals( expression ) || "maven.build.timestamp".equals( expression ) ) + { + if ( formattedDate == null && startTime != null ) + { + formattedDate = new SimpleDateFormat( format ).format( startTime ); + } + + return formattedDate; + } + + return null; + } + +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/ModelInterpolationException.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/ModelInterpolationException.java new file mode 100644 index 00000000..5b02880b --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/ModelInterpolationException.java @@ -0,0 +1,72 @@ +package org.apache.maven.project.interpolation; + +/* + * 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 jdcasey + * <p/> + * Created on Feb 2, 2005 + */ +@SuppressWarnings( "serial" ) +@Deprecated +public class ModelInterpolationException + extends Exception +{ + private String expression; + + private String originalMessage; + + public ModelInterpolationException( String message ) + { + super( message ); + } + + public ModelInterpolationException( String message, Throwable cause ) + { + super( message, cause ); + } + + public ModelInterpolationException( String expression, String message, Throwable cause ) + { + super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message, cause ); + + this.expression = expression; + this.originalMessage = message; + } + + public ModelInterpolationException( String expression, String message ) + { + super( "The POM expression: " + expression + " could not be evaluated. Reason: " + message ); + + this.expression = expression; + this.originalMessage = message; + } + + public String getExpression() + { + return expression; + } + + public String getOriginalMessage() + { + return originalMessage; + } + +}
\ No newline at end of file diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java new file mode 100644 index 00000000..e1556eaf --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/ModelInterpolator.java @@ -0,0 +1,66 @@ +package org.apache.maven.project.interpolation; + +/* + * 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 org.apache.maven.model.Model; +import org.apache.maven.project.ProjectBuilderConfiguration; + +import java.io.File; +import java.util.Map; + +/** + * @author jdcasey + * <p/> + * Created on Feb 2, 2005 + */ +@Deprecated +public interface ModelInterpolator +{ + String DEFAULT_BUILD_TIMESTAMP_FORMAT = "yyyyMMdd-HHmm"; + + String BUILD_TIMESTAMP_FORMAT_PROPERTY = "maven.build.timestamp.format"; + + String ROLE = ModelInterpolator.class.getName(); + + /** + * @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead. + */ + Model interpolate( Model project, Map<String, ?> context ) + throws ModelInterpolationException; + + /** + * @deprecated Use {@link ModelInterpolator#interpolate(Model, File, ProjectBuilderConfiguration, boolean)} instead. + */ + Model interpolate( Model model, Map<String, ?> context, boolean strict ) + throws ModelInterpolationException; + + Model interpolate( Model model, + File projectDir, + ProjectBuilderConfiguration config, + boolean debugEnabled ) + throws ModelInterpolationException; + + String interpolate( String src, + Model model, + File projectDir, + ProjectBuilderConfiguration config, + boolean debugEnabled ) + throws ModelInterpolationException; +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/PathTranslatingPostProcessor.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/PathTranslatingPostProcessor.java new file mode 100644 index 00000000..da2ba056 --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/PathTranslatingPostProcessor.java @@ -0,0 +1,64 @@ +package org.apache.maven.project.interpolation; + +/* + * 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 org.apache.maven.project.path.PathTranslator; +import org.codehaus.plexus.interpolation.InterpolationPostProcessor; +import org.codehaus.plexus.interpolation.util.ValueSourceUtils; + +import java.io.File; +import java.util.List; + +/** + * + */ +@Deprecated +public class PathTranslatingPostProcessor + implements InterpolationPostProcessor +{ + + private final List<String> unprefixedPathKeys; + private final File projectDir; + private final PathTranslator pathTranslator; + private final List<String> expressionPrefixes; + + public PathTranslatingPostProcessor( List<String> expressionPrefixes, List<String> unprefixedPathKeys, + File projectDir, PathTranslator pathTranslator ) + { + this.expressionPrefixes = expressionPrefixes; + this.unprefixedPathKeys = unprefixedPathKeys; + this.projectDir = projectDir; + this.pathTranslator = pathTranslator; + } + + public Object execute( String expression, + Object value ) + { + expression = ValueSourceUtils.trimPrefix( expression, expressionPrefixes, true ); + + if ( projectDir != null && value != null && unprefixedPathKeys.contains( expression ) ) + { + return pathTranslator.alignToBaseDirectory( String.valueOf( value ), projectDir ); + } + + return value; + } + +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java new file mode 100644 index 00000000..4e251aee --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/RegexBasedModelInterpolator.java @@ -0,0 +1,58 @@ +package org.apache.maven.project.interpolation; + +/* + * 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.IOException; +import java.util.Properties; + +import org.apache.maven.project.path.PathTranslator; +import org.codehaus.plexus.interpolation.Interpolator; +import org.codehaus.plexus.interpolation.RegexBasedInterpolator; + +/** + * Use a regular expression search to find and resolve expressions within the POM. + * + * @author jdcasey Created on Feb 3, 2005 + * @todo Consolidate this logic with the PluginParameterExpressionEvaluator, minus deprecations/bans. + */ +@Deprecated +public class RegexBasedModelInterpolator + extends AbstractStringBasedModelInterpolator +{ + + public RegexBasedModelInterpolator() + throws IOException + { + } + + public RegexBasedModelInterpolator( PathTranslator pathTranslator ) + { + super( pathTranslator ); + } + + public RegexBasedModelInterpolator( Properties envars ) + { + } + + protected Interpolator createInterpolator() + { + return new RegexBasedInterpolator( true ); + } +} diff --git a/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/StringSearchModelInterpolator.java b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/StringSearchModelInterpolator.java new file mode 100644 index 00000000..864f57f0 --- /dev/null +++ b/framework/src/maven/apache-maven-3.3.3/maven-compat/src/main/java/org/apache/maven/project/interpolation/StringSearchModelInterpolator.java @@ -0,0 +1,410 @@ +package org.apache.maven.project.interpolation; + +/* + * 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 org.apache.maven.model.Model; +import org.apache.maven.project.ProjectBuilderConfiguration; +import org.apache.maven.project.path.PathTranslator; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.interpolation.InterpolationPostProcessor; +import org.codehaus.plexus.interpolation.Interpolator; +import org.codehaus.plexus.interpolation.StringSearchInterpolator; +import org.codehaus.plexus.interpolation.ValueSource; +import org.codehaus.plexus.logging.Logger; + +import java.io.File; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; + +@Deprecated +@Component( role = ModelInterpolator.class ) +public class StringSearchModelInterpolator + extends AbstractStringBasedModelInterpolator +{ + + private static final Map<Class<?>, Field[]> fieldsByClass = new WeakHashMap<Class<?>, Field[]>(); + private static final Map<Class<?>, Boolean> fieldIsPrimitiveByClass = new WeakHashMap<Class<?>, Boolean>(); + + public StringSearchModelInterpolator() + { + } + + public StringSearchModelInterpolator( PathTranslator pathTranslator ) + { + super( pathTranslator ); + } + + public Model interpolate( Model model, File projectDir, ProjectBuilderConfiguration config, boolean debugEnabled ) + throws ModelInterpolationException + { + interpolateObject( model, model, projectDir, config, debugEnabled ); + + return model; + } + + protected void interpolateObject( Object obj, Model model, File projectDir, ProjectBuilderConfiguration config, + boolean debugEnabled ) + throws ModelInterpolationException + { + try + { + List<ValueSource> valueSources = createValueSources( model, projectDir, config ); + List<InterpolationPostProcessor> postProcessors = createPostProcessors( model, projectDir, config ); + + InterpolateObjectAction action = + new InterpolateObjectAction( obj, valueSources, postProcessors, debugEnabled, + this, getLogger() ); + + ModelInterpolationException error = AccessController.doPrivileged( action ); + + if ( error != null ) + { + throw error; + } + } + finally + { + getInterpolator().clearAnswers(); + } + } + + protected Interpolator createInterpolator() + { + StringSearchInterpolator interpolator = new StringSearchInterpolator(); + interpolator.setCacheAnswers( true ); + + return interpolator; + } + + private static final class InterpolateObjectAction implements PrivilegedAction<ModelInterpolationException> + { + + private final boolean debugEnabled; + private final LinkedList<Object> interpolationTargets; + private final StringSearchModelInterpolator modelInterpolator; + private final Logger logger; + private final List<ValueSource> valueSources; + private final List<InterpolationPostProcessor> postProcessors; + + public InterpolateObjectAction( Object target, List<ValueSource> valueSources, + List<InterpolationPostProcessor> postProcessors, boolean debugEnabled, + StringSearchModelInterpolator modelInterpolator, Logger logger ) + { + this.valueSources = valueSources; + this.postProcessors = postProcessors; + this.debugEnabled = debugEnabled; + + this.interpolationTargets = new LinkedList<Object>(); + interpolationTargets.add( target ); + + this.modelInterpolator = modelInterpolator; + this.logger = logger; + } + + public ModelInterpolationException run() + { + while ( !interpolationTargets.isEmpty() ) + { + Object obj = interpolationTargets.removeFirst(); + + try + { + traverseObjectWithParents( obj.getClass(), obj ); + } + catch ( ModelInterpolationException e ) + { + return e; + } + } + + return null; + } + + @SuppressWarnings( "unchecked" ) + private void traverseObjectWithParents( Class<?> cls, Object target ) + throws ModelInterpolationException + { + if ( cls == null ) + { + return; + } + + + if ( cls.isArray() ) + { + evaluateArray( target ); + } + else if ( isQualifiedForInterpolation( cls ) ) + { + Field[] fields = fieldsByClass.get( cls ); + if ( fields == null ) + { + fields = cls.getDeclaredFields(); + fieldsByClass.put( cls, fields ); + } + + for ( Field field : fields ) + { + Class<?> type = field.getType(); + if ( isQualifiedForInterpolation( field, type ) ) + { + boolean isAccessible = field.isAccessible(); + field.setAccessible( true ); + try + { + try + { + if ( String.class == type ) + { + String value = (String) field.get( target ); + if ( value != null ) + { + String interpolated = + modelInterpolator.interpolateInternal( value, valueSources, postProcessors, + debugEnabled ); + + if ( !interpolated.equals( value ) ) + { + field.set( target, interpolated ); + } + } + } + else if ( Collection.class.isAssignableFrom( type ) ) + { + Collection<Object> c = (Collection<Object>) field.get( target ); + if ( c != null && !c.isEmpty() ) + { + List<Object> originalValues = new ArrayList<Object>( c ); + try + { + c.clear(); + } + catch ( UnsupportedOperationException e ) + { + if ( debugEnabled && logger != null ) + { + logger.debug( "Skipping interpolation of field: " + field + " in: " + + cls.getName() + + "; it is an unmodifiable collection." ); + } + continue; + } + + for ( Object value : originalValues ) + { + if ( value != null ) + { + if ( String.class == value.getClass() ) + { + String interpolated = + modelInterpolator.interpolateInternal( (String) value, + valueSources, + postProcessors, + debugEnabled ); + + if ( !interpolated.equals( value ) ) + { + c.add( interpolated ); + } + else + { + c.add( value ); + } + } + else + { + c.add( value ); + if ( value.getClass().isArray() ) + { + evaluateArray( value ); + } + else + { + interpolationTargets.add( value ); + } + } + } + else + { + // add the null back in...not sure what else to do... + c.add( value ); + } + } + } + } + else if ( Map.class.isAssignableFrom( type ) ) + { + Map<Object, Object> m = (Map<Object, Object>) field.get( target ); + if ( m != null && !m.isEmpty() ) + { + for ( Map.Entry<Object, Object> entry : m.entrySet() ) + { + Object value = entry.getValue(); + + if ( value != null ) + { + if ( String.class == value.getClass() ) + { + String interpolated = + modelInterpolator.interpolateInternal( (String) value, + valueSources, + postProcessors, + debugEnabled ); + + if ( !interpolated.equals( value ) ) + { + try + { + entry.setValue( interpolated ); + } + catch ( UnsupportedOperationException e ) + { + if ( debugEnabled && logger != null ) + { + logger.debug( + "Skipping interpolation of field: " + field + + " (key: " + entry.getKey() + ") in: " + + cls.getName() + + "; it is an unmodifiable collection." ); + } + } + } + } + else + { + if ( value.getClass().isArray() ) + { + evaluateArray( value ); + } + else + { + interpolationTargets.add( value ); + } + } + } + } + } + } + else + { + Object value = field.get( target ); + if ( value != null ) + { + if ( field.getType().isArray() ) + { + evaluateArray( value ); + } + else + { + interpolationTargets.add( value ); + } + } + } + } + catch ( IllegalArgumentException e ) + { + throw new ModelInterpolationException( + "Failed to interpolate field: " + field + " on class: " + cls.getName(), e ); + } + catch ( IllegalAccessException e ) + { + throw new ModelInterpolationException( + "Failed to interpolate field: " + field + " on class: " + cls.getName(), e ); + } + } + finally + { + field.setAccessible( isAccessible ); + } + } + } + + traverseObjectWithParents( cls.getSuperclass(), target ); + } + } + + private boolean isQualifiedForInterpolation( Class<?> cls ) + { + return !cls.getPackage().getName().startsWith( "java" ); + } + + private boolean isQualifiedForInterpolation( Field field, Class<?> fieldType ) + { + if ( !fieldIsPrimitiveByClass.containsKey( fieldType ) ) + { + fieldIsPrimitiveByClass.put( fieldType, fieldType.isPrimitive() ); + } + + if ( fieldIsPrimitiveByClass.get( fieldType ) ) + { + return false; + } + +// if ( fieldType.isPrimitive() ) +// { +// return false; +// } + + if ( "parent".equals( field.getName() ) ) + { + return false; + } + + return true; + } + + private void evaluateArray( Object target ) + throws ModelInterpolationException + { + int len = Array.getLength( target ); + for ( int i = 0; i < len; i++ ) + { + Object value = Array.get( target, i ); + if ( value != null ) + { + if ( String.class == value.getClass() ) + { + String interpolated = + modelInterpolator.interpolateInternal( (String) value, valueSources, postProcessors, + debugEnabled ); + + if ( !interpolated.equals( value ) ) + { + Array.set( target, i, interpolated ); + } + } + else + { + interpolationTargets.add( value ); + } + } + } + } + } + +} |