FileDocCategorySizeDatePackage
VersionExtractor.javaAPI DocAndroid 1.5 API5217Wed May 06 22:41:56 BST 2009android.webkit.gears

VersionExtractor

public 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
TAG
Logging tag
private static final String
VERSION
XML element names.
private static final String
URL
Constructors Summary
Methods Summary
public static booleanextract(java.lang.String xml, long nativeObject)
Parses the input xml string and invokes the native setVersionAndUrl method.

param
xml is the XML string to parse.
return
true if the extraction is successful and false otherwise.


                                  
         
    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.StringextractText(org.w3c.dom.Document doc, java.lang.String elementName)
Extracts the text content of the first element with the given name.

param
doc is the Document where the element is searched for.
param
elementName is name of the element to searched for.
return
the text content of the element or null if no such element is found.

    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 voidsetVersionAndUrl(java.lang.String version, java.lang.String url, long nativeObject)
Native method used to send the version and url back to the C++ side.