Java Code Examples for javax.xml.stream.events.XMLEvent#isStartDocument()

The following examples show how to use javax.xml.stream.events.XMLEvent#isStartDocument() . 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: SkipRootElementXMLEventReader.java    From exificient with MIT License 5 votes vote down vote up
public SkipRootElementXMLEventReader(XMLEventReader parent)
		throws XMLStreamException {
	events = new ArrayList<XMLEvent>();
	int openElement = 0;

	while (parent.hasNext()) {
		XMLEvent next = parent.nextEvent();

		if (next.isStartDocument()) {
			events.add(next);
		} else if (next.isEndDocument()) {
			events.add(next);
		} else if (next.isStartElement()) {
			if (openElement > 0) {
				events.add(next);
			}
			openElement++;
		} else if (next.isEndElement()) {
			openElement--;
			if (openElement > 0) {
				events.add(next);
			}
		} else {
			if (openElement > 0) {
				events.add(next);
			}
		}

	}

}
 
Example 2
Source File: UnmarshallerImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
        throw new IllegalArgumentException(
                Messages.format(Messages.NULL_READER));
    }

    try {
        XMLEvent event = reader.peek();

        if (!event.isStartElement() && !event.isStartDocument()) {
            // TODO: convert event into event name
            throw new IllegalStateException(
                Messages.format(
                    Messages.ILLEGAL_READER_STATE,event.getEventType()));
        }

        // Quick hack until SJSXP fixes 6270116
        boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
        XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
        if(!isZephyr) {
            h = new InterningXmlVisitor(h);
        }
        new StAXEventConnector(reader,h).bridge();
        return h.getContext().getResult();
    } catch (XMLStreamException e) {
        throw handleStreamException(e);
    }
}
 
Example 3
Source File: StaxEventXMLReader.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Constructs a new instance of the {@code StaxEventXmlReader} that reads from
 * the given {@code XMLEventReader}. The supplied event reader must be in
 * {@code XMLStreamConstants.START_DOCUMENT} or {@code XMLStreamConstants.START_ELEMENT} state.
 * @param reader the {@code XMLEventReader} to read from
 * @throws IllegalStateException if the reader is not at the start of a document or element
 */
StaxEventXMLReader(XMLEventReader reader) {
	try {
		XMLEvent event = reader.peek();
		if (event != null && !(event.isStartDocument() || event.isStartElement())) {
			throw new IllegalStateException("XMLEventReader not at start of document or element");
		}
	}
	catch (XMLStreamException ex) {
		throw new IllegalStateException("Could not read first element: " + ex.getMessage());
	}
	this.reader = reader;
}
 
Example 4
Source File: UnmarshallerImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
        throw new IllegalArgumentException(
                Messages.format(Messages.NULL_READER));
    }

    try {
        XMLEvent event = reader.peek();

        if (!event.isStartElement() && !event.isStartDocument()) {
            // TODO: convert event into event name
            throw new IllegalStateException(
                Messages.format(
                    Messages.ILLEGAL_READER_STATE,event.getEventType()));
        }

        // Quick hack until SJSXP fixes 6270116
        boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
        XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
        if(!isZephyr) {
            h = new InterningXmlVisitor(h);
        }
        new StAXEventConnector(reader,h).bridge();
        return h.getContext().getResult();
    } catch (XMLStreamException e) {
        throw handleStreamException(e);
    }
}
 
Example 5
Source File: UnmarshallerImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
        throw new IllegalArgumentException(
                Messages.format(Messages.NULL_READER));
    }

    try {
        XMLEvent event = reader.peek();

        if (!event.isStartElement() && !event.isStartDocument()) {
            // TODO: convert event into event name
            throw new IllegalStateException(
                Messages.format(
                    Messages.ILLEGAL_READER_STATE,event.getEventType()));
        }

        // Quick hack until SJSXP fixes 6270116
        boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
        XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
        if(!isZephyr) {
            h = new InterningXmlVisitor(h);
        }
        new StAXEventConnector(reader,h).bridge();
        return h.getContext().getResult();
    } catch (XMLStreamException e) {
        throw handleStreamException(e);
    }
}
 
