JSJaCPacket.js
Summary
Contains all Jabber/XMPP packet related classes.
Version: $Revision: 480 $
Author: Stefan Strigler steve@zeank.in-berlin.de
|
Class Summary
|
| JSJaCIQ |
Models the XMPP notion of an 'iq' packet
|
| JSJaCMessage |
Models the XMPP notion of an 'message' packet
|
| JSJaCPacket |
Somewhat abstract base class for all kinds of specialised packets
|
| JSJaCPresence |
Models the XMPP notion of a 'presence' packet
|
var JSJACPACKET_USE_XMLNS = true;
function JSJaCPacket(name) {
this.name = name;
if (typeof(JSJACPACKET_USE_XMLNS) != 'undefined' && JSJACPACKET_USE_XMLNS)
this.doc = XmlDocument.create(name,'jabber:client');
else
this.doc = XmlDocument.create(name,'');
}
JSJaCPacket.prototype.pType = function() { return this.name; };
JSJaCPacket.prototype.getDoc = function() {
return this.doc;
};
JSJaCPacket.prototype.getNode = function() {
if (this.getDoc() && this.getDoc().documentElement)
return this.getDoc().documentElement;
else
return null;
};
JSJaCPacket.prototype.setTo = function(to) {
if (!to || to == '')
this.getNode().removeAttribute('to');
else if (typeof(to) == 'string')
this.getNode().setAttribute('to',to);
else
this.getNode().setAttribute('to',to.toString());
return this;
};
JSJaCPacket.prototype.setFrom = function(from) {
if (!from || from == '')
this.getNode().removeAttribute('from');
else if (typeof(from) == 'string')
this.getNode().setAttribute('from',from);
else
this.getNode().setAttribute('from',from.toString());
return this;
};
JSJaCPacket.prototype.setID = function(id) {
if (!id || id == '')
this.getNode().removeAttribute('id');
else
this.getNode().setAttribute('id',id);
return this;
};
JSJaCPacket.prototype.setType = function(type) {
if (!type || type == '')
this.getNode().removeAttribute('type');
else
this.getNode().setAttribute('type',type);
return this;
};
JSJaCPacket.prototype.setXMLLang = function(xmllang) {
if (!xmllang || xmllang == '')
this.getNode().removeAttribute('xml:lang');
else
this.getNode().setAttribute('xml:lang',xmllang);
return this;
};
JSJaCPacket.prototype.getTo = function() {
return this.getNode().getAttribute('to');
};
JSJaCPacket.prototype.getFrom = function() {
return this.getNode().getAttribute('from');
};
JSJaCPacket.prototype.getToJID = function() {
return new JSJaCJID(this.getTo());
};
JSJaCPacket.prototype.getFromJID = function() {
return new JSJaCJID(this.getFrom());
};
JSJaCPacket.prototype.getID = function() {
return this.getNode().getAttribute('id');
};
JSJaCPacket.prototype.getType = function() {
return this.getNode().getAttribute('type');
};
JSJaCPacket.prototype.getXMLLang = function() {
return this.getNode().getAttribute('xml:lang');
};
JSJaCPacket.prototype.getXMLNS = function() {
return this.getNode().namespaceURI;
};
JSJaCPacket.prototype.getChild = function(name, ns) {
if (!this.getNode()) {
return null;
}
name = name || '*';
ns = ns || '*';
if (this.getNode().getElementsByTagNameNS) {
return this.getNode().getElementsByTagNameNS(ns, name).item(0);
}
var nodes = this.getNode().getElementsByTagName(name);
if (ns != '*') {
for (var i=0; i<nodes.length; i++) {
if (nodes.item(i).namespaceURI == ns) {
return nodes.item(i);
}
}
} else {
return nodes.item(0);
}
return null;
}
JSJaCPacket.prototype.getChildVal = function(name, ns) {
var node = this.getChild(name, ns);
var ret = '';
if (node && node.hasChildNodes()) {
for (var i=0; i<node.childNodes.length; i++)
if (node.childNodes.item(i).nodeValue)
ret += node.childNodes.item(i).nodeValue;
}
return ret;
};
JSJaCPacket.prototype.clone = function() {
return JSJaCPacket.wrapNode(this.getNode());
};
JSJaCPacket.prototype.isError = function() {
return (this.getType() == 'error');
};
JSJaCPacket.prototype.errorReply = function(stanza_error) {
var rPacket = this.clone();
rPacket.setTo(this.getFrom());
rPacket.setFrom();
rPacket.setType('error');
rPacket.appendNode('error',
{code: stanza_error.code, type: stanza_error.type},
[[stanza_error.cond]]);
return rPacket;
};
JSJaCPacket.prototype.xml = typeof XMLSerializer != 'undefined' ?
function() {
var r = (new XMLSerializer()).serializeToString(this.getNode());
if (typeof(r) == 'undefined')
r = (new XMLSerializer()).serializeToString(this.doc);
return r
} :
function() {
return this.getDoc().xml
};
JSJaCPacket.prototype._getAttribute = function(attr) {
return this.getNode().getAttribute(attr);
};
JSJaCPacket.prototype._replaceNode = function(aNode) {
for (var i=0; i<aNode.attributes.length; i++)
if (aNode.attributes.item(i).nodeName != 'xmlns')
this.getNode().setAttribute(aNode.attributes.item(i).nodeName,
aNode.attributes.item(i).nodeValue);
for (var i=0; i<aNode.childNodes.length; i++)
if (this.getDoc().importNode)
this.getNode().appendChild(this.getDoc().importNode(aNode.
childNodes.item(i),
true));
else
this.getNode().appendChild(aNode.childNodes.item(i).cloneNode(true));
};
JSJaCPacket.prototype._setChildNode = function(nodeName, nodeValue) {
var aNode = this.getChild(nodeName);
var tNode = this.getDoc().createTextNode(nodeValue);
if (aNode)
try {
aNode.replaceChild(tNode,aNode.firstChild);
} catch (e) { }
else {
try {
aNode = this.getDoc().createElementNS(this.getNode().namespaceURI,
nodeName);
} catch (ex) {
aNode = this.getDoc().createElement(nodeName)
}
this.getNode().appendChild(aNode);
aNode.appendChild(tNode);
}
return aNode;
};
JSJaCPacket.prototype.buildNode = function(elementName) {
return JSJaCBuilder.buildNode(this.getDoc(),
elementName,
arguments[1],
arguments[2]);
};
JSJaCPacket.prototype.appendNode = function(element) {
if (typeof element=='object') {
return this.getNode().appendChild(element)
} else {
return this.getNode().appendChild(this.buildNode(element,
arguments[1],
arguments[2],
null,
this.getNode().namespaceURI));
}
};
function JSJaCPresence() {
this.base = JSJaCPacket;
this.base('presence');
}
JSJaCPresence.prototype = new JSJaCPacket;
JSJaCPresence.prototype.setStatus = function(status) {
this._setChildNode("status", status);
return this;
};
JSJaCPresence.prototype.setShow = function(show) {
if (show == 'chat' || show == 'away' || show == 'xa' || show == 'dnd')
this._setChildNode("show",show);
return this;
};
JSJaCPresence.prototype.setPriority = function(prio) {
this._setChildNode("priority", prio);
return this;
};
JSJaCPresence.prototype.setPresence = function(show,status,prio) {
if (show)
this.setShow(show);
if (status)
this.setStatus(status);
if (prio)
this.setPriority(prio);
return this;
};
JSJaCPresence.prototype.getStatus = function() {
return this.getChildVal('status');
};
JSJaCPresence.prototype.getShow = function() {
return this.getChildVal('show');
};
JSJaCPresence.prototype.getPriority = function() {
return this.getChildVal('priority');
};
function JSJaCIQ() {
this.base = JSJaCPacket;
this.base('iq');
}
JSJaCIQ.prototype = new JSJaCPacket;
JSJaCIQ.prototype.setIQ = function(to,type,id) {
if (to)
this.setTo(to);
if (type)
this.setType(type);
if (id)
this.setID(id);
return this;
};
JSJaCIQ.prototype.setQuery = function(xmlns) {
var query;
try {
query = this.getDoc().createElementNS(xmlns,'query');
} catch (e) {
query = this.getDoc().createElement('query');
}
if (query && query.getAttribute('xmlns') != xmlns)
query.setAttribute('xmlns',xmlns);
this.getNode().appendChild(query);
return query;
};
JSJaCIQ.prototype.getQuery = function() {
return this.getNode().getElementsByTagName('query').item(0);
};
JSJaCIQ.prototype.getQueryXMLNS = function() {
if (this.getQuery())
return this.getQuery().namespaceURI;
else
return null;
};
JSJaCIQ.prototype.reply = function(payload) {
var rIQ = this.clone();
rIQ.setTo(this.getFrom());
rIQ.setType('result');
if (payload) {
if (typeof payload == 'string')
rIQ.getChild().appendChild(rIQ.getDoc().loadXML(payload));
else if (payload.constructor == Array) {
var node = rIQ.getChild();
for (var i=0; i<payload.length; i++)
if(typeof payload[i] == 'string')
node.appendChild(rIQ.getDoc().loadXML(payload[i]));
else if (typeof payload[i] == 'object')
node.appendChild(payload[i]);
}
else if (typeof payload == 'object')
rIQ.getChild().appendChild(payload);
}
return rIQ;
};
function JSJaCMessage() {
this.base = JSJaCPacket;
this.base('message');
}
JSJaCMessage.prototype = new JSJaCPacket;
JSJaCMessage.prototype.setBody = function(body) {
this._setChildNode("body",body);
return this;
};
JSJaCMessage.prototype.setSubject = function(subject) {
this._setChildNode("subject",subject);
return this;
};
JSJaCMessage.prototype.setThread = function(thread) {
this._setChildNode("thread", thread);
return this;
};
JSJaCMessage.prototype.getThread = function() {
return this.getChildVal('thread');
};
JSJaCMessage.prototype.getBody = function() {
return this.getChildVal('body');
};
JSJaCMessage.prototype.getSubject = function() {
return this.getChildVal('subject')
};
JSJaCPacket.wrapNode = function(node) {
var aNode;
switch (node.nodeName.toLowerCase()) {
case 'presence':
aNode = new JSJaCPresence();
break;
case 'message':
aNode = new JSJaCMessage();
break;
case 'iq':
aNode = new JSJaCIQ();
break;
default :
return null;
}
aNode._replaceNode(node);
return aNode;
};
Documentation generated by
JSDoc on Tue Dec 9 15:09:23 2008