Java Code Examples for javax.xml.stream.XMLStreamReader#END_DOCUMENT

The following examples show how to use javax.xml.stream.XMLStreamReader#END_DOCUMENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamReaderBufferCreator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void storeDocumentAndChildren(XMLStreamReader reader) throws XMLStreamException {
    storeStructure(T_DOCUMENT);

    _eventType = reader.next();
    while (_eventType != XMLStreamReader.END_DOCUMENT) {
        switch (_eventType) {
            case XMLStreamReader.START_ELEMENT:
                storeElementAndChildren(reader);
                continue;
            case XMLStreamReader.COMMENT:
                storeComment(reader);
                break;
            case XMLStreamReader.PROCESSING_INSTRUCTION:
                storeProcessingInstruction(reader);
                break;
        }
        _eventType = reader.next();
    }

    storeStructure(T_END);
}
 
Example 2
Source File: XMLStreamReaderTest.java    From FHIR with Apache License 2.0 6 votes vote down vote up
private static void debug(XMLStreamReader reader) throws XMLStreamException {
    int eventType = reader.getEventType();
    switch (eventType) {
    case XMLStreamReader.START_DOCUMENT:
        System.out.println("START_DOCUMENT");
        break;
    case XMLStreamReader.START_ELEMENT:
        System.out.println("START_ELEMENT: " + reader.getLocalName());
        break;
    case XMLStreamReader.SPACE:
        System.out.println("SPACE");
        break;
    case XMLStreamReader.CHARACTERS:
        System.out.println("CHARACTERS");
        break;
    case XMLStreamReader.END_ELEMENT:
        System.out.println("END_ELEMENT: " + reader.getLocalName());
        break;
    case XMLStreamReader.END_DOCUMENT:
        System.out.println("END_DOCUMENT");
        break;
    default:
        System.out.println("OTHER: " + eventType);
        break;
    }
}
 
Example 3
Source File: StreamReaderBufferCreator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void storeDocumentAndChildren(XMLStreamReader reader) throws XMLStreamException {
    storeStructure(T_DOCUMENT);

    _eventType = reader.next();
    while (_eventType != XMLStreamReader.END_DOCUMENT) {
        switch (_eventType) {
            case XMLStreamReader.START_ELEMENT:
                storeElementAndChildren(reader);
                continue;
            case XMLStreamReader.COMMENT:
                storeComment(reader);
                break;
            case XMLStreamReader.PROCESSING_INSTRUCTION:
                storeProcessingInstruction(reader);
                break;
        }
        _eventType = reader.next();
    }

    storeStructure(T_END);
}
 
Example 4
Source File: StreamReaderBufferCreator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void storeDocumentAndChildren(XMLStreamReader reader) throws XMLStreamException {
    storeStructure(T_DOCUMENT);

    _eventType = reader.next();
    while (_eventType != XMLStreamReader.END_DOCUMENT) {
        switch (_eventType) {
            case XMLStreamReader.START_ELEMENT:
                storeElementAndChildren(reader);
                continue;
            case XMLStreamReader.COMMENT:
                storeComment(reader);
                break;
            case XMLStreamReader.PROCESSING_INSTRUCTION:
                storeProcessingInstruction(reader);
                break;
        }
        _eventType = reader.next();
    }

    storeStructure(T_END);
}
 
Example 5
Source File: XmlPullProcessor.java    From morf with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the next tag name from the XML parser so long as it lies within the parent tag name.
 * If the close tag event for the parent is read this method will return null. Otherwise it
 * returns the name of the tag read.
 *
 * @param parentTagName The enclosing tag that forms the limit for the read operation.
 * @return The next tag name or null if there are no more tags to read inside the specified parent.
 */
