~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/extjs/source/data/XmlReader.js

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 3.0 RC2
 
3
 * Copyright(c) 2006-2009, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
/**
 
10
 * @class Ext.data.XmlReader
 
11
 * @extends Ext.data.DataReader
 
12
 * <p>Data reader class to create an Array of {@link Ext.data.Record} objects from an XML document
 
13
 * based on mappings in a provided {@link Ext.data.Record} constructor.</p>
 
14
 * <p><b>Note</b>: that in order for the browser to parse a returned XML document, the Content-Type
 
15
 * header in the HTTP response must be set to "text/xml" or "application/xml".</p>
 
16
 * <p>Example code:</p>
 
17
 * <pre><code>
 
18
var Employee = Ext.data.Record.create([
 
19
   {name: 'name', mapping: 'name'},     // "mapping" property not needed if it is the same as "name"
 
20
   {name: 'occupation'}                 // This field will use "occupation" as the mapping.
 
21
]);
 
22
var myReader = new Ext.data.XmlReader({
 
23
   totalRecords: "results", // The element which contains the total dataset size (optional)
 
24
   record: "row",           // The repeated element which contains row information
 
25
   id: "id"                 // The element within the row that provides an ID for the record (optional)
 
26
}, Employee);
 
27
</code></pre>
 
28
 * <p>
 
29
 * This would consume an XML file like this:
 
30
 * <pre><code>
 
31
&lt;?xml version="1.0" encoding="UTF-8"?>
 
32
&lt;dataset>
 
33
 &lt;results>2&lt;/results>
 
34
 &lt;row>
 
35
   &lt;id>1&lt;/id>
 
36
   &lt;name>Bill&lt;/name>
 
37
   &lt;occupation>Gardener&lt;/occupation>
 
38
 &lt;/row>
 
39
 &lt;row>
 
40
   &lt;id>2&lt;/id>
 
41
   &lt;name>Ben&lt;/name>
 
42
   &lt;occupation>Horticulturalist&lt;/occupation>
 
43
 &lt;/row>
 
44
&lt;/dataset>
 
45
</code></pre>
 
46
 * @cfg {String} totalRecords The DomQuery path from which to retrieve the total number of records
 
47
 * in the dataset. This is only needed if the whole dataset is not passed in one go, but is being
 
48
 * paged from the remote server.
 
49
 * @cfg {String} record The DomQuery path to the repeated element which contains record information.
 
50
 * @cfg {String} success The DomQuery path to the success attribute used by forms.
 
51
 * @cfg {String} idPath The DomQuery path relative from the record element to the element that contains
 
52
 * a record identifier value.
 
53
 * @constructor
 
54
 * Create a new XmlReader.
 
55
 * @param {Object} meta Metadata configuration options
 
56
 * @param {Object} recordType Either an Array of field definition objects as passed to
 
57
 * {@link Ext.data.Record#create}, or a Record constructor object created using {@link Ext.data.Record#create}.
 
58
 */
 
59
Ext.data.XmlReader = function(meta, recordType){
 
60
    meta = meta || {};
 
61
    Ext.data.XmlReader.superclass.constructor.call(this, meta, recordType || meta.fields);
 
62
};
 
63
Ext.extend(Ext.data.XmlReader, Ext.data.DataReader, {
 
64
    /**
 
65
     * This method is only used by a DataProxy which has retrieved data from a remote server.
 
66
     * @param {Object} response The XHR object which contains the parsed XML document.  The response is expected
 
67
     * to contain a property called <tt>responseXML</tt> which refers to an XML document object.
 
68
     * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
 
69
     * a cache of Ext.data.Records.
 
70
     */
 
71
    read : function(response){
 
72
        var doc = response.responseXML;
 
73
        if(!doc) {
 
74
            throw {message: "XmlReader.read: XML Document not available"};
 
75
        }
 
76
        return this.readRecords(doc);
 
77
    },
 
78
 
 
79
    /**
 
80
     * Create a data block containing Ext.data.Records from an XML document.
 
81
     * @param {Object} doc A parsed XML document.
 
82
     * @return {Object} records A data block which is used by an {@link Ext.data.Store} as
 
83
     * a cache of Ext.data.Records.
 
84
     */
 
85
    readRecords : function(doc){
 
86
        /**
 
87
         * After any data loads/reads, the raw XML Document is available for further custom processing.
 
88
         * @type XMLDocument
 
89
         */
 
90
        this.xmlData = doc;
 
91
        var root = doc.documentElement || doc;
 
92
        var q = Ext.DomQuery;
 
93
        var recordType = this.recordType, fields = recordType.prototype.fields;
 
94
        var sid = this.meta.idPath || this.meta.id;
 
95
        var totalRecords = 0, success = true;
 
96
        if(this.meta.totalRecords){
 
97
            totalRecords = q.selectNumber(this.meta.totalRecords, root, 0);
 
98
        }
 
99
 
 
100
        if(this.meta.success){
 
101
            var sv = q.selectValue(this.meta.success, root, true);
 
102
            success = sv !== false && sv !== 'false';
 
103
        }
 
104
        var records = [];
 
105
        var ns = q.select(this.meta.record, root);
 
106
        for(var i = 0, len = ns.length; i < len; i++) {
 
107
            var n = ns[i];
 
108
            var values = {};
 
109
            var id = sid ? q.selectValue(sid, n) : undefined;
 
110
            for(var j = 0, jlen = fields.length; j < jlen; j++){
 
111
                var f = fields.items[j];
 
112
                var v = q.selectValue(Ext.value(f.mapping, f.name, true), n, f.defaultValue);
 
113
                v = f.convert(v, n);
 
114
                values[f.name] = v;
 
115
            }
 
116
            var record = new recordType(values, id);
 
117
            record.node = n;
 
118
            records[records.length] = record;
 
119
        }
 
120
 
 
121
        return {
 
122
            success : success,
 
123
            records : records,
 
124
            totalRecords : totalRecords || records.length
 
125
        };
 
126
    },
 
127
 
 
128
    // TODO: implement readResponse for XmlReader
 
129
    readResponse : Ext.emptyFn
 
130
});
 
 
b'\\ No newline at end of file'