Example usage for javax.xml.stream XMLInputFactory getClass

List of usage examples for javax.xml.stream XMLInputFactory getClass

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:ca.uhn.fhir.util.XmlUtil.java

private static XMLInputFactory getOrCreateInputFactory() throws FactoryConfigurationError {
    if (ourInputFactory == null) {

        try {//ww  w .  ja  v a  2  s . c o m
            // Detect if we're running with the Android lib, and force repackaged Woodstox to be used
            Class.forName("ca.uhn.fhir.repackage.javax.xml.stream.XMLInputFactory");
            System.setProperty("javax.xml.stream.XMLInputFactory", "com.ctc.wstx.stax.WstxInputFactory");
        } catch (ClassNotFoundException e) {
            // ok
        }

        XMLInputFactory inputFactory = newInputFactory();

        if (!ourHaveLoggedStaxImplementation) {
            logStaxImplementation(inputFactory.getClass());
        }

        /*
         * These two properties disable external entity processing, which can 
         * be a security vulnerability.
         * 
         * See https://github.com/jamesagnew/hapi-fhir/issues/339
         * https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Processing
         */
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); // This disables DTDs entirely for that factory
        inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", false); // disable external entities

        /*
         * In the following few lines, you can uncomment the first and comment the second to disable automatic
         * parsing of extended entities, e.g. &sect;
         * 
         * Note that these properties are Woodstox specific and they cause a crash in environments where SJSXP is
         * being used (e.g. glassfish) so we don't set them there.
         */
        try {
            Class.forName("com.ctc.wstx.stax.WstxInputFactory");
            if (inputFactory instanceof com.ctc.wstx.stax.WstxInputFactory) {
                // inputFactory.setProperty(WstxInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
                inputFactory.setProperty(WstxInputProperties.P_UNDECLARED_ENTITY_RESOLVER, XML_RESOLVER);
                try {
                    inputFactory.setProperty(WstxInputProperties.P_MAX_ATTRIBUTE_SIZE, "100000000");
                } catch (IllegalArgumentException e) {
                    // ignore
                }
            }
        } catch (ClassNotFoundException e) {
            ourLog.debug("WstxOutputFactory (Woodstox) not found on classpath");
        }
        ourInputFactory = inputFactory;
    }
    return ourInputFactory;
}

From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java

/**
 * parses an xml configuration from an input streams. throwes
 * IllegalArgumentExceptions in case of syntax error.
 * /*w  w w  .j  ava2 s .  c o  m*/
 * @param in
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 *             in case there are problems with an params type converter
 */
public static TranslationRule[] parse(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    @SuppressWarnings("deprecation")
    final XMLInputFactory factory = XMLInputFactory.newInstance(
            "com.sun.xml.internal.stream.XMLInputFactoryImpl", TranslationParser.class.getClassLoader());
    LOG.info("XMLInputFactory loaded: " + factory.getClass().getName());
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    // not supported be the reference implementation
    // factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("rules")) {
            return parseRules(parser, loader);
        }
    }
    throw new IllegalArgumentException("No rules section found.");
}

From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java

/**
 * parses an single file translation//from  w  w w  .j  a  v a 2 s.co m
 * 
 * @param in
 * @param loader
 * @return value
 * @throws XMLStreamException
 * @throws ClassNotFoundException
 *             in case there are problems with an params type converter
 */
public static TranslationRule parseSingle(final InputStream in, final ClassLoader loader)
        throws XMLStreamException, ClassNotFoundException {
    @SuppressWarnings("deprecation")
    final XMLInputFactory factory = XMLInputFactory.newInstance(
            "com.sun.xml.internal.stream.XMLInputFactoryImpl", TranslationParser.class.getClassLoader());
    LOG.info("XMLInputFactory loaded: " + factory.getClass().getName());
    factory.setProperty("javax.xml.stream.isCoalescing", true);
    final XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        final int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT && parser.getName().getLocalPart().equals("rule")) {
            return parseRule(parser, loader);
        }
    }
    throw new IllegalArgumentException("No rules section found.");
}

From source file:org.apache.axiom.om.util.ElementHelperTest.java

public void testGetTextAsStreamWithoutCaching() throws Exception {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (factory.getClass().getName().equals("com.bea.xml.stream.MXParserFactory")) {
        // Skip the test on the StAX reference implementation because it
        // causes an out of memory error
        return;/*from w  w w  .  ja  v  a  2s .  c o  m*/
    }
    DataSource ds = new RandomDataSource(654321, 64, 128, 20000000);
    Vector/*<InputStream>*/ v = new Vector/*<InputStream>*/();
    v.add(new ByteArrayInputStream("<a>".getBytes("ascii")));
    v.add(ds.getInputStream());
    v.add(new ByteArrayInputStream("</a>".getBytes("ascii")));
    factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
    XMLStreamReader reader = factory.createXMLStreamReader(new SequenceInputStream(v.elements()), "ascii");
    OMElement element = new StAXOMBuilder(reader).getDocumentElement();
    Reader in = ElementHelper.getTextAsStream(element, false);
    IOTestUtils.compareStreams(new InputStreamReader(ds.getInputStream(), "ascii"), in);
}

From source file:org.apache.axiom.om.util.StAXUtils.java

