From e7d0adbb0184bcbf78a0abb87d00d2131f031054 Mon Sep 17 00:00:00 2001 From: Josiah Gaskin Date: Thu, 8 Aug 2013 15:33:38 -0700 Subject: SAX parsing uses UTF-8 format This change sets the encoding scheme for the byte array passed to the SAX parser for Freemaker template files. Should solve https://code.google.com/p/android/issues/detail?id=58746 Change-Id: I7ae2183733f63b976184df60c638bdfbbec27559 --- .../src/com/android/tools/idea/templates/Template.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/android/src/com/android/tools/idea/templates/Template.java b/android/src/com/android/tools/idea/templates/Template.java index f68d6df8ecc..5c45a07e3ec 100755 --- a/android/src/com/android/tools/idea/templates/Template.java +++ b/android/src/com/android/tools/idea/templates/Template.java @@ -52,6 +52,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.w3c.dom.*; import org.xml.sax.Attributes; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; @@ -306,7 +307,12 @@ public class Template { xml = processFreemarkerTemplate(freemarker, paramMap, path); } - SAXParserFactory.newInstance().newSAXParser().parse(new ByteArrayInputStream(xml.getBytes()), new DefaultHandler() { + // Handle UTF-8 since processed file may contain file paths + ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8.toString())); + Reader reader = new InputStreamReader(inputStream, Charsets.UTF_8.toString()); + InputSource inputSource = new InputSource(reader); + inputSource.setEncoding(Charsets.UTF_8.toString()); + SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new DefaultHandler() { @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { if (TAG_PARAMETER.equals(name)) { @@ -433,8 +439,13 @@ public class Template { myLoader.setTemplateFile(getTemplateFile(file)); String xml = processFreemarkerTemplate(freemarker, paramMap, file); - // Parse and execute the resulting instruction list. - SAXParserFactory.newInstance().newSAXParser().parse(new ByteArrayInputStream(xml.getBytes()), new DefaultHandler() { + // Parse and execute the resulting instruction list. We handle UTF-8 since the processed file contains paths which may + // have UTF-8 characters. + ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8.toString())); + Reader reader = new InputStreamReader(inputStream, Charsets.UTF_8.toString()); + InputSource inputSource = new InputSource(reader); + inputSource.setEncoding(Charsets.UTF_8.toString()); + SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new DefaultHandler() { @Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { try { @@ -487,7 +498,6 @@ public class Template { } } }); - } catch (Exception e) { ourMostRecentException = e; LOG.warn(e); -- cgit v1.2.3