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.wso2.dss.integration.common.utils.DSSTestCaseUtils.java

/**
 * Loads the specified resource from the file system and returns its content as an OMElement.
 *
 * @param filePath A relative path to the resource file
 * @return An OMElement containing the resource content
 */// w ww  .ja v a 2  s .  c  o  m
public OMElement loadResourceFrom(String filePath) throws FileNotFoundException, XMLStreamException {
    OMElement documentElement = null;
    FileInputStream inputStream = null;
    XMLStreamReader parser = null;
    StAXOMBuilder builder = null;
    File file = new File(filePath);
    if (file.exists()) {
        try {
            inputStream = new FileInputStream((getClass().getResource(filePath).getPath()));
            parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            //create the builder
            builder = new StAXOMBuilder(parser);
            //get the root element (in this case the envelope)
            documentElement = builder.getDocumentElement().cloneOMElement();
        } finally {
            if (builder != null) {
                builder.close();
            }
            if (parser != null) {
                try {
                    parser.close();
                } catch (XMLStreamException e) {
                    //ignore
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    //ignore
                }
            }

        }
    } else {
        throw new FileNotFoundException("File Not Exist at " + filePath);
    }
    return documentElement;
}

From source file:org.wso2.esb.integration.common.utils.ESBTestCaseUtils.java

/**
 * Loads the specified resource from the classpath and returns its content as an OMElement.
 *
 * @param path A relative path to the resource file
 * @return An OMElement containing the resource content
 *///from   w w w .j  a v a2s .co m
public OMElement loadResource(String path) throws FileNotFoundException, XMLStreamException {
    OMElement documentElement = null;
    FileInputStream inputStream = null;
    XMLStreamReader parser = null;
    StAXOMBuilder builder = null;
    path = TestConfigurationProvider.getResourceLocation() + path;
    File file = new File(path);
    if (file.exists()) {
        try {
            inputStream = new FileInputStream(file);
            parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            //create the builder
            builder = new StAXOMBuilder(parser);
            //get the root element (in this case the envelope)
            documentElement = builder.getDocumentElement().cloneOMElement();
        } finally {
            if (builder != null) {
                builder.close();
            }
            if (parser != null) {
                try {
                    parser.close();
                } catch (XMLStreamException e) {
                    //ignore
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    //ignore
                }
            }

        }
    } else {
        throw new FileNotFoundException("File Not Exist at " + path);
    }
    return documentElement;
}

From source file:org.wso2.esb.integration.common.utils.ESBTestCaseUtils.java

/**
 * Loads the configuration of the specified sample into the ESB.
 *
 * @param number Sample number//from  w  w  w. j  av  a2  s  . c o m
 * @throws Exception If an error occurs while loading the sample configuration
 */
public OMElement loadESBSampleConfiguration(int number) throws Exception {
    String filePath = TestConfigurationProvider.getResourceLocation("ESB") + File.separator + "samples"
            + File.separator + "synapse_sample_" + number + ".xml";
    File configFile = new File(filePath);
    FileInputStream inputStream = null;
    XMLStreamReader parser = null;
    StAXOMBuilder builder = null;
    OMElement documentElement = null;
    try {
        inputStream = new FileInputStream(configFile.getAbsolutePath());
        parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        builder = new StAXOMBuilder(parser);
        documentElement = builder.getDocumentElement().cloneOMElement();

    } finally {
        if (builder != null) {
            builder.close();
        }
        if (parser != null) {
            try {
                parser.close();
            } catch (XMLStreamException e) {
                //ignore
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                //ignore
            }
        }
    }
    return documentElement;
}

From source file:org.wso2.maven.AbstractMavenReleaseMojo.java

/**
 * Update versions in the given artifact.xml file of a ESB/DSS project.
 *
 * @param artifactXml artifact.xml file of a ESB/DSS project.
 * @param newVersion  new version to which, the artifacts should be updated.
 * @throws Exception/* w ww .ja  v  a 2 s. c o m*/
 */
