Author: jdcasey Date: Mon Feb 18 11:43:52 2008 New Revision: 628851 URL: http://svn.apache.org/viewvc?rev=628851&view=rev Log: [MASSEMBLY-152] Add support for Ant '@' filter-token delimiters, and windows path escaping. Submitted by: John Franey
Added: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReflectionProperties.java (with props) Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java maven/plugins/trunk/maven-assembly-plugin/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java Modified: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java?rev=628851&r1=628850&r2=628851&view=diff ============================================================================== --- maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java (original) +++ maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/FileFormatter.java Mon Feb 18 11:43:52 2008 @@ -26,16 +26,16 @@ import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.interpolation.MapBasedValueSource; -import org.codehaus.plexus.util.interpolation.ObjectBasedValueSource; -import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator; +import org.codehaus.plexus.util.InterpolationFilterReader; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.util.Iterator; @@ -93,7 +93,7 @@ if ( filter ) { - contents = filter( contents ); + contents = filter( contents, source ); } contentIsChanged = !contents.equals( rawContents ); @@ -182,27 +182,40 @@ return fileWritten; } - private String filter( String rawContents ) - throws AssemblyFormattingException + private String filter( String rawContents, File source ) + throws IOException, AssemblyFormattingException { initializeFiltering(); - String contents = rawContents; + Reader reader = new BufferedReader(new StringReader(rawContents)); // support ${token} - RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); - interpolator.addValueSource( new MapBasedValueSource( filterProperties ) ); - interpolator.addValueSource( new ObjectBasedValueSource( configSource.getProject() ) ); + reader = new InterpolationFilterReader( reader, filterProperties, "${", "}" ); + + // support @token@ + reader = new InterpolationFilterReader( reader, filterProperties, "@", "@" ); + + boolean isPropertiesFile = false; + + if ( source.isFile() && source.getName().endsWith( ".properties" ) ) + { + isPropertiesFile = true; + } + + reader = new InterpolationFilterReader( reader, new ReflectionProperties( configSource.getProject(), isPropertiesFile ), "${", "}" ); + + StringWriter sw = new StringWriter(); + IOUtil.copy( reader, new BufferedWriter(sw)); + + return sw.getBuffer().toString(); - contents = interpolator.interpolate( contents, "project" ); - return contents; } private void initializeFiltering() throws AssemblyFormattingException { - logger.info( "Initializing assembly filters..." ); + logger.debug( "Initializing assembly filters..." ); if ( filterProperties == null ) { @@ -215,7 +228,7 @@ List filters = configSource.getFilters(); - if ( filters != null && !filters.isEmpty() ) + if ( ( filters != null ) && !filters.isEmpty() ) { for ( Iterator i = filters.iterator(); i.hasNext(); ) { Added: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReflectionProperties.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReflectionProperties.java?rev=628851&view=auto ============================================================================== --- maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReflectionProperties.java (added) +++ maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReflectionProperties.java Mon Feb 18 11:43:52 2008 @@ -0,0 +1,73 @@ +package org.apache.maven.plugin.assembly.format; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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.MavenProject; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.introspection.ReflectionValueExtractor; + +import java.util.Properties; + + +/** + * @author Andreas Hoheneder (ahoh_at_inode.at) + * @version $Id$ + */ +public class ReflectionProperties + extends Properties +{ + + private MavenProject project; + + boolean escapedBackslashesInFilePath; + + public ReflectionProperties( MavenProject aProject, boolean escapedBackslashesInFilePath ) + { + super(); + + project = aProject; + + this.escapedBackslashesInFilePath = escapedBackslashesInFilePath; + } + + public Object get( Object key ) + { + Object value = null; + try + { + value = ReflectionValueExtractor.evaluate( "" + key , project ); + + if ( escapedBackslashesInFilePath && value != null && + "java.lang.String".equals( value.getClass().getName() ) ) + { + String val = (String) value; + + // Check if it's a windows path + if ( val.indexOf( ":\\" ) == 1 ) + { + value = StringUtils.replace( (String)value, "\\", "\\\\" ); + value = StringUtils.replace( (String)value, ":", "\\:" ); + } + } + } + catch ( Exception e ) + { + //TODO: remove the try-catch block when ReflectionValueExtractor.evaluate() throws no more exceptions + } + return value; + } +} Propchange: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReflectionProperties.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-assembly-plugin/src/main/java/org/apache/maven/plugin/assembly/format/ReflectionProperties.java ------------------------------------------------------------------------------ svn:keywords = "Author Date Id Revision" Modified: maven/plugins/trunk/maven-assembly-plugin/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-assembly-plugin/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java?rev=628851&r1=628850&r2=628851&view=diff ============================================================================== --- maven/plugins/trunk/maven-assembly-plugin/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java (original) +++ maven/plugins/trunk/maven-assembly-plugin/src/test/java/org/apache/maven/plugin/assembly/format/FileFormatterTest.java Mon Feb 18 11:43:52 2008 @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.List; +import org.apache.maven.model.Build; import org.apache.maven.model.Model; import org.apache.maven.plugin.assembly.AssemblerConfigurationSource; import org.apache.maven.plugin.assembly.testutils.MockManager; @@ -160,17 +161,67 @@ enableBasicFilteringConfiguration( basedir, Collections.EMPTY_LIST ); - File file = fileManager.createFile( basedir, "one.txt", "This is a test for project: ${artifactId}." ); + File file = fileManager.createFile( basedir, "one.txt", "This is a test for project: ${artifactId} @[EMAIL PROTECTED]" ); mockManager.replayAll(); File result = new FileFormatter( configSource, logger ).format( file, true, null ); - assertEquals( "This is a test for project: artifact.", fileManager.getFileContents( result ) ); + assertEquals( "This is a test for project: artifact @[EMAIL PROTECTED]", fileManager.getFileContents( result ) ); mockManager.verifyAll(); } + public void testShouldFilterExpressionInPropertiesFileWithWindowsEscapes() + throws IOException, AssemblyFormattingException + { + + File sourceDir = fileManager.createTempDir(); + MavenProject project = createBasicMavenProject(); + Build build = new Build(); + + // project.build.outputDirectory = C:\out\deeper + build.setOutputDirectory( "C:\\out\\deeper" ); + project.setBuild(build); + + enableBasicFilteringConfiguration(project, sourceDir, Collections.EMPTY_LIST); + + File file = fileManager.createFile(sourceDir, "one.properties", "out=${project.build.outputDirectory}"); + + mockManager.replayAll(); + + File result = new FileFormatter(configSource, logger).format(file, true, null); + + // expect: C\:\\out\\deeper + assertEquals("out=C\\:\\\\out\\\\deeper",fileManager.getFileContents(result)); + + mockManager.verifyAll(); + } + + public void testShouldFilterExpressionInPropertiesFileWithoutWindowsEscapes() + throws IOException, AssemblyFormattingException + { + + File sourceDir = fileManager.createTempDir(); + MavenProject project = createBasicMavenProject(); + Build build = new Build(); + build.setOutputDirectory( "C:\\out\\deeper" ); + project.setBuild(build); + + enableBasicFilteringConfiguration(project, sourceDir, Collections.EMPTY_LIST); + + File file = fileManager.createFile(sourceDir, "one.txt", "project.basedirA=${project.build.outputDirectory}"); + + mockManager.replayAll(); + + File result = new FileFormatter(configSource, logger).format(file, true, null); + + assertEquals("project.basedirA=C:\\out\\deeper",fileManager.getFileContents(result)); + + mockManager.verifyAll(); + } + + public void testShouldFilterExpressionFromFiltersFileInFile() throws IOException, AssemblyFormattingException { @@ -180,16 +231,36 @@ enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) ); - File file = fileManager.createFile( basedir, "one.txt", "This is a test for project: ${property}." ); + File file = fileManager.createFile( basedir, "one.txt", "This is a test for project: ${property} @[EMAIL PROTECTED]" ); mockManager.replayAll(); File result = new FileFormatter( configSource, logger ).format( file, true, null ); - assertEquals( "This is a test for project: Test.", fileManager.getFileContents( result ) ); + assertEquals( "This is a test for project: Test Test.", fileManager.getFileContents( result ) ); mockManager.verifyAll(); } + + public void testShouldFilterExpressionFromFiltersFileInPropertiesFileWithoutWindowsEscapes() + throws IOException, AssemblyFormattingException + { + File basedir = fileManager.createTempDir(); + + File filterProps = fileManager.createFile( basedir, "filter.properties", "property=C:\\\\Test" ); + + enableBasicFilteringConfiguration( basedir, Collections.singletonList( filterProps.getCanonicalPath() ) ); + + File file = fileManager.createFile( basedir, "one.properties", "This is a test for project: ${property} @[EMAIL PROTECTED]" ); + + mockManager.replayAll(); + + File result = new FileFormatter( configSource, logger ).format( file, true, null ); + + assertEquals( "This is a test for project: C:\\Test C:\\Test.", fileManager.getFileContents( result ) ); + + mockManager.verifyAll(); + } public void testShouldFilterExpressionsFromTwoFiltersFilesInFile() throws IOException, AssemblyFormattingException @@ -206,13 +277,13 @@ enableBasicFilteringConfiguration( basedir, filters ); File file = fileManager.createFile( basedir, "one.txt", - "property: ${property} otherProperty: ${otherProperty}." ); + "property: ${property} @property@ otherProperty: ${otherProperty} @[EMAIL PROTECTED]" ); mockManager.replayAll(); File result = new FileFormatter( configSource, logger ).format( file, true, null ); - assertEquals( "property: Test otherProperty: OtherValue.", fileManager.getFileContents( result ) ); + assertEquals( "property: Test Test otherProperty: OtherValue OtherValue.", fileManager.getFileContents( result ) ); mockManager.verifyAll(); } @@ -231,13 +302,13 @@ enableBasicFilteringConfiguration( basedir, filters ); - File file = fileManager.createFile( basedir, "one.txt", "property: ${property}." ); + File file = fileManager.createFile( basedir, "one.txt", "property: ${property} @[EMAIL PROTECTED]" ); mockManager.replayAll(); File result = new FileFormatter( configSource, logger ).format( file, true, null ); - assertEquals( "property: OtherValue.", fileManager.getFileContents( result ) ); + assertEquals( "property: OtherValue OtherValue.", fileManager.getFileContents( result ) ); mockManager.verifyAll(); } @@ -254,35 +325,44 @@ enableBasicFilteringConfiguration( basedir, filters ); - File file = fileManager.createFile( basedir, "one.txt", "project artifact-id: ${artifactId}." ); + File file = fileManager.createFile( basedir, "one.txt", "project artifact-id: ${artifactId} @[EMAIL PROTECTED]" ); mockManager.replayAll(); File result = new FileFormatter( configSource, logger ).format( file, true, null ); - assertEquals( "project artifact-id: Test.", fileManager.getFileContents( result ) ); + assertEquals( "project artifact-id: Test Test.", fileManager.getFileContents( result ) ); mockManager.verifyAll(); } - private void enableBasicFilteringConfiguration( File basedir, List filterFilenames ) - { - configSource.getTemporaryRootDirectory(); - configSourceControl.setReturnValue( basedir ); - + private MavenProject createBasicMavenProject() { Model model = new Model(); model.setArtifactId( "artifact" ); model.setGroupId( "group" ); model.setVersion( "version" ); - MavenProject project = new MavenProject( model ); + return new MavenProject( model ); + } + + private void enableBasicFilteringConfiguration(MavenProject project, File basedir, List filterFilenames) + { + configSource.getTemporaryRootDirectory(); + configSourceControl.setReturnValue( basedir ); - configSource.getProject(); - configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE ); + configSource.getProject(); + configSourceControl.setReturnValue( project, MockControl.ONE_OR_MORE ); - // list of filenames that contain filter definitions. - configSource.getFilters(); - configSourceControl.setReturnValue( filterFilenames ); + // list of filenames that contain filter definitions. + configSource.getFilters(); + configSourceControl.setReturnValue( filterFilenames ); + + } + + private void enableBasicFilteringConfiguration( File basedir, List filterFilenames ) + { + MavenProject project = createBasicMavenProject(); + enableBasicFilteringConfiguration( project, basedir, filterFilenames ); } }