rdf query javascript question

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

rdf query javascript question

Daniel Wood-2
Hello Everyone!

As my first rdf experiment, I created a very simple local chrome app
with basically has a rdf file and a javascript file which pulls two
values from the local rdf file and writes them to the main document.  I
can get it to work, but I had to play with the javascript a bit.

The following is the key code snippet which WORKS :

// begin

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

    var main_ds =
rdfService.GetDataSourceBlocking("chrome://test/content/rdf/main.rdf");

    var subject =
rdfService.GetResource("http://www.digitaljiggery.com/test/subject");
    var t;
    var version;
    var appname;

    t = this.main_ds.GetTarget(subject,

rdfService.GetResource("http://www.digitaljiggery.com/test/version"),
                               true);

    if (t instanceof Components.interfaces.nsIRDFLiteral){
      version = t.Value;
    }

    t = this.main_ds.GetTarget(subject,

rdfService.GetResource("http://www.digitaljiggery.com/test/appName"),
                               true);

    if (t instanceof Components.interfaces.nsIRDFLiteral){
      appname = t.Value;
    }

    document.write("Welcome to " + appname + " v" + version);

// end

Running that in my pages produces a correct 'welcome to' message with
the data from the local rdf file.

my question / confusion is that if don't do the two 'instanceof'
checks, but just _assume_ t is a literal, it doesn't work.  here is the
snippet of code for the one which does NOT work :

// begin

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

    var main_ds =
rdfService.GetDataSourceBlocking("chrome://test/content/rdf/main.rdf");

    var subject =
rdfService.GetResource("http://www.digitaljiggery.com/test/subject");
    var t;
    var version;
    var appname;

    t = this.main_ds.GetTarget(subject,

rdfService.GetResource("http://www.digitaljiggery.com/test/version"),
                               true);

    version = t.Value;

    t = this.main_ds.GetTarget(subject,

rdfService.GetResource("http://www.digitaljiggery.com/test/appName"),
                               true);

    appname = t.Value;

    document.write("Welcome to " + appname + " v" + version);

// end

that gets me :  "Welcome to undefined vundefined"

Can anyone help me understand why the first works and the 2nd does not?

in case it helps, I'm running this in "Mozilla/5.0 (Windows; U; Windows
NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
XpcomViewer/0.9"

thanks in advance!
daniel

_______________________________________________
dev-tech-rdf mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-rdf
Reply | Threaded
Open this post in threaded view
|

Re: rdf query javascript question

Neil Deakin
[hidden email] wrote:

> Running that in my pages produces a correct 'welcome to' message with
> the data from the local rdf file.
>
> my question / confusion is that if don't do the two 'instanceof'
> checks, but just _assume_ t is a literal, it doesn't work.  here is the
> snippet of code for the one which does NOT work :
>

Because GetTarget returns an nsIRDFNode, which may or may not be an
nsIRDFLiteral. The instanceof operator calls QueryInterface on the
object which then makes it a literal.

Neil
_______________________________________________
dev-tech-rdf mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-rdf