You are currently viewing a snapshot of www.mozilla.org taken on April 21, 2008. Most of this content is highly out of date (some pages haven't been updated since the project began in 1998) and exists for historical purposes only. If there are any pages on this archive site that you think should be added back to www.mozilla.org, please file a bug.



Examples of Using RDF with Mozilla

Contact: Chris Waterson (waterson@netscape.com)

This page provides links to some examples that demonstrate how to use RDF in Mozilla.

Examples marked with a “*” won't run directly from HTTP due to security issues. They are, however, self-contained, and can be downloaded and saved to your hard drive. Once you've done that, you'll need to package the example properly into a chrome JAR to run it.

Examples marked with a “†” will run if you've installed this certificate in your Mozilla-based browser.

*RDF, JS, and XUL templates

A quick example that demonstrates how to create an in-memory datasource from trusted JavaScript, populate it, and display its contents using a XUL template in an outliner.

Displaying the Contents of a Datasource

This example (submitted by Adam Fletcher <adamf@rovia.com>) demonstrates use of the low-level RDF APIs to dump the complete contents of a datasource using the GetAllResources API of nsIRDFDataSource.

An example of a template-within-a-template

This example was created as straw-man for integrating the AOL Instant Messenger ``offline'' buddy list into the normal buddy tab. It's interesting because it contains a template within a template: a statically create <treeitem> is mixed in with the generated content to display the set of Offline buddies using the same data.

*An example of a JS-implemented `wrapper' datasource

This example was created to demonstrate how you'd write a `wrapper' datasource in JavaScript that intercepts queries for another datasource, and augments or modifies them. In this example, the rdf:files datasource (which renders the file system as a datasource) is wrapped with a silly wrapper that computes the ROT-13'd value of the file name.

An example that illustrates how to observe Mozilla's global history

The following code illustrates how one might observe Mozilla's global history from trusted JavaScript (i.e., chrome or a signed script with the UniversalXPConnect privilege enabled).

var RDF =
  Components.classes["@mozilla.org/rdf/rdf-service;1"].
  getService(Components.interfaces.nsIRDFService);

var history =
  Components.classes["@mozilla.org/rdf/datasource;1?name=history"].
  getService(Compponents.interfaces.nsIRDFDataSource);

var observer = {
  onAssert : function(ds, s, p, t) {
    if (s == RDF.GetResource("NC:HistoryRoot") &&
        p == RDF.GetResource("http://home.netscape.com/NC-rdf#child")) {
      /* A new page was added; it's URL is 't.Value'. */
    }
  },

  onUnassert : function(ds, s, p, t) {
    /* Page removed from history, etc. */
  },

  onChange : function(ds, s, p, oldt, newt) {
    /* Visit date updated, etc. */
  },

  onMove : function(ds, olds, news, p, t) {},
  beginUpdateBatch : function(ds) {},
  endUpdateBatch : function(ds) {}
};

history.AddObserver(observer);

Note that the onMove, beginUpdateBatch, and endUpdateBatch methods need not provide implementation, as the global history datasource should not invoke these methods. For more information on the notifications that are sent by the global history datasource, take a look at the source, or augment the above listener to dump() verbosely as it receives notifications.

Refreshing a datasource

This example demonstrates how to use the nsIRDFRemoteDataSource::Refresh method to update the contents of a datasource (and the template that observes it) dynamically.