![]() |
rdf: datasource howtoThis document is a cookbook that describes how to create a native, client-side datasource that works with Mozilla's RDF implementation. It supercedes (and borrows from) the original document put together by Robert Churchill.
What is a datasource?The "RDF universe" consists of a set of statements about Internet resources; for example, "my home page was last modified April 2nd", or "that news article was sent by Bob". In the most abstract sense, a datasource is a collection of such statements. More concretely, a datasource is a translator that can present information as a collection of RDF statements. For example, a "file system datasource" would translate the file system into statements like "/tmp is a directory" and "/tmp/foo is contained within /tmp". An "IMAP datasource" would use the IMAP protocol to translate your mail server's inbox as a collection of statements like "message number 126's subject is 'make money fast on the Internet'" and "message number 126 was sent by 'spammer128@hotmail.com'". An "address book" datasource could translate a database file into statements like "spammer128@hotmail.com's real name is 'Billy Dumple'" and "spammer128@hotmail.com is considered an 'important friend'." Statements from one datasource can be combined with statements from another datasource using a composite datasource. By combining statements from the IMAP datasource and address book datasource, above, we'd be able to identify the sender of "message 126" as an "important friend". Deciding on a vocabularyThe vocabulary is the set of properties that you will use to express relationships between elements (resources and literals) in your data model. The first question that you must answer is "should I use an existing vocabulary, or invent my own?" A reasonable answer is, "use an existing vocabulary unless you absolutely must invent your own." This will allow your datasource to be integrated with other datasources with a minimum of effort. There are several existing vocabularies of note, including:
Mapping your data to nodes and arcs[write me!] Implementing the nsIRDFDataSource interfaceYour first chore will be to implement the nsIRDFDataSource interface. There are basically two approaches that you can take in this endeavor:
[More info on what each method needs to do here] RDF Commands
Registering the datasource componentA datasource is an XPCOM component. As such, it must (currently, see [1]) have:
Constructing a DLL for a component is beyond the scope of this document; the reader is referred to the RDF factory as a guideline. Registering an RDF datasource is fairly simple: in the DLL's NSRegisterSelf() method, you simply call the component manager's RegisterComponent() method:
extern "C" PR_IMPLEMENT(nsresult)
NSRegisterSelf(nsISupports* aServiceManager, const char* aPath)
{
nsresult rv;
...
// Assume compMgr refers to the component manager
rv = compMgr->RegisterComponent(kMyDataSourceCID,
"My Data Source",
NS_RDF_DATASOURCE_PROGID_PREFIX "my-datasource",
aPath, PR_TRUE, PR_TRUE);
...
}
Replace kMyDataSourceCID with your datasource's CLSID. Replace "My Data Source" with a descriptive string that should appear in the registry. Finally, replace "my-datasource" with a value appropriate for your datasource. This value, when prefixed with "rdf:", is a datasource identifier, and may be used with nsIRDFService::GetDataSource() to retrieve your datasource from the RDF service. For example, the above datasource would be accessable as follows:
nsIRDFService* rdf;
rv = nsServiceManager::GetService(kRDFServiceCID,
kIRDFServiceIID,
(nsISupports**) &rdf);
if (NS_SUCCEEDED(rv)) {
nsIRDFDataSource* myDataSource;
rv = rdf->GetDataSource("rdf:my-datasource",
&myDataSource);
if (NS_SUCCEEDED(rv)) {
// ...do something to myDataSource here...
NS_RELEASE(myDataSource);
}
nsServiceManager::ReleaseService(kRDFServiceCID, rdf);
}
Displaying RDF as contentNow that you've gone through all this pain to expose your information as a datasource, you probably want to see it. Using XUL, you can display the contents of your datasource in a tree control, a menu, or a toolbar. In fact, you can convert RDF to an arbitrary content model using XUL Templates. The following XUL fragment illustrates how to instantiate a tree control whose body is "rooted" to a resource (http://foo.bar.com/) that your datasource describes:
<window
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns:rdf="http://www.w3.org/TR/WD-rdf-syntax#"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<tree datasources="rdf:my-datasource" ref="http://foo.bar.com/">
<template>
<treechildren>
<treeitem uri="...">
<treerow>
<treecell>
<text value="rdf:http://home.netscape.com/NC-rdf#Name" />
</treecell>
<treecell>
<text value="rdf:http://home.netscape.com/NC-rdf#URL" />
</treecell>
</treerow>
</treeitem>
</treechildren>
</template>
<treehead>
<treeitem>
<treecell>Name</treecell>
<treecell>URL</treecell>
</treeitem>
</treehead>
<!-- treechildren built _here_ -->
</tree>
</window>
The important "magic attributes" have been called out in bold, above:
For a complete description of how content is built from RDF, see the XUL Template Reference. 1. As of this writing, it is not currently possible to implement JavaScript XPCOM components; however, it may soon be possible to do so via XPConnect. Last Modified: $Id: datasource-howto.html,v 1.5 1999/08/19 19:22:44 waterson%netscape.com Exp $ |
|
|
Copyright © 1998-2000 The Mozilla Organization.
Last modified August 19, 1999. |
|