@qooxdoo/framework
Version:
The JS Framework for Coders
181 lines (149 loc) • 5.69 kB
JavaScript
/* ************************************************************************
qooxdoo - the new era of web development
http://qooxdoo.org
Copyright:
2004-2008 1&1 Internet AG, Germany, http://www.1und1.de
License:
MIT: https://opensource.org/licenses/MIT
See the LICENSE file in the project's top-level directory for details.
Authors:
* Sebastian Werner (wpbasti)
* Andreas Ecker (ecker)
* Fabian Jakobs (fjakobs)
************************************************************************ */
/**
* Cross browser XML document creation API
*
* The main purpose of this class is to allow you to create XML document objects in a
* cross-browser fashion. Use <code>create</code> to create an empty document,
* <code>fromString</code> to create one from an existing XML text. Both methods
* return a *native DOM object*. That means you use standard DOM methods on such
* an object (e.g. <code>createElement</code>).
*
* The following links provide further information on XML documents:
*
* * <a href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#i-Document">W3C Interface Specification</a>
* * <a href="http://msdn2.microsoft.com/en-us/library/ms535918.aspx">MS xml Object</a>
* * <a href="http://msdn2.microsoft.com/en-us/library/ms764622.aspx">MSXML GUIDs and ProgIDs</a>
* * <a href="https://developer.mozilla.org/en-US/docs/Parsing_and_serializing_XML">MDN Parsing and Serializing XML</a>
*/
qx.Bootstrap.define("qx.xml.Document",
{
statics :
{
/** @type {String} ActiveX class name of DOMDocument (IE specific) */
DOMDOC : null,
/** @type {String} ActiveX class name of XMLHttpRequest (IE specific) */
XMLHTTP : null,
/**
* Whether the given element is a XML document or element
* which is part of a XML document.
*
* @param elem {Document|Element} Any DOM Document or Element
* @return {Boolean} Whether the document is a XML document
*/
isXmlDocument : function(elem)
{
if (elem.nodeType === 9) {
return elem.documentElement.nodeName !== "HTML";
} else if (elem.ownerDocument) {
return this.isXmlDocument(elem.ownerDocument);
} else {
return false;
}
},
/**
* Create an XML document.
*
* Returns a native DOM document object, set up for XML.
*
* @param namespaceUri {String ? null} The namespace URI of the document element to create or null.
* @param qualifiedName {String ? null} The qualified name of the document element to be created or null.
* @return {Document} empty XML object
*/
create : function(namespaceUri, qualifiedName)
{
// ActiveX - This is the preferred way for IE9 as well since it has no XPath
// support when using the native implementation.createDocument
if (qx.core.Environment.get("plugin.activex")) {
var obj = new ActiveXObject(this.DOMDOC);
//The SelectionLanguage property is no longer needed in MSXML 6; trying
// to set it causes an exception in IE9.
if (this.DOMDOC == "MSXML2.DOMDocument.3.0") {
obj.setProperty("SelectionLanguage", "XPath");
}
if (qualifiedName)
{
var str = '<\?xml version="1.0" encoding="utf-8"?>\n<';
str += qualifiedName;
if (namespaceUri) {
str += " xmlns='" + namespaceUri + "'";
}
str += " />";
obj.loadXML(str);
}
return obj;
}
if (qx.core.Environment.get("xml.implementation")) {
return document.implementation.createDocument(namespaceUri || "", qualifiedName || "", null);
}
throw new Error("No XML implementation available!");
},
/**
* The string passed in is parsed into a DOM document.
*
* @param str {String} the string to be parsed
* @return {Document} XML document with given content
* @signature function(str)
*/
fromString : function(str)
{
// Legacy IE/ActiveX
if (qx.core.Environment.get("plugin.activex")) {
var dom = qx.xml.Document.create();
dom.loadXML(str);
return dom;
}
if (qx.core.Environment.get("xml.domparser")) {
var parser = new DOMParser();
return parser.parseFromString(str, "text/xml");
}
throw new Error("No XML implementation available!");
}
},
/*
*****************************************************************************
DEFER
*****************************************************************************
*/
defer : function(statics)
{
// Detecting available ActiveX implementations.
if (qx.core.Environment.get("plugin.activex"))
{
// According to information on the Microsoft XML Team's WebLog
// it is recommended to check for availability of MSXML versions 6.0 and 3.0.
// http://blogs.msdn.com/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
var domDoc = [ "MSXML2.DOMDocument.6.0", "MSXML2.DOMDocument.3.0" ];
var httpReq = [ "MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0" ];
for (var i=0, l=domDoc.length; i<l; i++)
{
try
{
// Keep both objects in sync with the same version.
// This is important as there were compatibility issues detected.
new ActiveXObject(domDoc[i]);
new ActiveXObject(httpReq[i]);
}
catch(ex) {
continue;
}
// Update static constants
statics.DOMDOC = domDoc[i];
statics.XMLHTTP = httpReq[i];
// Stop loop here
break;
}
}
}
});