Example usage for javax.xml.stream XMLInputFactory setXMLReporter

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

Introduction

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

Prototype

public abstract void setXMLReporter(XMLReporter reporter);

Source Link

Document

The reporter that will be set on any XMLStreamReader or XMLEventReader created by this factory instance.

Usage

From source file:com.ggvaidya.scinames.model.Project.java

public static Project loadFromFile(File loadFromFile) throws IOException {
    Project project = null;// ww  w  .  jav a  2 s  .c om

    XMLInputFactory factory = XMLInputFactory.newFactory();
    factory.setXMLReporter(new XMLReporter() {
        @Override
        public void report(String message, String errorType, Object relatedInformation, Location location)
                throws XMLStreamException {
            LOGGER.warning(errorType + " while loading project from XML file '" + loadFromFile + "': " + message
                    + " (related info: " + relatedInformation.toString() + ", location: " + location);
        }
    });

    try {
        XMLEventReader reader = factory.createXMLEventReader(
                new XmlStreamReader(new GZIPInputStream(new FileInputStream(loadFromFile))));

        project = ProjectXMLReader.read(reader);
        project.setFile(loadFromFile);
        project.lastModifiedProperty().saved();

        reader.close();

    } catch (XMLStreamException ex) {
        throw new IOException("Could not read project from XML file '" + loadFromFile + "': " + ex, ex);
    }

    return project;

    /*
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            
    // Configure upcoming document builder.
    dbf.setIgnoringComments(true);
    dbf.setIgnoringElementContentWhitespace(true);
            
    Document docProject;
    try {
       DocumentBuilder db = dbf.newDocumentBuilder();
       docProject = db.parse(loadFromFile);
               
    } catch (SAXException ex) {
       throw new IOException("Could not load project XML file: " + ex);
               
    } catch (ParserConfigurationException ex) {
       throw new RuntimeException("Could not load XML parser configuration: " + ex);
            
    }
            
    // Load project.
    Project newProject;
    try {
       newProject = serializeFromDocument(docProject, loadFromFile);
    } catch(SAXException e) {
       throw new IOException("XML file loaded but project could not be read: " + e);
    } catch(IllegalStateException e) {
       throw new IOException("XML file contains errors in content: " + e);
    }
            
    return newProject;
    */
}

From source file:org.gephi.io.importer.api.ImportUtils.java

public static XMLStreamReader getXMLReader(Reader reader) {
    try {//from   w  w  w.j a v a 2  s  .c o m
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        if (inputFactory.isPropertySupported("javax.xml.stream.isValidating")) {
            inputFactory.setProperty("javax.xml.stream.isValidating", Boolean.FALSE);
        }
        inputFactory.setXMLReporter(new XMLReporter() {

            @Override
            public void report(String message, String errorType, Object relatedInformation, Location location)
                    throws XMLStreamException {
                throw new RuntimeException("Error:" + errorType + ", message : " + message);
                //System.out.println("Error:" + errorType + ", message : " + message);
            }
        });
        return inputFactory.createXMLStreamReader(reader);
    } catch (XMLStreamException ex) {
        throw new RuntimeException(NbBundle.getMessage(ImportUtils.class, "ImportUtils.error_io"));
    }
}