protected String readNextTagInsideParent(String parentTagName) {
  int event;
  try {
    do {
      event = xmlStreamReader.next();
    } while (event == XMLStreamReader.CHARACTERS ||
             event == XMLStreamReader.COMMENT ||
             event == XMLStreamReader.END_ELEMENT && !xmlStreamReader.getLocalName().equals(parentTagName));
  } catch (Exception e) {
    throw new RuntimeException("Error reading data from the XML pull parser", e);
  }

  if (event == XMLStreamReader.START_ELEMENT) {
    return xmlStreamReader.getLocalName();

  } else if (event == XMLStreamReader.END_ELEMENT) {
    return null;

  } else if (event == XMLStreamReader.END_DOCUMENT) {
    throw new IllegalStateException("Unexpected end of document while looking for a tag inside [" + parentTagName + "]");

  } else {
    throw new IllegalStateException("Expecting a tag inside [" + parentTagName + "] but got [" + event + "]");
  }
}
 
Example 6
Source File: XmlPullProcessor.java    From morf with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the next tag from the pull parser and throws an exception if its name does not
 * match <var>expectedTagName</var>.
 *
 * @param xmlPullParser The pull parser to read from
 * @param expectedTagName The tag name expected
 */
public static void readTag(XMLStreamReader xmlStreamReader, String expectedTagName) {
  // Look for any start tag event
  int event;
  try {
    do {
      event = xmlStreamReader.next();
    } while (event == XMLStreamReader.CHARACTERS || event == XMLStreamReader.END_ELEMENT || event == XMLStreamReader.COMMENT);
  } catch (Exception e) {
    throw new RuntimeException("Error reading data from the XML pull parser", e);
  }

  if (event == XMLStreamReader.START_ELEMENT) {
    if (!expectedTagName.equals(xmlStreamReader.getLocalName())) {
      throw new IllegalArgumentException("Expected tag [" + expectedTagName + "] but got [" + xmlStreamReader.getLocalName() + "]");
    }

  } else if (event == XMLStreamReader.END_DOCUMENT) {
    throw new IllegalStateException("Unexpected end of document while looking for tag [" + expectedTagName + "]");

  } else {
    throw new IllegalStateException("Expecting a tag but found [" + event + "]");
  }
}
 
Example 7
Source File: StreamReaderBufferCreator.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private void storeDocumentAndChildren(XMLStreamReader reader) throws XMLStreamException {
    storeStructure(T_DOCUMENT);

    _eventType = reader.next();
    while (_eventType != XMLStreamReader.END_DOCUMENT) {
        switch (_eventType) {
            case XMLStreamReader.START_ELEMENT:
                storeElementAndChildren(reader);
                continue;
            case XMLStreamReader.COMMENT:
                storeComment(reader);
                break;
            case XMLStreamReader.PROCESSING_INSTRUCTION:
                storeProcessingInstruction(reader);
                break;
        }
        _eventType = reader.next();
    }

    storeStructure(T_END);
}
 
Example 8
Source File: DataReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the data from the given XML stream reader.
 * 
 * @param xmlReader The reader
 */
private void read(XMLStreamReader xmlReader) throws DdlUtilsXMLException
{
    try
    {
        while (xmlReader.getEventType() != XMLStreamReader.START_ELEMENT)
        {
            if (xmlReader.next() == XMLStreamReader.END_DOCUMENT)
            {
                return;
            }
        }
        readDocument(xmlReader);
    }
    catch (XMLStreamException ex)
    {
        throw new DdlUtilsXMLException(ex);
    }
}
 
Example 9
Source File: StreamReaderBufferCreator.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void storeDocumentAndChildren(XMLStreamReader reader) throws XMLStreamException {
    storeStructure(T_DOCUMENT);

    _eventType = reader.next();
    while (_eventType != XMLStreamReader.END_DOCUMENT) {
        switch (_eventType) {
            case XMLStreamReader.START_ELEMENT:
                storeElementAndChildren(reader);
                continue;
            case XMLStreamReader.COMMENT:
                storeComment(reader);
                break;
            case XMLStreamReader.PROCESSING_INSTRUCTION:
                storeProcessingInstruction(reader);
                break;
        }
        _eventType = reader.next();
    }

    storeStructure(T_END);
}
 
