SDK Updater: fix repository.xml to read as binary, not text

to let the XML decoder handle the charset decoding.
This commit is contained in:
Raphael
2009-06-17 13:35:39 -07:00
parent f6030454c8
commit 340a79b590

View File

@@ -23,11 +23,9 @@ import org.w3c.dom.Node;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import java.io.BufferedReader; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
@@ -108,7 +106,7 @@ public class RepoSource implements IDescription {
monitor.setDescription("Fetching %1$s", url); monitor.setDescription("Fetching %1$s", url);
monitor.incProgress(1); monitor.incProgress(1);
String xml = fetchUrl(url, monitor); ByteArrayInputStream xml = fetchUrl(url, monitor);
if (xml == null) { if (xml == null) {
mDescription += String.format("\nFailed to fetch URL %1$s", url); mDescription += String.format("\nFailed to fetch URL %1$s", url);
@@ -155,34 +153,33 @@ public class RepoSource implements IDescription {
* Java URL Reader: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html * Java URL Reader: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
* Java set Proxy: http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html * Java set Proxy: http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html
*/ */
private String fetchUrl(String urlString, ITaskMonitor monitor) { private ByteArrayInputStream fetchUrl(String urlString, ITaskMonitor monitor) {
URL url; URL url;
try { try {
url = new URL(urlString); url = new URL(urlString);
StringBuilder xml = new StringBuilder();
InputStream is = null; InputStream is = null;
BufferedReader br = null;
int inc = 65536;
int curr = 0;
byte[] result = new byte[inc];
try { try {
is = url.openStream(); is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
String line; int n;
while ((line = br.readLine()) != null) { while ((n = is.read(result, curr, result.length - curr)) != -1) {
xml.append(line); curr += n;
if (curr == result.length) {
byte[] temp = new byte[curr + inc];
System.arraycopy(result, 0, temp, 0, curr);
result = temp;
}
} }
return xml.toString(); return new ByteArrayInputStream(result, 0, curr);
} finally { } finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// pass
}
}
if (is != null) { if (is != null) {
try { try {
is.close(); is.close();
@@ -203,11 +200,12 @@ public class RepoSource implements IDescription {
* Validates this XML against the SDK Repository schema. * Validates this XML against the SDK Repository schema.
* Returns true if the XML was correctly validated. * Returns true if the XML was correctly validated.
*/ */
private boolean validateXml(String xml, ITaskMonitor monitor) { private boolean validateXml(ByteArrayInputStream xml, ITaskMonitor monitor) {
try { try {
Validator validator = getValidator(); Validator validator = getValidator();
validator.validate(new StreamSource(new StringReader(xml))); xml.reset();
validator.validate(new StreamSource(xml));
return true; return true;
} catch (SAXException e) { } catch (SAXException e) {
@@ -240,7 +238,7 @@ public class RepoSource implements IDescription {
* Parse all packages defined in the SDK Repository XML and creates * Parse all packages defined in the SDK Repository XML and creates
* a new mPackages array with them. * a new mPackages array with them.
*/ */
private boolean parsePackages(String xml, ITaskMonitor monitor) { private boolean parsePackages(ByteArrayInputStream xml, ITaskMonitor monitor) {
try { try {
Document doc = getDocument(xml); Document doc = getDocument(xml);
@@ -321,14 +319,15 @@ public class RepoSource implements IDescription {
/** /**
* Takes an XML document as a string as parameter and returns a DOM for it. * Takes an XML document as a string as parameter and returns a DOM for it.
*/ */
private Document getDocument(String xml) private Document getDocument(ByteArrayInputStream xml)
throws ParserConfigurationException, SAXException, IOException { throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true); factory.setIgnoringComments(true);
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml))); xml.reset();
Document doc = builder.parse(new InputSource(xml));
return doc; return doc;
} }