Skip to content

Change default encoding in XML.save to UTF-8 #122

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 4 commits into from
May 25, 2017
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
23 changes: 23 additions & 0 deletions jvm/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,29 @@ class XMLTestJVM {
</wsdl:definitions>""", wsdlTemplate4("service4", () => "target4") toString)
}

// Issue found with ISO-8859-1 in #121 that was fixed with UTF-8 default
@UnitTest
def writeReadNoDeclarationDefaultEncoding: Unit = {
val chars = ((32 to 126) ++ (160 to 255)).map(_.toChar)
val xml = <x>{ chars.mkString }</x>

// com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException:
// Invalid byte 1 of 1-byte UTF-8 sequence.
// scala.xml.XML.save("foo.xml", xml)
// scala.xml.XML.loadFile("foo.xml").toString)

val outputStream = new java.io.ByteArrayOutputStream
val streamWriter = new java.io.OutputStreamWriter(outputStream, XML.encoding)

XML.write(streamWriter, xml, XML.encoding, false, null)
streamWriter.flush

val inputStream = new java.io.ByteArrayInputStream(outputStream.toByteArray)
val streamReader = new java.io.InputStreamReader(inputStream)

assertEquals(xml.toString, XML.load(streamReader).toString)
}

@UnitTest
def t0663 = {
val src = scala.io.Source.fromString("<?xml version='1.0' encoding='UTF-8'?><feed/>")
Expand Down
8 changes: 6 additions & 2 deletions shared/src/main/scala/scala/xml/XML.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object XML extends XMLLoader[Elem] {
val preserve = "preserve"
val space = "space"
val lang = "lang"
val encoding = "ISO-8859-1"
val encoding = "UTF-8"

/** Returns an XMLLoader whose load* methods will use the supplied SAXParser. */
def withSAXParser(p: SAXParser): XMLLoader[Elem] =
Expand All @@ -73,6 +73,10 @@ object XML extends XMLLoader[Elem] {
* Saves a node to a file with given filename using given encoding
* optionally with xmldecl and doctype declaration.
*
* Note: default encoding was ISO-8859-1 (latin1) in pre-1.0.7 scala-xml versions.
* If your code depends on characters in non-ASCII latin1 range, specify
* ISO-8859-1 encoding explicitly.
*
* @param filename the filename
* @param node the xml node we want to write
* @param enc encoding to use
Expand All @@ -82,7 +86,7 @@ object XML extends XMLLoader[Elem] {
final def save(
filename: String,
node: Node,
enc: String = encoding,
enc: String = "UTF-8",
xmlDecl: Boolean = false,
doctype: dtd.DocType = null): Unit =
{
Expand Down