protected void updateArtifactVersions(File artifactXml, String newVersion) throws Exception {
    InputStream inputStream = new FileInputStream(artifactXml);
    XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
    StAXOMBuilder builder = new StAXOMBuilder(xmlStreamReader);
    OMElement documentElement = builder.getDocumentElement();
    Iterator artifacts = documentElement.getChildrenWithName(new QName(ARTIFACT));
    while (artifacts.hasNext()) {
        OMElement artifact = (OMElement) artifacts.next();
        OMAttribute version = artifact.getAttribute(new QName(VERSION));
        if (version != null) {
            version.setAttributeValue(newVersion);
        }
    }
    if (isInDryRunMode()) {
        artifactXml = new File(artifactXml.getPath() + getDryRunFilePrefix());
    }
    FileOutputStream outputStream = new FileOutputStream(artifactXml);
    XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream);
    builder.getDocument().serialize(xmlStreamWriter);
    inputStream.close();
    xmlStreamReader.close();
    outputStream.close();
    xmlStreamWriter.close();
}

From source file:org.xwiki.xar.internal.XarUtils.java

/**
 * Extract {@link LocalDocumentReference} from a XAR document XML stream.
 * //from ww w .j  a  va 2 s .  co m
 * @param documentStream the stream to parse
 * @return the reference extracted from the stream
 * @throws XarException when failing to parse the document stream
 * @since 5.4M1
 */
public static LocalDocumentReference getReference(InputStream documentStream) throws XarException {
    XMLStreamReader xmlReader;
    try {
        xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(documentStream);
    } catch (XMLStreamException e) {
        throw new XarException("Failed to create a XML read", e);
    }

    EntityReference reference = null;
    Locale locale = null;

    String legacySpace = null;
    String legacyPage = null;

    try {
        // <xwikidoc>

        xmlReader.nextTag();

        xmlReader.require(XMLStreamReader.START_ELEMENT, null, XarDocumentModel.ELEMENT_DOCUMENT);

        // Reference
        String referenceString = xmlReader.getAttributeValue(null,
                XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE);
        if (referenceString != null) {
            reference = RESOLVER.resolve(referenceString, EntityType.DOCUMENT);
        }

        // Locale
        String localeString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE);
        if (localeString != null) {
            if (localeString.isEmpty()) {
                locale = Locale.ROOT;
            } else {
                locale = LocaleUtils.toLocale(localeString);
            }
        }

        // Legacy fallback
        if (reference == null || locale == null) {
            for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
                String elementName = xmlReader.getLocalName();

                if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) {
                    if (reference == null) {
                        legacyPage = xmlReader.getElementText();

                        if (legacySpace != null && locale != null) {
                            break;
                        }
                    } else if (locale != null) {
                        break;
                    }
                } else if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) {
                    if (reference == null) {
                        legacySpace = xmlReader.getElementText();

                        if (legacyPage != null && locale != null) {
                            break;
                        }
                    } else if (locale != null) {
                        break;
                    }
                } else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) {
                    if (locale == null) {
                        String value = xmlReader.getElementText();
                        if (value.length() == 0) {
                            locale = Locale.ROOT;
                        } else {
                            locale = LocaleUtils.toLocale(value);
                        }
                    }

                    if (reference != null || (legacySpace != null && legacyPage != null)) {
                        break;
                    }
                } else {
                    StAXUtils.skipElement(xmlReader);
                }
            }
        }
    } catch (XMLStreamException e) {
        throw new XarException("Failed to parse document", e);
    } finally {
        try {
            xmlReader.close();
        } catch (XMLStreamException e) {
            throw new XarException("Failed to close XML reader", e);
        }
    }

    if (reference == null) {
        if (legacySpace == null) {
            throw new XarException("Missing space element");
        }
        if (legacyPage == null) {
            throw new XarException("Missing page element");
        }

        reference = new LocalDocumentReference(legacySpace, legacyPage);
    }

    if (locale == null) {
        throw new XarException("Missing locale element");
    }

    return new LocalDocumentReference(reference, locale);
}

