o only one used interpolator method now, and it's generalized to taking the model and passing in properties so we can make different implementations

git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@769682 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2009-04-29 06:52:50 +00:00
parent a4be625de5
commit be02d72fe6
2 changed files with 505 additions and 544 deletions

View File

@ -31,73 +31,51 @@ import org.apache.maven.model.Reporting;
import org.apache.maven.model.Resource; import org.apache.maven.model.Resource;
import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Component;
@Component(role=Interpolator.class) @Component(role = Interpolator.class)
public class DefaultInterpolator implements Interpolator { public class DefaultInterpolator
implements Interpolator
public String interpolateXmlString(String xml, {
List<InterpolatorProperty> interpolatorProperties) throws IOException
{
List<ModelProperty> modelProperties = marshallXmlToModelProperties( new ByteArrayInputStream( xml.getBytes() ), ProjectUri.baseUri, URIS );
Map<String, String> aliases = new HashMap<String, String>();
aliases.put( "project.", "pom." );
List<InterpolatorProperty> ips = new ArrayList<InterpolatorProperty>( interpolatorProperties );
ips.addAll( createInterpolatorProperties( modelProperties, ProjectUri.baseUri, aliases, PomInterpolatorTag.PROJECT_PROPERTIES.name()) );
for ( ModelProperty mp : modelProperties )
{
if ( mp.getUri().startsWith( ProjectUri.properties ) && mp.getValue() != null )
{
String uri = mp.getUri();
ips.add( new InterpolatorProperty( "${" + uri.substring( uri.lastIndexOf( "/" ) + 1, uri.length() ) + "}", mp.getValue() ) );
}
}
interpolateModelProperties( modelProperties, ips );
return unmarshalModelPropertiesToXml( modelProperties, ProjectUri.baseUri );
}
public PomClassicDomainModel interpolateDomainModel( PomClassicDomainModel dm, Properties properties ) public PomClassicDomainModel interpolateDomainModel( PomClassicDomainModel dm, Properties properties )
throws IOException throws IOException
{ {
List<InterpolatorProperty> props = new ArrayList<InterpolatorProperty>(); List<InterpolatorProperty> props = new ArrayList<InterpolatorProperty>();
for(Entry<Object, Object> e : properties for ( Entry<Object, Object> e : properties.entrySet() )
.entrySet())
{ {
props.add(new InterpolatorProperty((String) e.getKey(), (String) e.getValue(), PomInterpolatorTag.EXECUTION_PROPERTIES.toString())); props.add( new InterpolatorProperty( (String) e.getKey(), (String) e.getValue(), PomInterpolatorTag.EXECUTION_PROPERTIES.toString() ) );
} }
return interpolateDomainModel(dm, props); return interpolateDomainModel( dm, props );
} }
public PomClassicDomainModel interpolateDomainModel( PomClassicDomainModel dm, List<InterpolatorProperty> interpolatorProperties ) public PomClassicDomainModel interpolateDomainModel( PomClassicDomainModel dm, List<InterpolatorProperty> interpolatorProperties )
throws IOException { throws IOException
{
if (dm == null) { if ( dm == null )
throw new IllegalArgumentException("dm: null"); {
throw new IllegalArgumentException( "dm: null" );
} }
if (!containsProjectVersion(interpolatorProperties)) { if ( !containsProjectVersion( interpolatorProperties ) )
aliases.put("\\$\\{project.version\\}", "\\$\\{version\\}"); {
aliases.put( "\\$\\{project.version\\}", "\\$\\{version\\}" );
} }
//TODO: Insert customized logic for parsing //TODO: Insert customized logic for parsing
List<ModelProperty> modelProperties = getModelProperties(dm.getInputStream()); List<ModelProperty> modelProperties = getModelProperties( dm.getInputStream() );
if ("jar".equals(dm.getModel().getPackaging())) { if ( "jar".equals( dm.getModel().getPackaging() ) )
modelProperties.add(new ModelProperty(ProjectUri.packaging, "jar")); {
modelProperties.add( new ModelProperty( ProjectUri.packaging, "jar" ) );
} }
List<ModelProperty> firstPassModelProperties = new ArrayList<ModelProperty>(); List<ModelProperty> firstPassModelProperties = new ArrayList<ModelProperty>();
List<ModelProperty> secondPassModelProperties = new ArrayList<ModelProperty>(); List<ModelProperty> secondPassModelProperties = new ArrayList<ModelProperty>();
ModelProperty buildProperty = new ModelProperty(ProjectUri.Build.xUri, ModelProperty buildProperty = new ModelProperty( ProjectUri.Build.xUri, null );
null);
for ( ModelProperty mp : modelProperties ) for ( ModelProperty mp : modelProperties )
{ {
if ( mp.getValue() != null && !mp.getUri().contains( "#property" ) && !mp.getUri().contains( "#collection" ) ) if ( mp.getValue() != null && !mp.getUri().contains( "#property" ) && !mp.getUri().contains( "#collection" ) )
{ {
if ( ( !buildProperty.isParentOf( mp ) && !mp.getUri().equals( ProjectUri.Reporting.outputDirectory ) || mp.getUri().equals( if ( ( !buildProperty.isParentOf( mp ) && !mp.getUri().equals( ProjectUri.Reporting.outputDirectory ) || mp.getUri().equals( ProjectUri.Build.finalName ) ) )
ProjectUri.Build.finalName ) ) )
{ {
firstPassModelProperties.add( mp ); firstPassModelProperties.add( mp );
} }
@ -110,114 +88,97 @@ public class DefaultInterpolator implements Interpolator {
List<InterpolatorProperty> standardInterpolatorProperties = new ArrayList<InterpolatorProperty>(); List<InterpolatorProperty> standardInterpolatorProperties = new ArrayList<InterpolatorProperty>();
if (dm.isPomInBuild()) { if ( dm.isPomInBuild() )
{
String basedir = dm.getProjectDirectory().getAbsolutePath(); String basedir = dm.getProjectDirectory().getAbsolutePath();
standardInterpolatorProperties.add(new InterpolatorProperty( standardInterpolatorProperties.add( new InterpolatorProperty( "${project.basedir}", basedir, PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
"${project.basedir}", basedir, standardInterpolatorProperties.add( new InterpolatorProperty( "${basedir}", basedir, PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
PomInterpolatorTag.PROJECT_PROPERTIES.name())); standardInterpolatorProperties.add( new InterpolatorProperty( "${pom.basedir}", basedir, PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
standardInterpolatorProperties.add(new InterpolatorProperty(
"${basedir}", basedir,
PomInterpolatorTag.PROJECT_PROPERTIES.name()));
standardInterpolatorProperties.add(new InterpolatorProperty(
"${pom.basedir}", basedir,
PomInterpolatorTag.PROJECT_PROPERTIES.name()));
String baseuri = dm.getProjectDirectory().toURI().toString(); String baseuri = dm.getProjectDirectory().toURI().toString();
standardInterpolatorProperties.add(new InterpolatorProperty( standardInterpolatorProperties.add( new InterpolatorProperty( "${project.baseUri}", baseuri, PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
"${project.baseUri}", baseuri, standardInterpolatorProperties.add( new InterpolatorProperty( "${pom.baseUri}", baseuri, PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
PomInterpolatorTag.PROJECT_PROPERTIES.name()));
standardInterpolatorProperties.add(new InterpolatorProperty(
"${pom.baseUri}", baseuri,
PomInterpolatorTag.PROJECT_PROPERTIES.name()));
} }
for (ModelProperty mp : modelProperties) { for ( ModelProperty mp : modelProperties )
if (mp.getUri().startsWith(ProjectUri.properties) {
&& mp.getValue() != null) { if ( mp.getUri().startsWith( ProjectUri.properties ) && mp.getValue() != null )
{
String uri = mp.getUri(); String uri = mp.getUri();
standardInterpolatorProperties.add(new InterpolatorProperty( standardInterpolatorProperties.add( new InterpolatorProperty( "${" + uri.substring( uri.lastIndexOf( "/" ) + 1, uri.length() ) + "}", mp.getValue(),
"${" PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
+ uri.substring(uri.lastIndexOf("/") + 1, uri
.length()) + "}", mp.getValue(),
PomInterpolatorTag.PROJECT_PROPERTIES.name()));
} }
} }
// FIRST PASS - Withhold using build directories as interpolator // FIRST PASS - Withhold using build directories as interpolator
// properties // properties
List<InterpolatorProperty> ips1 = new ArrayList<InterpolatorProperty>( List<InterpolatorProperty> ips1 = new ArrayList<InterpolatorProperty>( interpolatorProperties );
interpolatorProperties); ips1.addAll( standardInterpolatorProperties );
ips1.addAll(standardInterpolatorProperties); ips1.addAll( createInterpolatorProperties( firstPassModelProperties, ProjectUri.baseUri, aliases, PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
ips1.addAll(createInterpolatorProperties( Collections.sort( ips1, new Comparator<InterpolatorProperty>()
firstPassModelProperties, ProjectUri.baseUri, aliases, {
PomInterpolatorTag.PROJECT_PROPERTIES.name())); public int compare( InterpolatorProperty o, InterpolatorProperty o1 )
Collections.sort(ips1, new Comparator<InterpolatorProperty>() { {
public int compare(InterpolatorProperty o, InterpolatorProperty o1) { if ( o.getTag() == null || o1.getTag() == null )
if (o.getTag() == null || o1.getTag() == null) { {
return 0; return 0;
} }
return PomInterpolatorTag.valueOf(o.getTag()).compareTo( return PomInterpolatorTag.valueOf( o.getTag() ).compareTo( PomInterpolatorTag.valueOf( o1.getTag() ) );
PomInterpolatorTag.valueOf(o1.getTag()));
} }
}); } );
interpolateModelProperties(modelProperties, ips1); interpolateModelProperties( modelProperties, ips1 );
// SECOND PASS - Set absolute paths on build directories // SECOND PASS - Set absolute paths on build directories
if (dm.isPomInBuild()) { if ( dm.isPomInBuild() )
{
String basedir = dm.getProjectDirectory().getAbsolutePath(); String basedir = dm.getProjectDirectory().getAbsolutePath();
Map<ModelProperty, ModelProperty> buildDirectories = new HashMap<ModelProperty, ModelProperty>(); Map<ModelProperty, ModelProperty> buildDirectories = new HashMap<ModelProperty, ModelProperty>();
for (ModelProperty mp : secondPassModelProperties) { for ( ModelProperty mp : secondPassModelProperties )
if (mp.getUri().startsWith(ProjectUri.Build.xUri) {
|| mp.getUri().equals( if ( mp.getUri().startsWith( ProjectUri.Build.xUri ) || mp.getUri().equals( ProjectUri.Reporting.outputDirectory ) )
ProjectUri.Reporting.outputDirectory)) { {
File file = new File(mp.getResolvedValue()); File file = new File( mp.getResolvedValue() );
if (!file.isAbsolute() if ( !file.isAbsolute() && !mp.getResolvedValue().startsWith( "${project.build." ) && !mp.getResolvedValue().equals( "${project.basedir}" ) )
&& !mp.getResolvedValue().startsWith( {
"${project.build.") buildDirectories.put( mp, new ModelProperty( mp.getUri(), new File( basedir, file.getPath() ).getAbsolutePath() ) );
&& !mp.getResolvedValue().equals(
"${project.basedir}")) {
buildDirectories.put(mp, new ModelProperty(mp.getUri(),
new File(basedir, file.getPath())
.getAbsolutePath()));
} }
} }
} }
for (Map.Entry<ModelProperty, ModelProperty> e : buildDirectories for ( Map.Entry<ModelProperty, ModelProperty> e : buildDirectories.entrySet() )
.entrySet()) { {
secondPassModelProperties.remove(e.getKey()); secondPassModelProperties.remove( e.getKey() );
secondPassModelProperties.add(e.getValue()); secondPassModelProperties.add( e.getValue() );
} }
} }
// THIRD PASS - Use build directories as interpolator properties // THIRD PASS - Use build directories as interpolator properties
List<InterpolatorProperty> ips2 = new ArrayList<InterpolatorProperty>( List<InterpolatorProperty> ips2 = new ArrayList<InterpolatorProperty>( interpolatorProperties );
interpolatorProperties); ips2.addAll( standardInterpolatorProperties );
ips2.addAll(standardInterpolatorProperties); ips2.addAll( createInterpolatorProperties( secondPassModelProperties, ProjectUri.baseUri, aliases, PomInterpolatorTag.PROJECT_PROPERTIES.name() ) );
ips2.addAll(createInterpolatorProperties( ips2.addAll( interpolatorProperties );
secondPassModelProperties, ProjectUri.baseUri, aliases, Collections.sort( ips2, new Comparator<InterpolatorProperty>()
PomInterpolatorTag.PROJECT_PROPERTIES.name())); {
ips2.addAll(interpolatorProperties); public int compare( InterpolatorProperty o, InterpolatorProperty o1 )
Collections.sort(ips2, new Comparator<InterpolatorProperty>() { {
public int compare(InterpolatorProperty o, InterpolatorProperty o1) { if ( o.getTag() == null || o1.getTag() == null )
if (o.getTag() == null || o1.getTag() == null) { {
return 0; return 0;
} }
return PomInterpolatorTag.valueOf(o.getTag()).compareTo( return PomInterpolatorTag.valueOf( o.getTag() ).compareTo( PomInterpolatorTag.valueOf( o1.getTag() ) );
PomInterpolatorTag.valueOf(o1.getTag()));
} }
}); } );
interpolateModelProperties(modelProperties, ips2); interpolateModelProperties( modelProperties, ips2 );
try try
{ {
String xml = unmarshalModelPropertiesToXml( modelProperties, ProjectUri.baseUri ); String xml = unmarshalModelPropertiesToXml( modelProperties, ProjectUri.baseUri );
PomClassicDomainModel domainModel = new PomClassicDomainModel( new ByteArrayInputStream ( xml.getBytes( "UTF-8" ))); PomClassicDomainModel domainModel = new PomClassicDomainModel( new ByteArrayInputStream( xml.getBytes( "UTF-8" ) ) );
if ( dm.getProjectDirectory() != null ) if ( dm.getProjectDirectory() != null )
{ {
alignPaths(domainModel.getModel(), dm.getProjectDirectory()); alignPaths( domainModel.getModel(), dm.getProjectDirectory() );
} }
return domainModel; return domainModel;
} }
@ -226,8 +187,6 @@ public class DefaultInterpolator implements Interpolator {
throw new IllegalStateException( "Unmarshalling of model properties failed", e ); throw new IllegalStateException( "Unmarshalling of model properties failed", e );
} }
/* /*
for(ModelProperty mp : modelProperties) for(ModelProperty mp : modelProperties)
{ {
@ -241,9 +200,10 @@ public class DefaultInterpolator implements Interpolator {
} }
*/ */
} }
/** /**
* Post-processes the paths of build directories by aligning relative paths to the project directory and normalizing * Post-processes the paths of build directories by aligning relative paths to the project
* file separators to the platform-specific separator. * directory and normalizing file separators to the platform-specific separator.
* *
* @param model The model to process, must not be {@code null}. * @param model The model to process, must not be {@code null}.
* @param basedir The project directory, must not be {@code null}. * @param basedir The project directory, must not be {@code null}.
@ -286,7 +246,7 @@ public class DefaultInterpolator implements Interpolator {
} }
private static String getAlignedPathFor(String path, File basedir) private static String getAlignedPathFor( String path, File basedir )
{ {
if ( path != null ) if ( path != null )
{ {
@ -309,32 +269,40 @@ public class DefaultInterpolator implements Interpolator {
} }
return path; return path;
} }
private static void interpolateModelProperties(List<ModelProperty> modelProperties,
List<InterpolatorProperty> interpolatorProperties ) private static void interpolateModelProperties( List<ModelProperty> modelProperties, List<InterpolatorProperty> interpolatorProperties )
{ {
if (modelProperties == null) { if ( modelProperties == null )
throw new IllegalArgumentException("modelProperties: null"); {
throw new IllegalArgumentException( "modelProperties: null" );
} }
if (interpolatorProperties == null) { if ( interpolatorProperties == null )
throw new IllegalArgumentException("interpolatorProperties: null"); {
throw new IllegalArgumentException( "interpolatorProperties: null" );
} }
List<ModelProperty> unresolvedProperties = new ArrayList<ModelProperty>(); List<ModelProperty> unresolvedProperties = new ArrayList<ModelProperty>();
for (ModelProperty mp : modelProperties) { for ( ModelProperty mp : modelProperties )
if (!mp.isResolved()) { {
unresolvedProperties.add(mp); if ( !mp.isResolved() )
{
unresolvedProperties.add( mp );
} }
} }
LinkedHashSet<InterpolatorProperty> ips = new LinkedHashSet<InterpolatorProperty>(); LinkedHashSet<InterpolatorProperty> ips = new LinkedHashSet<InterpolatorProperty>();
ips.addAll(interpolatorProperties); ips.addAll( interpolatorProperties );
boolean continueInterpolation = true; boolean continueInterpolation = true;
while (continueInterpolation) { while ( continueInterpolation )
{
continueInterpolation = false; continueInterpolation = false;
for (InterpolatorProperty ip : ips) { for ( InterpolatorProperty ip : ips )
for (ModelProperty mp : unresolvedProperties) { {
if (mp.resolveWith(ip) && !continueInterpolation) { for ( ModelProperty mp : unresolvedProperties )
{
if ( mp.resolveWith( ip ) && !continueInterpolation )
{
continueInterpolation = true; continueInterpolation = true;
break; break;
} }
@ -343,73 +311,74 @@ public class DefaultInterpolator implements Interpolator {
} }
} }
private static List<InterpolatorProperty> createInterpolatorProperties(List<ModelProperty> modelProperties, private static List<InterpolatorProperty> createInterpolatorProperties( List<ModelProperty> modelProperties, String baseUriForModel, Map<String, String> aliases, String interpolatorTag )
String baseUriForModel,
Map<String, String> aliases,
String interpolatorTag)
{ {
if (modelProperties == null) { if ( modelProperties == null )
throw new IllegalArgumentException("modelProperties: null"); {
throw new IllegalArgumentException( "modelProperties: null" );
} }
if (baseUriForModel == null) { if ( baseUriForModel == null )
throw new IllegalArgumentException("baseUriForModel: null"); {
throw new IllegalArgumentException( "baseUriForModel: null" );
} }
List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>(); List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
for (ModelProperty mp : modelProperties) { for ( ModelProperty mp : modelProperties )
InterpolatorProperty ip = mp {
.asInterpolatorProperty(baseUriForModel); InterpolatorProperty ip = mp.asInterpolatorProperty( baseUriForModel );
if (ip != null) { if ( ip != null )
ip.setTag(interpolatorTag); {
interpolatorProperties.add(ip); ip.setTag( interpolatorTag );
for (Map.Entry<String, String> a : aliases.entrySet()) { interpolatorProperties.add( ip );
interpolatorProperties.add(new InterpolatorProperty(ip for ( Map.Entry<String, String> a : aliases.entrySet() )
.getKey().replaceAll(a.getKey(), a.getValue()), ip {
.getValue().replaceAll(a.getKey(), a.getValue()), interpolatorProperties.add( new InterpolatorProperty( ip.getKey().replaceAll( a.getKey(), a.getValue() ), ip.getValue().replaceAll( a.getKey(), a.getValue() ), interpolatorTag ) );
interpolatorTag));
} }
} }
} }
List<InterpolatorProperty> ips = new ArrayList<InterpolatorProperty>(); List<InterpolatorProperty> ips = new ArrayList<InterpolatorProperty>();
for (InterpolatorProperty ip : interpolatorProperties) { for ( InterpolatorProperty ip : interpolatorProperties )
if (!ips.contains(ip)) { {
ips.add(ip); if ( !ips.contains( ip ) )
{
ips.add( ip );
} }
} }
return ips; return ips;
} }
private static List<ModelProperty> getModelProperties(InputStream is) throws IOException private static List<ModelProperty> getModelProperties( InputStream is )
throws IOException
{ {
Set<String> s = new HashSet<String>(); Set<String> s = new HashSet<String>();
//TODO: Should add all collections from ProjectUri //TODO: Should add all collections from ProjectUri
s.addAll(URIS); s.addAll( URIS );
s.add(ProjectUri.Build.PluginManagement.Plugins.Plugin.Executions.xUri); s.add( ProjectUri.Build.PluginManagement.Plugins.Plugin.Executions.xUri );
s.add(ProjectUri.DependencyManagement.Dependencies.Dependency.Exclusions.xUri); s.add( ProjectUri.DependencyManagement.Dependencies.Dependency.Exclusions.xUri );
s.add(ProjectUri.Dependencies.Dependency.Exclusions.xUri); s.add( ProjectUri.Dependencies.Dependency.Exclusions.xUri );
s.add(ProjectUri.Build.Plugins.Plugin.Executions.xUri); s.add( ProjectUri.Build.Plugins.Plugin.Executions.xUri );
s.add(ProjectUri.Build.Plugins.Plugin.Executions.Execution.Goals.xURI); s.add( ProjectUri.Build.Plugins.Plugin.Executions.Execution.Goals.xURI );
s.add(ProjectUri.Reporting.Plugins.Plugin.ReportSets.xUri); s.add( ProjectUri.Reporting.Plugins.Plugin.ReportSets.xUri );
s.add(ProjectUri.Reporting.Plugins.Plugin.ReportSets.ReportSet.configuration); s.add( ProjectUri.Reporting.Plugins.Plugin.ReportSets.ReportSet.configuration );
s.add(ProjectUri.Build.Plugins.Plugin.Executions.Execution.configuration); s.add( ProjectUri.Build.Plugins.Plugin.Executions.Execution.configuration );
//TODO: More profile info //TODO: More profile info
s.add(ProjectUri.Profiles.Profile.Build.PluginManagement.Plugins.Plugin.Executions.xUri); s.add( ProjectUri.Profiles.Profile.Build.PluginManagement.Plugins.Plugin.Executions.xUri );
s.add(ProjectUri.Profiles.Profile.DependencyManagement.Dependencies.Dependency.Exclusions.xUri); s.add( ProjectUri.Profiles.Profile.DependencyManagement.Dependencies.Dependency.Exclusions.xUri );
s.add(ProjectUri.Profiles.Profile.Dependencies.Dependency.Exclusions.xUri); s.add( ProjectUri.Profiles.Profile.Dependencies.Dependency.Exclusions.xUri );
s.add(ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.xUri); s.add( ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.xUri );
s.add(ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.Execution.Goals.xURI); s.add( ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.Execution.Goals.xURI );
s.add(ProjectUri.Profiles.Profile.Reporting.Plugins.Plugin.ReportSets.xUri); s.add( ProjectUri.Profiles.Profile.Reporting.Plugins.Plugin.ReportSets.xUri );
s.add(ProjectUri.Profiles.Profile.Reporting.Plugins.Plugin.ReportSets.ReportSet.configuration); s.add( ProjectUri.Profiles.Profile.Reporting.Plugins.Plugin.ReportSets.ReportSet.configuration );
s.add(ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.Execution.configuration); s.add( ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.Execution.configuration );
s.add(ProjectUri.Profiles.Profile.properties); s.add( ProjectUri.Profiles.Profile.properties );
s.add(ProjectUri.Profiles.Profile.modules); s.add( ProjectUri.Profiles.Profile.modules );
s.add(ProjectUri.Profiles.Profile.Dependencies.xUri); s.add( ProjectUri.Profiles.Profile.Dependencies.xUri );
s.add(ProjectUri.Profiles.Profile.Build.Plugins.Plugin.configuration); s.add( ProjectUri.Profiles.Profile.Build.Plugins.Plugin.configuration );
return new ArrayList<ModelProperty>(marshallXmlToModelProperties(is, ProjectUri.baseUri, s )); return new ArrayList<ModelProperty>( marshallXmlToModelProperties( is, ProjectUri.baseUri, s ) );
} }
/** /**
@ -449,9 +418,7 @@ public class DefaultInterpolator implements Interpolator {
// System.out.println("new ModelProperty(\"" + mp.getUri() +"\" , " + val +"),"); // System.out.println("new ModelProperty(\"" + mp.getUri() +"\" , " + val +"),");
if ( !uri.startsWith( baseUri ) ) if ( !uri.startsWith( baseUri ) )
{ {
throw new IllegalArgumentException( throw new IllegalArgumentException( "Passed in model property that does not match baseUri: Property URI = " + uri + ", Base URI = " + baseUri );
"Passed in model property that does not match baseUri: Property URI = " + uri + ", Base URI = " +
baseUri );
} }
List<String> tagNames = getTagNamesFromUri( basePosition, uri ); List<String> tagNames = getTagNamesFromUri( basePosition, uri );
@ -464,14 +431,14 @@ public class DefaultInterpolator implements Interpolator {
String tag = tagNames.get( tagNames.size() - 1 ); String tag = tagNames.get( tagNames.size() - 1 );
List<ModelProperty> attributes = new ArrayList<ModelProperty>(); List<ModelProperty> attributes = new ArrayList<ModelProperty>();
for(int peekIndex = modelProperties.indexOf( mp ) + 1; peekIndex < modelProperties.size(); peekIndex++) for ( int peekIndex = modelProperties.indexOf( mp ) + 1; peekIndex < modelProperties.size(); peekIndex++ )
{ {
if ( peekIndex <= modelProperties.size() - 1 ) if ( peekIndex <= modelProperties.size() - 1 )
{ {
ModelProperty peekProperty = modelProperties.get( peekIndex ); ModelProperty peekProperty = modelProperties.get( peekIndex );
if ( peekProperty.getUri().contains( "#property" ) ) if ( peekProperty.getUri().contains( "#property" ) )
{ {
attributes.add(peekProperty); attributes.add( peekProperty );
} }
else else
{ {
@ -503,8 +470,8 @@ public class DefaultInterpolator implements Interpolator {
} }
/** /**
* Returns list of tag names parsed from the specified uri. All #collection parts of the tag are removed from the * Returns list of tag names parsed from the specified uri. All #collection parts of the tag are
* tag names. * removed from the tag names.
* *
* @param basePosition the base position in the specified URI to start the parse * @param basePosition the base position in the specified URI to start the parse
* @param uri the uri to parse for tag names * @param uri the uri to parse for tag names
@ -512,8 +479,7 @@ public class DefaultInterpolator implements Interpolator {
*/ */
private static List<String> getTagNamesFromUri( int basePosition, String uri ) private static List<String> getTagNamesFromUri( int basePosition, String uri )
{ {
return Arrays.asList( uri.substring( basePosition ).replaceAll( "#collection", "" ) return Arrays.asList( uri.substring( basePosition ).replaceAll( "#collection", "" ).replaceAll( "#set", "" ).split( "/" ) );
.replaceAll("#set", "").split( "/" ) );
} }
/** /**
@ -529,11 +495,9 @@ public class DefaultInterpolator implements Interpolator {
sb.append( "\r\n<" ).append( value ); sb.append( "\r\n<" ).append( value );
if ( attributes != null ) if ( attributes != null )
{ {
for(ModelProperty attribute : attributes) for ( ModelProperty attribute : attributes )
{ {
sb.append( " " ).append( sb.append( " " ).append( attribute.getUri().substring( attribute.getUri().indexOf( "#property/" ) + 10 ) ).append( "=\"" ).append( attribute.getResolvedValue() ).append( "\" " );
attribute.getUri().substring( attribute.getUri().indexOf( "#property/" ) + 10 ) ).append( "=\"" )
.append( attribute.getResolvedValue() ).append( "\" " );
} }
} }
sb.append( ">" ); sb.append( ">" );
@ -557,37 +521,24 @@ public class DefaultInterpolator implements Interpolator {
return sb.toString(); return sb.toString();
} }
private static final Set<String> URIS = Collections.unmodifiableSet( new HashSet<String>( Arrays.asList( ProjectUri.Build.Extensions.xUri, ProjectUri.Build.PluginManagement.Plugins.xUri,
private static final Set<String> URIS = Collections.unmodifiableSet(new HashSet<String>( Arrays.asList( ProjectUri.Build.Extensions.xUri,
ProjectUri.Build.PluginManagement.Plugins.xUri,
ProjectUri.Build.PluginManagement.Plugins.Plugin.configuration, ProjectUri.Build.PluginManagement.Plugins.Plugin.configuration,
ProjectUri.Build.PluginManagement.Plugins.Plugin.Executions.xUri, ProjectUri.Build.PluginManagement.Plugins.Plugin.Executions.xUri,
ProjectUri.Build.PluginManagement.Plugins.Plugin.Executions.Execution.Goals.xURI, ProjectUri.Build.PluginManagement.Plugins.Plugin.Executions.Execution.Goals.xURI,
ProjectUri.Build.PluginManagement.Plugins.Plugin.Dependencies.xUri, ProjectUri.Build.PluginManagement.Plugins.Plugin.Dependencies.xUri,
ProjectUri.Build.PluginManagement.Plugins.Plugin.Dependencies.Dependency.Exclusions.xUri, ProjectUri.Build.PluginManagement.Plugins.Plugin.Dependencies.Dependency.Exclusions.xUri,
ProjectUri.Build.Plugins.xUri, ProjectUri.Build.Plugins.xUri, ProjectUri.properties,
ProjectUri.properties, ProjectUri.Build.Plugins.Plugin.configuration, ProjectUri.Reporting.Plugins.xUri,
ProjectUri.Build.Plugins.Plugin.configuration,
ProjectUri.Reporting.Plugins.xUri,
ProjectUri.Reporting.Plugins.Plugin.configuration, ProjectUri.Reporting.Plugins.Plugin.configuration,
ProjectUri.Build.Plugins.Plugin.Dependencies.xUri, ProjectUri.Build.Plugins.Plugin.Dependencies.xUri, ProjectUri.Build.Resources.xUri,
ProjectUri.Build.Resources.xUri,
ProjectUri.Build.Resources.Resource.includes, ProjectUri.Build.Resources.Resource.includes,
ProjectUri.Build.Resources.Resource.excludes, ProjectUri.Build.Resources.Resource.excludes, ProjectUri.Build.TestResources.xUri,
ProjectUri.Build.TestResources.xUri, ProjectUri.Build.Filters.xUri, ProjectUri.CiManagement.Notifiers.xUri,
ProjectUri.Build.Filters.xUri, ProjectUri.Contributors.xUri, ProjectUri.Dependencies.xUri,
ProjectUri.CiManagement.Notifiers.xUri, ProjectUri.DependencyManagement.Dependencies.xUri, ProjectUri.Developers.xUri,
ProjectUri.Contributors.xUri, ProjectUri.Developers.Developer.roles, ProjectUri.Licenses.xUri,
ProjectUri.Dependencies.xUri, ProjectUri.MailingLists.xUri, ProjectUri.Modules.xUri, ProjectUri.PluginRepositories.xUri,
ProjectUri.DependencyManagement.Dependencies.xUri, ProjectUri.Profiles.xUri, ProjectUri.Profiles.Profile.Build.Plugins.xUri,
ProjectUri.Developers.xUri,
ProjectUri.Developers.Developer.roles,
ProjectUri.Licenses.xUri,
ProjectUri.MailingLists.xUri,
ProjectUri.Modules.xUri,
ProjectUri.PluginRepositories.xUri,
ProjectUri.Profiles.xUri,
ProjectUri.Profiles.Profile.Build.Plugins.xUri,
ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Dependencies.xUri, ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Dependencies.xUri,
ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.xUri, ProjectUri.Profiles.Profile.Build.Plugins.Plugin.Executions.xUri,
ProjectUri.Profiles.Profile.Build.Resources.xUri, ProjectUri.Profiles.Profile.Build.Resources.xUri,
@ -599,123 +550,130 @@ public class DefaultInterpolator implements Interpolator {
ProjectUri.Profiles.Profile.Repositories.xUri, ProjectUri.Profiles.Profile.Repositories.xUri,
ProjectUri.Profiles.Profile.Build.PluginManagement.Plugins.xUri, ProjectUri.Profiles.Profile.Build.PluginManagement.Plugins.xUri,
ProjectUri.Profiles.Profile.Build.PluginManagement.Plugins.Plugin.Dependencies.xUri, ProjectUri.Profiles.Profile.Build.PluginManagement.Plugins.Plugin.Dependencies.xUri,
ProjectUri.Reporting.Plugins.xUri, ProjectUri.Reporting.Plugins.xUri, ProjectUri.Repositories.xUri ) ) );
ProjectUri.Repositories.xUri) ));
/** /**
* Returns list of model properties transformed from the specified input stream. * Returns list of model properties transformed from the specified input stream.
* *
* @param inputStream input stream containing the xml document. May not be null. * @param inputStream input stream containing the xml document. May not be null.
* @param baseUri the base uri of every model property. May not be null or empty. * @param baseUri the base uri of every model property. May not be null or empty.
* @param collections set of uris that are to be treated as a collection (multiple entries). May be null. * @param collections set of uris that are to be treated as a collection (multiple entries). May
* be null.
* @return list of model properties transformed from the specified input stream. * @return list of model properties transformed from the specified input stream.
* @throws IOException if there was a problem doing the transform * @throws IOException if there was a problem doing the transform
*/ */
private static List<ModelProperty> marshallXmlToModelProperties( InputStream inputStream, String baseUri, private static List<ModelProperty> marshallXmlToModelProperties( InputStream inputStream, String baseUri, Set<String> collections )
Set<String> collections ) throws IOException
throws IOException { {
if (inputStream == null) { if ( inputStream == null )
throw new IllegalArgumentException("inputStream: null"); {
throw new IllegalArgumentException( "inputStream: null" );
} }
if (baseUri == null || baseUri.trim().length() == 0) { if ( baseUri == null || baseUri.trim().length() == 0 )
throw new IllegalArgumentException("baseUri: null"); {
throw new IllegalArgumentException( "baseUri: null" );
} }
if (collections == null) { if ( collections == null )
{
collections = Collections.emptySet(); collections = Collections.emptySet();
} }
List<ModelProperty> modelProperties = new ArrayList<ModelProperty>(); List<ModelProperty> modelProperties = new ArrayList<ModelProperty>();
XMLInputFactory xmlInputFactory = new com.ctc.wstx.stax.WstxInputFactory(); XMLInputFactory xmlInputFactory = new com.ctc.wstx.stax.WstxInputFactory();
xmlInputFactory.setProperty( xmlInputFactory.setProperty( XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE );
XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); xmlInputFactory.setProperty( XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE );
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,
Boolean.FALSE);
Uri uri = new Uri(baseUri); Uri uri = new Uri( baseUri );
String tagName = baseUri; String tagName = baseUri;
StringBuilder tagValue = new StringBuilder(256); StringBuilder tagValue = new StringBuilder( 256 );
int depth = 0; int depth = 0;
int depthOfTagValue = depth; int depthOfTagValue = depth;
XMLStreamReader xmlStreamReader = null; XMLStreamReader xmlStreamReader = null;
try { try
xmlStreamReader = xmlInputFactory {
.createXMLStreamReader(inputStream); xmlStreamReader = xmlInputFactory.createXMLStreamReader( inputStream );
Map<String, String> attributes = new HashMap<String, String>(); Map<String, String> attributes = new HashMap<String, String>();
for (;; xmlStreamReader.next()) { for ( ;; xmlStreamReader.next() )
{
int type = xmlStreamReader.getEventType(); int type = xmlStreamReader.getEventType();
switch (type) { switch ( type )
{
case XMLStreamConstants.CDATA: case XMLStreamConstants.CDATA:
case XMLStreamConstants.CHARACTERS: { case XMLStreamConstants.CHARACTERS:
if (depth == depthOfTagValue) { {
tagValue.append(xmlStreamReader.getTextCharacters(), if ( depth == depthOfTagValue )
xmlStreamReader.getTextStart(), xmlStreamReader {
.getTextLength()); tagValue.append( xmlStreamReader.getTextCharacters(), xmlStreamReader.getTextStart(), xmlStreamReader.getTextLength() );
} }
break; break;
} }
case XMLStreamConstants.START_ELEMENT: { case XMLStreamConstants.START_ELEMENT:
if (!tagName.equals(baseUri)) { {
if ( !tagName.equals( baseUri ) )
{
String value = null; String value = null;
if (depth < depthOfTagValue) { if ( depth < depthOfTagValue )
{
value = tagValue.toString().trim(); value = tagValue.toString().trim();
} }
modelProperties.add(new ModelProperty(tagName, value)); modelProperties.add( new ModelProperty( tagName, value ) );
if (!attributes.isEmpty()) { if ( !attributes.isEmpty() )
for (Map.Entry<String, String> e : attributes {
.entrySet()) { for ( Map.Entry<String, String> e : attributes.entrySet() )
modelProperties.add(new ModelProperty(e {
.getKey(), e.getValue())); modelProperties.add( new ModelProperty( e.getKey(), e.getValue() ) );
} }
attributes.clear(); attributes.clear();
} }
} }
depth++; depth++;
tagName = uri.getUriFor(xmlStreamReader.getName() tagName = uri.getUriFor( xmlStreamReader.getName().getLocalPart(), depth );
.getLocalPart(), depth); if ( collections.contains( tagName + "#collection" ) )
if (collections.contains(tagName + "#collection")) { {
tagName = tagName + "#collection"; tagName = tagName + "#collection";
uri.addTag(xmlStreamReader.getName().getLocalPart() uri.addTag( xmlStreamReader.getName().getLocalPart() + "#collection" );
+ "#collection");
} else if (collections.contains(tagName + "#set")) {
tagName = tagName + "#set";
uri.addTag(xmlStreamReader.getName().getLocalPart()
+ "#set");
} else {
uri.addTag(xmlStreamReader.getName().getLocalPart());
} }
tagValue.setLength(0); else if ( collections.contains( tagName + "#set" ) )
{
tagName = tagName + "#set";
uri.addTag( xmlStreamReader.getName().getLocalPart() + "#set" );
}
else
{
uri.addTag( xmlStreamReader.getName().getLocalPart() );
}
tagValue.setLength( 0 );
depthOfTagValue = depth; depthOfTagValue = depth;
} }
case XMLStreamConstants.ATTRIBUTE: { case XMLStreamConstants.ATTRIBUTE:
for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { {
for ( int i = 0; i < xmlStreamReader.getAttributeCount(); i++ )
{
attributes.put(tagName attributes.put( tagName + "#property/" + xmlStreamReader.getAttributeName( i ).getLocalPart(), xmlStreamReader.getAttributeValue( i ) );
+ "#property/"
+ xmlStreamReader.getAttributeName(i)
.getLocalPart(), xmlStreamReader
.getAttributeValue(i));
} }
break; break;
} }
case XMLStreamConstants.END_ELEMENT: { case XMLStreamConstants.END_ELEMENT:
{
depth--; depth--;
break; break;
} }
case XMLStreamConstants.END_DOCUMENT: { case XMLStreamConstants.END_DOCUMENT:
modelProperties.add(new ModelProperty(tagName, tagValue {
.toString().trim())); modelProperties.add( new ModelProperty( tagName, tagValue.toString().trim() ) );
if (!attributes.isEmpty()) { if ( !attributes.isEmpty() )
for (Map.Entry<String, String> e : attributes {
.entrySet()) { for ( Map.Entry<String, String> e : attributes.entrySet() )
modelProperties.add(new ModelProperty(e.getKey(), e {
.getValue())); modelProperties.add( new ModelProperty( e.getKey(), e.getValue() ) );
} }
attributes.clear(); attributes.clear();
} }
@ -723,19 +681,30 @@ public class DefaultInterpolator implements Interpolator {
} }
} }
} }
} catch (XMLStreamException e) { }
throw new IOException(":" + e.toString()); catch ( XMLStreamException e )
} finally { {
if (xmlStreamReader != null) { throw new IOException( ":" + e.toString() );
try { }
finally
{
if ( xmlStreamReader != null )
{
try
{
xmlStreamReader.close(); xmlStreamReader.close();
} catch (XMLStreamException e) { }
catch ( XMLStreamException e )
{
e.printStackTrace(); e.printStackTrace();
} }
} }
try { try
{
inputStream.close(); inputStream.close();
} catch (IOException e) { }
catch ( IOException e )
{
} }
} }
@ -774,8 +743,7 @@ public class DefaultInterpolator implements Interpolator {
private static boolean containsProjectVersion( List<InterpolatorProperty> interpolatorProperties ) private static boolean containsProjectVersion( List<InterpolatorProperty> interpolatorProperties )
{ {
InterpolatorProperty versionInterpolatorProperty = InterpolatorProperty versionInterpolatorProperty = new ModelProperty( ProjectUri.version, "" ).asInterpolatorProperty( ProjectUri.baseUri );
new ModelProperty( ProjectUri.version, "" ).asInterpolatorProperty( ProjectUri.baseUri );
for ( InterpolatorProperty ip : interpolatorProperties ) for ( InterpolatorProperty ip : interpolatorProperties )
{ {
if ( ip.equals( versionInterpolatorProperty ) ) if ( ip.equals( versionInterpolatorProperty ) )
@ -785,6 +753,7 @@ public class DefaultInterpolator implements Interpolator {
} }
return false; return false;
} }
/** /**
* Class for storing information about URIs. * Class for storing information about URIs.
*/ */

View File

@ -1,20 +1,12 @@
package org.apache.maven.model.interpolator; package org.apache.maven.model.interpolator;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.apache.maven.model.PomClassicDomainModel; import org.apache.maven.model.PomClassicDomainModel;
public interface Interpolator public interface Interpolator
{ {
String interpolateXmlString( String xml, List<InterpolatorProperty> interpolatorProperties )
throws IOException;
PomClassicDomainModel interpolateDomainModel( PomClassicDomainModel dm, List<InterpolatorProperty> interpolatorProperties )
throws IOException ;
PomClassicDomainModel interpolateDomainModel( PomClassicDomainModel dm, Properties interpolatorProperties ) PomClassicDomainModel interpolateDomainModel( PomClassicDomainModel dm, Properties interpolatorProperties )
throws IOException ; throws IOException;
} }