com.nlocketz.plugins.Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-repeater-maven-plugin Show documentation
Show all versions of plugin-repeater-maven-plugin Show documentation
A Maven plugin that repeats a plugin while replacing placeholder variables. Used to reduce duplication within the POM.
package com.nlocketz.plugins;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import java.util.Map;
final class Util {
private Util () {
//
}
public static String filter(Map subs, String toFilter) {
String output = toFilter;
// TODO precompiled pattern (or pattern cache)
for (String varName : subs.keySet()) {
String varExpr = String.format("@%s@", varName);
if (output.contains(varExpr)) {
output = output.replaceAll(varExpr, subs.get(varName));
}
}
return output;
}
private static void filterInternal(Map subs, PlexusConfiguration toFilter) {
// If its a leaf, just do it.
if (toFilter.getChildCount() == 0) {
toFilter.setValue(filter(subs, toFilter.getValue()));
return;
}
// Filter children
for (int i = 0; i < toFilter.getChildCount(); i++) {
PlexusConfiguration child = toFilter.getChild(i);
if (child.getChildCount() == 0) {
child.setValue(filter(subs, child.getValue()));
} else {
filter(subs, child);
}
}
// Filter attributes
for (String attrName : toFilter.getAttributeNames()) {
toFilter.setAttribute(attrName, filter(subs, toFilter.getAttribute(attrName)));
}
}
static PlexusConfiguration filter(Map subs, PlexusConfiguration config) {
PlexusConfiguration output = new XmlPlexusConfiguration(plexusToDom(config));
// This one mutates the given config
filterInternal(subs, output);
return output;
}
static Xpp3Dom plexusToDom(PlexusConfiguration config) {
Xpp3Dom output = new Xpp3Dom(config.getName());
output.setValue(config.getValue(null));
for (String attrName : config.getAttributeNames()) {
output.setAttribute(attrName, config.getAttribute(attrName));
}
for (PlexusConfiguration child : config.getChildren()) {
// Convert children.
output.addChild(plexusToDom(child));
}
return output;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy