final XML ecmaToXml(Object object) {
   //    See ECMA357 10.3
   if (object == null || object == Undefined.instance) {
     throw ScriptRuntime.typeError("Cannot convert " + object + " to XML");
   }
   if (object instanceof XML) return (XML) object;
   if (object instanceof XMLList) {
     XMLList list = (XMLList) object;
     if (list.getXML() != null) {
       return list.getXML();
     } else {
       throw ScriptRuntime.typeError("Cannot convert list of >1 element to XML");
     }
   }
   //    TODO    Technically we should fail on anything except a String, Number or Boolean
   //            See ECMA357 10.3
   // Extension: if object is a DOM node, use that to construct the XML
   // object.
   if (object instanceof Wrapper) {
     object = ((Wrapper) object).unwrap();
   }
   if (object instanceof org.w3c.dom.Node) {
     org.w3c.dom.Node node = (org.w3c.dom.Node) object;
     return newXML(XmlNode.createElementFromNode(node));
   }
   //    Instead we just blindly cast to a String and let them convert anything.
   String s = ScriptRuntime.toString(object);
   //    TODO    Could this get any uglier?
   if (s.length() > 0 && s.charAt(0) == '<') {
     return parse(s);
   } else {
     return newXML(XmlNode.createText(options, s));
   }
 }
 private XML parse(String frag) {
   try {
     return newXML(
         XmlNode.createElement(
             options, getDefaultNamespaceURI(Context.getCurrentContext()), frag));
   } catch (SAXException e) {
     throw ScriptRuntime.typeError("Cannot parse XML: " + e.getMessage());
   }
 }
  private void exportToScope(boolean sealed) {
    xmlPrototype = newXML(XmlNode.createText(options, ""));
    xmlListPrototype = newXMLList();
    namespacePrototype = Namespace.create(this.globalScope, null, XmlNode.Namespace.GLOBAL);
    qnamePrototype =
        QName.create(
            this, this.globalScope, null, XmlNode.QName.create(XmlNode.Namespace.create(""), ""));

    xmlPrototype.exportAsJSClass(sealed);
    xmlListPrototype.exportAsJSClass(sealed);
    namespacePrototype.exportAsJSClass(sealed);
    qnamePrototype.exportAsJSClass(sealed);
  }
  /* TODO: Can this can be replaced by ecmaToXml below?
   */
  final XML newXMLFromJs(Object inputObject) {
    String frag;

    if (inputObject == null || inputObject == Undefined.instance) {
      frag = "";
    } else if (inputObject instanceof XMLObjectImpl) {
      // todo: faster way for XMLObjects?
      frag = ((XMLObjectImpl) inputObject).toXMLString();
    } else {
      frag = ScriptRuntime.toString(inputObject);
    }

    if (frag.trim().startsWith("<>")) {
      throw ScriptRuntime.typeError("Invalid use of XML object anonymous tags <></>.");
    }

    if (frag.indexOf("<") == -1) {
      //    Solo text node
      return newXML(XmlNode.createText(options, frag));
    }
    return parse(frag);
  }
 final XML newTextElementXML(XmlNode reference, XmlNode.QName qname, String value) {
   return newXML(XmlNode.newElementWithText(options, reference, qname, value));
 }