org.hisrc.jsonix.Jsonix.DOM.js Maven / Gradle / Ivy
Jsonix.DOM = {
isDomImplementationAvailable : function () {
if (typeof _jsonix_xmldom !== 'undefined')
{
return true;
} else if (typeof document !== 'undefined' && Jsonix.Util.Type.exists(document.implementation) && Jsonix.Util.Type.isFunction(document.implementation.createDocument)) {
return true;
} else {
return false;
}
},
createDocument : function() {
// REWORK
// Node.js
if (typeof _jsonix_xmldom !== 'undefined')
{
return new (_jsonix_xmldom.DOMImplementation)().createDocument();
} else if (typeof document !== 'undefined' && Jsonix.Util.Type.exists(document.implementation) && Jsonix.Util.Type.isFunction(document.implementation.createDocument)) {
return document.implementation.createDocument('', '', null);
} else if (typeof ActiveXObject !== 'undefined') {
return new ActiveXObject('MSXML2.DOMDocument');
} else {
throw new Error('Error created the DOM document.');
}
},
serialize : function(node) {
Jsonix.Util.Ensure.ensureExists(node);
// REWORK
// Node.js
if (typeof _jsonix_xmldom !== 'undefined')
{
return (new (_jsonix_xmldom).XMLSerializer()).serializeToString(node);
} else if (Jsonix.Util.Type.exists(XMLSerializer)) {
return (new XMLSerializer()).serializeToString(node);
} else if (Jsonix.Util.Type.exists(node.xml)) {
return node.xml;
} else {
throw new Error('Could not serialize the node, neither XMLSerializer nor the [xml] property were found.');
}
},
parse : function(text) {
Jsonix.Util.Ensure.ensureExists(text);
if (typeof _jsonix_xmldom !== 'undefined')
{
return (new (_jsonix_xmldom).DOMParser()).parseFromString(text, 'application/xml');
} else if (typeof DOMParser != 'undefined') {
return (new DOMParser()).parseFromString(text, 'application/xml');
} else if (typeof ActiveXObject != 'undefined') {
var doc = Jsonix.DOM.createDocument('', '');
doc.loadXML(text);
return doc;
} else {
var url = 'data:text/xml;charset=utf-8,' + encodeURIComponent(text);
var request = new XMLHttpRequest();
request.open('GET', url, false);
if (request.overrideMimeType) {
request.overrideMimeType("text/xml");
}
request.send(null);
return request.responseXML;
}
},
load : function(url, callback, options) {
var request = Jsonix.Request.INSTANCE;
request.issue(
url,
function(transport) {
var result;
if (Jsonix.Util.Type.exists(transport.responseXML) && Jsonix.Util.Type.exists(transport.responseXML.documentElement)) {
result = transport.responseXML;
} else if (Jsonix.Util.Type.isString(transport.responseText)) {
result = Jsonix.DOM.parse(transport.responseText);
} else {
throw new Error('Response does not have valid [responseXML] or [responseText].');
}
callback(result);
}, function(transport) {
throw new Error('Could not retrieve XML from URL [' + url + '].');
}, options);
},
xlinkFixRequired : null,
isXlinkFixRequired : function ()
{
if (Jsonix.DOM.xlinkFixRequired === null)
{
if (typeof navigator === 'undefined')
{
Jsonix.DOM.xlinkFixRequired = false;
}
else if (!!navigator.userAgent && (/Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor)))
{
var doc = Jsonix.DOM.createDocument();
var el = doc.createElement('test');
el.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', 'urn:test');
doc.appendChild(el);
var testString = Jsonix.DOM.serialize(doc);
Jsonix.DOM.xlinkFixRequired = (testString.indexOf('xmlns:xlink') === -1);
}
else
{
Jsonix.DOM.xlinkFixRequired = false;
}
}
return Jsonix.DOM.xlinkFixRequired;
}
};
© 2015 - 2024 Weber Informatics LLC | Privacy Policy