Example 6
Source File: UnmarshallerImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
        throw new IllegalArgumentException(
                Messages.format(Messages.NULL_READER));
    }

    try {
        XMLEvent event = reader.peek();

        if (!event.isStartElement() && !event.isStartDocument()) {
            // TODO: convert event into event name
            throw new IllegalStateException(
                Messages.format(
                    Messages.ILLEGAL_READER_STATE,event.getEventType()));
        }

        // Quick hack until SJSXP fixes 6270116
        boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
        XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
        if(!isZephyr) {
            h = new InterningXmlVisitor(h);
        }
        new StAXEventConnector(reader,h).bridge();
        return h.getContext().getResult();
    } catch (XMLStreamException e) {
        throw handleStreamException(e);
    }
}
 
Example 7
Source File: UnmarshallerImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
        throw new IllegalArgumentException(
                Messages.format(Messages.NULL_READER));
    }

    try {
        XMLEvent event = reader.peek();

        if (!event.isStartElement() && !event.isStartDocument()) {
            // TODO: convert event into event name
            throw new IllegalStateException(
                Messages.format(
                    Messages.ILLEGAL_READER_STATE,event.getEventType()));
        }

        // Quick hack until SJSXP fixes 6270116
        boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
        XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
        if(!isZephyr) {
            h = new InterningXmlVisitor(h);
        }
        new StAXEventConnector(reader,h).bridge();
        return h.getContext().getResult();
    } catch (XMLStreamException e) {
        throw handleStreamException(e);
    }
}
 
Example 8
Source File: UnmarshallerImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
        throw new IllegalArgumentException(
                Messages.format(Messages.NULL_READER));
    }

    try {
        XMLEvent event = reader.peek();

        if (!event.isStartElement() && !event.isStartDocument()) {
            // TODO: convert event into event name
            throw new IllegalStateException(
                Messages.format(
                    Messages.ILLEGAL_READER_STATE,event.getEventType()));
        }

        // Quick hack until SJSXP fixes 6270116
        boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
        XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
        if(!isZephyr) {
            h = new InterningXmlVisitor(h);
        }
        new StAXEventConnector(reader,h).bridge();
        return h.getContext().getResult();
    } catch (XMLStreamException e) {
        throw handleStreamException(e);
    }
}
 
Example 9
Source File: StaxEventXMLReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Constructs a new instance of the {@code StaxEventXmlReader} that reads from
 * the given {@code XMLEventReader}. The supplied event reader must be in
 * {@code XMLStreamConstants.START_DOCUMENT} or {@code XMLStreamConstants.START_ELEMENT} state.
 * @param reader the {@code XMLEventReader} to read from
 * @throws IllegalStateException if the reader is not at the start of a document or element
 */
StaxEventXMLReader(XMLEventReader reader) {
	try {
		XMLEvent event = reader.peek();
		if (event != null && !(event.isStartDocument() || event.isStartElement())) {
			throw new IllegalStateException("XMLEventReader not at start of document or element");
		}
	}
	catch (XMLStreamException ex) {
		throw new IllegalStateException("Could not read first element: " + ex.getMessage());
	}
	this.reader = reader;
}
 
Example 10
Source File: UnmarshallerImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private Object unmarshal0(XMLEventReader reader,JaxBeanInfo expectedType) throws JAXBException {
    if (reader == null) {
        throw new IllegalArgumentException(
                Messages.format(Messages.NULL_READER));
    }

    try {
        XMLEvent event = reader.peek();

        if (!event.isStartElement() && !event.isStartDocument()) {
            // TODO: convert event into event name
            throw new IllegalStateException(
                Messages.format(
                    Messages.ILLEGAL_READER_STATE,event.getEventType()));
        }

        // Quick hack until SJSXP fixes 6270116
        boolean isZephyr = reader.getClass().getName().equals("com.sun.xml.internal.stream.XMLReaderImpl");
        XmlVisitor h = createUnmarshallerHandler(null,false,expectedType);
        if(!isZephyr) {
            h = new InterningXmlVisitor(h);
        }
        new StAXEventConnector(reader,h).bridge();
        return h.getContext().getResult();
    } catch (XMLStreamException e) {
        throw handleStreamException(e);
    }
}
 
