Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Writing ASP Chat using JavaScript and XML (Part 4)

, 21 Jan 2004
The last part of the ASP chat series. This article explains how to provide a chat with multiple rooms.
// SharedXML class.
function SharedXML(identifier) {
	this.addNode = SharedXML_addNode;
	this.insertNode = SharedXML_insertNode;
	this.insertXmlNode = SharedXML_insertXmlNode;
	this.findNode = SharedXML_findNode;
	this.findNodeByName = SharedXML_findNodeByName;
	this.removeNode = SharedXML_removeNode;
	//this.getXMLDocument = SharedXML_getXMLDocument;
	this.getWorkingNode = SharedXML_getWorkingNode;
	this.setAttribte = SharedXML_setAttribte;
	this.getItems = SharedXML_getItems;
	this.save = SharedXML_save;
	this.load = SharedXML_load;
	this.applicationIdentifier = identifier;
	this.xmlSection = "ROOM";
}

/**
 *  Add new xml node in shared xml. Assign new id to this node.
 *  @return Newly created xml node.
 */
function SharedXML_addNode() {
	var now = new Date();
	var id = now.valueOf();
	return this.insertNode(id)
}

/**
 *  Iserts new xml node in shared xml. Assign specified id to this node.
 *  @return Newly created xml node.
 */
function SharedXML_insertNode(id) {
	var workingNode = this.getWorkingNode();
	var xmlDoc = workingNode.ownerDocument;	
	var element = xmlDoc.createElement(this.xmlSection);
	workingNode.appendChild(element);
	this.setAttribte(element, "ID", id);
	return element;
}

/**
 *  Inserts specified node to this branch.
 */
function SharedXML_insertXmlNode(xmlNode) {
	var workingNode = this.getWorkingNode();
	workingNode.appendChild(xmlNode);
}

/**
 *  Create or rename name-value pair of attribute.
 *  @param name the attribute name.
 *  @param value attribute value.
 */
function SharedXML_setAttribte(xmlNode, name, value) {
	var attributeNode = xmlNode.attributes.getNamedItem(name);
	if(attributeNode != null) {
		attributeNode.nodeValue = value;
	}
	else {
		var xmlDoc = xmlNode.ownerDocument;
		attributeNode = xmlDoc.createAttribute(name);
		attributeNode.nodeValue = value;
		xmlNode.attributes.setNamedItem(attributeNode);
	}
}

/**
 *  Find node by id.
 *  @param id unique id to search.
 *  @return XML node if found or undefined value;
 */
function SharedXML_findNode(id) {
	var workingNode = this.getWorkingNode();
	var xmlDoc = workingNode.ownerDocument;
	xmlDoc.setProperty("SelectionLanguage", "XPath");
	var queryString = "*[@ID=" + id + "]";
	return workingNode.selectSingleNode(queryString);
}

/**
 *  Find node by name.
 *  @param name unique name to search.
 *  @return XML node if found or null;
 */
function SharedXML_findNodeByName(name) {
	var workingNode = this.getWorkingNode();
	var xmlDoc = workingNode.ownerDocument;
	xmlDoc.setProperty("SelectionLanguage", "XPath");
	var queryString = "*[@NAME=\"" + name + "\"]";
	return workingNode.selectSingleNode(queryString);
}

/**
 *  Removes specified node.
 *  @param xmlNode node to delete.
 */
function SharedXML_removeNode(xmlNode) {
	var workingNode = this.getWorkingNode();
	workingNode.removeChild(xmlNode);
}

/**
 *  Provides access to root node and first time creation 
 * of shared XML Document.
 *  @return Root node of shared FreeThreadedDOMDocument object.
 */
function SharedXML_getWorkingNode() {
	var xmlDoc = Application(this.applicationIdentifier);
	if(typeof(xmlDoc) == "undefined" || xmlDoc == null) {
		xmlDoc = Server.CreateObject("MSXML.FreeThreadedDOMDocument");
	}
	if(xmlDoc.firstChild == null) {
		var rootElement=xmlDoc.createElement("ROOT");
		xmlDoc.appendChild(rootElement);
		Application(this.applicationIdentifier) = xmlDoc;
	}
	return xmlDoc.firstChild;
}

/**
 *  Provides access and first time creation of shared XML Document.
 *  @return Shared FreeThreadedDOMDocument object.
 */
/*function SharedXML_getXMLDocument() {
	//if(typeof(Application) != "undefined") {
		var xmlDoc = Application(this.applicationIdentifier);
		if(typeof(xmlDoc) == "undefined" || xmlDoc == null) {
			xmlDoc = Server.CreateObject("MSXML.FreeThreadedDOMDocument");
		}
		if(xmlDoc.firstChild == null) {
			var rootElement=xmlDoc.createElement("ROOT");
			xmlDoc.appendChild(rootElement);
			Application(this.applicationIdentifier) = xmlDoc;
		}
		return xmlDoc;
	//}
	//else {
		// Testing purposes only.
	//	var xmlDoc = app;
	//	if(typeof(xmlDoc) == "undefined") {
	//		xmlDoc = WScript.CreateObject("MSXML.FreeThreadedDOMDocument");
	//		var rootElement=xmlDoc.createElement("ROOT");
	//		xmlDoc.appendChild(rootElement);
	//		app = xmlDoc;
	//	}
	//	return xmlDoc;
	//}
}*/
//var app;

/**
 *  Queries list of data items.
 *  @return XMLNodesList of managed child items.
 */
function SharedXML_getItems() {
	var workingNode = this.getWorkingNode();
	var itemsList = workingNode.childNodes;
	return itemsList;
}

/**
 *  Save shared data to file.
 */
function SharedXML_save(fileName) {
	var workingNode = this.getWorkingNode();
	var xmlDoc = workingNode.ownerDocument;
	xmlDoc.save(fileName);
}

/**
 *  Load shared data from file.
 */
function SharedXML_load(fileName) {
	var workingNode = this.getWorkingNode();
	var xmlDoc = workingNode.ownerDocument;
	xmlDoc.load(fileName);
}
// SharedXML class.

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Alexander Fedorenko
Web Developer
Ukraine Ukraine
Alexander Fedorenko is a professional C++ developer since 1996. He completed many custom and shareware projects. One of the successful projects is DevPlanner - tool for personal planning and time management.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 22 Jan 2004
Article Copyright 2004 by Alexander Fedorenko
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid