Skip to content

Commit

Permalink
[MNG-7751] Provide a way to inject XmlNode into plugins (#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Jun 12, 2023
1 parent be3625d commit 56ee6a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.configuration.BeanConfigurationException;
import org.apache.maven.configuration.BeanConfigurationPathTranslator;
import org.apache.maven.configuration.BeanConfigurationRequest;
import org.apache.maven.configuration.BeanConfigurationValuePreprocessor;
import org.apache.maven.configuration.BeanConfigurator;
import org.apache.maven.internal.xml.XmlNodeImpl;
import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
import org.codehaus.plexus.component.configurator.ConfigurationListener;
import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
import org.codehaus.plexus.component.configurator.converters.basic.AbstractBasicConverter;
import org.codehaus.plexus.component.configurator.converters.composite.ObjectWithFieldsConverter;
import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
Expand Down Expand Up @@ -133,6 +138,41 @@ public File alignToBaseDirectory(File file) {
}
}

static class XmlConverter extends AbstractConfigurationConverter {
@Override
public boolean canConvert(Class<?> type) {
return XmlNode.class.equals(type);
}

@Override
public Object fromConfiguration(
final ConverterLookup lookup,
final PlexusConfiguration configuration,
final Class<?> type,
final Class<?> enclosingType,
final ClassLoader loader,
final ExpressionEvaluator evaluator,
final ConfigurationListener listener)
throws ComponentConfigurationException {

try {
return toXml(configuration, evaluator);
} catch (ExpressionEvaluationException e) {
throw new ComponentConfigurationException("Unable to convert configuration to xml node", e);
}
}

XmlNode toXml(PlexusConfiguration config, ExpressionEvaluator evaluator) throws ExpressionEvaluationException {
List<XmlNode> children = new ArrayList<>();
for (PlexusConfiguration c : config.getChildren()) {
children.add(toXml(c, evaluator));
}
String name = config.getName();
Object value = evaluator.evaluate(config.getValue());
return new XmlNodeImpl(name, value != null ? value.toString() : null, null, children, null);
}
}

static class PathConverter extends AbstractBasicConverter {
@Override
public boolean canConvert(Class<?> type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class EnhancedConverterLookup implements ConverterLookup {

EnhancedConverterLookup() {
registerConverter(new DefaultBeanConfigurator.PathConverter());
registerConverter(new DefaultBeanConfigurator.XmlConverter());
}

@Override
Expand Down

0 comments on commit 56ee6a8

Please sign in to comment.