aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraefimov <none@none>2016-05-13 18:34:27 +0300
committeraefimov <none@none>2016-05-13 18:34:27 +0300
commit237f439f5a6ed68f8853a49a41f4a7229548e2e4 (patch)
tree97d6b1d60b38e5f4a7dd71708193d9756ba64b78
parent35c873855daed1b1499511bd688756b0c1e3bc49 (diff)
downloadjdk8u_jaxp-237f439f5a6ed68f8853a49a41f4a7229548e2e4.tar.gz
8145974: XMLStreamWriter produces invalid XML for surrogate pairs on OutputStreamWriter
Reviewed-by: joehw
-rw-r--r--src/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java36
1 files changed, 27 insertions, 9 deletions
diff --git a/src/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java b/src/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java
index 8262111..f3b9b50 100644
--- a/src/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java
+++ b/src/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -1346,6 +1346,15 @@ public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamW
}
/**
+ * Writes character reference in hex format.
+ */
+ private void writeCharRef(int codePoint) throws IOException {
+ fWriter.write( "&#x" );
+ fWriter.write( Integer.toHexString(codePoint) );
+ fWriter.write( ';' );
+ }
+
+ /**
* Writes XML content to underlying writer. Escapes characters unless
* escaping character feature is turned off.
*/
@@ -1368,10 +1377,14 @@ public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamW
if (fEncoder != null && !fEncoder.canEncode(ch)){
fWriter.write(content, startWritePos, index - startWritePos );
- // Escape this char as underlying encoder cannot handle it
- fWriter.write( "&#x" );
- fWriter.write(Integer.toHexString(ch));
- fWriter.write( ';' );
+ // Check if current and next characters forms a surrogate pair
+ // and escape it to avoid generation of invalid xml content
+ if ( index != end - 1 && Character.isSurrogatePair(ch, content[index+1])) {
+ writeCharRef(Character.toCodePoint(ch, content[index+1]));
+ index++;
+ } else {
+ writeCharRef(ch);
+ }
startWritePos = index + 1;
continue;
}
@@ -1439,10 +1452,15 @@ public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamW
if (fEncoder != null && !fEncoder.canEncode(ch)){
fWriter.write(content, startWritePos, index - startWritePos );
- // Escape this char as underlying encoder cannot handle it
- fWriter.write( "&#x" );
- fWriter.write(Integer.toHexString(ch));
- fWriter.write( ';' );
+ // Check if current and next characters forms a surrogate pair
+ // and escape it to avoid generation of invalid xml content
+ if ( index != end - 1 && Character.isSurrogatePair(ch, content.charAt(index+1))) {
+ writeCharRef(Character.toCodePoint(ch, content.charAt(index+1)));
+ index++;
+ } else {
+ writeCharRef(ch);
+ }
+
startWritePos = index + 1;
continue;
}