Skip to content

StaxStreamXMLReader ignores significant whitespace #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -217,11 +217,6 @@ private void handleEndElement() throws SAXException {
}

private void handleCharacters() throws SAXException {
if (getContentHandler() != null && this.reader.isWhiteSpace()) {
getContentHandler().ignorableWhitespace(this.reader.getTextCharacters(),
this.reader.getTextStart(), this.reader.getTextLength());
return;
}
if (XMLStreamConstants.CDATA == this.reader.getEventType() && getLexicalHandler() != null) {
getLexicalHandler().startCDATA();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@

package org.springframework.util.xml;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXSource;

import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.BDDMockito.*;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
Expand Down Expand Up @@ -103,6 +110,25 @@ public void contentHandlerNoNamespacesPrefixes() throws Exception {
verifyIdenticalInvocations(standardContentHandler, contentHandler);
}

@Test
public void whitespace() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><node1> </node1><node2> Some text </node2></test>";

Transformer transformer = TransformerFactory.newInstance().newTransformer();

AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(
new ByteArrayInputStream(xml.getBytes("UTF-8")));

SAXSource source = new SAXSource(staxXmlReader, new InputSource());
DOMResult result = new DOMResult();

transformer.transform(source, result);

Node node1 = result.getNode().getFirstChild().getFirstChild();
assertEquals(" ", node1.getTextContent());
assertEquals(" Some text ", node1.getNextSibling().getTextContent());
}

@Test
public void lexicalHandler() throws Exception {
Resource testLexicalHandlerXml = new ClassPathResource("testLexicalHandler.xml", getClass());
Expand Down Expand Up @@ -130,7 +156,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
verifyIdenticalInvocations(expectedLexicalHandler, actualLexicalHandler);
}

private final LexicalHandler mockLexicalHandler() throws Exception {
private LexicalHandler mockLexicalHandler() throws Exception {
LexicalHandler lexicalHandler = mock(LexicalHandler.class);
willAnswer(new CopyCharsAnswer()).given(lexicalHandler).comment(any(char[].class), anyInt(), anyInt());
return lexicalHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h:hello xmlns:h="http://www.greeting.com/hello/" id="a1" h:person="David"><prefix:goodbye
xmlns:prefix="http://www.greeting.com/goodbye/" h:person="Arjen"/></h:hello>
xmlns:prefix="http://www.greeting.com/goodbye/" h:person="Arjen"> Some text </prefix:goodbye><h:so-long> </h:so-long></h:hello>