Skip to content

Commit

Permalink
Merge "SAX parsing uses UTF-8 format"
Browse files Browse the repository at this point in the history
  • Loading branch information
android-studio authored and Gerrit Code Review committed Aug 8, 2013
2 parents b94088c e7d0adb commit c8a54a8
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions android/src/com/android/tools/idea/templates/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 52,7 @@
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;

Expand Down Expand Up @@ -306,7 307,12 @@ private void processFile(@NotNull final Configuration freemarker, @NotNull Strin
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)) {
Expand Down Expand Up @@ -433,8 439,13 @@ private void executeRecipeFile(@NotNull final Configuration freemarker, @NotNull
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 {
Expand Down Expand Up @@ -487,7 498,6 @@ else if (!name.equals("recipe")) {
}
}
});

} catch (Exception e) {
ourMostRecentException = e;
LOG.warn(e);
Expand Down

0 comments on commit c8a54a8

Please sign in to comment.