Example usage for javax.xml.stream XMLStreamConstants END_DOCUMENT

List of usage examples for javax.xml.stream XMLStreamConstants END_DOCUMENT

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants END_DOCUMENT.

Prototype

int END_DOCUMENT

To view the source code for javax.xml.stream XMLStreamConstants END_DOCUMENT.

Click Source Link

Document

Indicates an event is an end document

Usage

From source file:com.widowcrawler.exo.parse.Parser.java

private static String getEventTypeString(int eventType) {
    switch (eventType) {
    case XMLStreamConstants.START_ELEMENT:
        return "START_ELEMENT";
    case XMLStreamConstants.END_ELEMENT:
        return "END_ELEMENT";
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        return "PROCESSING_INSTRUCTION";
    case XMLStreamConstants.CHARACTERS:
        return "CHARACTERS";
    case XMLStreamConstants.COMMENT:
        return "COMMENT";
    case XMLStreamConstants.START_DOCUMENT:
        return "START_DOCUMENT";
    case XMLStreamConstants.END_DOCUMENT:
        return "END_DOCUMENT";
    case XMLStreamConstants.ENTITY_REFERENCE:
        return "ENTITY_REFERENCE";
    case XMLStreamConstants.ATTRIBUTE:
        return "ATTRIBUTE";
    case XMLStreamConstants.DTD:
        return "DTD";
    case XMLStreamConstants.CDATA:
        return "CDATA";
    case XMLStreamConstants.SPACE:
        return "SPACE";
    case XMLStreamConstants.ENTITY_DECLARATION:
        return "ENTITY_DECLARATION";
    case XMLStreamConstants.NAMESPACE:
        return "NAMESPACE";
    case XMLStreamConstants.NOTATION_DECLARATION:
        return "NOTATION_DECLARATION";
    }/*from   w w w .j a  v a  2 s.c o m*/
    return "UNKNOWN_EVENT_TYPE: " + eventType;
}

From source file:au.org.ala.bhl.DocumentPaginator.java

private void paginateImpl(XMLStreamReader parser, PageHandler handler) throws Exception {

    if (parser == null) {
        return;//from  www.  j a  va  2  s.c  o m
    }

    StringBuilder buffer = new StringBuilder();

    String currentPage = null;

    while (true) {
        int event = parser.next();
        if (event == XMLStreamConstants.END_DOCUMENT) {
            parser.close();
            break;
        }

        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.getLocalName().equals("PARAM")) {
                String attrName = parser.getAttributeValue("", "name");
                if (attrName.equals("PAGE")) {

                    if (!StringUtils.isEmpty(currentPage)) {
                        if (handler != null) {
                            handler.handlePage(currentPage, buffer.toString());
                        }
                    }

                    buffer = new StringBuilder();
                    currentPage = parser.getAttributeValue("", "value");
                }
            }
        }

        if (event == XMLStreamConstants.CHARACTERS) {
            String value = StringUtils.trim(parser.getText());
            if (!StringUtils.isEmpty(value)) {
                buffer.append(value).append(" ");
            }
        }
    }

}

From source file:ar.com.zauber.commons.spring.test.impl.TamperdataHttpServletRequestFactory.java

/** hace el trabajo sucio 
 * @throws UnsupportedEncodingException */
private HttpServletRequest parse(final XMLStreamReader reader)
        throws XMLStreamException, UnsupportedEncodingException {
    final MockHttpServletRequest ret = new MockHttpServletRequest();
    ret.setMethod("POST");
    String header = null;//from ww  w.java2  s . com
    String postHeader = null;
    int event;
    while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            final String name = reader.getLocalName();
            if (name.equals("tdRequestHeader") || name.equals("tdPostHeader")) {
                header = reader.getAttributeValue(0);
            } else if (name.equals("tdPostElements")) {
                ret.setMethod("POST");
            } else if (name.equals("tdPostElement")) {
                postHeader = reader.getAttributeValue(0);
            }
        } else if (event == XMLStreamConstants.CHARACTERS) {
            String text = reader.getText();
            if (text.length() > 1 && Character.isWhitespace(text.charAt(0))) {
                text = text.substring(1);
            }
            if (text.length() > 1 && Character.isWhitespace(text.charAt(text.length() - 1))) {
                text = text.substring(0, text.length() - 1);
            }

            final String value = URLDecoder.decode(URLDecoder.decode(text, encoding), encoding);
            if (header != null) {
                ret.addHeader(header, value);
            } else if (postHeader != null) {
                ret.addParameter(postHeader, value);
            }
            header = null;
            postHeader = null;
        } else {
            header = null;
            postHeader = null;
        }
    }
    reader.close();
    return ret;
}

From source file:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java

