1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
function JSJaCPacket(name) {
this.name = name;
/* [TODO]
* For W3C Dom Level 3 compliant browsers we need to have an
* XmlDocument.create that allows creating appropriate top-level
* nodes as they may not be altered afterwards
*/
this.doc = XmlDocument.create();
this.doc.appendChild(this.doc.createElement(name));
this.pType = function() { return this.name; };
this.getDoc = function() { return this.doc; };
this.getNode = function() { return this.getDoc().firstChild; };
this.setTo = function(to) {
if (!to || to == '')
this.getNode().removeAttribute('to');
else
this.getNode().setAttribute('to',to);
return this;
};
this.setFrom = function(from) {
if (!from || from == '')
this.getNode().removeAttribute('from');
else
this.getNode().setAttribute('from',from);
return this;
};
this.setID = function(id) {
if (!id || id == '')
this.getNode().removeAttribute('id');
else
this.getNode().setAttribute('id',id);
return this;
};
this.setType = function(type) {
if (!type || type == '')
this.getNode().removeAttribute('type');
else
this.getNode().setAttribute('type',type);
return this;
};
this.setXMLLang = function(xmllang) {
if (!xmllang || xmllang == '')
this.getNode().removeAttribute('xml:lang');
else
this.getNode().setAttribute('xml:lang',xmllang);
return this;
};
this.setXMLNS = function(xmlns) {
if (!xmlns || xmlns == '')
this.getNode().removeAttribute('xmlns');
else
this.getNode().setAttribute('xmlns',xmlns);
return this;
};
this.getTo = function() { return this.getNode().getAttribute('to'); }
this.getFrom = function() { return this.getNode().getAttribute('from'); }
this.getID = function() { return this.getNode().getAttribute('id'); }
this.getType = function() { return this.getNode().getAttribute('type'); }
this.getXMLLang = function() { return this.getNode().getAttribute('xml:lang'); };
this.getXMLNS = function() { return this.getNode().getAttribute('xmlns',xmlns); };
this.xml = function() {
return this.getDoc().xml ? this.getDoc().xml : (new XMLSerializer()).serializeToString(this.doc);
};
this._childElVal = function(nodeName) {
for (var i=0; i<this.getNode().childNodes.length; i++) {
if (this.getNode().childNodes.item(i).nodeName == nodeName) {
if (this.getNode().childNodes.item(i).firstChild)
return this.getNode().childNodes.item(i).firstChild.nodeValue;
else
return '';
}
}
return null;
}
this._replaceNode = function(aNode) {
/* hot-fix for safari
* don't ask - just shake heads
*/
if (this.getDoc().importNode)
this.getDoc().importNode(aNode,true);
return this.getDoc().replaceChild(aNode.cloneNode(true),this.getNode());
};
this.clone = function() { return JSJaCPWrapNode(this.getNode()); }
}
function JSJaCPresence() {
this.base = JSJaCPacket;
this.base('presence');
// this.setXMLNS('jabber:client');
this.setStatus = function(status) {
this.getNode().appendChild(this.getDoc().createElement('status')).appendChild(this.getDoc().createTextNode(status));
return this;
};
this.setShow = function(show) {
this.getNode().appendChild(this.getDoc().createElement('show')).appendChild(this.getDoc().createTextNode(show));
return this;
};
this.setPriority = function(prio) {
this.getNode().appendChild(this.getDoc().createElement('priority')).appendChild(this.getDoc().createTextNode(prio));
return this;
};
this.setPresence = function(show,status,prio) {
if (show)
this.setShow(show);
if (status)
this.setStatus(status);
if (prio)
this.setPriority(prio);
return this;
};
this.getStatus = function() { return this._childElVal('status'); };
this.getShow = function() { return this._childElVal('show'); };
this.getPriority = function() { return this._childElVal('priority'); };
}
function JSJaCIQ() {
this.base = JSJaCPacket;
this.base('iq');
this.setIQ = function(to,from,type,id) {
if (to)
this.setTo(to);
if (type)
this.setType(type);
if (from)
this.setFrom(from);
if (id)
this.setID(id);
return this;
};
this.setQuery = function(xmlns) {
query = this.getNode().appendChild(this.getDoc().createElement('query'));
query.setAttribute('xmlns',xmlns);
return query;
};
this.getQuery = function() {
return this.getNode().getElementsByTagName('query').item(0);
};
this.getQueryXMLNS = function() {
if (this.getQuery())
return this.getQuery().getAttribute('xmlns');
else
return null;
};
}
function JSJaCMessage() {
this.base = JSJaCPacket;
this.base('message');
// this.setXMLNS('jabber:client');
this.setBody = function(body) {
var aNode = this.getNode().appendChild(this.getDoc().createElement('body'));
aNode.appendChild(this.getDoc().createTextNode(body));
return this;
};
this.setSubject = function(subject) {
var aNode = this.getNode().appendChild(this.getDoc().createElement('subject'));
aNode.appendChild(this.getDoc().createTextNode(subject));
return this;
};
this.getBody = function() { return this._childElVal('body'); };
this.getSubject = function() { return this._childElVal('subject') };
}
/* ***
* (static) JSJaCPWrapNode
* transforms node to JSJaC internal representation (JSJaCPacket type)
*/
function JSJaCPWrapNode(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 : // unknown
return node;
}
aNode._replaceNode(node);
return aNode;
}
|