Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.io.*;
import java.nio.charset.*;
import java.util.*;

import javax.xml.stream.*;

import javax.xml.stream.events.*;

public class Main {
    /**
     * 
     * @param elementName
     * @param is
     * @return Collection
     * @throws XMLStreamException
     * @throws UnsupportedEncodingException
     */
    public static Collection<String> getElements(final String elementName, final InputStream is)
            throws XMLStreamException, UnsupportedEncodingException {
        return getElements(elementName, is, false);
    }

    /**
     * 
     * @param elementName
     * @param is
     * @param onlyValues
     * @return Collection
     * @throws XMLStreamException
     * @throws UnsupportedEncodingException
     */
    public static Collection<String> getElements(final String elementName, final InputStream is,
            final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException {
        final Collection<String> elements = new ArrayList<>();
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final XMLEventReader reader = XMLInputFactory.newInstance()
                .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
        final XMLEventWriter writer = XMLOutputFactory.newInstance()
                .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name()));
        boolean read = false;
        String characters = null;

        while (reader.peek() != null) {
            final XMLEvent event = (XMLEvent) reader.next();

            switch (event.getEventType()) {
            case XMLStreamConstants.START_DOCUMENT:
            case XMLStreamConstants.END_DOCUMENT: {
                // Ignore.
                break;
            }
            case XMLStreamConstants.START_ELEMENT: {
                read = read || elementName.equals(event.asStartElement().getName().getLocalPart());

                if (read && !onlyValues) {
                    writer.add(event);
                }

                break;
            }
            case XMLStreamConstants.ATTRIBUTE: {
                if (read && !onlyValues) {
                    writer.add(event);
                }
                break;
            }
            case XMLStreamConstants.CHARACTERS: {
                if (read && !onlyValues) {
                    writer.add(event);
                }
                characters = event.asCharacters().getData();
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                if (read && !onlyValues) {
                    writer.add(event);
                }
                if (elementName.equals(event.asEndElement().getName().getLocalPart())) {
                    writer.flush();

                    if (characters != null) {
                        elements.add(characters);
                    }

                    os.reset();
                    read = false;
                }
                break;
            }
            default: {
                // Ignore
                break;
            }
            }
        }

        return elements;
    }
}