private String pullNextXmlChunkFromTopElementOnStack(XMLChunkerState data) throws KettleException {
    Stack<String> elementStack = data.getElementStack();
    XMLStreamReader xmlStreamReader = data.getXmlStreamReader();

    int elementStackDepthOnEntry = elementStack.size();
    StringWriter stringWriter = new StringWriter();

    try {//from   w w  w  . j av a  2s.c  o  m
        XMLStreamWriter xmlStreamWriter = data.getXmlOutputFactory().createXMLStreamWriter(stringWriter);

        xmlStreamWriter.writeStartDocument(CharEncoding.UTF_8, "1.0");

        // put the current element on because presumably it's the open element for the one
        // that is being looked for.

        XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);

        while (xmlStreamReader.hasNext() & elementStack.size() >= elementStackDepthOnEntry) {

            switch (xmlStreamReader.next()) {

            case XMLStreamConstants.END_DOCUMENT:
                break; // handled below explicitly.

            case XMLStreamConstants.END_ELEMENT:
                elementStack.pop();
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            case XMLStreamConstants.START_ELEMENT:
                elementStack.push(xmlStreamReader.getLocalName());
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            default:
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            }

        }

        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
    } catch (Exception e) {
        throw new KettleException("unable to process a chunk of the xero xml stream", e);
    }

    return stringWriter.toString();
}

From source file:com.microsoft.tfs.core.memento.XMLMemento.java

