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.namespace.*;

import javax.xml.stream.*;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.events.*;

public class Main {
    /**
     * 
     * @param is
     * @param os
     * @param elementNames
     * @throws XMLStreamException
     * @throws FactoryConfigurationError
     * @throws UnsupportedEncodingException
     */
    public static void stripElements(final InputStream is, final OutputStream os,
            final Collection<String> elementNames)
            throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError {
        final XMLEventReader xmlEventReader = XMLInputFactory.newInstance()
                .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
        final XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(os);

        String elementName = null;

        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: {
                final StartElement startElement = event.asStartElement();
                final QName name = startElement.getName();

                if (elementNames.contains(name.getLocalPart())) {
                    elementName = name.getLocalPart();
                }

                if (elementName == null) {
                    xmlEventWriter.add(event);
                }

                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                final EndElement endElement = event.asEndElement();
                final QName name = endElement.getName();

                if (elementName == null) {
                    xmlEventWriter.add(event);
                } else if (elementName.equals(name.getLocalPart())) {
                    elementName = null;
                }

                break;
            }
            default: {
                if (elementName == null) {
                    xmlEventWriter.add(event);
                }
            }
            }
        }

        xmlEventWriter.flush();
    }
}