[Jslib] Error getting RDF

David Huynh dfhuynh at mit.edu
Wed Oct 27 12:07:21 EDT 2004


Thanks!... I pretty much tried everything and then resorted to loading 
the file using XML HTTP client and using the RDF XML parser to parse the 
response. I have it working now but I still don't know why it didn't 
work before. Attached in my RDF utilities code in case anyone else needs 
it. (It needs jslib.)

Best,

David


Pete Collins wrote:
> 
>>
>> If I remember correctly, everytime I use 
>> nsIRDFRemoteDataSource.Refresh(true) I always get some error. If the 
>> flag is false, everything seems fine.
>>
>> Does anybody have any idea about what this error means?
>>
> 
> True will block the UI thread while it's trying to reload the 
> datasource. But it should work either way.
> 
> Here is a snip from XUL Planet
> 
> var ds=rdfService.GetDataSource("file:///main/data/animals.rdf");
> ds.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
> ds.Refresh(true);
> 
> 
> http://www.xulplanet.com/tutorials/mozsdk/rdfsrcdetails.php
> 
> --pete
> 

-------------- next part --------------
/*
 * Utilities for handling RDF
 */
 
var RDFUtilities = new class_RDFUtilities();

function class_RDFUtilities() {};

class_RDFUtilities.prototype.RDF = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService(Components.interfaces.nsIRDFService);
class_RDFUtilities.prototype.ContainerUtils = Components.classes['@mozilla.org/rdf/container-utils;1'].getService(Components.interfaces.nsIRDFContainerUtils);

class_RDFUtilities.prototype.rdfPrefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
class_RDFUtilities.prototype.rdfsPrefix = "http://www.w3.org/2000/01/rdf-schema#";
class_RDFUtilities.prototype.dcPrefix = "http://purl.org/dc/elements/1.1/";

class_RDFUtilities.prototype.urnRoot = RDFUtilities.RDF.GetResource('urn:root');

class_RDFUtilities.prototype.typePredicate = RDFUtilities.RDF.GetResource(RDFUtilities.rdfPrefix + 'type');
class_RDFUtilities.prototype.labelPredicate = RDFUtilities.RDF.GetResource(RDFUtilities.rdfsPrefix + 'label');
class_RDFUtilities.prototype.titlePredicate = RDFUtilities.RDF.GetResource(RDFUtilities.dcPrefix + 'title');


class_RDFUtilities.prototype.getContainer = function (source, resource) {
	var container = Components.classes['@mozilla.org/rdf/container;1'].createInstance(Components.interfaces.nsIRDFContainer);
	container.Init(source, resource);
	
	return container;
}

/*
 * Copy all statements from one RDF datasource to another
 */
class_RDFUtilities.prototype.copyRDF = function (source, target) {
	var allSubjects = source.GetAllResources();
	
	var f = function(subject, predicate, objects, flag) {
		while (objects.hasMoreElements()) {
			var o = objects.getNext();
			var object = o.QueryInterface(Components.interfaces.nsIRDFNode);
			
			target.Assert(subject, predicate, object, flag);
		}
	}
	
	while (allSubjects.hasMoreElements()) {
		var o1 = allSubjects.getNext();
		var subject = o1.QueryInterface(Components.interfaces.nsIRDFResource);
		
		var allPredicates = source.ArcLabelsOut(subject);
		while (allPredicates.hasMoreElements()) {
			var o2 = allPredicates.getNext();
			var predicate = o2.QueryInterface(Components.interfaces.nsIRDFResource);
			
			f(subject, predicate, source.GetTargets(subject, predicate, false), false);
			f(subject, predicate, source.GetTargets(subject, predicate, true), true);
		}
	}
}

/*
 * Re-assert all statements as true.
 */
