new XML Input Factory - Java XML

Java examples for XML:StAX

Description

new XML Input Factory

Demo Code

//Licensed under the Apache License, Version 2.0 (the "License");
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;

public class Main{
    public static void main(String[] argv) throws Exception{
        System.out.println(newXMLInputFactory());
    }/*from  www.  ja  v  a2 s .c om*/
    static final String CONFIGURED_INPUT_FACTORY = System
            .getProperty("javax.xml.stream.XMLInputFactory");
    static final boolean CHECK_PARENT = Boolean
            .getBoolean("protostuff.loader.check_parent");
    private static final String[] INPUT_FACTORY_IMPLS = new String[] {
            "com.fasterxml.aalto.stax.InputFactoryImpl",
            "com.ctc.wstx.stax.WstxInputFactory",
            "com.sun.xml.fastinfoset.stax.factory.StAXInputFactory",
            "com.sun.xml.internal.stream.XMLInputFactoryImpl" };
    static XMLInputFactory newXMLInputFactory() {
        if (CONFIGURED_INPUT_FACTORY != null) {
            Class<?> c = loadClass(CONFIGURED_INPUT_FACTORY,
                    XmlIOFactoryUtil.class, CHECK_PARENT);
            if (c == null)
                throw new IllegalStateException("Could not load class: "
                        + CONFIGURED_INPUT_FACTORY);

            try {
                return (XMLInputFactory) c.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        for (String s : INPUT_FACTORY_IMPLS) {
            Class<?> c = loadClass(s, XmlIOFactoryUtil.class, CHECK_PARENT);
            if (c != null) {
                try {
                    return (XMLInputFactory) c.newInstance();
                } catch (Exception e) {
                    // print stacktrace?
                    // e.printStackTrace();
                    continue;
                }
            }
        }

        throw new IllegalStateException(
                "Cannot find impl for javax.xml.stream.XMLInputFactory");
    }
    /**
     * Loads a class from the classloader; 
     * If not found, the classloader of the {@code context} class specified will be used.
     * If the flag {@code checkParent} is true, the classloader's parent is included in 
     * the lookup.
     */
    static Class<?> loadClass(String className, Class<?> context,
            boolean checkParent) {
        Class<?> clazz = null;
        try {
            clazz = Thread.currentThread().getContextClassLoader()
                    .loadClass(className);
        } catch (ClassNotFoundException e) {
            if (context != null) {
                ClassLoader loader = context.getClassLoader();
                while (loader != null) {
                    try {
                        clazz = loader.loadClass(className);
                        return clazz;
                    } catch (ClassNotFoundException e1) {
                        loader = checkParent ? loader.getParent() : null;
                    }
                }
            }
        }
        return clazz;
    }
}

Related Tutorials