Skip to content

Commit b00258b

Browse files
committed
Fixes #101 - possible problem with some StAX streams supplying SPACE content outside the root element. Not able to reproduce with my StAX libraries, but the fix is 'safe'.
1 parent 453cb9a commit b00258b

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

core/src/java/org/jdom2/input/StAXEventBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ private static final Document process(final JDOMFactory factory,
162162
current.addContent(emt);
163163
}
164164
current = emt;
165-
} else if (event.isCharacters()) {
165+
} else if (event.isCharacters() && current != null) {
166+
// ignore any character-based content (should only be spaces)
167+
// outside of the root element.
166168
final Characters chars = event.asCharacters();
167169
if (chars.isCData()) {
168170
current.addContent(factory.cdata(

core/src/java/org/jdom2/input/StAXStreamBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
7171
import org.jdom2.JDOMException;
7272
import org.jdom2.JDOMFactory;
7373
import org.jdom2.Namespace;
74+
import org.jdom2.Verifier;
7475
import org.jdom2.input.stax.DTDParser;
7576
import org.jdom2.input.stax.StAXFilter;
7677

@@ -160,9 +161,16 @@ private static final Document process(final JDOMFactory factory,
160161
case CDATA:
161162
throw new JDOMException("Unexpected XMLStream event at Document level: CDATA");
162163
case SPACE:
163-
throw new JDOMException("Unexpected XMLStream event at Document level: SPACE");
164+
// I have not been able to identify a StAX Stream handler that produces
165+
// space data outside the root element, but just in case, we ignore it.
166+
break; //throw new JDOMException("Unexpected XMLStream event at Document level: SPACE");
164167
case CHARACTERS:
165-
throw new JDOMException("Unexpected XMLStream event at Document level: CHARACTERS");
168+
final String badtxt = stream.getText();
169+
if (!Verifier.isAllXMLWhitespace(badtxt)) {
170+
throw new JDOMException("Unexpected XMLStream event at Document level: CHARACTERS (" + badtxt + ")");
171+
}
172+
// otherwise ignore the chars.
173+
break;
166174

167175
case COMMENT:
168176
document.addContent(

0 commit comments

Comments
 (0)