class_RDFUtilities.prototype.reassertTruths = function (source) {
	var allSubjects = source.GetAllResources();
	while (allSubjects.hasMoreElements()) {
		var o1 = allSubjects.getNext();
		var subject = o1.QueryInterface(Components.interfaces.nsIRDFResource);
		
		var allPredicates = source.ArcLabelsOut(subject);
		while (allPredicates.hasMoreElements()) {
			var o2 = allPredicates.getNext();
			var predicate = o2.QueryInterface(Components.interfaces.nsIRDFResource);
			
			var objects = source.GetTargets(subject, predicate, false);
			while (objects.hasMoreElements()) {
				var o = objects.getNext();
				var object = o.QueryInterface(Components.interfaces.nsIRDFNode);
				
				source.Assert(subject, predicate, object, true);
			}
		}
	}
}

/*
 * Determine whether a MIME type suggests RDF content
 */
class_RDFUtilities.prototype.linkTypeIsRDF = function (type) {
	var r =
		(type == 'application/rdf+xml') || 
		(type == 'application/rss+xml') ||
		(type == 'text/xml');

	return r;
}

/*
 * Asynchronously load RDF from an (somewhat) arbitrary URL
 *    url = URL of RDF content to load
 *    baseURI = base URI to use when RDF node does not have namespace; can be null
 *    onData = function(dataSource) that receives a new in-memory data source containing the loaded RDF content
 *    onProgress = function(percent) that receives a percentage of how much the resource has been loaded
 *    onError = function(status, statusText) that receives error notification
 */
class_RDFUtilities.prototype.asyncLoadRDF = function (url, onData, onProgress, onError, baseURI) {
	if (url.indexOf('http://') == 0) {
		return RDFUtilities.asyncLoadRDFThroughHTTP(url, onData, onProgress, onError, baseURI);
	} else if (url.indexOf('chrome://') == 0) {
		return RDFUtilities.asyncLoadRDFThroughChrome(url, onData, onProgress, onError, baseURI);
	} else {
		return RDFUtilities.asyncLoadRDFThroughFile(url, onData, onProgress, onError, baseURI);
	}
}

/*
 * Asynchronously load RDF from a chrome URL
 *    url = URL of RDF content to load
 *    baseURI = base URI to use when RDF node does not have namespace; can be null
 *    onData = function(dataSource) that receives a new in-memory data source containing the loaded RDF content
 *    onProgress = function(percent) that receives a percentage of how much the resource has been loaded
 *    onError = function(status, statusText) that receives error notification
 */
