VersionExtractorpublic final class VersionExtractor extends Object A class that can extract the Gears version and upgrade URL from an
xml document. |
Fields Summary |
---|
private static final String | TAGLogging tag | private static final String | VERSIONXML element names. | private static final String | URL |
Methods Summary |
---|
public static boolean | extract(java.lang.String xml, long nativeObject)Parses the input xml string and invokes the native
setVersionAndUrl method.
try {
// Build the builders.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
// Create the document.
Document doc = builder.parse(new InputSource(new StringReader(xml)));
// Look for the version and url elements and get their text
// contents.
String version = extractText(doc, VERSION);
String url = extractText(doc, URL);
// If we have both, let the native side know.
if (version != null && url != null) {
setVersionAndUrl(version, url, nativeObject);
return true;
}
return false;
} catch (FactoryConfigurationError ex) {
Log.e(TAG, "Could not create the DocumentBuilderFactory " + ex);
} catch (ParserConfigurationException ex) {
Log.e(TAG, "Could not create the DocumentBuilder " + ex);
} catch (SAXException ex) {
Log.e(TAG, "Could not parse the xml " + ex);
} catch (IOException ex) {
Log.e(TAG, "Could not read the xml " + ex);
}
return false;
| private static java.lang.String | extractText(org.w3c.dom.Document doc, java.lang.String elementName)Extracts the text content of the first element with the given name.
String text = null;
NodeList node_list = doc.getElementsByTagName(elementName);
if (node_list.getLength() > 0) {
// We are only interested in the first node. Normally there
// should not be more than one anyway.
Node node = node_list.item(0);
// Iterate through the text children.
NodeList child_list = node.getChildNodes();
try {
for (int i = 0; i < child_list.getLength(); ++i) {
Node child = child_list.item(i);
if (child.getNodeType() == Node.TEXT_NODE) {
if (text == null) {
text = new String();
}
text += child.getNodeValue();
}
}
} catch (DOMException ex) {
Log.e(TAG, "getNodeValue() failed " + ex);
}
}
if (text != null) {
text = text.trim();
}
return text;
| private static native void | setVersionAndUrl(java.lang.String version, java.lang.String url, long nativeObject)Native method used to send the version and url back to the C++
side.
|
|