Search in sources :

Example 1 with CompositeClassLoader

use of com.thoughtworks.xstream.core.util.CompositeClassLoader in project camel by apache.

the class AbstractXStreamWrapper method createXStream.

protected XStream createXStream(ClassResolver resolver, ClassLoader classLoader) {
    if (xstreamDriver != null) {
        xstream = new XStream(xstreamDriver);
    } else {
        xstream = new XStream();
    }
    if (mode != null) {
        xstream.setMode(getModeFromString(mode));
    }
    ClassLoader xstreamLoader = xstream.getClassLoader();
    if (classLoader != null && xstreamLoader instanceof CompositeClassLoader) {
        ((CompositeClassLoader) xstreamLoader).add(classLoader);
    }
    try {
        if (this.implicitCollections != null) {
            for (Entry<String, String[]> entry : this.implicitCollections.entrySet()) {
                for (String name : entry.getValue()) {
                    xstream.addImplicitCollection(resolver.resolveMandatoryClass(entry.getKey()), name);
                }
            }
        }
        if (this.aliases != null) {
            for (Entry<String, String> entry : this.aliases.entrySet()) {
                xstream.alias(entry.getKey(), resolver.resolveMandatoryClass(entry.getValue()));
                // It can turn the auto-detection mode off
                xstream.processAnnotations(resolver.resolveMandatoryClass(entry.getValue()));
            }
        }
        if (this.omitFields != null) {
            for (Entry<String, String[]> entry : this.omitFields.entrySet()) {
                for (String name : entry.getValue()) {
                    xstream.omitField(resolver.resolveMandatoryClass(entry.getKey()), name);
                }
            }
        }
        if (this.converters != null) {
            for (String name : this.converters) {
                Class<Converter> converterClass = resolver.resolveMandatoryClass(name, Converter.class);
                Converter converter;
                Constructor<Converter> con = null;
                try {
                    con = converterClass.getDeclaredConstructor(new Class[] { XStream.class });
                } catch (Exception e) {
                //swallow as we null check in a moment.
                }
                if (con != null) {
                    converter = con.newInstance(xstream);
                } else {
                    converter = converterClass.newInstance();
                    try {
                        Method method = converterClass.getMethod("setXStream", new Class[] { XStream.class });
                        if (method != null) {
                            ObjectHelper.invokeMethod(method, converter, xstream);
                        }
                    } catch (Throwable e) {
                    // swallow, as it just means the user never add an XStream setter, which is optional
                    }
                }
                xstream.registerConverter(converter);
            }
        }
        addDefaultPermissions(xstream);
        if (this.permissions != null) {
            // permissions ::= pterm (',' pterm)*   # consits of one or more terms
            // pterm       ::= aod? wterm           # each term preceded by an optional sign 
            // aod         ::= '+' | '-'            # indicates allow or deny where allow if omitted
            // wterm       ::= a class name with optional wildcard characters
            addPermissions(xstream, permissions);
        }
    } catch (Exception e) {
        throw new RuntimeException("Unable to build XStream instance", e);
    }
    return xstream;
}
Also used : XStream(com.thoughtworks.xstream.XStream) Method(java.lang.reflect.Method) XMLStreamException(javax.xml.stream.XMLStreamException) CompositeClassLoader(com.thoughtworks.xstream.core.util.CompositeClassLoader) StaxConverter(org.apache.camel.converter.jaxp.StaxConverter) Converter(com.thoughtworks.xstream.converters.Converter) CompositeClassLoader(com.thoughtworks.xstream.core.util.CompositeClassLoader)

Aggregations

XStream (com.thoughtworks.xstream.XStream)1 Converter (com.thoughtworks.xstream.converters.Converter)1 CompositeClassLoader (com.thoughtworks.xstream.core.util.CompositeClassLoader)1 Method (java.lang.reflect.Method)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 StaxConverter (org.apache.camel.converter.jaxp.StaxConverter)1