private static XMLInputFactory newXMLInputFactory(final ClassLoader classLoader,
        final StAXParserConfiguration configuration) {

    return (XMLInputFactory) AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
            ClassLoader savedClassLoader;
            if (classLoader == null) {
                savedClassLoader = null;
            } else {
                savedClassLoader = Thread.currentThread().getContextClassLoader();
                Thread.currentThread().setContextClassLoader(classLoader);
            }/*from w w w  .java2  s . co m*/
            try {
                XMLInputFactory factory = XMLInputFactory.newInstance();
                // Woodstox by default creates coalescing parsers. Even if this violates
                // the StAX specs, for compatibility with Woodstox, we always enable the
                // coalescing mode. Note that we need to do that before loading
                // XMLInputFactory.properties so that this setting can be overridden.
                factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
                Map props = loadFactoryProperties("XMLInputFactory.properties");
                if (props != null) {
                    for (Iterator it = props.entrySet().iterator(); it.hasNext();) {
                        Map.Entry entry = (Map.Entry) it.next();
                        factory.setProperty((String) entry.getKey(), entry.getValue());
                    }
                }
                StAXDialect dialect = StAXDialectDetector.getDialect(factory.getClass());
                if (configuration != null) {
                    factory = configuration.configure(factory, dialect);
                }
                return new ImmutableXMLInputFactory(dialect.normalize(dialect.makeThreadSafe(factory)));
            } finally {
                if (savedClassLoader != null) {
                    Thread.currentThread().setContextClassLoader(savedClassLoader);
                }
            }
        }
    });
}

From source file:org.apache.axiom.om.util.StAXUtils.java

/**
 * @return XMLInputFactory for the current classloader
 *///from  w ww. j a va  2s  .com
private static XMLInputFactory getXMLInputFactory_perClassLoader(StAXParserConfiguration configuration) {

    ClassLoader cl = getContextClassLoader();
    XMLInputFactory factory;
    if (cl == null) {
        factory = getXMLInputFactory_singleton(configuration);
    } else {
        // Check the cache
        if (configuration == null) {
            configuration = StAXParserConfiguration.DEFAULT;
        }
        Map map = (Map) inputFactoryPerCLMap.get(configuration);
        if (map == null) {
            map = Collections.synchronizedMap(new WeakHashMap());
            inputFactoryPerCLMap.put(configuration, map);
            factory = null;
        } else {
            factory = (XMLInputFactory) map.get(cl);
        }

        // If not found in the cache map, crate a new factory
        if (factory == null) {

            if (log.isDebugEnabled()) {
                log.debug("About to create XMLInputFactory implementation with " + "classloader=" + cl);
                log.debug("The classloader for javax.xml.stream.XMLInputFactory is: "
                        + XMLInputFactory.class.getClassLoader());
            }
            try {
                factory = newXMLInputFactory(null, configuration);
            } catch (ClassCastException cce) {
                if (log.isDebugEnabled()) {
                    log.debug("Failed creation of XMLInputFactory implementation with " + "classloader=" + cl);
                    log.debug("Exception is=" + cce);
                    log.debug("Attempting with classloader: " + XMLInputFactory.class.getClassLoader());
                }
                factory = newXMLInputFactory(XMLInputFactory.class.getClassLoader(), configuration);
            }

            if (factory != null) {
                // Cache the new factory
                map.put(cl, factory);

                if (log.isDebugEnabled()) {
                    log.debug("Created XMLInputFactory = " + factory.getClass() + " with classloader=" + cl);
                    log.debug("Configuration = " + configuration);
                    log.debug("Size of XMLInputFactory map for this configuration = " + map.size());
                    log.debug("Configurations for which factories have been cached = "
                            + inputFactoryPerCLMap.keySet());
                }
            } else {
                factory = getXMLInputFactory_singleton(configuration);
            }
        }

    }
    return factory;
}

From source file:org.apache.axiom.om.util.StAXUtils.java

/**
 * @return singleton XMLInputFactory loaded with the StAXUtils classloader
 *///w ww.j  av a2 s.  c o  m
private static XMLInputFactory getXMLInputFactory_singleton(StAXParserConfiguration configuration) {
    if (configuration == null) {
        configuration = StAXParserConfiguration.DEFAULT;
    }
    XMLInputFactory f = (XMLInputFactory) inputFactoryMap.get(configuration);
    if (f == null) {
        f = newXMLInputFactory(StAXUtils.class.getClassLoader(), configuration);
        inputFactoryMap.put(configuration, f);
        if (log.isDebugEnabled()) {
            if (f != null) {
                log.debug("Created singleton XMLInputFactory " + f.getClass() + " with configuration "
                        + configuration);
            }
        }
    }

    return f;
}

From source file:org.apache.axiom.util.stax.dialect.StAXDialectDetector.java

/**
 * Detect the dialect of a given {@link XMLInputFactory} and normalize it.
 * /*from ww w .ja v a 2s .  co  m*/
 * @param factory the factory to normalize
 * @return the normalized factory
 * 
 * @see StAXDialect#normalize(XMLInputFactory)
 */
public static XMLInputFactory normalize(XMLInputFactory factory) {
    return getDialect(factory.getClass()).normalize(factory);
}