/**
 * Reads an {@link XMLMemento} from the next XML element in the given given
 * {@link InputStream} in the encoding specified as
 * {@link #DEFAULT_ENCODING}./*from  ww w.j a v  a  2 s .  com*/
 *
 * @param inputStream
 *        the {@link InputStream} read to read the {@link XMLMemento} from
 *        (must not be <code>null</code>)
 * @param encoding
 *        the encoding to use when reading the {@link InputStream},
 *        <code>null</code> to use the default encoding (
 *        {@link #DEFAULT_ENCODING})
 * @return a Memento modeled as the first Element in the document.
 * @throws MementoException
 *         if an error prevented the creation of the Memento.
 */
public static XMLMemento read(final InputStream inputStream, final String encoding) throws MementoException {
    Check.notNull(inputStream, "inputStream"); //$NON-NLS-1$

    try {
        final XMLStreamReader reader = StaxFactoryProvider.getXMLInputFactory(true)
                .createXMLStreamReader(inputStream, (encoding != null) ? encoding : DEFAULT_ENCODING);

        XMLMemento memento = null;
        String localName;
        int event;

        do {
            event = reader.next();

            if (event == XMLStreamConstants.START_ELEMENT) {
                localName = reader.getLocalName();

                memento = new XMLMemento(localName);
                memento.readFromElement(reader);
            }
        } while (event != XMLStreamConstants.END_ELEMENT && event != XMLStreamConstants.END_DOCUMENT);

        reader.close();

        return memento;
    } catch (final XMLStreamException e) {
        log.error("Error reading", e); //$NON-NLS-1$
        throw new MementoException(e);
    }
}

From source file:at.lame.hellonzb.parser.NzbParser.java

/**
 * This is the constructor of the class.
 * It parses the given XML file.//from   www.  j ava 2s. co m
 *
 * @param mainApp The main application object
 * @param file The file name of the nzb file to parse
 * @throws XMLStreamException 
 * @throws IOException 
 */
public NzbParser(HelloNzbCradle mainApp, String file) throws XMLStreamException, IOException, ParseException {
    this.mainApp = mainApp;

    DownloadFile currentFile = null;
    DownloadFileSegment currentSegment = null;
    boolean groupFlag = false;
    boolean segmentFlag = false;

    this.name = file.trim();
    this.name = file.substring(0, file.length() - 4);
    this.downloadFiles = new Vector<DownloadFile>();

    this.origTotalSize = 0;
    this.downloadedBytes = 0;

    // create XML parser
    String string = reformatInputStream(file);
    InputStream in = new ByteArrayInputStream(string.getBytes());
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);

    // parse nzb file with a Java XML parser
    while (parser.hasNext()) {
        switch (parser.getEventType()) {
        // parser has reached the end of the xml file
        case XMLStreamConstants.END_DOCUMENT:
            parser.close();
            break;

        // parser has found a new element
        case XMLStreamConstants.START_ELEMENT:
            String elemName = parser.getLocalName().toLowerCase();
            if (elemName.equals("file")) {
                currentFile = newDownloadFile(parser);
                boolean found = false;
                for (DownloadFile dlf : downloadFiles)
                    if (dlf.getFilename().equals(currentFile.getFilename())) {
                        found = true;
                        break;
                    }

                if (!found)
                    downloadFiles.add(currentFile);
            } else if (elemName.equals("group"))
                groupFlag = true;
            else if (elemName.equals("segment")) {
                currentSegment = newDownloadFileSegment(parser, currentFile);
                currentFile.addSegment(currentSegment);
                segmentFlag = true;
            }
            break;

        // end of element
        case XMLStreamConstants.END_ELEMENT:
            groupFlag = false;
            segmentFlag = false;
            break;

        // get the elements value(s)
        case XMLStreamConstants.CHARACTERS:
            if (!parser.isWhiteSpace()) {
                if (groupFlag && (currentFile != null))
                    currentFile.addGroup(parser.getText());
                else if (segmentFlag && (currentSegment != null))
                    currentSegment.setArticleId(parser.getText());
            }
            break;

        // any other parser event?
        default:
            break;
        }

        parser.next();
    }

    checkFileSegments();
    this.origTotalSize = getCurrTotalSize();
}

From source file:com.wavemaker.runtime.data.parser.BaseHbmParser.java

protected String nextInternal(String parentElementName, int numTries, String... elementNames) {

    int i = 1;//from   w w  w .  ja v  a2s .c o m

    try {

        for (int event = this.xmlReader.next(); event != XMLStreamConstants.END_DOCUMENT; event = this.xmlReader
                .next()) {

            switch (event) {
            case XMLStreamConstants.START_ELEMENT:

                this.currentText.delete(0, this.currentText.length());
                this.currentElementName = this.xmlReader.getName().toString();

                if (numTries > -1) {
                    if (i == numTries) {
                        return this.currentElementName;
                    } else if (i > numTries) {
                        return null;
                    }
                }

                i++;

                if (elementNames != null) {
                    for (String s : elementNames) {
                        if (s.equals(this.currentElementName)) {
                            return this.currentElementName;
                        }
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                String endElementName = this.xmlReader.getName().toString();
                if (endElementName.equals(parentElementName)) {
                    return null;
                }
                break;
            case XMLStreamConstants.CHARACTERS:
                this.currentText.append(this.xmlReader.getText());
                break;
            case XMLStreamConstants.CDATA:
                break;
            }
        }

    } catch (XMLStreamException ex) {
        throw new WMRuntimeException(ex);
    }

    return null;

}

From source file:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java

@Override
public String pullNextXmlChunk() throws KettleException {
    Stack<String> elementStack = xmlChunkerState.getElementStack();
    XMLStreamReader xmlStreamReader = xmlChunkerState.getXmlStreamReader();

    try {/*from  w  w  w .  java 2s . co  m*/

        while (xmlStreamReader.hasNext()) {

            switch (xmlStreamReader.next()) {

            case XMLStreamConstants.END_DOCUMENT:
                return null;

            case XMLStreamConstants.END_ELEMENT:
                elementStack.pop();
                break;

            case XMLStreamConstants.START_ELEMENT:
                elementStack.push(xmlStreamReader.getLocalName());

                if (actualElementStackHasExpectedElements(xmlChunkerState)) {
                    return pullNextXmlChunkFromTopElementOnStack(xmlChunkerState);
                }

                break;

            }

        }
    } catch (Exception e) {
        throw new KettleException("a problem has arisen reading the xero xml stream", e);
    }

    return null;
}

From source file:com.flexive.chemistry.webdav.TextDocumentResource.java

protected void processXmlProperties(InputStream in) {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    try {//from  www . j a va 2 s . c o  m
        final XMLStreamReader parser = factory.createXMLStreamReader(in);
        for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if ("property".equals(parser.getLocalName())) {
                    processProperty(parser);
                } else if ("name".equals(parser.getLocalName())) {
                    processName(parser);
                }

            }
        }
    } catch (XMLStreamException e) {
        throw new RuntimeException("Failed to replace content: " + e.getMessage(), e);
    }
}

From source file:com.graphhopper.reader.OSMInputFile.java

private OSMElement getNextXML() throws XMLStreamException {

    int event = parser.next();
    if (fileheader != null) {
        OSMElement copyfileheader = fileheader;
        fileheader = null;/*  ww w . ja v  a 2  s .  co m*/
        return copyfileheader;
    }

    while (event != XMLStreamConstants.END_DOCUMENT) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            String idStr = parser.getAttributeValue(null, "id");
            if (idStr != null) {
                String name = parser.getLocalName();
                long id = 0;
                switch (name.charAt(0)) {
                case 'n':
                    // note vs. node
                    if ("node".equals(name)) {
                        id = Long.parseLong(idStr);
                        return OSMNode.create(id, parser);
                    }
                    break;

                case 'w': {
                    id = Long.parseLong(idStr);
                    return OSMWay.create(id, parser);
                }
                case 'r':
                    id = Long.parseLong(idStr);
                    return OSMRelation.create(id, parser);
                }
            }
        }
        event = parser.next();
    }
    parser.close();
    return null;
}