コード例 #1
0
 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));
   }
 }