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.FactoryConfigurationError;
import javax.xml.stream.events.*;

public class Main {
    /**
     * 
     * @param elementName
     * @param attributeValue
     * @param is
     * @return Collection
     * @throws XMLStreamException
     * @throws FactoryConfigurationError
     * @throws UnsupportedEncodingException
     */
    public static Collection<String> getElementValues(final String elementName, final String attributeValue,
            final InputStream is)
            throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError {
        final Collection<String> elementValues = new ArrayList<>();
        final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
                .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
        final StringBuffer characters = new StringBuffer();
        boolean read = false;

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

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

                if (read && attributeValue != null) {
                    read = false;

                    for (Iterator<Attribute> iterator = event.asStartElement().getAttributes(); iterator
                            .hasNext();) {
                        Attribute attribute = iterator.next();

                        if (attribute.getValue().equals(attributeValue)) {
                            read = true;
                            break;
                        }
                    }
                }

                break;
            }
            case XMLStreamConstants.CHARACTERS: {
                if (read) {
                    characters.append(event.asCharacters().getData());
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                if (read) {
                    elementValues.add(characters.toString());
                    characters.setLength(0);
                }

                read = false;
                break;
            }
            default: {
                // Ignore
                break;
            }
            }
        }

        return elementValues;
    }
}