Example usage for javax.xml.stream XMLEventReader close

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

Introduction

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

Prototype

public void close() throws XMLStreamException;

Source Link

Document

Frees any resources associated with this Reader.

Usage

From source file:org.apache.olingo.fit.utils.v3.XMLUtilities.java

private Map.Entry<String, Boolean> getTargetInfo(final StartElement element, final String linkName)
        throws Exception {
    final InputStream metadata = fsManager.readFile(Constants.METADATA, Accept.XML);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(metadata, bos);//from www . j a  va2s  .c  om
    IOUtils.closeQuietly(metadata);

    // ------------------------------------
    // Retrieve association
    // ------------------------------------
    XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));

    final String associationName = element.getAttributeByName(new QName("Relationship")).getValue();

    final Map.Entry<Integer, XmlElement> association = extractElement(reader, null,
            Collections.<String>singletonList("Association"),
            Collections.<Map.Entry<String, String>>singleton(new SimpleEntry<String, String>("Name",
                    associationName.substring(associationName.lastIndexOf(".") + 1))),
            false, 0, 4, 4);

    reader.close();
    // ------------------------------------

    // ------------------------------------
    // check for feed or not from Association role
    // ------------------------------------
    InputStream associationContent = association.getValue().toStream();
    reader = getEventReader(associationContent);

    Map.Entry<Integer, XmlElement> associationEnd = extractElement(reader, null,
            Collections.<String>singletonList("End"),
            Collections.<Map.Entry<String, String>>singleton(new SimpleEntry<String, String>("Role", linkName)),
            false, 0, -1, -1);

    reader.close();
    IOUtils.closeQuietly(associationContent);

    final boolean feed = associationEnd.getValue().getStart().getAttributeByName(new QName("Multiplicity"))
            .getValue().equals("*");
    // ------------------------------------

    // ------------------------------------
    // Retrieve target association set name
    // ------------------------------------
    reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));

    final Map.Entry<Integer, XmlElement> associationSet = extractElement(reader, null,
            Collections.<String>singletonList("AssociationSet"),
            Collections.<Map.Entry<String, String>>singleton(
                    new SimpleEntry<String, String>("Association", associationName)),
            false, 0, -1, -1);

    reader.close();

    associationContent = associationSet.getValue().toStream();
    reader = getEventReader(associationContent);

    associationEnd = extractElement(reader, null, Collections.<String>singletonList("End"),
            Collections.<Map.Entry<String, String>>singleton(new SimpleEntry<String, String>("Role", linkName)),
            false, 0, -1, -1);

    reader.close();
    IOUtils.closeQuietly(associationContent);

    final String target = associationEnd.getValue().getStart().getAttributeByName(new QName("EntitySet"))
            .getValue();
    // ------------------------------------

    return new SimpleEntry<String, Boolean>(target, feed);
}

From source file:org.apache.olingo.fit.utils.v4.XMLUtilities.java

@Override
public void retrieveLinkInfoFromMetadata() throws Exception {

    final MetadataLinkInfo metadataLinkInfo = new MetadataLinkInfo();
    Commons.getLinkInfo().put(version, metadataLinkInfo);

    final InputStream metadata = fsManager.readFile(Constants.METADATA, Accept.XML);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(metadata, bos);/*from   w ww .  j av a 2 s .co m*/
    IOUtils.closeQuietly(metadata);

    XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));

    final Set<String> singletons = new HashSet<String>();

    try {
        while (true) {
            final Map.Entry<Integer, XmlElement> entitySetElement = extractElement(reader, null,
                    Collections.<String>singletonList("Singleton"), null, false, 0, -1, -1);

            final String entitySetName = entitySetElement.getValue().getStart()
                    .getAttributeByName(new QName("Name")).getValue().trim();
            singletons.add(entitySetName);
        }
    } catch (Exception e) {
    } finally {
        reader.close();
    }

    reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));

    try {
        while (true) {
            final Map.Entry<Integer, XmlElement> entitySetElement = extractElement(reader, null,
                    Collections.<String>singletonList("EntitySet"), null, false, 0, -1, -1);

            retrieveLinks(entitySetElement.getValue(), metadataLinkInfo, singletons);
        }
    } catch (Exception e) {
    } finally {
        reader.close();
    }

    reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));

    try {
        while (true) {
            final Map.Entry<Integer, XmlElement> entitySetElement = extractElement(reader, null,
                    Collections.<String>singletonList("Singleton"), null, false, 0, -1, -1);

            retrieveLinks(entitySetElement.getValue(), metadataLinkInfo, singletons);
        }
    } catch (Exception e) {
    } finally {
        reader.close();
    }
}

