Creating Applications with Mozilla-Chapter 10. RDF, RDF Tools, and the Content Model-P5

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:16

0
54
lượt xem
7
download

Creating Applications with Mozilla-Chapter 10. RDF, RDF Tools, and the Content Model-P5

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'creating applications with mozilla-chapter 10. rdf, rdf tools, and the content model-p5', công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Creating Applications with Mozilla-Chapter 10. RDF, RDF Tools, and the Content Model-P5

  1. 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;
  2. 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( );
  3. 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);
  4. } else gRDF.addSeq('types'); for(i=0;i
  5. 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( ));
  6. } 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.
  7. 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 and a , 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.
  8. 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
  9. 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);
  10. 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),
  11. have manifests 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
  12. 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
  13. the browser) by this package. This section contains 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 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 Manifests can also add new menu items to existing Mozilla menus. When you add a new package to Mozilla, you should make it accessible 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 URL or 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 file are to be "composed," or use RDF to register and
  14. 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. Overlay for a sample application menu
  15. chrome://xfly/content/xflyOverlay.xul 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 contents are placed. In this case, the overlays can overlay other overlay files arbitrarily. An overlay can define new content anywhere in the application. Overlays are often responsible for putting new items in menus. As long as the target and overlay ids match, any two RDF datasources are merged. You can try this example by putting a single new menu item in an overlay structure like the one shown in Example 10-17. Save it as xflyOverlay.xul in the xfly content subdirectory and use the manifest information in Example 10-16 as part of the packaging process described in Chapter 6. Example 10-17. Overlay for an xFly menu item in the browser
  16. The menupopup in Mozilla with the ID "tools_menu" gets a new menu item when this overlay is processed and its content included.
Đồng bộ tài khoản