Example 11
Source File: XmlMerger.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, Object object, List out) throws Exception {
    try {
        if (object instanceof  XMLEvent) {
            final XMLEvent event = (XMLEvent) object;

            if (event.isStartDocument() || event.isEndDocument()) {
                return;
            }

            if (event.isCharacters() && depth <= 1) {
                return;
            }

            if (depth < 1 && event.isStartElement()) {
                out.add(object);
                depth++;
                return;
            }

            if (depth <= 1 && event.isEndElement()) {
                out.add(object);
                depth--;
                return;
            }

            writer.add(event);

            if (event.isStartElement()) {
                depth++;
            } else if (event.isEndElement()) {
                depth--;

                if (depth == 1) {
                    writer.flush();
                    org.dom4j.Element xmlElement = transform().getRootElement();
                    out.add(xmlElement);
                    writer.close();
                    resetWriter();
                }
            }
        }
    } catch (Exception e) {
        logger.info(e.getCause().getMessage());
        throw e;
    }
}
 
Example 12
Source File: StaxEventXMLReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
	boolean documentStarted = false;
	boolean documentEnded = false;
	int elementDepth = 0;
	while (this.reader.hasNext() && elementDepth >= 0) {
		XMLEvent event = this.reader.nextEvent();
		if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
			handleStartDocument(event);
			documentStarted = true;
		}
		switch (event.getEventType()) {
			case XMLStreamConstants.START_DOCUMENT:
				handleStartDocument(event);
				documentStarted = true;
				break;
			case XMLStreamConstants.START_ELEMENT:
				elementDepth++;
				handleStartElement(event.asStartElement());
				break;
			case XMLStreamConstants.END_ELEMENT:
				elementDepth--;
				if (elementDepth >= 0) {
					handleEndElement(event.asEndElement());
				}
				break;
			case XMLStreamConstants.PROCESSING_INSTRUCTION:
				handleProcessingInstruction((ProcessingInstruction) event);
				break;
			case XMLStreamConstants.CHARACTERS:
			case XMLStreamConstants.SPACE:
			case XMLStreamConstants.CDATA:
				handleCharacters(event.asCharacters());
				break;
			case XMLStreamConstants.END_DOCUMENT:
				handleEndDocument();
				documentEnded = true;
				break;
			case XMLStreamConstants.NOTATION_DECLARATION:
				handleNotationDeclaration((NotationDeclaration) event);
				break;
			case XMLStreamConstants.ENTITY_DECLARATION:
				handleEntityDeclaration((EntityDeclaration) event);
				break;
			case XMLStreamConstants.COMMENT:
				handleComment((Comment) event);
				break;
			case XMLStreamConstants.DTD:
				handleDtd((DTD) event);
				break;
			case XMLStreamConstants.ENTITY_REFERENCE:
				handleEntityReference((EntityReference) event);
				break;
		}
	}
	if (documentStarted && !documentEnded) {
		handleEndDocument();
	}

}
 
Example 13
Source File: DisplayPluginUpdatesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a set of Strings which correspond to the plugin coordinates where there is a version specified.
 *
 * @param pomContents The project to get the plugins with versions specified.
 * @param path Path that points to the source of the XML
 * @return a set of Strings which correspond to the plugin coordinates where there is a version specified.
 */
private Set<String> findPluginsWithVersionsSpecified( StringBuilder pomContents, String path )
    throws IOException, XMLStreamException
{
    Set<String> result = new HashSet<>();
    ModifiedPomXMLEventReader pom = newModifiedPomXER( pomContents, path );

    Pattern pathRegex = Pattern.compile( "/project(/profiles/profile)?"
        + "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin" );
    Stack<StackState> pathStack = new Stack<StackState>();
    StackState curState = null;
    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();
        if ( event.isStartDocument() )
        {
            curState = new StackState( "" );
            pathStack.clear();
        }
        else if ( event.isStartElement() )
        {
            String elementName = event.asStartElement().getName().getLocalPart();
            if ( curState != null && pathRegex.matcher( curState.path ).matches() )
            {
                if ( "groupId".equals( elementName ) )
                {
                    curState.groupId = pom.getElementText().trim();
                    continue;
                }
                else if ( "artifactId".equals( elementName ) )
                {
                    curState.artifactId = pom.getElementText().trim();
                    continue;

                }
                else if ( "version".equals( elementName ) )
                {
                    curState.version = pom.getElementText().trim();
                    continue;
                }
            }

            pathStack.push( curState );
            curState = new StackState( curState.path + "/" + elementName );
        }
        else if ( event.isEndElement() )
        {
            if ( curState != null && pathRegex.matcher( curState.path ).matches() )
            {
                if ( curState.artifactId != null && curState.version != null )
                {
                    if ( curState.groupId == null )
                    {
                        curState.groupId = PomHelper.APACHE_MAVEN_PLUGINS_GROUPID;
                    }
                    result.add( curState.groupId + ":" + curState.artifactId );
                }
            }
            curState = pathStack.pop();
        }
    }

    return result;

}
 
