Author: rafale Date: Tue Sep 16 14:39:24 2008 New Revision: 696076 URL: http://svn.apache.org/viewvc?rev=696076&view=rev Log: applied patch for archetype-191 from Brad Szabo
Added: maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/ArbitraryProperty-__property-with-default-1__.java (with props) maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/SkipsUndefinedProperty-__undefined-property__-__property-with-default-2__.java (with props) Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java maven/archetype/trunk/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java Modified: maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java?rev=696076&r1=696075&r2=696076&view=diff ============================================================================== --- maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java (original) +++ maven/archetype/trunk/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java Tue Sep 16 14:39:24 2008 @@ -54,6 +54,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.apache.maven.archetype.ArchetypeGenerationRequest; @@ -76,6 +77,17 @@ /** @plexus.requirement */ private VelocityComponent velocity; + /** + * Token delimiter. + */ + private static final String DELIMITER = "__"; + + /** + * Pattern used to detect tokens in a string. Tokens are any text surrounded + * by the delimiter. + */ + private static final Pattern TOKEN_PATTERN = Pattern.compile(".*" + DELIMITER + ".*" + DELIMITER + ".*"); + public void generateArchetype( ArchetypeGenerationRequest request, File archetypeFile, String basedir ) @@ -369,13 +381,92 @@ String outputFileName = directory + "/" + (packaged ? getPackageAsDirectory(packageName) : "") + "/" + templateName.substring(moduleOffset.length() ); - outputFileName = StringUtils.replace(outputFileName, "__rootArtifactId__", (String)context.get("rootArtifactId")); - outputFileName = StringUtils.replace(outputFileName, "__artifactId__", (String)context.get("artifactId")); - File outputFile = new File(outputDirectoryFile, outputFileName); + + if ( TOKEN_PATTERN.matcher(outputFileName).matches() ) + { + outputFileName = replaceFilenameTokens( outputFileName, context ); + } + + File outputFile = new File( outputDirectoryFile, outputFileName ); return outputFile; } + /** + * Replaces all tokens (text surrounded by the [EMAIL PROTECTED] #DELIMITER}) within + * the given string, using properties contained within the context. If a + * property does not exist in the context, the token is left unmodified + * and a warning is logged. + * + * @param filePath the file name and path to be interpolated + * @param context contains the available properties + */ + private String replaceFilenameTokens( String filePath, Context context ) + { + String interpolatedResult = filePath; + String propertyToken = null; + String contextPropertyValue = null; + + int start = 0; + int end = 0; + int skipUndefinedPropertyIndex = 0; + + int maxAttempts = StringUtils.countMatches( interpolatedResult, + DELIMITER ) / 2; + + for ( int x = 0; x < maxAttempts && start != -1; x++ ) + { + start = interpolatedResult.indexOf( DELIMITER, skipUndefinedPropertyIndex ); + + if ( start != -1 ) + { + end = interpolatedResult.indexOf( DELIMITER, + start + DELIMITER.length() ); + + if ( end != -1 ) + { + propertyToken = interpolatedResult.substring( + start + DELIMITER.length(), end ); + } + + contextPropertyValue = (String) context.get( propertyToken ); + + if ( contextPropertyValue != null && + contextPropertyValue.trim().length() > 0 ) + { + if (getLogger().isDebugEnabled()) + { + getLogger().debug( "Replacing '" + DELIMITER + propertyToken + + DELIMITER + "' in file path '" + + interpolatedResult + "' with value '" + + contextPropertyValue + "'."); + } + + interpolatedResult = StringUtils.replace( + interpolatedResult, + DELIMITER + propertyToken + DELIMITER, + contextPropertyValue ); + + } else + { + // Need to skip the undefined property + skipUndefinedPropertyIndex = end + DELIMITER.length() + 1; + + getLogger().warn( "Property '" + propertyToken + + "' was not specified, so the token in '" + + interpolatedResult + "' is not being replaced." ); + } + } + } + + if (getLogger().isDebugEnabled()) + { + getLogger().debug( "Final interpolated file path: '" + interpolatedResult + "'" ); + } + + return interpolatedResult; + } + private String getPackageInPathFormat( String aPackage ) { return StringUtils.replace( aPackage, ".", "/" ); Added: maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/ArbitraryProperty-__property-with-default-1__.java URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/ArbitraryProperty-__property-with-default-1__.java?rev=696076&view=auto ============================================================================== --- maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/ArbitraryProperty-__property-with-default-1__.java (added) +++ maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/ArbitraryProperty-__property-with-default-1__.java Tue Sep 16 14:39:24 2008 @@ -0,0 +1,14 @@ +groupId=${groupId} +rootArtifactId=${rootArtifactId} +artifactId=${artifactId} +version=${version} +package=${package} +packageInPathFormat=${packageInPathFormat} +property-without-default-1=${property-without-default-1} +property-without-default-2=${property-without-default-2} +property-without-default-3=${property-without-default-3} +property-without-default-4=${property-without-default-4} +property-with-default-1=${property-with-default-1} +property-with-default-2=${property-with-default-2} +property-with-default-3=${property-with-default-3} +property-with-default-4=${property-with-default-4} \ No newline at end of file Propchange: maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/ArbitraryProperty-__property-with-default-1__.java ------------------------------------------------------------------------------ svn:eol-style = native Added: maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/SkipsUndefinedProperty-__undefined-property__-__property-with-default-2__.java URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/SkipsUndefinedProperty-__undefined-property__-__property-with-default-2__.java?rev=696076&view=auto ============================================================================== --- maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/SkipsUndefinedProperty-__undefined-property__-__property-with-default-2__.java (added) +++ maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/SkipsUndefinedProperty-__undefined-property__-__property-with-default-2__.java Tue Sep 16 14:39:24 2008 @@ -0,0 +1,14 @@ +groupId=${groupId} +rootArtifactId=${rootArtifactId} +artifactId=${artifactId} +version=${version} +package=${package} +packageInPathFormat=${packageInPathFormat} +property-without-default-1=${property-without-default-1} +property-without-default-2=${property-without-default-2} +property-without-default-3=${property-without-default-3} +property-without-default-4=${property-without-default-4} +property-with-default-1=${property-with-default-1} +property-with-default-2=${property-with-default-2} +property-with-default-3=${property-with-default-3} +property-with-default-4=${property-with-default-4} \ No newline at end of file Propchange: maven/archetype/trunk/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/__rootArtifactId__/inner/__artifactId__/innest/SkipsUndefinedProperty-__undefined-property__-__property-with-default-2__.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: maven/archetype/trunk/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java URL: http://svn.apache.org/viewvc/maven/archetype/trunk/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java?rev=696076&r1=696075&r2=696076&view=diff ============================================================================== --- maven/archetype/trunk/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java (original) +++ maven/archetype/trunk/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java Tue Sep 16 14:39:24 2008 @@ -722,6 +722,24 @@ "subsubproject" ); + template = "subproject/subsubproject/src/main/java/file/value/package/" + + "file-value/inner/subsubproject/innest/ArbitraryProperty-file-value.java"; + assertTemplateContentGeneratedWithFileSetArchetype( + projectDirectory, + template, + "subsubproject" + ); + + // Test that undefined properties are safely ignored (and skipped) + template = "subproject/subsubproject/src/main/java/file/value/package/" + + "file-value/inner/subsubproject/innest/" + + "SkipsUndefinedProperty-__undefined-property__-file-value.java"; + assertTemplateContentGeneratedWithFileSetArchetype( + projectDirectory, + template, + "subsubproject" + ); + model = readPom( getProjectFile( @@ -833,9 +851,9 @@ { assertTrue( "Exception not correct", - result.getCause().getMessage().startsWith( + result.getCause().getMessage().startsWith( "Archetype archetypes:basic:1.0 is not configured" ) && - result.getCause().getMessage().endsWith( + result.getCause().getMessage().endsWith( "Property property-without-default-4 is missing." ) ); }