From source file:savant.plugin.PluginIndex.java

public PluginIndex(URL url) throws IOException {
    urls = new HashMap<String, URL>();
    try {/*from  ww w  . j a v  a 2  s  .co  m*/
        XMLStreamReader reader = XMLInputFactory.newInstance()
                .createXMLStreamReader(NetworkUtils.openStream(url));
        boolean done = false;
        String id = null;
        do {
            switch (reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                String elemName = reader.getLocalName();
                if (elemName.equals("leaf")) {
                    id = reader.getAttributeValue(null, "id");
                } else if (elemName.equals("url")) {
                    if (id != null) {
                        try {
                            urls.put(id, new URL(reader.getElementText()));
                        } catch (MalformedURLException x) {
                            LOG.info("Unable to parse \"" + reader.getElementText() + "\" as a plugin URL.");
                        }
                        id = null;
                    }
                }
                break;
            case XMLStreamConstants.END_DOCUMENT:
                reader.close();
                done = true;
                break;
            }
        } while (!done);
    } catch (XMLStreamException x) {
        throw new IOException("Unable to get version number from web-site.", x);
    }
}

From source file:savant.plugin.Tool.java

/**
 * The tool's arguments are contained in the associated plugin.xml file.
 *//*from   w  w  w. j  a  v a  2  s  . c  om*/
void parseDescriptor() throws XMLStreamException, FileNotFoundException {
    XMLStreamReader reader = XMLInputFactory.newInstance()
            .createXMLStreamReader(new FileInputStream(getDescriptor().getFile()));
    do {
        switch (reader.next()) {
        case XMLStreamConstants.START_ELEMENT:
            String elemName = reader.getLocalName().toLowerCase();
            if (elemName.equals("tool")) {
                baseCommand = reader.getElementText();
            } else if (elemName.equals("arg")) {
                // There's lots of crud in the XML file; we're just interested in the <arg> elements.
                arguments.add(new ToolArgument(reader));
            } else if (elemName.equals("progress")) {
                progressRegex = Pattern.compile(reader.getElementText());
            } else if (elemName.equals("error")) {
                errorRegex = Pattern.compile(reader.getElementText());
            }
            break;
        case XMLStreamConstants.END_DOCUMENT:
            reader.close();
            reader = null;
            break;
        }
    } while (reader != null);
}

From source file:savant.util.Version.java

/**
 * Factory method which construct a Version object from a URL pointing to an XML file.
 * @param url URL of our version.xml file
 * @return the version number read from the file
 *//*from   www.  j av a2  s . c o  m*/
public static Version fromURL(URL url) throws IOException {
    try {
        XMLStreamReader reader = XMLInputFactory.newInstance()
                .createXMLStreamReader(NetworkUtils.openStream(url));
        boolean done = false;
        boolean foundCurrentVersion = false;
        do {
            switch (reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                String elemName = reader.getLocalName();
                if (elemName.equals("version")
                        && "current_release".equals(reader.getAttributeValue(null, "status"))) {
                    foundCurrentVersion = true;
                } else if (foundCurrentVersion && elemName.equals("name")) {
                    return new Version(reader.getElementText());
                } else {
                    foundCurrentVersion = false;
                }
                break;
            case XMLStreamConstants.END_DOCUMENT:
                reader.close();
                done = true;
                break;
            }
        } while (!done);
    } catch (XMLStreamException x) {
        throw new IOException("Unable to get version number from web-site.", x);
    }
    return null;
}

From source file:se.skl.skltpservices.npoadapter.mapper.AbstractMapper.java

protected void close(final XMLStreamReader reader) {
    try {/*from  w  w w .j a va 2  s  .  co m*/
        reader.close();
    } catch (XMLStreamException | NullPointerException e) {
        ;
    }
}