Example 14
Source File: StaxEventXMLReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void handleStartDocument(final XMLEvent event) throws SAXException {
	if (event.isStartDocument()) {
		StartDocument startDocument = (StartDocument) event;
		String xmlVersion = startDocument.getVersion();
		if (StringUtils.hasLength(xmlVersion)) {
			this.xmlVersion = xmlVersion;
		}
		if (startDocument.encodingSet()) {
			this.encoding = startDocument.getCharacterEncodingScheme();
		}
	}
	if (getContentHandler() != null) {
		final Location location = event.getLocation();
		getContentHandler().setDocumentLocator(new Locator2() {
			@Override
			public int getColumnNumber() {
				return (location != null ? location.getColumnNumber() : -1);
			}
			@Override
			public int getLineNumber() {
				return (location != null ? location.getLineNumber() : -1);
			}
			@Override
			public String getPublicId() {
				return (location != null ? location.getPublicId() : null);
			}
			@Override
			public String getSystemId() {
				return (location != null ? location.getSystemId() : null);
			}
			@Override
			public String getXMLVersion() {
				return xmlVersion;
			}
			@Override
			public String getEncoding() {
				return encoding;
			}
		});
		getContentHandler().startDocument();
	}
}
 
Example 15
Source File: StaxEventXMLReader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
	boolean documentStarted = false;
	boolean documentEnded = false;
	int elementDepth = 0;
	while (this.reader.hasNext() && elementDepth >= 0) {
		XMLEvent event = this.reader.nextEvent();
		if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
			handleStartDocument(event);
			documentStarted = true;
		}
		switch (event.getEventType()) {
			case XMLStreamConstants.START_DOCUMENT:
				handleStartDocument(event);
				documentStarted = true;
				break;
			case XMLStreamConstants.START_ELEMENT:
				elementDepth++;
				handleStartElement(event.asStartElement());
				break;
			case XMLStreamConstants.END_ELEMENT:
				elementDepth--;
				if (elementDepth >= 0) {
					handleEndElement(event.asEndElement());
				}
				break;
			case XMLStreamConstants.PROCESSING_INSTRUCTION:
				handleProcessingInstruction((ProcessingInstruction) event);
				break;
			case XMLStreamConstants.CHARACTERS:
			case XMLStreamConstants.SPACE:
			case XMLStreamConstants.CDATA:
				handleCharacters(event.asCharacters());
				break;
			case XMLStreamConstants.END_DOCUMENT:
				handleEndDocument();
				documentEnded = true;
				break;
			case XMLStreamConstants.NOTATION_DECLARATION:
				handleNotationDeclaration((NotationDeclaration) event);
				break;
			case XMLStreamConstants.ENTITY_DECLARATION:
				handleEntityDeclaration((EntityDeclaration) event);
				break;
			case XMLStreamConstants.COMMENT:
				handleComment((Comment) event);
				break;
			case XMLStreamConstants.DTD:
				handleDtd((DTD) event);
				break;
			case XMLStreamConstants.ENTITY_REFERENCE:
				handleEntityReference((EntityReference) event);
				break;
		}
	}
	if (documentStarted && !documentEnded) {
		handleEndDocument();
	}

}
 
