org.eclipse.sisu.plexus.PlexusXmlBeanConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-embedder Show documentation
Show all versions of maven-embedder Show documentation
Maven embeddable component, with CLI and logging support.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.eclipse.sisu.plexus;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.TypeConverter;
import com.google.inject.spi.TypeConverterBinding;
import org.apache.maven.api.xml.XmlNode;
import org.apache.maven.internal.xml.XmlNodeBuilder;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.pull.MXParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.sisu.Priority;
import org.eclipse.sisu.bean.BeanProperties;
import org.eclipse.sisu.bean.BeanProperty;
import org.eclipse.sisu.inject.Logs;
import org.eclipse.sisu.inject.TypeArguments;
/**
* {@link PlexusBeanConverter} {@link Module} that converts Plexus XML configuration into beans.
*/
@Singleton
@Priority(10)
public final class PlexusXmlBeanConverter implements PlexusBeanConverter {
// ----------------------------------------------------------------------
// Constants
// ----------------------------------------------------------------------
private static final String CONVERSION_ERROR = "Cannot convert: \"%s\" to: %s";
// ----------------------------------------------------------------------
// Implementation fields
// ----------------------------------------------------------------------
private final Collection typeConverterBindings;
// ----------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------
@Inject
PlexusXmlBeanConverter(final Injector injector) {
typeConverterBindings = injector.getTypeConverterBindings();
}
// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
@SuppressWarnings({"unchecked", "rawtypes"})
public Object convert(final TypeLiteral role, final String value) {
if (value.trim().startsWith("<")) {
try {
final MXParser parser = new MXParser();
parser.setInput(new StringReader(value));
parser.nextTag();
return parse(parser, role);
} catch (final Exception e) {
throw new IllegalArgumentException(String.format(CONVERSION_ERROR, value, role), e);
}
}
return convertText(value, role);
}
// ----------------------------------------------------------------------
// Implementation methods
// ----------------------------------------------------------------------
/**
* Parses a sequence of XML elements and converts them to the given target type.
*
* @param parser The XML parser
* @param toType The target type
* @return Converted instance of the target type
*/
private Object parse(final MXParser parser, final TypeLiteral> toType) throws Exception {
parser.require(XmlPullParser.START_TAG, null, null);
final Class> rawType = toType.getRawType();
if (XmlNode.class.isAssignableFrom(rawType)) {
return XmlNodeBuilder.build(parser);
}
if (Xpp3Dom.class.isAssignableFrom(rawType)) {
return new Xpp3Dom(XmlNodeBuilder.build(parser));
}
if (Properties.class.isAssignableFrom(rawType)) {
return parseProperties(parser);
}
if (Map.class.isAssignableFrom(rawType)) {
return parseMap(parser, TypeArguments.get(toType.getSupertype(Map.class), 1));
}
if (Collection.class.isAssignableFrom(rawType)) {
return parseCollection(parser, TypeArguments.get(toType.getSupertype(Collection.class), 0));
}
if (rawType.isArray()) {
return parseArray(parser, TypeArguments.get(toType, 0));
}
return parseBean(parser, toType, rawType);
}
/**
* Parses a sequence of XML elements and converts them to the appropriate {@link Properties} type.
*
* @param parser The XML parser
* @return Converted Properties instance
*/
private static Properties parseProperties(final XmlPullParser parser) throws Exception {
final Properties properties = newImplementation(parser, Properties.class);
while (parser.nextTag() == XmlPullParser.START_TAG) {
parser.nextTag();
// 'name-then-value' or 'value-then-name'
if ("name".equals(parser.getName())) {
final String name = parser.nextText();
parser.nextTag();
properties.put(name, parser.nextText());
} else {
final String value = parser.nextText();
parser.nextTag();
properties.put(parser.nextText(), value);
}
parser.nextTag();
}
return properties;
}
/**
* Parses a sequence of XML elements and converts them to the appropriate {@link Map} type.
*
* @param parser The XML parser
* @return Converted Map instance
*/
private Map parseMap(final MXParser parser, final TypeLiteral> toType) throws Exception {
@SuppressWarnings("unchecked")
final Map map = newImplementation(parser, HashMap.class);
while (parser.nextTag() == XmlPullParser.START_TAG) {
map.put(parser.getName(), parse(parser, toType));
}
return map;
}
/**
* Parses a sequence of XML elements and converts them to the appropriate {@link Collection} type.
*
* @param parser The XML parser
* @return Converted Collection instance
*/
private Collection