class_RDFUtilities.prototype.asyncLoadRDFThroughChrome = function (url, onData, onProgress, onError, baseURI) {
	var fileUtils = new FileUtils();
	var path = fileUtils.chromeToPath(url);
	if (navigator.platform == "Win32") {
		path = path.replace(/\//g, "\\");
		path = path.replace(/%20/g, " ");
	}
	return RDFUtilities.asyncLoadRDFThroughFile(path, onData, onProgress, onError, baseURI);
}

/*
 * Asynchronously load RDF from a filepath URL
 *    filePath = filepath of RDF content to load
 *    baseURI = base URI to use when RDF node does not have namespace; can be null
 *    onData = function(dataSource) that receives a new in-memory data source containing the loaded RDF content
 *    onProgress = function(percent) that receives a percentage of how much the resource has been loaded
 *    onError = function(status, statusText) that receives error notification
 */
class_RDFUtilities.prototype.asyncLoadRDFThroughFile = function (filePath, onData, onProgress, onError, baseURI) {
	try {
		var file = new File(filePath);
	
		file.open("r");
	
		var content = file.read();
		if (onProgress) {
			onProgress(100);
		}

		var dataSource = rdfUtilities_parseContentIntoDataSource(content);
		onData(dataSource);
		
		file.close();
	} catch (e) {
		if (onError) {
			onError(0, 'Failed to read or parse file ' + filePath + '{' + e.message + '}');
		} else {
			Debug.onCaughtException(e);
		}
	}
}

/*
 * Asynchronously load RDF from an HTTP URL
 *    url = URL of RDF content to load
 *    baseURI = base URI to use when RDF node does not have namespace; can be null
 *    onData = function(dataSource) that receives a new in-memory data source containing the loaded RDF content
 *    onProgress = function(percent) that receives a percentage of how much the resource has been loaded
 *    onError = function(status, statusText) that receives error notification
 */
class_RDFUtilities.prototype.asyncLoadRDFThroughHTTP = function (url, onData, onProgress, onError, baseURI) {
	var client = new HTTPClient();
	
	var handler = new HTTPClientHandler();
	
	handler.baseURI = baseURI;
	handler.userOnData = onData;
	handler.userOnProgress = onProgress;
	handler.userOnError = onError;
	
	handler.onLoad = function(responseText) {
		try {
			if (this.userOnData) {
				this.userOnData(rdfUtilities_parseContentIntoDataSource(responseText));
			}
		} catch (e) {
			Debug.onCaughtException(e);
		}
	};
	handler.onProgress = function(responseText, contentLength) {
		try {
			if (this.userOnProgress) {
				this.userOnProgress(responseText.length * 100 / contentLength);
			}
		} catch (e) {
			Debug.onCaughtException(e);
		}
	};
	handler.onError = function (status, statusText) {
		try {
			if (this.userOnError) {
				this.userOnError(status, statusText);
			} else {
				jslibPrint('Error loading xml resource: ' + url + '\n\t' + status + '\n\t' + statusText);
			}
		} catch (e) {
			Debug.onCaughtException(e);
		}
	};
	
	client.init(url);
	try {
		client.asyncGET(handler);
		return true;
	} catch (e) {
		Debug.onCaughtException(e);
		return false;
	}
}

/*
 * Return the type of a resource.
 */
class_RDFUtilities.prototype.getType = function (resource, dataSource) {
	var node = dataSource.GetTarget(resource, RDFUtilities.typePredicate, true);
	if (node) {
		var type = node.QueryInterface(Components.interfaces.nsIRDFResource);
		return type;
	} else {
		return null;
	}
}

class_RDFUtilities.prototype.getTypeURI = function (resource, dataSource, defaultURI) {
	var type = RDFUtilities.getType(resource, dataSource);
	if (type) {
		return type.Value;
	} else {
		return (defaultURI) ? defaultURI : 'unknown';
	}
}

class_RDFUtilities.prototype.getTypeLabel = function (resource, dataSource, defaultLabel) {
	var type = RDFUtilities.getType(resource, dataSource);
	if (type) {
		return RDFUtilities.getLabel (
			type, dataSource, (defaultLabel) ? defaultLabel : '<' + type.Value + '>');
	} else {
		return (defaultLabel) ? defaultLabel : 'unknown';
	}
}

class_RDFUtilities.prototype.getLabel = function (resource, dataSource, defaultLabel) {
	var node = dataSource.GetTarget(resource, RDFUtilities.labelPredicate, true);
	if (node) {
		var label = node.QueryInterface(Components.interfaces.nsIRDFLiteral);
		return label.Value;
	} else {
		return (defaultLabel) ? defaultLabel : '';
	}
}

function rdfUtilities_parseContentIntoDataSource(content) {
	var ios = Components.classes["@mozilla.org/network/io-service;1"]
		.getService(Components.interfaces.nsIIOService);
	var parser = Components.classes['@mozilla.org/rdf/xml-parser;1']
		.createInstance(Components.interfaces.nsIRDFXMLParser);
	var uri = ios.newURI((this.baseURI) ? this.baseURI : "urn:root", null, null);
	
	var dataSource = Components.classes['@mozilla.org/rdf/datasource;1?name=in-memory-datasource']
		.createInstance(Components.interfaces.nsIRDFDataSource);
		
	parser.parseString(dataSource, uri, content);
	
	return dataSource;
}


More information about the Jslib mailing list