Example 16
Source File: StaxEventXMLReader.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void handleStartDocument(final XMLEvent event) throws SAXException {
	if (event.isStartDocument()) {
		StartDocument startDocument = (StartDocument) event;
		String xmlVersion = startDocument.getVersion();
		if (StringUtils.hasLength(xmlVersion)) {
			this.xmlVersion = xmlVersion;
		}
		if (startDocument.encodingSet()) {
			this.encoding = startDocument.getCharacterEncodingScheme();
		}
	}
	if (getContentHandler() != null) {
		final Location location = event.getLocation();
		getContentHandler().setDocumentLocator(new Locator2() {
			@Override
			public int getColumnNumber() {
				return (location != null ? location.getColumnNumber() : -1);
			}
			@Override
			public int getLineNumber() {
				return (location != null ? location.getLineNumber() : -1);
			}
			@Override
			public String getPublicId() {
				return (location != null ? location.getPublicId() : null);
			}
			@Override
			public String getSystemId() {
				return (location != null ? location.getSystemId() : null);
			}
			@Override
			public String getXMLVersion() {
				return xmlVersion;
			}
			@Override
			public String getEncoding() {
				return encoding;
			}
		});
		getContentHandler().startDocument();
	}
}
 
Example 17
Source File: AbstractXMLEventWriter.java    From jettison with Apache License 2.0 4 votes vote down vote up
public void add(XMLEvent event) throws XMLStreamException {
	if (event.isStartDocument()) {
		streamWriter.writeStartDocument();
	} else if (event.isStartElement()) {
		StartElement element = event.asStartElement();
		QName elQName = element.getName();
		if (elQName.getPrefix().length() > 0
				&& elQName.getNamespaceURI().length() > 0)
			streamWriter.writeStartElement(elQName.getPrefix(), elQName
					.getLocalPart(), elQName.getNamespaceURI());
		else if (elQName.getNamespaceURI().length() > 0)
			streamWriter.writeStartElement(elQName.getNamespaceURI(),
					elQName.getLocalPart());
		else
			streamWriter.writeStartElement(elQName.getLocalPart());

		// Add element namespaces
		Iterator namespaces = element.getNamespaces();
		while (namespaces.hasNext()) {
			Namespace ns = (Namespace) namespaces.next();
			String prefix = ns.getPrefix();
			String nsURI = ns.getNamespaceURI();
			streamWriter.writeNamespace(prefix, nsURI);
		}

		// Add element attributes
		Iterator attris = element.getAttributes();
		while (attris.hasNext()) {
			Attribute attr = (Attribute) attris.next();
			QName atQName = attr.getName();
			String value = attr.getValue();
			if (atQName.getPrefix().length() > 0
					&& atQName.getNamespaceURI().length() > 0)
				streamWriter.writeAttribute(atQName.getPrefix(), atQName
						.getNamespaceURI(), atQName.getLocalPart(), value);
			else if (atQName.getNamespaceURI().length() > 0)
				streamWriter.writeAttribute(atQName.getNamespaceURI(),
						atQName.getLocalPart(), value);
			else
				streamWriter.writeAttribute(atQName.getLocalPart(), value);
		}
	} else if (event.isCharacters()) {
		Characters chars = event.asCharacters();
		streamWriter.writeCharacters(chars.getData());
	} else if (event.isEndElement()) {
		streamWriter.writeEndElement();
	} else if (event.isEndDocument()) {
		streamWriter.writeEndDocument();
	} else {
		throw new XMLStreamException("Unsupported event type: " + event);
	}
}
 
Example 18
Source File: StaxEventXMLReader.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
	boolean documentStarted = false;
	boolean documentEnded = false;
	int elementDepth = 0;
	while (this.reader.hasNext() && elementDepth >= 0) {
		XMLEvent event = this.reader.nextEvent();
		if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
			handleStartDocument(event);
			documentStarted = true;
		}
		switch (event.getEventType()) {
			case XMLStreamConstants.START_DOCUMENT:
				handleStartDocument(event);
				documentStarted = true;
				break;
			case XMLStreamConstants.START_ELEMENT:
				elementDepth++;
				handleStartElement(event.asStartElement());
				break;
			case XMLStreamConstants.END_ELEMENT:
				elementDepth--;
				if (elementDepth >= 0) {
					handleEndElement(event.asEndElement());
				}
				break;
			case XMLStreamConstants.PROCESSING_INSTRUCTION:
				handleProcessingInstruction((ProcessingInstruction) event);
				break;
			case XMLStreamConstants.CHARACTERS:
			case XMLStreamConstants.SPACE:
			case XMLStreamConstants.CDATA:
				handleCharacters(event.asCharacters());
				break;
			case XMLStreamConstants.END_DOCUMENT:
				handleEndDocument();
				documentEnded = true;
				break;
			case XMLStreamConstants.NOTATION_DECLARATION:
				handleNotationDeclaration((NotationDeclaration) event);
				break;
			case XMLStreamConstants.ENTITY_DECLARATION:
				handleEntityDeclaration((EntityDeclaration) event);
				break;
			case XMLStreamConstants.COMMENT:
				handleComment((Comment) event);
				break;
			case XMLStreamConstants.DTD:
				handleDtd((DTD) event);
				break;
			case XMLStreamConstants.ENTITY_REFERENCE:
				handleEntityReference((EntityReference) event);
				break;
		}
	}
	if (documentStarted && !documentEnded) {
		handleEndDocument();
	}

}
 