Example 10
Source File: DataReader.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Reads the data from the given XML stream reader.
 * 
 * @param xmlReader The reader
 */
private void read(XMLStreamReader xmlReader) throws DdlUtilsXMLException
{
    try
    {
        while (xmlReader.getEventType() != XMLStreamReader.START_ELEMENT)
        {
            if (xmlReader.next() == XMLStreamReader.END_DOCUMENT)
            {
                return;
            }
        }
        readDocument(xmlReader);
    }
    catch (XMLStreamException ex)
    {
        throw new DdlUtilsXMLException(ex);
    }
}
 
Example 11
Source File: XMLReaderComposite.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int nextTag() throws XMLStreamException {
    int e = next();
    if (e == XMLStreamReader.END_DOCUMENT) return e;
    while (e != XMLStreamReader.END_DOCUMENT) {
        if (e == XMLStreamReader.START_ELEMENT) return e;
        if (e == XMLStreamReader.END_ELEMENT) return e;
        e = next();
    }
    return e;
}
 
Example 12
Source File: XMLReaderComposite.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int nextTag() throws XMLStreamException {
    int e = next();
    if (e == XMLStreamReader.END_DOCUMENT) return e;
    while (e != XMLStreamReader.END_DOCUMENT) {
        if (e == XMLStreamReader.START_ELEMENT) return e;
        if (e == XMLStreamReader.END_ELEMENT) return e;
        e = next();
    }
    return e;
}
 
Example 13
Source File: StreamReaderBufferCreator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates the buffer from a stream reader that is an element fragment.
 * <p>
 * The stream reader will be moved to the position of the next start of
 * an element if the stream reader is not already positioned at the start
 * of an element.
 * <p>
 * The element and all its children will be stored and after storing the stream
 * will be positioned at the next event after the end of the element.
 * <p>
 * @param storeInScopeNamespaces true if in-scope namespaces of the element
 *        fragment should be stored.
 * @return the mutable stream buffer.
 * @throws XMLStreamException if the stream reader cannot be positioned at
 *         the start of an element.
 */
public MutableXMLStreamBuffer createElementFragment(XMLStreamReader reader,
        boolean storeInScopeNamespaces) throws XMLStreamException {
    if (_buffer == null) {
        createBuffer();
    }

    if (!reader.hasNext()) {
        return _buffer;
    }

    _storeInScopeNamespacesOnElementFragment = storeInScopeNamespaces;

    _eventType = reader.getEventType();
    if (_eventType != XMLStreamReader.START_ELEMENT) {
        do {
            _eventType = reader.next();
        } while(_eventType != XMLStreamReader.START_ELEMENT && _eventType != XMLStreamReader.END_DOCUMENT);
    }

    if (storeInScopeNamespaces) {
        _inScopePrefixes = new HashMap<String,Integer>();
    }

    storeElementAndChildren(reader);

    return getXMLStreamBuffer();
}
 
Example 14
Source File: BackupImport.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
private static <T> void readList(Unmarshaller inUnmarshaller, File baseDir, String fileName, String listNodeName, String nodeName, Class<T> clazz, Consumer<T> consumer, boolean notThow) {
	File xml = new File(baseDir, fileName);
	if (!xml.exists()) {
		final String msg = fileName + " missing";
		if (notThow) {
			log.debug(msg);
			return;
		} else {
			throw new BackupException(msg);
		}
	}
	try {
		Unmarshaller unmarshaller = inUnmarshaller;
		if (inUnmarshaller == null) {
			JAXBContext jc = JAXBContext.newInstance(clazz);
			unmarshaller = jc.createUnmarshaller();
		}
		XMLInputFactory xif = XmlHelper.createInputFactory();
		StreamSource xmlSource = new StreamSource(xml);
		XMLStreamReader xsr = xif.createXMLStreamReader(xmlSource);
		boolean listNodeFound = false;
		while (xsr.getEventType() != XMLStreamReader.END_DOCUMENT) {
			if (xsr.isStartElement()) {
				if (!listNodeFound && listNodeName.equals(xsr.getLocalName())) {
					listNodeFound = true;
				} else if (nodeName.equals(xsr.getLocalName())) {
					T o = unmarshaller.unmarshal(xsr, clazz).getValue();
					consumer.accept(o);
				}
			}
			xsr.next();
		}
	} catch (Exception e) {
		throw new BackupException(e);
	}
}
 
