JSJaCJID.js
Summary
This file contains all things that make life easier when
dealing with JIDs
Version: $Revision$
Author: Stefan Strigler
var JSJACJID_FORBIDDEN = ['"',' ','&','\'','/',':','<','>','@'];
/**
* Creates a new JSJaCJID object
* @class JSJaCJID models xmpp jid objects
* @constructor
* @param {Object} jid jid may be either of type String or a JID represented
* by JSON with fields 'node', 'domain' and 'resource'
* @throws JSJaCJIDInvalidException Thrown if jid is not valid
* @return a new JSJaCJID object
*/
function JSJaCJID(jid) {
/**
*@private
*/
this._node = '';
/**
*@private
*/
this._domain = '';
/**
*@private
*/
this._resource = '';
if (typeof(jid) == 'string') {
if (jid.indexOf('@') != -1) {
this.setNode(jid.substring(0,jid.indexOf('@')));
jid = jid.substring(jid.indexOf('@')+1);
}
if (jid.indexOf('/') != -1) {
this.setResource(jid.substring(jid.indexOf('/')+1));
jid = jid.substring(0,jid.indexOf('/'));
}
this.setDomain(jid);
} else {
this.setNode(jid.node);
this.setDomain(jid.domain);
this.setResource(jid.resource);
}
}
/**
* Gets the node part of the jid
* @return A string representing the node name
* @type String
*/
JSJaCJID.prototype.getNode = function() { return this._node; };
/**
* Gets the domain part of the jid
* @return A string representing the domain name
* @type String
*/
JSJaCJID.prototype.getDomain = function() { return this._domain; };
/**
* Gets the resource part of the jid
* @return A string representing the resource
* @type String
*/
JSJaCJID.prototype.getResource = function() { return this._resource; };
/**
* Sets the node part of the jid
* @param {String} node Name of the node
* @throws JSJaCJIDInvalidException Thrown if node name contains invalid chars
* @return This object
* @type JSJaCJID
*/
JSJaCJID.prototype.setNode = function(node) {
JSJaCJID._checkNodeName(node);
this._node = node || '';
return this;
};
/**
* Sets the domain part of the jid
* @param {String} domain Name of the domain
* @throws JSJaCJIDInvalidException Thrown if domain name contains invalid
* chars or is empty
* @return This object
* @type JSJaCJID
*/
JSJaCJID.prototype.setDomain = function(domain) {
if (!domain || domain == '')
throw new JSJaCJIDInvalidException("domain name missing");
// chars forbidden for a node are not allowed in domain names
// anyway, so let's check
JSJaCJID._checkNodeName(domain);
this._domain = domain;
return this;
};
JSJaCJID.prototype.setResource = function(resource) {
this._resource = resource || '';
return this;
};
JSJaCJID.prototype.toString = function() {
var jid = '';
if (this.getNode() && this.getNode() != '')
jid = this.getNode() + '@';
jid += this.getDomain();
if (this.getResource() && this.getResource() != "")
jid += '/' + this.getResource();
return jid;
};
JSJaCJID.prototype.removeResource = function() {
return this.setResource();
};
JSJaCJID.prototype.clone = function() {
return new JSJaCJID(this.toString());
};
JSJaCJID.prototype.isEntity = function(jid) {
if (typeof jid == 'string')
jid = (new JSJaCJID(jid));
jid.removeResource();
return (this.clone().removeResource().toString() === jid.toString());
};
JSJaCJID._checkNodeName = function(nodeprep) {
if (!nodeprep || nodeprep == '')
return;
for (var i=0; i< JSJACJID_FORBIDDEN.length; i++) {
if (nodeprep.indexOf(JSJACJID_FORBIDDEN[i]) != -1) {
throw new JSJaCJIDInvalidException("forbidden char in nodename: "+JSJACJID_FORBIDDEN[i]);
}
}
};
function JSJaCJIDInvalidException(message) {
this.message = message;
this.name = "JSJaCJIDInvalidException";
}
Documentation generated by
JSDoc on Sun Feb 7 13:21:05 2021