|
1 /* |
|
2 Copyright (c) 2009, Yahoo! Inc. All rights reserved. |
|
3 Code licensed under the BSD License: |
|
4 http://developer.yahoo.net/yui/license.txt |
|
5 version: 3.0.0 |
|
6 build: 1549 |
|
7 */ |
|
8 YUI.add('dataschema-text', function(Y) { |
|
9 |
|
10 /** |
|
11 * Provides a DataSchema implementation which can be used to work with delimited text data. |
|
12 * |
|
13 * @module dataschema |
|
14 * @submodule dataschema-text |
|
15 */ |
|
16 |
|
17 /** |
|
18 * Text subclass for the DataSchema Utility. |
|
19 * @class DataSchema.Text |
|
20 * @extends DataSchema.Base |
|
21 * @static |
|
22 */ |
|
23 |
|
24 var LANG = Y.Lang, |
|
25 |
|
26 SchemaText = { |
|
27 |
|
28 ///////////////////////////////////////////////////////////////////////////// |
|
29 // |
|
30 // DataSchema.Text static methods |
|
31 // |
|
32 ///////////////////////////////////////////////////////////////////////////// |
|
33 /** |
|
34 * Applies a given schema to given delimited text data. |
|
35 * |
|
36 * @method apply |
|
37 * @param schema {Object} Schema to apply. |
|
38 * @param data {Object} Text data. |
|
39 * @return {Object} Schema-parsed data. |
|
40 * @static |
|
41 */ |
|
42 apply: function(schema, data) { |
|
43 var data_in = data, |
|
44 data_out = {results:[],meta:{}}; |
|
45 |
|
46 if(LANG.isString(data_in) && LANG.isString(schema.resultDelimiter)) { |
|
47 // Parse results data |
|
48 data_out = SchemaText._parseResults(schema, data_in, data_out); |
|
49 } |
|
50 else { |
|
51 Y.log("Text data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", "dataschema-text"); |
|
52 data_out.error = new Error("Text schema parse failure"); |
|
53 } |
|
54 |
|
55 return data_out; |
|
56 }, |
|
57 |
|
58 /** |
|
59 * Schema-parsed list of results from full data |
|
60 * |
|
61 * @method _parseResults |
|
62 * @param schema {Array} Schema to parse against. |
|
63 * @param text_in {String} Text to parse. |
|
64 * @param data_out {Object} In-progress parsed data to update. |
|
65 * @return {Object} Parsed data object. |
|
66 * @static |
|
67 * @protected |
|
68 */ |
|
69 _parseResults: function(schema, text_in, data_out) { |
|
70 var resultDelim = schema.resultDelimiter, |
|
71 results = [], |
|
72 results_in, fields_in, result, item, fields, field, key, value, i, j, |
|
73 |
|
74 // Delete final delimiter at end of string if there |
|
75 tmpLength = text_in.length-resultDelim.length; |
|
76 if(text_in.substr(tmpLength) == resultDelim) { |
|
77 text_in = text_in.substr(0, tmpLength); |
|
78 } |
|
79 |
|
80 // Split into results |
|
81 results_in = text_in.split(schema.resultDelimiter); |
|
82 |
|
83 for(i=results_in.length-1; i>-1; i--) { |
|
84 result = {}; |
|
85 item = results_in[i]; |
|
86 |
|
87 if(LANG.isString(schema.fieldDelimiter)) { |
|
88 fields_in = item.split(schema.fieldDelimiter); |
|
89 |
|
90 if(LANG.isArray(schema.resultFields)) { |
|
91 fields = schema.resultFields; |
|
92 for(j=fields.length-1; j>-1; j--) { |
|
93 field = fields[j]; |
|
94 key = (!LANG.isUndefined(field.key)) ? field.key : field; |
|
95 value = (!LANG.isUndefined(fields_in[key])) ? fields_in[key] : fields_in[j]; |
|
96 result[key] = Y.DataSchema.Base.parse(value, field); |
|
97 } |
|
98 } |
|
99 |
|
100 } |
|
101 else { |
|
102 result = item; |
|
103 } |
|
104 |
|
105 results[i] = result; |
|
106 } |
|
107 data_out.results = results; |
|
108 |
|
109 return data_out; |
|
110 } |
|
111 }; |
|
112 |
|
113 Y.DataSchema.Text = Y.mix(SchemaText, Y.DataSchema.Base); |
|
114 |
|
115 |
|
116 |
|
117 }, '3.0.0' ,{requires:['dataschema-base']}); |