RDFHelper.jsm
Go to the documentation of this file.
1 
55 EXPORTED_SYMBOLS = [ "RDFHelper" ];
56 
57 Ci = Components.interfaces;
58 Cc = Components.classes;
59 
60 // make a constructor
61 function RDFHelper(aRdf, aDatasource, aResource, aNamespaces) {
62  // this is a Crockfordian constructor with private methods.
63  // see: http://www.crockford.com/javascript/private.html
64  // the actual construction logic takes place after all these method def'ns.
65  // this enables me to hide these methods from the outside world so that
66  // users of the class can iterate over properties without seeing them.
67  var that = this; // for use in private functions
68  this.Value = aResource.Value; // TODO: is this Good Enough?
69 
70  _containerUtils = Cc["@mozilla.org/rdf/container-utils;1"]
71  .getService(Ci.nsIRDFContainerUtils);
72 
73  var createProperties = function() {
74  //dump("Resource "+that._resource.Value+" is a ")
75  if (_containerUtils.IsContainer(aDatasource, aResource)) {
76  //dump("container.\n");
77  createContainerProperties(aResource);
78  }
79  else {
80  //dump("normal resource.\n");
81  createStandardProperties(aResource);
82  }
83  };
84 
85  var createContainerProperties = function(resource) {
86  var container = _containerUtils.MakeSeq(aDatasource, resource);
87  var contents = container.GetElements();
88 
89  // urgh, this doesn't actually mean "this" is an array
90  // but at least it's sort of like one.
91  var i = 0;
92  while (contents.hasMoreElements()) {
93  var resource = contents.getNext()
94  resource.QueryInterface(Ci.nsIRDFResource);
95  that[i] = new RDFHelper(
96  aRdf,
97  aDatasource,
98  resource,
99  aNamespaces
100  );
101  i++;
102  }
103  that.length = i;
104  };
105 
106  var createStandardProperties = function(resource) {
107  var labels = aDatasource.ArcLabelsOut(aResource);
108  while(labels.hasMoreElements()) {
109  var arc = labels.getNext().QueryInterface(Ci.nsIRDFResource)
110  createStandardProperty(arc);
111  }
112  };
113 
114  var createStandardProperty = function(arc) {
115  var alias = arc.Value;
116  for (n in aNamespaces) {
117  alias = alias.replace(n, aNamespaces[n]);
118  }
119 
120  //dump("Arc "+arc.Value+" is a normal prop aliased to "+alias+".\n")
121 
122  // define a little get-result function
123  // this lets us lazily evaluate resource links so that we don't
124  // descend into infinite recursion unless we work at it and try
125  // to depth-first search a cyclical graph or something
126  var getResult = function() {
127  ary = [];
128  var itr = aDatasource.GetTargets(aResource, arc, true);
129  while(itr.hasMoreElements()) {
130  var resource = itr.getNext();
131  if (resource instanceof Ci.nsIRDFLiteral) {
132  //dump(resource.Value + "is a literal property for "+alias+".\n");
133  ary.push(resource.Value);
134  }
135  else {
136  //dump(resource.Value + " inserted an RDF property.\n");
137  ary.push(new RDFHelper(
138  aRdf,
139  aDatasource,
140  resource,
141  aNamespaces
142  ));
143  }
144  }
145  // this turns out to be a really horrible idea if you want to write nice
146  // code.
147  // but it's a nice idea and i'd like to talk to someone about whether or
148  // not it's worth salvaging (pvh jan08)
149  /*if (ary.length == 1) {
150  ary = ary[0];
151  }*/
152 
153  // memoize the result to avoid n^2 iterations
154  delete that[alias];
155  that[alias] = ary;
156  return ary;
157  }
158 
159  that.__defineGetter__(alias, getResult);
160  // TODO: do i really want to keep the original name?
161  // i mean, really? it really ruins for (i in obj) syntax.
162  /*if (alias != arc.Value) {
163  that.__defineGetter__(arc.Value, getResult);
164  }*/
165  };
166 
167  createProperties();
168 }
169 
170 RDFHelper.help = function(datasource, resource, namespaces) {
171  // look up the initial values from the input strings.
172  var rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
173  var helper = new RDFHelper(
174  rdf,
175  rdf.GetDataSourceBlocking(datasource),
176  rdf.GetResource(resource),
177  namespaces
178  );
179  return helper;
180 };
181 
182 RDFHelper.DEFAULT_RDF_NAMESPACES = {
183  "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "",
184  "http://www.mozilla.org/2004/em-rdf#": "",
185  "http://www.songbirdnest.com/2007/addon-metadata-rdf#": ""
186 };
187 
188 RDFHelper.DEFAULT_RDF_NAMESPACES_PREFIXED = {
189  "http://www.w3.org/1999/02/22-rdf-syntax-ns#": "rdf_",
190  "http://www.mozilla.org/2004/em-rdf#": "moz_",
191  "http://www.songbirdnest.com/2007/addon-metadata-rdf#": "sb_"
192 };
Ci
Definition: RDFHelper.jsm:57
Cc
Definition: RDFHelper.jsm:58
function RDFHelper(aRdf, aDatasource, aResource, aNamespaces)
Definition: RDFHelper.jsm:61
Native alias
EXPORTED_SYMBOLS
Definition: RDFHelper.jsm:55
_getSelectedPageStyle s i