From source file:org.apache.olingo.fit.utils.v4.XMLUtilities.java

private void retrieveLinks(final XmlElement entitySetElement, final MetadataLinkInfo metadataLinkInfo,
        final Set<String> singletons) throws Exception {

    final String entitySetName = entitySetElement.getStart().getAttributeByName(new QName("Name")).getValue()
            .trim();/* w w w  .j a v a  2 s  .  c  o  m*/

    final XMLEventReader entityReader = entitySetElement.getContentReader();
    int size = 0;

    try {
        while (true) {
            final XmlElement navProperty = extractElement(entityReader, null,
                    Collections.<String>singletonList("NavigationPropertyBinding"), null, false, 0, -1, -1)
                            .getValue();

            final String linkName = navProperty.getStart().getAttributeByName(new QName("Path")).getValue();
            final String target = navProperty.getStart().getAttributeByName(new QName("Target")).getValue();
            final boolean feed = !singletons.contains(target);

            metadataLinkInfo.addLink(entitySetName, linkName, target, feed);

            size++;
        }
    } catch (Exception e) {
    } finally {
        entityReader.close();
    }

    if (size == 0) {
        metadataLinkInfo.addEntitySet(entitySetName);
    }
}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

/**
 * {@inheritDoc }// ww  w  . ja v a 2 s  . c  om
 */
@Override
protected InputStream addLinks(final String entitySetName, final String entitykey, final InputStream is,
        final Set<String> links) throws Exception {

    // -----------------------------------------
    // 0. Build reader and writer
    // -----------------------------------------
    final XMLEventReader reader = getEventReader(is);
    final XMLEventFactory eventFactory = XMLEventFactory.newInstance();

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);
    // -----------------------------------------
    final Map.Entry<Integer, XMLElement> entry = extractElement(reader, writer,
            Collections.singletonList("entry"), 0, 1, 1);

    writer.add(entry.getValue().getStart());

    final Map<String, NavigationProperty> navigationProperties = metadata
            .getNavigationProperties(entitySetName);

    // add for links
    for (String link : links) {
        final Set<Attribute> attributes = new HashSet<Attribute>();
        attributes.add(eventFactory.createAttribute(new QName("title"), link));
        attributes.add(eventFactory.createAttribute(new QName("href"),
                Commons.getLinksURI(entitySetName, entitykey, link)));
        attributes.add(eventFactory.createAttribute(new QName("rel"),
                Constants.get(ConstantKey.ATOM_LINK_REL) + link));
        attributes.add(eventFactory.createAttribute(new QName("type"),
                navigationProperties.get(link).isEntitySet() ? Constants.get(ConstantKey.ATOM_LINK_FEED)
                        : Constants.get(ConstantKey.ATOM_LINK_ENTRY)));

        writer.add(eventFactory.createStartElement(new QName(Constants.get(ConstantKey.LINK)),
                attributes.iterator(), null));
        writer.add(eventFactory.createEndElement(new QName(Constants.get(ConstantKey.LINK)), null));
    }

    writer.add(entry.getValue().getContentReader());
    writer.add(entry.getValue().getEnd());
    writer.add(reader);
    IOUtils.closeQuietly(is);

    writer.flush();
    writer.close();
    reader.close();

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

/**
 * {@inheritDoc }/*from  ww  w .j  a va2 s  .  c  o m*/
 */
@Override
protected Set<String> retrieveAllLinkNames(final InputStream is) throws Exception {
    final Set<String> links = new HashSet<String>();

    final XMLEventReader reader = getEventReader(is);

    try {

        int startDepth = 0;

        while (true) {
            final Map.Entry<Integer, XMLElement> linkInfo = extractElement(reader, null,
                    Collections.<String>singletonList(Constants.get(ConstantKey.LINK)), startDepth, 2, 2);

            startDepth = linkInfo.getKey();

            links.add(linkInfo.getValue().getStart().getAttributeByName(new QName("title")).getValue());
        }
    } catch (Exception ignore) {
        // ignore
    } finally {
        reader.close();
        IOUtils.closeQuietly(is);
    }

    return links;
}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

/**
 * {@inheritDoc }/*from w w  w.j  a va2 s. co  m*/
 */
