Here, we'll look at additional datasources and how to use your own RDF files as datasources.
In the last section, we looked at how to display the user's bookmarks. If you try the examples from the last section, the data displayed will correspond to the bookmarks that you have. If you add or remove bookmarks, they will update automatically. Try adding a bookmark while you have one of the example windows open. You'll notice that it gets updated instantly.
Mozilla provides a number of other datasources also. Some of them are listed here with a few examples. They work very similarly to the bookmarks, although the fields will be different in each case.
The history datasource is similar to bookmarks in that provides a list of URLs. Of course, it provides access to the user's history list which is the list of URLs the user has visited recently. The resource can be refered to using rdf:history as the datasource. The table below shows the resources (or fields) that you can retrieve from the history datasource. Put the URL values below where you want the value of the resource to be used. Note that the history is not generally hierarchical.
| Date | rdf:http://home.netscape.com/NC-rdf#Date | Date of last visit |
| Name | rdf:http://home.netscape.com/NC-rdf#Name | Title of the page |
| Page | rdf:http://home.netscape.com/NC-rdf#Page | Page name |
| Referrer | rdf:http://home.netscape.com/NC-rdf#Referrer | Referrer of the page |
| URL | rdf:http://home.netscape.com/NC-rdf#URL | URL of the page |
| Visit Count | rdf:http://home.netscape.com/NC-rdf#VisitCount | Number of page visits |
A typical history list will display a tree with a selection of these fields. To use them, just put the URL values above in the value attributes of the buttons or treecells. You can use NC:HistoryRoot as the value of the ref attribute.
Let's see an example of displaying the history list. We'll display the history in a tree with three columns, the Name, the Page and the Date. Only one rule is used here to simplify the code a bit but you could add the separator code from the bookmarks examples. You can use the same rule (there is no special history separator). The history list shouldn't really have separators in it anyway.
Example 6.7.1
<window
id="example-window"
title="History List"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<tree flex="1" datasources="rdf:history" ref="NC:HistoryRoot">
<template>
<rule>
<treechildren>
<treeitem uri="rdf:*">
<treerow>
<treecell class="treecell-indent" value="rdf:http://home.netscape.com/NC-rdf#Name"/>
<treecell value="rdf:http://home.netscape.com/NC-rdf#URL"/>
<treecell value="rdf:http://home.netscape.com/NC-rdf#Date"/>
</treerow>
</treeitem>
</treechildren>
</rule>
</template>
</tree>
</window>
|
The tables below list some of the other datasources available with Mozilla. You can use any of the resources that you want.
Bookmarks (rdf:bookmarks): The bookmarks are generated from the user's bookmark list.
| Resources | ||
| Added Date | rdf:http://home.netscape.com/NC-rdf#BookmarkAddDate | The date the bookmark was added |
| Description | rdf:http://home.netscape.com/NC-rdf#Description | Bookmark description |
| Last Modified | rdf:http://home.netscape.com/WEB-rdf#LastModifiedDate | The date of last modification |
| Last Visited | rdf:http://home.netscape.com/WEB-rdf#LastVisitDate | Date of last visit |
| Name | rdf:http://home.netscape.com/NC-rdf#Name | The bookmark name |
| Shortcut URL | rdf:http://home.netscape.com/NC-rdf#ShortcutURL | Custom keywords field |
| URL | rdf:http://home.netscape.com/NC-rdf#URL | The URL to link to |
| Possible Bookmarks Roots | |
| NC:BookmarksRoot | Top level of the bookmarks hierarchy |
| NC:IEFavoritesRoot | The bookmark folder that corresponds to the user's IE favorites. |
| NC:PersonalToolbarFolder | The bookmark folder that corresponds to the personal toolbar folder |
Files (rdf:files): A view of the user's files.
| Resources | ||
| Name | rdf:http://home.netscape.com/NC-rdf#Name | The name of the file |
| URL | rdf:http://home.netscape.com/NC-rdf#URL | The URL of the file |
| Possible Files Roots | |
| NC:FilesRoot | Top level of the filesystem (usually the list of drives) |
| A file URL | By using a file URL for the ref attribute, you can select a specific directory to be returned. For example, you might use file:///windows or files:///usr/local. |
The files datasource is an example of a datasource that determines its resources only when necessary. We don't want every file in the filesystem to be determined before the data is displayed. Instead, only the files and directories that the tree element (or other elements) will need to display at a given time will be determined.
The following is a simple file browser:
Example 6.7.2
<tree flex="1" datasources="rdf:files" ref="file:///">
<template>
<rule>
<treechildren>
<treeitem uri="rdf:*">
<treerow>
<treecell class="treecell-indent" value="rdf:http://home.netscape.com/NC-rdf#Name"/>
</treerow>
</treeitem>
</treechildren>
</rule>
</template>
</tree>
|
If you try this example, you'll notice that it displays the top-level directory immediately but will have a slight delay whenever you open a directory as the list of files in the directory are read.
You can specify multiple datasources in the datasources attribute by separating them with whitespace as in the example below. This has the effect of reading the data from all the datasources mentioned.
<tree datasources="rdf:bookmarks
rdf:history
animals.rdf" ref="NC:BookmarksRoot">
|
This example reads the resources from the bookmarks, history and the animals.rdf file. They are combined into a single composite datasource and can be used as if they were one.
The special datasource rdf:null corresponds to nothing. You can use this datasource if you want to dynamically set the datasource using a script.
You can use any of the above internal datasources if you wish. There are several others for mail, address books and searching and so on. However, you might want to use your own RDF datasource stored in an RDF file. The file can be either a local file or a remote file. Just put the URL of the RDF file in the datasources attribute.
Using RDF files provides just as much functionality as any of the internal datasources. You can use rules to match specific types of content. The attributes on the rule element will match if they match the attributes on an RDF Description element. You can also create RDF files that are hierarchical.
The following is an example of how an RDF file can be used as a datasource. The RDF file is fairly large and can be viewed separately. (Note that you'll have to rename it to animals.rdf to test it.)
Example 6.7.3
<tree flex="1" datasources="animals.rdf" ref="urn:animals:data">
<treehead>
<treerow>
<treecell value="Name"/>
<treecell value="Species"/>
</treerow>
</treehead>
<treecolgroup>
<treecol flex="1"/>
<treecol flex="1"/>
</treecolgroup>
<template>
<rule>
<treechildren flex="1">
<treeitem uri="rdf:*">
<treerow>
<treecell class="treecell-indent" value="rdf:http://www.some-ficticious-zoo.com/rdf#name"/>
<treecell value="rdf:http://www.some-ficticious-zoo.com/rdf#species"/>
</treerow>
</treeitem>
</treechildren>
</rule>
</template>
</tree>
|
Here, the data has been generated from the file. The ref attribute has been set to root element in the RDF file, which is the top-level Seq. This will give us a complete list of animals. If we wanted to, we could set the ref attribute to any of the other about attribute values to limit the set of data that is returned. For example, to display only the reptiles, use a value of urn:animals:reptiles.
The example below shows how to display a particular piece of an RDF datasource by setting the ref attribute.
Example 6.7.4
<window
id="example-window"
title="History List"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ANIMALS="http://www.some-ficticious-zoo.com/rdf#"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<popupset>
<popup id="mammals" datasources="animals.rdf" ref="urn:animals:mammals">
<template>
<rule ANIMALS:specimins="0"></rule>
<rule>
<menuitem uri="rdf:*" value="rdf:http://www.some-ficticious-zoo.com/rdf#name"/>
</rule>
</template>
</popup>
</popupset>
<box popup="mammals">
<button value="Click here to see the mammals the zoo has"/>
</box>
</window>
|
In this case only the mammals are desired, so we select the URI of the mammals list. You will notice that the value of the ref attribute in the example is urn:animals:mammals which corresponds to one of the Seq elements in the RDF file. This causes only the descendants of this list to be returned.
Two rules have been used here. The first rule catches all the resources that have their ANIMALS:specimins attribute set to 0. You can see this attribute in the RDF file on each of the Description elements. Some of them have a value of 0. So in these cases, rule one will match. Because rule one has no content, nothing will be displayed for these ones. This is an effective way to hide data that we don't want to display.
The second rule applies to all other resources and creates a row in a popup menu. The end effect is that we get a popup menu containing all the mammals which have a specimin that is not 0.
(Next) Next, we'll find out how to save the state of XUL elements.
Examples: 6.7.1 6.7.2 6.7.3 6.7.4