Example 15
Source File: Jaxb2CollectionHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private int moveToNextElement(XMLStreamReader streamReader) throws XMLStreamException {
	int event = streamReader.getEventType();
	while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
		event = streamReader.next();
	}
	return event;
}
 
Example 16
Source File: StreamReaderBufferCreator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates the buffer from a stream reader that is an element fragment.
 * <p>
 * The stream reader will be moved to the position of the next start of
 * an element if the stream reader is not already positioned at the start
 * of an element.
 * <p>
 * The element and all its children will be stored and after storing the stream
 * will be positioned at the next event after the end of the element.
 * <p>
 * @param storeInScopeNamespaces true if in-scope namespaces of the element
 *        fragment should be stored.
 * @return the mutable stream buffer.
 * @throws XMLStreamException if the stream reader cannot be positioned at
 *         the start of an element.
 */
public MutableXMLStreamBuffer createElementFragment(XMLStreamReader reader,
        boolean storeInScopeNamespaces) throws XMLStreamException {
    if (_buffer == null) {
        createBuffer();
    }

    if (!reader.hasNext()) {
        return _buffer;
    }

    _storeInScopeNamespacesOnElementFragment = storeInScopeNamespaces;

    _eventType = reader.getEventType();
    if (_eventType != XMLStreamReader.START_ELEMENT) {
        do {
            _eventType = reader.next();
        } while(_eventType != XMLStreamReader.START_ELEMENT && _eventType != XMLStreamReader.END_DOCUMENT);
    }

    if (storeInScopeNamespaces) {
        _inScopePrefixes = new HashMap<String,Integer>();
    }

    storeElementAndChildren(reader);

    return getXMLStreamBuffer();
}
 
Example 17
Source File: Jaxb2CollectionHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private int moveToNextElement(XMLStreamReader streamReader) throws XMLStreamException {
	int event = streamReader.getEventType();
	while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
		event = streamReader.next();
	}
	return event;
}
 
Example 18
Source File: DataReader.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Consumes the rest of the current element. This assumes that the current XML stream
 * event type is not START_ELEMENT.
 * 
 * @param reader The xml reader
 */
private void consumeRestOfElement(XMLStreamReader reader) throws XMLStreamException
{
    int eventType = reader.getEventType();

    while ((eventType != XMLStreamReader.END_ELEMENT) && (eventType != XMLStreamReader.END_DOCUMENT))
    {
        eventType = reader.next();
    }
}
 
Example 19
Source File: XMLReaderComposite.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int nextTag() throws XMLStreamException {
    int e = next();
    if (e == XMLStreamReader.END_DOCUMENT) return e;
    while (e != XMLStreamReader.END_DOCUMENT) {
        if (e == XMLStreamReader.START_ELEMENT) return e;
        if (e == XMLStreamReader.END_ELEMENT) return e;
        e = next();
    }
    return e;
}
 
Example 20
Source File: Jaxb2CollectionHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private int moveToFirstChildOfRootElement(XMLStreamReader streamReader) throws XMLStreamException {
	// root
	int event = streamReader.next();
	while (event != XMLStreamReader.START_ELEMENT) {
		event = streamReader.next();
	}

	// first child
	event = streamReader.next();
	while ((event != XMLStreamReader.START_ELEMENT) && (event != XMLStreamReader.END_DOCUMENT)) {
		event = streamReader.next();
	}
	return event;
}