private static XMLStreamReader getReader()

in src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java [591:685]


    private static XMLStreamReader getReader(final Configuration configuration, final URL url, final String path,
                                             final InputStream stream, final Reader reader, final Source source)
            throws IOException, XMLStreamException {

        // Instantiate the XMLInputFactory
        XMLInputFactory factory = XMLInputFactory.newInstance();
        if (configuration.factoryId != null && configuration.factoryClassLoader != null) {
            factory = XMLInputFactory.newFactory(configuration.factoryId, configuration.factoryClassLoader);
        }
        factory.setEventAllocator(configuration.allocator);
        if (factory.isPropertySupported(XMLInputFactory_JDK_PROP_REPORT_CDATA)) {
            factory.setProperty(XMLInputFactory_JDK_PROP_REPORT_CDATA, Boolean.TRUE);
        }
        for (final Map.Entry<String, Object> property : configuration.properties.entrySet()) {
            if (factory.isPropertySupported(property.getKey())) {
                factory.setProperty(property.getKey(), property.getValue());
            }
        }
        factory.setXMLReporter(configuration.reporter);
        factory.setXMLResolver(configuration.resolver);

        // Consolidate InputStream options
        InputStream urlStream = null;
        if (url != null || path != null) {
            final URL scxml = url != null ? url : new URL(path);
            final URLConnection conn = scxml.openConnection();
            conn.setUseCaches(false);
            urlStream = conn.getInputStream();
        } else if (stream != null) {
            urlStream = stream;
        }

        // Create the XMLStreamReader
        XMLStreamReader xsr = null;

        if (configuration.validate) {
            // Validation requires us to use a Source

            final URL scxmlSchema = new URL("TODO"); // TODO, point to appropriate location
            final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
            Schema schema;
            try {
                schema = schemaFactory.newSchema(scxmlSchema);
            } catch (final SAXException se) {
                throw new XMLStreamException("Failed to create SCXML Schema for validation", se);
            }

            final Validator validator = schema.newValidator();
            validator.setErrorHandler(new SimpleErrorHandler());

            Source src = null;
            if (urlStream != null) {
                // configuration.encoding is ignored
                if (configuration.systemId != null) {
                    src = new StreamSource(urlStream, configuration.systemId);
                } else {
                    src = new StreamSource(urlStream);
                }
            } else if (reader != null) {
                if (configuration.systemId != null) {
                    src = new StreamSource(reader, configuration.systemId);
                } else {
                    src = new StreamSource(reader);
                }
            } else if (source != null) {
                src = source;
            }
            xsr = factory.createXMLStreamReader(src);
            try {
                validator.validate(src);
            } catch (final SAXException se) {
                throw new XMLStreamException("Failed to create apply SCXML Validator", se);
            }

        } else if (urlStream != null) {
            // systemId gets preference, then encoding if either are present
            if (configuration.systemId != null) {
                xsr = factory.createXMLStreamReader(configuration.systemId, urlStream);
            } else if (configuration.encoding != null) {
                xsr = factory.createXMLStreamReader(urlStream, configuration.encoding);
            } else {
                xsr = factory.createXMLStreamReader(urlStream);
            }
        } else if (reader != null) {
            if (configuration.systemId != null) {
                xsr = factory.createXMLStreamReader(configuration.systemId, reader);
            } else {
                xsr = factory.createXMLStreamReader(reader);
            }
        } else if (source != null) {
            xsr = factory.createXMLStreamReader(source);
        }

        return xsr;
    }