2014-11-22 93 views
0

我想讀取github上的firefox插件的隨機install.rdf文件。我無法弄清楚如何解析字符串和讀取屬性。RDFParser讀取字符串

我探索了rdf解析器,但沒有得到任何地方。這是我的失敗嘗試:

var xmlString = '<?xml version="1.0" encoding="utf-8"?><RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"><Description about="urn:mozilla:install-manifest"><em:id>[email protected]</em:id><em:version>initial</em:version><em:type>2</em:type><em:bootstrap>true</em:bootstrap><em:unpack>false</em:unpack><!--Firefox--><em:targetApplication><Description><em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id><em:minVersion>7.0</em:minVersion><em:maxVersion>27.0</em:maxVersion></Description></em:targetApplication><!--Front End MetaData--><em:name>PortableTester</em:name><em:description>Test addon that tries to figure out if Firefox is portable or not.</em:description><em:creator>Noitidart</em:creator><em:optionsURL>options url here</em:optionsURL></Description></RDF>'; 

var rdfParser = Cc['@mozilla.org/rdf/xml-parser;1'].createInstance(Ci.nsIRDFXMLParser); 
var ds = Cc['@mozilla.org/rdf/datasource;1?name=in-memory-datasource'].createInstance(Ci.nsIRDFDataSource); 

var emptyUri = Services.io.newURI('urn:none', null, null); 
rdfParser.parseString(ds, emptyUri, xmlString); 
+0

謝謝所有這些重新打開和重新打開這個MOD。 – Noitidart 2014-11-22 22:44:11

回答

1

看起來它是a process more complicated than it should be。下面的代碼是隻是一個起點

const PREFIX_NS_EM = "http://www.mozilla.org/2004/em-rdf#"; 
var xmlString = '<?xml version="1.0" encoding="utf-8"?><RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"><Description about="urn:mozilla:install-manifest"><em:id>[email protected]</em:id><em:version>initial</em:version><em:type>2</em:type><em:bootstrap>true</em:bootstrap><em:unpack>false</em:unpack><!--Firefox--><em:targetApplication><Description><em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id><em:minVersion>7.0</em:minVersion><em:maxVersion>27.0</em:maxVersion></Description></em:targetApplication><!--Front End MetaData--><em:name>PortableTester</em:name><em:description>Test addon that tries to figure out if Firefox is portable or not.</em:description><em:creator>Noitidart</em:creator><em:optionsURL>options url here</em:optionsURL></Description></RDF>'; 
var rdfParser = Cc['@mozilla.org/rdf/xml-parser;1'].createInstance(Ci.nsIRDFXMLParser); 
var ds = Cc['@mozilla.org/rdf/datasource;1?name=in-memory-datasource'].createInstance(Ci.nsIRDFDataSource); 

var emptyUri = Services.io.newURI('urn:none', null, null); 
rdfParser.parseString(ds, emptyUri, xmlString); 
var resources = ds.GetAllResources() 
while (resources.hasMoreElements()) { 
    let resource = resources.getNext().QueryInterface(Ci.nsIRDFResource); 
    let arcs = ds.ArcLabelsOut(resource); 
    while (arcs.hasMoreElements()) { 
    let arc = arcs.getNext().QueryInterface(Ci.nsIRDFResource); 
    let prop = arc.ValueUTF8.substring(PREFIX_NS_EM.length) 
    let targets = ds.GetTargets(resource, arc, true); 
    while (targets.hasMoreElements()) { 
     let target = targets.getNext(); 
     if (target instanceof Ci.nsIRDFResource) { 
     console.log(prop, "Resource node, recurse"); 
     } 
     else if (target instanceof Ci.nsIRDFLiteral) { 
     console.log(prop, target.Value); //are non ASCII characters encoded? 
     } 
     else if (target instanceof Ci.nsIRDFInt) { 
     console.log(prop, target.Value); 
     } 
    } 
    } 
} 
+0

非常感謝paa!我會分享我在下面使用的,它的工作原理,但它很複雜,所以我希望能有另一個答覆。你的回答有很大幫助! – Noitidart 2014-11-24 15:44:21

+0

我剛剛測試過你,它非常棒,我喜歡它! 用你的方法,我可以列出所有的屬性,而不是事先知道屬性名稱! – Noitidart 2014-11-24 15:52:44

0

這對我的作品,但很複雜,@ PAA的答案有助於瞭解的東西,在你需要知道前手的屬性名PAA的其不同的,那麼你可以調用以獲得它的價值。

//start boilerplate stuff straing ocpied from https://github.com/johan/greasemonkey-old-partial-history/blob/2431aa17187e7a84ed2f7a5f5f757bcc4a34e4d1/src/chrome/chromeFiles/content/updater.js#L198 
var RDFURI_INSTALL_MANIFEST_ROOT = 'urn:mozilla:install-manifest'; 
var PREFIX_NS_EM = 'http://www.mozilla.org/2004/em-rdf#'; 
var PROP_METADATA = ["id", "version", "type", "internalName", "updateURL", "updateKey", "optionsURL", "optionsType", "aboutURL", "iconURL", "icon64URL"]; 

var gRDF = Cc['@mozilla.org/rdf/rdf-service;1'].getService(Ci.nsIRDFService) 

function EM_R(aProperty) { 
    return gRDF.GetResource(PREFIX_NS_EM + aProperty); 
} 

function getRDFValue(aLiteral) { 
    if (aLiteral instanceof Ci.nsIRDFLiteral) 
     return aLiteral.Value; 
    if (aLiteral instanceof Ci.nsIRDFResource) 
     return aLiteral.Value; 
    if (aLiteral instanceof Ci.nsIRDFInt) 
     return aLiteral.Value; 
    return null; 
} 

function getRDFProperty(aDs, aResource, aProperty) { 
     return getRDFValue(aDs.GetTarget(aResource, EM_R(aProperty), true)); 
} 
//end boilerplate stuff straing ocpied from https://github.com/johan/greasemonkey-old-partial-history/blob/2431aa17187e7a84ed2f7a5f5f757bcc4a34e4d1/src/chrome/chromeFiles/content/updater.js#L198 

//start - my personal stuff, all the above is just boilerplate 
var xmlString = '<?xml version="1.0" encoding="utf-8"?><RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"><Description about="urn:mozilla:install-manifest"><em:id>[email protected]</em:id><em:version>initial</em:version><em:type>2</em:type><em:bootstrap>true</em:bootstrap><em:unpack>false</em:unpack><!--Firefox--><em:targetApplication><Description><em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id><em:minVersion>7.0</em:minVersion><em:maxVersion>27.0</em:maxVersion></Description></em:targetApplication><!--Front End MetaData--><em:name>PortableTester</em:name><em:description>Test addon that tries to figure out if Firefox is portable or not.</em:description><em:creator>Noitidart</em:creator><em:optionsURL>options url here</em:optionsURL></Description></RDF>'; 

var uri = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI('urn:empty', null, null); 
var parser = Cc["@mozilla.org/rdf/xml-parser;1"].createInstance(Ci.nsIRDFXMLParser); 
var memoryDS = Cc["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance(Ci.nsIRDFDataSource); 
parser.parseString(memoryDS, uri, xmlString); 

var dsResources = memoryDS.GetAllResources(); 

let root = gRDF.GetResource(RDFURI_INSTALL_MANIFEST_ROOT); 

let addon = {}; 
addon.unpack = getRDFProperty(ds, root, 'unpack'); 
PROP_METADATA.forEach(function(aProp) { 
    addon[aProp] = getRDFProperty(ds, root, aProp); 
}); 
console.log('addon:', addon);