Example usage for javax.xml.stream XMLStreamReader close

List of usage examples for javax.xml.stream XMLStreamReader close

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader close.

Prototype

public void close() throws XMLStreamException;

Source Link

Document

Frees any resources associated with this Reader.

Usage

From source file:org.osaf.cosmo.xml.DomReader.java

public static Node read(Reader in) throws ParserConfigurationException, XMLStreamException, IOException {
    XMLStreamReader reader = null;
    try {//from w  w w . java2 s .co  m
        Document d = BUILDER_FACTORY.newDocumentBuilder().newDocument();
        reader = XML_INPUT_FACTORY.createXMLStreamReader(in);
        return readNode(d, reader);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (XMLStreamException e2) {
                log.warn("Unable to close XML reader", e2);
            }
        }
    }
}

From source file:org.pentaho.platform.dataaccess.datasource.api.AnalysisService.java

private String getSchemaName(String encoding, InputStream inputStream) throws XMLStreamException, IOException {
    String domainId = null;// w  w w  .j av a 2 s  .  co m
    XMLStreamReader reader = null;
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        if (StringUtils.isEmpty(encoding)) {
            reader = factory.createXMLStreamReader(inputStream);
        } else {
            reader = factory.createXMLStreamReader(inputStream, encoding);
        }

        while (reader.next() != XMLStreamReader.END_DOCUMENT) {
            if (reader.getEventType() == XMLStreamReader.START_ELEMENT
                    && reader.getLocalName().equalsIgnoreCase("Schema")) {
                domainId = reader.getAttributeValue("", "name");
                return domainId;
            }
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
        inputStream.reset();
    }

    return domainId;
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

/**
 * Reads the given input stream in its entirety, and closes the stream when 
 * complete. The data graph result is retrieved using the {@link #getResult() getResult}
 * method. /*from  www.ja v a 2s .  com*/
 * @param stream the XML stream to read
 * @throws XMLStreamException if the given document is found to be malformed
 * @throws UnmarshallerException if undefined properties, or classes are found in the 
 * XML stream. 
 */
public void unmarshal(InputStream stream) throws XMLStreamException, UnmarshallerException {
    XMLStreamReader streamReader = factory.createXMLStreamReader(stream);

    init();

    try {
        unmarshal(streamReader);
    } finally {
        streamReader.close();
    }
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

/**
 * Reads the given source as a stream in its entirety, and closes the stream when 
 * complete. The data graph result is retrieved using the {@link #getResult() getResult}
 * method. /*from w  ww .  java2 s . c  o  m*/
 * @param source the XML source to read
 * @throws XMLStreamException if the given document is found to be malformed
 * @throws UnmarshallerException if undefined properties, or classes are found in the 
 * XML stream. 
 */
public void unmarshal(Source source) throws XMLStreamException, UnmarshallerException {
    XMLStreamReader streamReader = factory.createXMLStreamReader(source);
    init();
    try {
        unmarshal(streamReader);
    } finally {
        streamReader.close();
    }
}

From source file:org.plasma.sdo.xml.StreamUnmarshaller.java

/**
 * Reads the given reader as a stream in its entirety, and closes the stream when 
 * complete. The data graph result is retrieved using the {@link #getResult() getResult}
 * method. /* w ww. j  a  v a2 s.c om*/
 * @param reader the XML reader to read
 * @throws XMLStreamException if the given document is found to be malformed
 * @throws UnmarshallerException if undefined properties, or classes are found in the 
 * XML stream. 
 */
public void unmarshal(Reader reader) throws XMLStreamException, UnmarshallerException {
    XMLStreamReader streamReader = factory.createXMLStreamReader(reader);
    init();
    try {
        unmarshal(streamReader);
    } finally {
        streamReader.close();
    }
}

From source file:org.restcomm.connect.interpreter.rcml.Parser.java

public Parser(final Reader reader, final String xml, final ActorRef sender) throws IOException {
    super();//from  w w w .  j a v a  2  s.c o  m
    if (logger.isDebugEnabled()) {
        logger.debug("About to create new Parser for xml: " + xml);
    }
    this.xml = xml;
    this.sender = sender;
    final XMLInputFactory inputs = XMLInputFactory.newInstance();
    inputs.setProperty("javax.xml.stream.isCoalescing", true);
    XMLStreamReader stream = null;
    try {
        stream = inputs.createXMLStreamReader(reader);
        document = parse(stream);
        if (document == null) {
            throw new IOException("There was an error parsing the RCML.");
        }
        iterator = document.iterator();
    } catch (final XMLStreamException exception) {
        if (logger.isInfoEnabled()) {
            logger.info("There was an error parsing the RCML for xml: " + xml + " excpetion: ", exception);
        }
        sender.tell(new ParserFailed(exception, xml), null);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (final XMLStreamException nested) {
                throw new IOException(nested);
            }
        }
    }
}

From source file:org.reusables.dbunit.autocomplete.AutoCompletionRules.java

private void closeParser(final XMLStreamReader parser) {
    try {/* w w w.j  av a2 s .c  o m*/
        if (parser != null) {
            parser.close();
        }
    } catch (final XMLStreamException e) {
        throw new DbUnitAutoCompletionException("Error closing parser", e);
    }
}

From source file:org.rhq.enterprise.server.sync.SynchronizationManagerBean.java

private void validateExport(Subject subject, InputStream exportFile, Map<String, Configuration> importConfigs)
        throws ValidationException, XMLStreamException {
    XMLStreamReader rdr = XMLInputFactory.newInstance().createXMLStreamReader(exportFile);

    try {/*from   ww w  . j  av  a2s. co  m*/
        Set<ConsistencyValidatorFailureReport> failures = new HashSet<ConsistencyValidatorFailureReport>();
        Set<ConsistencyValidator> consistencyValidators = new HashSet<ConsistencyValidator>();

        while (rdr.hasNext()) {
            switch (rdr.next()) {
            case XMLStreamConstants.START_ELEMENT:
                String tagName = rdr.getName().getLocalPart();
                if (SynchronizationConstants.VALIDATOR_ELEMENT.equals(tagName)) {
                    ConsistencyValidator validator = null;
                    String validatorClass = rdr.getAttributeValue(null,
                            SynchronizationConstants.CLASS_ATTRIBUTE);
                    if (!isConsistencyValidatorClass(validatorClass)) {
                        LOG.info("The export file contains an unknown consistency validator: " + validatorClass
                                + ". Ignoring.");
                        continue;
                    }

                    try {
                        validator = validateSingle(rdr, subject);
                    } catch (Exception e) {
                        failures.add(new ConsistencyValidatorFailureReport(validatorClass,
                                printExceptionToString(e)));
                    }
                    if (validator != null) {
                        consistencyValidators.add(validator);
                    }
                } else if (SynchronizationConstants.ENTITIES_EXPORT_ELEMENT.equals(tagName)) {
                    String synchronizerClass = rdr.getAttributeValue(null,
                            SynchronizationConstants.ID_ATTRIBUTE);
                    try {
                        failures.addAll(validateEntities(rdr, subject, consistencyValidators, importConfigs));
                    } catch (Exception e) {
                        throw new ValidationException(
                                "Validation failed unexpectedly while processing the entities exported by the synchronizer '"
                                        + synchronizerClass + "'.",
                                e);
                    }
                }
            }
        }

        if (!failures.isEmpty()) {
            throw new ValidationException(failures);
        }
    } finally {
        rdr.close();
    }
}

From source file:org.rhq.enterprise.server.sync.test.DeployedAgentPluginsValidatorTest.java

public void testCanExportAndImportState() throws Exception {
    final PluginManagerLocal pluginManager = context.mock(PluginManagerLocal.class);

    final DeployedAgentPluginsValidator validator = new DeployedAgentPluginsValidator(pluginManager);

    context.checking(new Expectations() {
        {/* w  w w . j a v  a  2s  . c o  m*/
            oneOf(pluginManager).getInstalledPlugins();
            will(returnValue(new ArrayList<Plugin>(getDeployedPlugins())));
        }
    });

    validator.initialize(null, null);

    StringWriter output = new StringWriter();
    try {
        XMLOutputFactory ofactory = XMLOutputFactory.newInstance();

        XMLStreamWriter wrt = ofactory.createXMLStreamWriter(output);
        //wrap the exported plugins in "something" so that we produce
        //a valid xml
        wrt.writeStartDocument();
        wrt.writeStartElement("root");

        validator.exportState(new ExportWriter(wrt));

        wrt.writeEndDocument();

        wrt.close();

        StringReader input = new StringReader(output.toString());

        try {
            XMLInputFactory ifactory = XMLInputFactory.newInstance();
            XMLStreamReader rdr = ifactory.createXMLStreamReader(input);

            //push the reader to the start of the plugin elements
            //this is what is expected by the validators
            rdr.nextTag();

            validator.initializeExportedStateValidation(new ExportReader(rdr));

            rdr.close();

            assertEquals(validator.getPluginsToValidate(), getDeployedPlugins());
        } finally {
            input.close();
        }
    } catch (Exception e) {
        LOG.error("Test failed. Output generated so far:\n" + output, e);
        throw e;
    } finally {
        output.close();
    }
}

From source file:org.rhq.plugins.hadoop.HadoopServerConfigurationDelegate.java

public static void parseAndAssignProps(File configFile, Map<String, PropertySimple> props)
        throws XMLStreamException, IOException {
    FileInputStream in = new FileInputStream(configFile);
    XMLStreamReader rdr = XML_INPUT_FACTORY.createXMLStreamReader(in);
    try {//from   w w  w. j  av  a  2 s . co m
        boolean inProperty = false;
        String propertyName = null;
        String propertyValue = null;

        while (rdr.hasNext()) {
            int event = rdr.next();

            String tag = null;

            switch (event) {
            case XMLStreamReader.START_ELEMENT:
                tag = rdr.getName().getLocalPart();
                if (PROPERTY_TAG_NAME.equals(tag)) {
                    inProperty = true;
                } else if (inProperty && NAME_TAG_NAME.equals(tag)) {
                    propertyName = rdr.getElementText();
                } else if (inProperty && VALUE_TAG_NAME.equals(tag)) {
                    propertyValue = rdr.getElementText();
                }
                break;
            case XMLStreamReader.END_ELEMENT:
                tag = rdr.getName().getLocalPart();
                if (PROPERTY_TAG_NAME.equals(tag)) {
                    inProperty = false;

                    PropertySimple prop = props.get(propertyName);
                    if (prop != null) {
                        prop.setValue(propertyValue);
                    }

                    propertyName = null;
                    propertyValue = null;
                }
                break;
            }
        }
    } finally {
        rdr.close();
        in.close();
    }
}