001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.data;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013:
014: /**
015: * Data reader class to create an Array of{@link Record} objects from an XML document based on mappings.
016: * <p/>
017: * <p/>
018: * Note that in order for the browser to parse a returned XML document, the Content-Type header in the HTTP response must be set to "text/xml".
019: * Example code :
020: * <p/>
021: * <pre>
022: * <code>
023: * <p/>
024: * RecordDef recordDef = new RecordDef(new FieldDef[] {
025: * new StringFieldDef("name", "name"), // "mapping" property not needed if it's the same as "name"
026: * new StringFieldDef("occupation") // This field will use "occupation" as the mapping.
027: * });
028: * XmlReaderConfig config = new XmlReaderConfig();
029: * config.setRecord("row");
030: * config.setId("id");
031: * config.setTotalRecords("results");
032: * <p/>
033: * XmlReader reader = new XmlReader(config, recordDef);
034: * </code>
035: * </pre>
036: * <p/>
037: * This would consume XML like:
038: * <pre>
039: * <code>
040: * <p/>
041: * <?xml?>
042: * <dataset>
043: * <results>2</results>
044: * <row>
045: * <id>1</id>
046: * <name>Bill</name>
047: * <occupation>Gardener</occupation>
048: * </row>
049: * <row>
050: * <id>2</id>
051: * <name>Ben</name>
052: * <occupation>Horticulturalist</occupation>
053: * </row>
054: * </dataset></code></pre>
055: */
056: public class XmlReader extends Reader {
057:
058: /**
059: * Construct a new XmlReader.
060: *
061: * @param record the name of the record tag in the XML data
062: * @param recordDef the record def
063: */
064: public XmlReader(String record, RecordDef recordDef) {
065: setRecord(record);
066: setRecordDef(recordDef);
067: }
068:
069: protected native JavaScriptObject create(JavaScriptObject config,
070: JavaScriptObject recordDef) /*-{
071: return new $wnd.Ext.data.XmlReader(config, recordDef);
072: }-*/;
073:
074: //config
075: /**
076: * The {@link com.gwtext.client.core.DomQuery} path relative from the record element to the element that contains a record identifier value..
077: * The simples case is the tag name in the XML that maps to the Record ID
078: *
079: * @param id the ID
080: */
081: public void setId(String id) {
082: JavaScriptObjectHelper.setAttribute(configJS, "id", id);
083: }
084:
085: /**
086: * The {@link com.gwtext.client.core.DomQuery} path to the repeated element which contains record information. The simples
087: * case is the tag name in the XML that maps to the root tag of what corresponnds to a "record".
088: *
089: * @param record the record mapping
090: */
091: public void setRecord(String record) {
092: JavaScriptObjectHelper.setAttribute(configJS, "record", record);
093: }
094:
095: /**
096: * The {@link com.gwtext.client.core.DomQuery} path to the success attribute used by forms.
097: *
098: * @param success the success {@link com.gwtext.client.core.DomQuery} path.
099: */
100: public void setSuccess(String success) {
101: JavaScriptObjectHelper.setAttribute(configJS, "success",
102: success);
103: }
104:
105: /**
106: * The {@link com.gwtext.client.core.DomQuery} path from which to retrieve the total number of records in the dataset.
107: * This is only needed if the whole dataset is not passed in one go, but is being paged from the remote server.
108: *
109: * @param totalRecords the totalRecords DomQuery path
110: */
111: public void setTotalRecords(String totalRecords) {
112: JavaScriptObjectHelper.setAttribute(configJS, "totalRecords",
113: totalRecords);
114: }
115: }
|