@Override
protected NavigationLinks retrieveNavigationInfo(final String entitySetName, final InputStream is)
        throws Exception {

    final NavigationLinks links = new NavigationLinks();

    final XMLEventReader reader = getEventReader(is);

    try {
        final List<Map.Entry<String, String>> filter = new ArrayList<Map.Entry<String, String>>();
        filter.add(new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=entry"));
        filter.add(new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=feed"));

        int startDepth = 0;

        while (true) {
            // a. search for link with type attribute equals to "application/atom+xml;type=entry/feed"
            final Map.Entry<Integer, XMLElement> linkInfo = extractElement(reader, null,
                    Collections.<String>singletonList(Constants.get(ConstantKey.LINK)), filter, true,
                    startDepth, 2, 2);
            final XMLElement link = linkInfo.getValue();
            startDepth = linkInfo.getKey();

            final String title = link.getStart().getAttributeByName(new QName("title")).getValue();

            final Attribute hrefAttr = link.getStart().getAttributeByName(new QName("href"));
            final String href = hrefAttr == null ? null : hrefAttr.getValue();

            try {
                final XMLElement inlineElement = extractElement(link.getContentReader(), null,
                        Collections.<String>singletonList(Constants.get(ConstantKey.INLINE)), 0, -1, -1)
                                .getValue();
                final XMLEventReader inlineReader = inlineElement.getContentReader();

                try {
                    while (true) {
                        final XMLElement entry = extractElement(inlineReader, null,
                                Collections.<String>singletonList("entry"), 0, -1, -1).getValue();
                        links.addInlines(title, entry.toStream());
                    }
                } catch (Exception e) {
                    // Reached the end of document
                }

                inlineReader.close();
            } catch (Exception ignore) {
                // inline element not found (inlines are not mondatory).
                if (StringUtils.isNotBlank(href) && ENTITY_URI_PATTERN.matcher(href).matches()) {
                    links.addLinks(title, href.substring(href.lastIndexOf('/') + 1));
                }
            }
        }
    } catch (Exception ignore) {
        // ignore
    } finally {
        reader.close();
    }

    return links;
}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

/**
 * {@inheritDoc }/*  w  w w.  j a  v  a  2s .c  o  m*/
 */
@Override
protected InputStream normalizeLinks(final String entitySetName, final String entityKey, final InputStream is,
        final NavigationLinks links) throws Exception {

    // -----------------------------------------
    // 0. Build reader and writer
    // -----------------------------------------
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    IOUtils.copy(is, bos);
    is.close();

    final ByteArrayOutputStream tmpBos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(tmpBos);

    final XMLEventReader reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));
    // -----------------------------------------

    // -----------------------------------------
    // 1. Normalize links
    // -----------------------------------------
    final Set<String> added = new HashSet<String>();

    try {
        final List<Map.Entry<String, String>> filter = new ArrayList<Map.Entry<String, String>>();
        filter.add(new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=entry"));
        filter.add(new AbstractMap.SimpleEntry<String, String>("type", "application/atom+xml;type=feed"));

        Map.Entry<Integer, XMLElement> linkInfo = null;

        while (true) {
            // a. search for link with type attribute equals to "application/atom+xml;type=entry/feed"
            linkInfo = extractElement(reader, writer,
                    Collections.<String>singletonList(Constants.get(ConstantKey.LINK)), filter, true,
                    linkInfo == null ? 0 : linkInfo.getKey(), 2, 2);
            final XMLElement link = linkInfo.getValue();

            final String title = link.getStart().getAttributeByName(new QName("title")).getValue();

            if (!added.contains(title)) {
                added.add(title);

                final String normalizedLink = String.format(
                        "<link href=\"%s(%s)/%s\" rel=\"%s\" title=\"%s\" type=\"%s\"/>", entitySetName,
                        entityKey, title, link.getStart().getAttributeByName(new QName("rel")).getValue(),
                        title, link.getStart().getAttributeByName(new QName("type")).getValue());

                addAtomElement(IOUtils.toInputStream(normalizedLink, Constants.ENCODING), writer);
            }
        }
    } catch (Exception ignore) {
        // ignore
    } finally {
        writer.close();
        reader.close();
    }
    // -----------------------------------------

    // -----------------------------------------
    // 2. Add/replace edit link
    // -----------------------------------------
    final InputStream content = addEditLink(new ByteArrayInputStream(tmpBos.toByteArray()), entitySetName,
            Constants.get(ConstantKey.DEFAULT_SERVICE_URL) + entitySetName + "(" + entityKey + ")");
    // -----------------------------------------

    // -----------------------------------------
    // 3. Add content element if missing
    // -----------------------------------------
    return addAtomContent(content, entitySetName,
            Constants.get(ConstantKey.DEFAULT_SERVICE_URL) + entitySetName + "(" + entityKey + ")");
    // -----------------------------------------

}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

@Override
public InputStream addEditLink(final InputStream content, final String title, final String href)
        throws Exception {

    final ByteArrayOutputStream copy = new ByteArrayOutputStream();
    IOUtils.copy(content, copy);/*from   w  w  w. ja v  a  2  s.c o  m*/
    IOUtils.closeQuietly(content);

    XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLEventWriter writer = getEventWriter(bos);

    final String editLinkElement = String.format("<link rel=\"edit\" title=\"%s\" href=\"%s\" />", title, href);

    try {
        // check edit link existence
        extractElement(reader, writer, Collections.<String>singletonList(Constants.get(ConstantKey.LINK)),
                Collections.<Map.Entry<String, String>>singletonList(
                        new AbstractMap.SimpleEntry<String, String>("rel", "edit")),
                false, 0, -1, -1);

        addAtomElement(IOUtils.toInputStream(editLinkElement, Constants.ENCODING), writer);
        writer.add(reader);

    } catch (Exception e) {
        reader.close();
        reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

        bos = new ByteArrayOutputStream();
        writer = getEventWriter(bos);

        final XMLElement entryElement = extractElement(reader, writer,
                Collections.<String>singletonList("entry"), 0, 1, 1).getValue();

        writer.add(entryElement.getStart());

        addAtomElement(IOUtils.toInputStream(editLinkElement, Constants.ENCODING), writer);

        writer.add(entryElement.getContentReader());
        writer.add(entryElement.getEnd());

        writer.add(reader);

        writer.flush();
        writer.close();
    } finally {
        reader.close();
    }

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

private InputStream addAtomContent(final InputStream content, final String title, final String href)
        throws Exception {

    final ByteArrayOutputStream copy = new ByteArrayOutputStream();
    IOUtils.copy(content, copy);/* w  w w .ja  v  a2 s  .  c  o  m*/

    IOUtils.closeQuietly(content);

    XMLEventReader reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLEventWriter writer = getEventWriter(bos);

    try {
        // check edit link existence
        XMLElement contentElement = extractElement(reader, writer, Collections.<String>singletonList("content"),
                0, 2, 2).getValue();
        writer.add(contentElement.getStart());
        writer.add(contentElement.getContentReader());
        writer.add(contentElement.getEnd());
        writer.add(reader);
    } catch (Exception e) {
        reader.close();
        reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

        bos = new ByteArrayOutputStream();
        writer = getEventWriter(bos);

        if (isMediaContent(title)) {
            final XMLElement entryElement = extractElement(reader, writer,
                    Collections.<String>singletonList("entry"), 0, 1, 1).getValue();

            writer.add(entryElement.getStart());
            writer.add(entryElement.getContentReader());

            addAtomElement(
                    IOUtils.toInputStream(String.format("<content type=\"*/*\" src=\"%s/$value\" />", href)),
                    writer);

            writer.add(entryElement.getEnd());
        } else {
            try {
                final XMLElement entryElement = extractElement(reader, writer,
                        Collections.<String>singletonList(Constants.get(ConstantKey.PROPERTIES)), 0, 2, 3)
                                .getValue();

                addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\">"), writer);

                writer.add(entryElement.getStart());
                writer.add(entryElement.getContentReader());
                writer.add(entryElement.getEnd());

                addAtomElement(IOUtils.toInputStream("</content>"), writer);
            } catch (Exception nf) {
                reader.close();
                reader = getEventReader(new ByteArrayInputStream(copy.toByteArray()));

                bos = new ByteArrayOutputStream();
                writer = getEventWriter(bos);

                final XMLElement entryElement = extractElement(reader, writer,
                        Collections.<String>singletonList("entry"), 0, 1, 1).getValue();
                writer.add(entryElement.getStart());
                writer.add(entryElement.getContentReader());

                addAtomElement(IOUtils.toInputStream("<content type=\"application/xml\"/>"), writer);

                writer.add(entryElement.getEnd());
            }
        }

        writer.add(reader);

        writer.flush();
        writer.close();
    } finally {
        reader.close();
    }

    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:org.apache.olingo.fit.utils.XMLUtilities.java

public InputStream addAtomInlinecount(final InputStream feed, final int count) throws Exception {
    final XMLEventReader reader = getEventReader(feed);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLEventWriter writer = getEventWriter(bos);

    try {//  w w  w.j  av a  2 s.  c o  m

        final XMLElement feedElement = extractElement(reader, writer, Collections.<String>singletonList("feed"),
                0, 1, 1).getValue();

        writer.add(feedElement.getStart());
        addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count), Constants.ENCODING),
                writer);
        writer.add(feedElement.getContentReader());
        writer.add(feedElement.getEnd());

        while (reader.hasNext()) {
            writer.add(reader.nextEvent());
        }

    } finally {
        writer.flush();
        writer.close();
        reader.close();
        IOUtils.closeQuietly(feed);
    }

    return new ByteArrayInputStream(bos.toByteArray());
}