Example 19
Source File: StaxEventXMLReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private void handleStartDocument(final XMLEvent event) throws SAXException {
	if (event.isStartDocument()) {
		StartDocument startDocument = (StartDocument) event;
		String xmlVersion = startDocument.getVersion();
		if (StringUtils.hasLength(xmlVersion)) {
			this.xmlVersion = xmlVersion;
		}
		if (startDocument.encodingSet()) {
			this.encoding = startDocument.getCharacterEncodingScheme();
		}
	}
	if (getContentHandler() != null) {
		final Location location = event.getLocation();
		getContentHandler().setDocumentLocator(new Locator2() {
			@Override
			public int getColumnNumber() {
				return (location != null ? location.getColumnNumber() : -1);
			}
			@Override
			public int getLineNumber() {
				return (location != null ? location.getLineNumber() : -1);
			}
			@Override
			@Nullable
			public String getPublicId() {
				return (location != null ? location.getPublicId() : null);
			}
			@Override
			@Nullable
			public String getSystemId() {
				return (location != null ? location.getSystemId() : null);
			}
			@Override
			public String getXMLVersion() {
				return xmlVersion;
			}
			@Override
			@Nullable
			public String getEncoding() {
				return encoding;
			}
		});
		getContentHandler().startDocument();
	}
}
 
Example 20
Source File: StaxEventXMLReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected void parseInternal() throws SAXException, XMLStreamException {
	boolean documentStarted = false;
	boolean documentEnded = false;
	int elementDepth = 0;
	while (this.reader.hasNext() && elementDepth >= 0) {
		XMLEvent event = this.reader.nextEvent();
		if (!event.isStartDocument() && !event.isEndDocument() && !documentStarted) {
			handleStartDocument(event);
			documentStarted = true;
		}
		switch (event.getEventType()) {
			case XMLStreamConstants.START_DOCUMENT:
				handleStartDocument(event);
				documentStarted = true;
				break;
			case XMLStreamConstants.START_ELEMENT:
				elementDepth++;
				handleStartElement(event.asStartElement());
				break;
			case XMLStreamConstants.END_ELEMENT:
				elementDepth--;
				if (elementDepth >= 0) {
					handleEndElement(event.asEndElement());
				}
				break;
			case XMLStreamConstants.PROCESSING_INSTRUCTION:
				handleProcessingInstruction((ProcessingInstruction) event);
				break;
			case XMLStreamConstants.CHARACTERS:
			case XMLStreamConstants.SPACE:
			case XMLStreamConstants.CDATA:
				handleCharacters(event.asCharacters());
				break;
			case XMLStreamConstants.END_DOCUMENT:
				handleEndDocument();
				documentEnded = true;
				break;
			case XMLStreamConstants.NOTATION_DECLARATION:
				handleNotationDeclaration((NotationDeclaration) event);
				break;
			case XMLStreamConstants.ENTITY_DECLARATION:
				handleEntityDeclaration((EntityDeclaration) event);
				break;
			case XMLStreamConstants.COMMENT:
				handleComment((Comment) event);
				break;
			case XMLStreamConstants.DTD:
				handleDtd((DTD) event);
				break;
			case XMLStreamConstants.ENTITY_REFERENCE:
				handleEntityReference((EntityReference) event);
				break;
		}
	}
	if (documentStarted && !documentEnded) {
		handleEndDocument();
	}

}