Tài liệu Creating Applications with Mozilla-Chapter 10. RDF, RDF Tools, and the Content Model-P5 pdf

16 564 0
Tài liệu Creating Applications with Mozilla-Chapter 10. RDF, RDF Tools, and the Content Model-P5 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 10. RDF, RDF Tools, and the Content Model-P5 10.1.3.2. Root resource In Example 10-11, everything you need to display a datasource dynamically is present. The only difference between this dynamically generated version and a static RDF-based template is the datasources="rdf:null", which specifies that the template does not refer to an actual datasource. Data that is edited, rearranged, or changed in a different way is often displayed dynamically in the UI with templates in this manner. 10.5. JSLib RDF Files Working with actual RDF files is not easy. However, JSLib (http://jslib.mozdev.org) provides an RDF file library that can help you develop an RDF-based application. The library provides many types of error checking, as well as a friendly abstraction away from the RDF/XML interfaces of Mozilla (see Section 10.3.11 , later in this chapter). Example 10- 12 shows some common uses of the RDFFile class in JSLib. This functionality can be used in situations in which you have data in RDF that you want to pull out "manually" and use piece by piece (rather than as a whole datasource in a template). Example 10-12. Creating and modifying an RDF file using JSLib var rdfFileURL = 'chrome://jarfly/content/jar.rdf'; var gTreeBody = null; var gListbox = null; var gRDF = null; function onload( ) { fileUtils = new FileUtils( ); path = fileUtils.chrome_to_path(rdfFileURL); if(navigator.platform == "Win32") { path = path.replace(/\//g,"\\"); // Only needed on Windows, until JSLib is fixed } gRDF = new RDFFile(path,'jar:flies','http://mozdev.org/fly- rdf#'); gTreeBody = document.getElementById('tb'); gTreeBody.database.AddDataSource(gRDF.dsource); gListbox = document.getElementById('list'); gListbox.database.AddDataSource(gRDF.dsource); rebuildLists( ); } function rebuildLists( ) { gTreeBody.builder.rebuild( ); gListbox.builder.rebuild( ); } function update( ) { name = document.getElementById('nameField').value; color = document.getElementById('colorField').value; quantity = document.getElementById('quantityField').value; seqNumber = -1; del = false; replace = false; if(document.getElementById('delete').checked) del = true; if(document.getElementById('replace').checked) replace = true; var seqLength = 0; if(gRDF.doesSeqExist('types')) { seqLength = gRDF.getSeqSubNodes('types').length; //if(del)gRDF.removeSeq('types',false); } else gRDF.addSeq('types'); for(i=0;i<seqLength;i++) { tempItem = 'types:_' + (i+1); if(gRDF.getAttribute(tempItem,'name')==name) seqNumber = gRDF.getAttribute(tempItem,'number'); } if(seqNumber == -1) { item = 'types:_' + (seqLength+1); gRDF.setAttribute(item,'name',name); gRDF.setAttribute(item,'number',seqLength+1); } else { item = 'types:_' + seqNumber; gRDF.setAttribute(item,'number',seqNumber); } if(color!='') gRDF.setAttribute(item,'color',color); if(quantity!='') { gRDF.setAttribute(item,'quantity',quantity); gRDF.setAttribute(item,'dead',calcDead(quantity,rep lace)); } if(!del) gRDF.addNode(item); else gRDF.removeNode(item); gRDF.flush( ); onload( ); } function calcDead(quantity,replace) { if(!replace) { v = parseInt( (quantity * Math.random( )) * 0.13 ); return (v.toString( )); } else return 0; } function changeC(color) { document.getElementById('colorField').value=color; } function changeQ(quantity) { document.getElementById('quantityField').value=quan tity; } This example contains a datasource that represents a collection of flies. These flies are built up dynamically with JavaScript objects from the RDF library, which represent the datasource itself (gRDF = new RDFFile), methods that view and update the data (if(gRDF.getAttribute(tempItem,'name')==name), and utilities that make work with RDF files easier (path = fileUtils.chrome_to_path(rdfFileURL)). Example 10-13 initializes and updates a file after it changes. Example 10-13. Initialization var rdfFileURL = 'chrome://jarfly/content/jar.rdf'; var gTreeBody = null; var gListbox = null; var gRDF = null; function onload( ) { fileUtils = new FileUtils( ); path = fileUtils.chrome_to_path(rdfFileURL); if(navigator.platform == "Win32") { path = path.replace(/\//g,"\\"); // Only needed on Windows, until JSLib is fixed } gRDF = new RDFFile(path,'jar:flies','http://mozdev.org/fly- rdf#'); In Example 10-13 , the file URL is set to an RDF file in the chrome area. Note that both a <tree> and a <listbox>, which display the same data in different ways, will be updated with the same datasource. The onload function is called after the main XUL document is loaded. A class called FileUtils is initialized, which will create a path to the RDF file. If the file doesn't already exist, JSLib automatically creates it. Finally, the RDFFile is created by using the path and a root resource identifier, and the "xFly" namespace is used for the data references. Example 10-14 shows that the RDF file is ready to have its data added and deleted. Example 10-14. Data updating function update( ) { var seqLength = 0; if(gRDF.doesSeqExist('types')) { seqLength = gRDF.getSeqSubNodes('types').length; } else gRDF.addSeq('types'); for(i=0;i<seqLength;i++) { tempItem = 'types:_' + (i+1); if(gRDF.getAttribute(tempItem,'name')==name) seqNumber = gRDF.getAttribute(tempItem,'number'); } if(seqNumber == -1) { item = 'types:_' + (seqLength+1); gRDF.setAttribute(item,'name',name); gRDF.setAttribute(item,'number',seqLength+1); } else { item = 'types:_' + seqNumber; gRDF.setAttribute(item,'number',seqNumber); } if(color!='') gRDF.setAttribute(item,'color',color); if(quantity!='') { gRDF.setAttribute(item,'quantity',quantity); gRDF.setAttribute(item,'dead',calcDead(quantity,rep lace)); } if(!del) gRDF.addNode(item); else gRDF.removeNode(item); gRDF.flush( ); onload( ); Example 10-14 contains a modified version of the update function. First, the function checks to see if a sequence called types is in the RDF file. If not, it creates one. Next, it appends an item to the sequence using type:_+(seqLength+1). The same type of container setup was described in the section Section 10.3.10 , earlier in this chapter. The update function then adds the color, quantity, and "dead" properties of that new item in the sequence. Next, it ensures that you actually want to add the item to the RDF file and flushes it out if not. It then recalls the onload function to update the template display. These are the basics of using RDFFile. As you can see, using JSLib for RDF is often much easier than trying to implement a similar setup on your own. More information about RDFFile and the other JSLib libraries can be found at http://jslib.mozdev.org/. 10.6. Manifests The package descriptions, generally called manifests, use RDF to describe new packages and files to Mozilla. They can be added seamlessly because RDF provides a platform-like environment that facilitates the installation and use of new Mozilla software. All packages, including the ones that come preinstalled with Mozilla (such as the browser, the MailNews component, and the en-US language pack), [...]... > < /RDF: Seq> chrome://xfly /content/ xflyOverlay.xul< /RDF: li> < /RDF: Seq> < /RDF: RDF> The manifest in Example 10-16 names the file xflyOverlay.xul as an overlay Then it names tasksOverlay.xul as the base file into which the. .. more information on the theme, such as the author, the theme name, and a description The chrome:packages structure that completes the manifest describes the packages to which this theme should be applied All major components of the Netscape browser are listed in this example including the AIM client that is not a part of Mozilla but is skinned by themes such as Modern 10.6 .1 RDF and Dynamic Overlays... describing them in terms of their relation to other packages The manifests are typically files called contents .rdf, but they may also be called manifest .rdf Example 10-15 presents a contents .rdf file that describes a new skin for Mozilla Example 10-15 Skin manifest ... < /RDF: Description> < /RDF: RDF> As you can see, the manifest is divided up into sections After the preamble, where the XML processing instruction and the namespace declarations are made, an RDF sequence lists all the themes defined or supplemented (since you can create a package updated for only one Mozilla component, such as the browser) by this package This section contains only one RDF: li the modern theme The next... the overlay file are to be "composed," or use RDF to register and load overlay files at runtime This latter method will be used here to add an "xFly" item to the Tools menu of the Mozilla suite of applications Example 10-16 shows the contents .rdf manifest format that alerts Mozilla of the presence of an overlay, its target in the Mozilla application, and the package of which it is a part Example 10-16... from within the browser application, where users can access it easily This is where RDF and dynamic overlays come in The RDF you provide in your package makes it possible for the chrome registry, discussed in Chapter 6, to find, understand, and register your new files Packages must be registered if they are to be skinned, localized, or accessed using the special tools Mozilla provides (e.g., the chrome... < /RDF: Seq> < /RDF: Description> RDF: li resource="urn:mozilla:skin:modern/1.0:aim"/ > < /RDF: Seq> < /RDF: Description> < /RDF: RDF>... xmlns:chrome="http://www.mozilla.org /rdf/ chrome#"> the skins being supplied by this theme > < /RDF: Seq> ... XPConnect to the XPCOM libraries) If you do not register your package by providing the necessary RDF manifests, it cannot be accessed except as a disparate collection of files in the browser's main content window, which is not what you want You can add overlays in Mozilla in two ways: import them explicitly by using an overlay processing instruction at the top of the XUL file into which items in the overlay . Chapter 10. RDF, RDF Tools, and the Content Model-P5 10. 1.3.2. Root resource In Example 10- 11, everything you need to display. only one RDF: li the modern theme. The next section gives more information on the theme, such as the author, the theme name, and a description. The chrome:packages

Ngày đăng: 21/01/2014, 06:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan