Example usage for javax.xml.stream XMLInputFactory newInstance

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

Introduction

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

Prototype

public static XMLInputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:Main.java

/**
 * Creates XMLInputFactory with DTD support disabled.
 * @return xml input factory//from   w  ww. j  a v a 2s  .  com
 */
public static XMLInputFactory createBasicInputFactory() {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    return xmlFactory;
}

From source file:Main.java

protected static synchronized void initializeXMLInputFactory() {
    if (xmlInputFactory == null) {
        xmlInputFactory = XMLInputFactory.newInstance();
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); // This disables DTDs entirely for that factory
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
    }/*from  ww w  .j a va 2  s. c  om*/
}

From source file:Main.java

/**
 * 'safe' is here reflecting:/*from  w w w .  ja  va 2 s. c o  m*/
 * http://www.jorambarrez.be/blog/2013/02/19/uploading
 * -a-funny-xml-can-bring-down-your-server/ and
 * http://activiti.org/userguide/index.html#advanced.safe.bpmn.xml
 */
public static XMLInputFactory createSafeXmlInputFactory() {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    if (xif.isPropertySupported(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES)) {
        xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
    }

    if (xif.isPropertySupported(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES)) {
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    }

    if (xif.isPropertySupported(XMLInputFactory.SUPPORT_DTD)) {
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    }
    return xif;
}

From source file:Main.java

public static XMLStreamReader createXMLStreamReader(File file)
        throws FileNotFoundException, XMLStreamException {

    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (factory.isPropertySupported("javax.xml.stream.isValidating")) {
        factory.setProperty("javax.xml.stream.isValidating", Boolean.TRUE);
    }/*  w  w  w .  j av  a2s. c om*/

    XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file));

    return reader;
}

From source file:Main.java

/**
 * Get the XMLInputFactory.//from w  w  w  .j  a  va  2  s  .c o m
 * 
 * @return the XMLInputFactory
 */
public static XMLInputFactory getStaxReaderFactory() {
    if (s_staxReaderFactory == null) {
        s_staxReaderFactory = XMLInputFactory.newInstance();
    }
    return s_staxReaderFactory;
}

From source file:Main.java

/**
 * Build a new XMLInputFactory.//  w w w.j ava  2  s.  c  o  m
 *
 * @return XML input factory
 */
public static XMLInputFactory getXmlInputFactory() {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    return inputFactory;
}

From source file:Main.java

/**
 * Gets the version of the XML./*from w w w  . j  ava  2s  . c o  m*/
 *
 * @param path the XML file.
 * @return the corresponding version of the XML.
 */
public static String getXMLVersion(final Path path) {
    try (InputStream inputStream = Files.newInputStream(path)) {
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        for (int event; (event = reader.next()) != XMLStreamConstants.END_DOCUMENT;) {
            if (event == XMLStreamConstants.START_ELEMENT) {
                String tmp = reader.getLocalName();
                if ("persistence".equals(tmp)) {
                    for (int i = 0; i < reader.getAttributeCount(); i++) {
                        if ("version".equals(reader.getAttributeName(i).toString())) {
                            return reader.getAttributeValue(i);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Error reading the persistence.xml version.", ex);
    }
    return null;
}

From source file:Main.java

public static XMLStreamReader getXMLStreamReader(InputStream is)
        throws XMLStreamException, FactoryConfigurationError {
    //return XMLInputFactory.newInstance().createXMLStreamReader(is);
    return new StreamReaderDelegate(XMLInputFactory.newInstance().createXMLStreamReader(is)) {
        public int next() throws XMLStreamException {
            while (true) {
                int event = super.next();
                switch (event) {
                case XMLStreamConstants.COMMENT:
                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    continue;
                default:
                    return event;
                }//  w ww  .  j  a v a 2s  .  co m
            }
        }
    };
}

From source file:Main.java

/**
 * Determines if the given stream contains XML content. The stream will be
 * buffered and reset if necessary.//from w  w w . j  a  va 2 s  .c  om
 * 
 * @param stream
 *            The InputStream to read.
 * @return true if the stream contains XML content; false otherwise.
 */
public static boolean isXML(InputStream stream) {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream, 1024);
    }
    stream.mark(1024);
    byte[] bytes = new byte[1024];
    try {
        try {
            stream.read(bytes);
        } finally {
            stream.reset();
        }
    } catch (IOException iox) {
        throw new RuntimeException("Failed to read or reset stream. " + iox.getMessage());
    }
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(bytes));
        // If XML, now in START_DOCUMENT state; seek document element.
        reader.nextTag();
    } catch (XMLStreamException xse) {
        return false;
    }
    return true;
}

From source file:Main.java

public static XMLStreamReader parse(String fileName) throws IOException, XMLStreamException {
    FileInputStream input = new FileInputStream(fileName);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xif.createXMLStreamReader(input);

    return xmlStreamReader;
}