|
@@ -0,0 +1,575 @@
|
|
1
|
+
|
|
2
|
+ * Author: Heidi Pan <heidi.pan@intel.com>
|
|
3
|
+ * Copyright (c) 2015 Intel Corporation.
|
|
4
|
+ *
|
|
5
|
+ * Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+ * a copy of this software and associated documentation files (the
|
|
7
|
+ * "Software"), to deal in the Software without restriction, including
|
|
8
|
+ * without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+ * distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+ * permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+ * the following conditions:
|
|
12
|
+ *
|
|
13
|
+ * The above copyright notice and this permission notice shall be
|
|
14
|
+ * included in all copies or substantial portions of the Software.
|
|
15
|
+ *
|
|
16
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+ */
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+var peg = require('pegjs')
|
|
27
|
+ , fs = require('fs')
|
|
28
|
+ , path = require('path')
|
|
29
|
+ , Promise = require('bluebird')
|
|
30
|
+ , _ = require('lodash')
|
|
31
|
+ , util = require('util');
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+Promise.promisifyAll(fs);
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+var xml2js = {
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+ MODULE: '',
|
|
84
|
+ ENUMS: {},
|
|
85
|
+ ENUMS_BY_GROUP: {},
|
|
86
|
+ METHODS: {},
|
|
87
|
+ CLASSES: {},
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+ TYPEMAPS: {
|
|
92
|
+ '^(const)?\\s*(unsigned|signed)?\\s*(int|short|long|float|double|size_t|u?int\\d{1,2}_t)?$': 'Number',
|
|
93
|
+ '^bool$': 'Boolean',
|
|
94
|
+ '^(const)?\\s*(unsigned|signed)?\\s*(char|char\\s*\\*|std::string)$': 'String'
|
|
95
|
+ },
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+ addOptions: function(opts) {
|
|
100
|
+ xml2js.opts = opts;
|
|
101
|
+ return opts
|
|
102
|
+ .option('-i, --inputdir [directory]', 'directory for xml files', __dirname + '/xml/mraa')
|
|
103
|
+ .option('-c, --custom [file]', 'json for customizations', __dirname + '/custom.json')
|
|
104
|
+ .option('-s, --strict', 'leave out methods/variables if unknown type')
|
|
105
|
+ },
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+ parse: function() {
|
|
111
|
+ var XML_GRAMMAR_SPEC = 'grammars/xml.peg';
|
|
112
|
+ var NAMESPACE_SPEC = xml2js.opts.inputdir + '/namespace' + xml2js.opts.module + '.xml';
|
|
113
|
+ var CLASS_SPEC = function(c) { return xml2js.opts.inputdir + '/' + c + '.xml'; }
|
|
114
|
+ var TYPES_SPEC = xml2js.opts.inputdir + '/types_8h.xml';
|
|
115
|
+ xml2js.MODULE = xml2js.opts.module;
|
|
116
|
+ return Promise.join(createXmlParser(XML_GRAMMAR_SPEC),
|
|
117
|
+ fs.readFileAsync(NAMESPACE_SPEC, 'utf8'),
|
|
118
|
+ fs.existsSync(TYPES_SPEC) ? fs.readFileAsync(TYPES_SPEC, 'utf8') : Promise.resolve(null),
|
|
119
|
+ function(xmlparser, xml, xml_types) {
|
|
120
|
+ if (xml_types != null) {
|
|
121
|
+ _.extend(xml2js.ENUMS, getEnums(xmlparser.parse(xml_types)[0], false));
|
|
122
|
+ _.extend(xml2js.ENUMS_BY_GROUP, getEnums(xmlparser.parse(xml_types)[0], true));
|
|
123
|
+ }
|
|
124
|
+ var spec_c = xmlparser.parse(xml)[0];
|
|
125
|
+ _.extend(xml2js.ENUMS, getEnums(spec_c, false));
|
|
126
|
+ _.extend(xml2js.ENUMS_BY_GROUP, getEnums(spec_c, true));
|
|
127
|
+ _.extend(xml2js.METHODS, getMethods(spec_c));
|
|
128
|
+ return Promise.all(_.map(getClasses(spec_c), function(c) {
|
|
129
|
+ return fs.readFileAsync(CLASS_SPEC(c), 'utf8').then(function(xml) {
|
|
130
|
+ try {
|
|
131
|
+ var spec_c = xmlparser.parse(xml)[0];
|
|
132
|
+ var className = getClassName(spec_c);
|
|
133
|
+ xml2js.CLASSES[className] = {
|
|
134
|
+ description: getClassDescription(spec_c),
|
|
135
|
+ enums: getEnums(spec_c, false, className),
|
|
136
|
+ enums_by_group: getEnums(spec_c, true, className),
|
|
137
|
+ variables: getVariables(spec_c, className),
|
|
138
|
+ methods: getMethods(spec_c, className),
|
|
139
|
+ };
|
|
140
|
+ } catch(e) {
|
|
141
|
+ console.log(e.toString() + ': ' + c + ' was not parsed correctly.');
|
|
142
|
+ }
|
|
143
|
+ });
|
|
144
|
+ })).then(function() {
|
|
145
|
+ if (fs.existsSync(xml2js.opts.custom)) {
|
|
146
|
+ return fs.readFileAsync(xml2js.opts.custom, 'utf8').then(function(custom) {
|
|
147
|
+ try {
|
|
148
|
+ customizeMethods(JSON.parse(custom));
|
|
149
|
+ } catch(e) {
|
|
150
|
+ console.log('invalid custom.json, ignored. ' + e.toString());
|
|
151
|
+ }
|
|
152
|
+ });
|
|
153
|
+ } else {
|
|
154
|
+ console.log((xml2js.opts.custom == __dirname + '/custom.json') ? 'No customizations given.' : 'Error: No such customization file exists: ' + xml2js.opts.custom);
|
|
155
|
+ }
|
|
156
|
+ }).then(function() {
|
|
157
|
+ validateMethods();
|
|
158
|
+ validateVars();
|
|
159
|
+ return _.pick(xml2js, 'MODULE', 'ENUMS', 'ENUMS_BY_GROUP', 'METHODS', 'CLASSES');
|
|
160
|
+ });
|
|
161
|
+ });
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+};
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+function createXmlParser(XML_GRAMMAR_SPEC) {
|
|
169
|
+ return fs.readFileAsync(XML_GRAMMAR_SPEC, 'utf8').then(function(xmlgrammar) {
|
|
170
|
+ return peg.buildParser(xmlgrammar);
|
|
171
|
+ });
|
|
172
|
+}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+function customizeMethods(custom) {
|
|
177
|
+ _.each(custom, function(classMethods, className) {
|
|
178
|
+ _.extend(xml2js.CLASSES[className].methods, _.pick(classMethods, function(methodSpec, methodName) {
|
|
179
|
+ return isValidMethodSpec(methodSpec, className + '.' + methodName);
|
|
180
|
+ }));
|
|
181
|
+ });
|
|
182
|
+}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+function validateMethods() {
|
|
186
|
+ xml2js.METHODS = _.pick(xml2js.METHODS, function(methodSpec, methodName) {
|
|
187
|
+ hasValidTypes(methodSpec, methodName);
|
|
188
|
+ });
|
|
189
|
+ _.each(xml2js.CLASSES, function(classSpec, className) {
|
|
190
|
+ var valid = _.pick(classSpec.methods, function(methodSpec, methodName) {
|
|
191
|
+ return hasValidTypes(methodSpec, className + '.' + methodName, className);
|
|
192
|
+ });
|
|
193
|
+ if (xml2js.opts.strict) {
|
|
194
|
+ xml2js.CLASSES[className].methods = valid;
|
|
195
|
+ }
|
|
196
|
+ });
|
|
197
|
+}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+function validateVars() {
|
|
202
|
+ _.each(xml2js.CLASSES, function(classSpec, className) {
|
|
203
|
+ var valid = _.pick(classSpec.variables, function(varSpec, varName) {
|
|
204
|
+ return ofValidType(varSpec, className + '.' + varName, className);
|
|
205
|
+ });
|
|
206
|
+ if (xml2js.opts.strict) {
|
|
207
|
+ xml2js.CLASSES[className].variables = valid;
|
|
208
|
+ }
|
|
209
|
+ });
|
|
210
|
+}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+function isValidMethodSpec(methodSpec, methodName) {
|
|
215
|
+ var valid = true;
|
|
216
|
+ var printIgnoredMethodOnce = _.once(function() { console.log(methodName + ' from ' + path.basename(xml2js.opts.custom) + ' is omitted from JS documentation.'); });
|
|
217
|
+ function checkRule(rule, errMsg) {
|
|
218
|
+ if (!rule) {
|
|
219
|
+ printIgnoredMethodOnce();
|
|
220
|
+ console.log(' ' + errMsg);
|
|
221
|
+ valid = false;
|
|
222
|
+ }
|
|
223
|
+ }
|
|
224
|
+ checkRule(_.has(methodSpec, 'description'), 'no description given');
|
|
225
|
+ checkRule(_.has(methodSpec, 'params'), 'no params given (specify "params": {} for no params)');
|
|
226
|
+ _.each(methodSpec.params, function(paramSpec, paramName) {
|
|
227
|
+ checkRule(_.has(paramSpec, 'type'), 'no type given for param ' + paramName);
|
|
228
|
+ checkRule(_.has(paramSpec, 'description'), 'no description given for param ' + paramName);
|
|
229
|
+ });
|
|
230
|
+ checkRule(_.has(methodSpec, 'return'), 'no return given (specify "return": {} for no return value)');
|
|
231
|
+ checkRule(_.has(methodSpec.return, 'type'), 'no type given for return value');
|
|
232
|
+ checkRule(_.has(methodSpec.return, 'description'), 'no description given for return value');
|
|
233
|
+ return valid;
|
|
234
|
+}
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+function getEnums(spec_c, bygroup, parent) {
|
|
239
|
+ var spec_js = {};
|
|
240
|
+ var enumGroups = _.find(getChildren(spec_c, 'sectiondef'), function(section) {
|
|
241
|
+ var kind = getAttr(section, 'kind');
|
|
242
|
+ return ((kind == 'enum') || (kind == 'public-type'));
|
|
243
|
+ });
|
|
244
|
+ if (enumGroups) {
|
|
245
|
+ _.each(enumGroups.children, function(enumGroup) {
|
|
246
|
+ var enumGroupName = getText(getChild(enumGroup, 'name'), 'name');
|
|
247
|
+ var enumGroupDescription = getText(getChild(enumGroup, 'detaileddescription'), 'description');
|
|
248
|
+ var enumGroupVals = getChildren(enumGroup, 'enumvalue');
|
|
249
|
+ if (bygroup) {
|
|
250
|
+ spec_js[enumGroupName] = {
|
|
251
|
+ description: enumGroupDescription,
|
|
252
|
+ members: []
|
|
253
|
+ };
|
|
254
|
+ }
|
|
255
|
+ _.each(enumGroupVals, function(e) {
|
|
256
|
+
|
|
257
|
+ var enumName = getText(getChild(e, 'name'), 'name').replace(/^MRAA_/, '');
|
|
258
|
+ var enumDescription = getText(getChild(e, 'detaileddescription'), 'description');
|
|
259
|
+ if (!bygroup) {
|
|
260
|
+ spec_js[enumName] = {
|
|
261
|
+ type: enumGroupName,
|
|
262
|
+ description: enumDescription
|
|
263
|
+ };
|
|
264
|
+ } else {
|
|
265
|
+ spec_js[enumGroupName].members.push(enumName);
|
|
266
|
+ }
|
|
267
|
+ });
|
|
268
|
+ });
|
|
269
|
+ }
|
|
270
|
+ return spec_js;
|
|
271
|
+}
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+function getClasses(spec_c) {
|
|
276
|
+ return _.map(getChildren(spec_c, 'innerclass'), function(innerclass) {
|
|
277
|
+ return getAttr(innerclass, 'refid');
|
|
278
|
+ });
|
|
279
|
+}
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+function getClassDescription(spec_c) {
|
|
284
|
+ return getText(getChild(spec_c, 'detaileddescription'), 'description');
|
|
285
|
+}
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+function hasParams(paramsSpec) {
|
|
289
|
+ return !(_.isEmpty(paramsSpec) ||
|
|
290
|
+ ((_.size(paramsSpec) == 1) && getText(getChild(paramsSpec[0], 'type')) == 'void'));
|
|
291
|
+}
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+function getMethods(spec_c, parent) {
|
|
298
|
+ var spec_js = {};
|
|
299
|
+ var methods = _.find(getChildren(spec_c, 'sectiondef'), function(section) {
|
|
300
|
+ var kind = getAttr(section, 'kind');
|
|
301
|
+ return ((kind == 'public-func') || (kind == 'func'));
|
|
302
|
+ });
|
|
303
|
+ if (methods) {
|
|
304
|
+ _.each(methods.children, function(method) {
|
|
305
|
+ var methodName = getText(getChild(method, 'name'), 'name');
|
|
306
|
+ if (methodName[0] != '~') {
|
|
307
|
+ try {
|
|
308
|
+ var description = getChild(method, 'detaileddescription');
|
|
309
|
+ var methodDescription = getText(description, 'description');
|
|
310
|
+ var paramsSpec = getChildren(method, 'param');
|
|
311
|
+ var params = {};
|
|
312
|
+ if (hasParams(paramsSpec)) {
|
|
313
|
+ params = getParams(paramsSpec, getParamsDetails(description), (parent ? (parent + '.') : '') + methodName);
|
|
314
|
+ }
|
|
315
|
+ var returnSpec = getChild(method, 'type');
|
|
316
|
+ var retval = {};
|
|
317
|
+ if (!_.isEmpty(returnSpec)) {
|
|
318
|
+ retval = getReturn(returnSpec, getReturnDetails(description));
|
|
319
|
+ }
|
|
320
|
+ spec_js[methodName] = {
|
|
321
|
+ description: methodDescription,
|
|
322
|
+ params: params,
|
|
323
|
+ return: retval
|
|
324
|
+ };
|
|
325
|
+ } catch(e) {
|
|
326
|
+ console.log((parent ? (parent + '.') : '') + methodName + ' is omitted from JS documentation.');
|
|
327
|
+ console.log(' ' + e.toString());
|
|
328
|
+ }
|
|
329
|
+ }
|
|
330
|
+ });
|
|
331
|
+ }
|
|
332
|
+ return spec_js;
|
|
333
|
+}
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+function getVariables(spec_c, parent) {
|
|
338
|
+ var spec_js = {};
|
|
339
|
+ var vars = _.find(getChildren(spec_c, 'sectiondef'), function(section) {
|
|
340
|
+ var kind = getAttr(section, 'kind');
|
|
341
|
+ return (kind == 'public-attrib');
|
|
342
|
+ });
|
|
343
|
+ if (vars) {
|
|
344
|
+ _.each(_.filter(vars.children, function(variable) {
|
|
345
|
+ return (getAttr(variable, 'kind') == 'variable');
|
|
346
|
+ }), function(variable) {
|
|
347
|
+ var varName = getText(getChild(variable, 'name'), 'name');
|
|
348
|
+ var varType = getType(getText(getChild(variable, 'type')));
|
|
349
|
+ var varDescription = getText(getChild(variable, 'detaileddescription'));
|
|
350
|
+ spec_js[varName] = {
|
|
351
|
+ type: varType,
|
|
352
|
+ description: varDescription
|
|
353
|
+ }
|
|
354
|
+ });
|
|
355
|
+ }
|
|
356
|
+ return spec_js;
|
|
357
|
+}
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+function getReturn(spec_c, details) {
|
|
362
|
+ var retType = getType(getText(spec_c, 'type'));
|
|
363
|
+ var retDescription = (details ? getText(details, 'description') : '');
|
|
364
|
+ return ((retType == 'void') ? {} : {
|
|
365
|
+ type: retType,
|
|
366
|
+ description: retDescription
|
|
367
|
+ });
|
|
368
|
+}
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+function getParams(spec_c, details, method) {
|
|
373
|
+ var spec_js = {};
|
|
374
|
+ _.each(spec_c, function(param) {
|
|
375
|
+ try {
|
|
376
|
+ var paramType = getType(getText(getChild(param, 'type'), 'type'));
|
|
377
|
+ var paramName = getText(getChild(param, 'declname'), 'name');
|
|
378
|
+ spec_js[paramName] = { type: paramType };
|
|
379
|
+ } catch(e) {
|
|
380
|
+ if (paramType == '...') {
|
|
381
|
+ spec_js['arguments'] = { type: paramType };
|
|
382
|
+ } else {
|
|
383
|
+ throw e;
|
|
384
|
+ }
|
|
385
|
+ }
|
|
386
|
+ });
|
|
387
|
+ _.each(details, function(param) {
|
|
388
|
+ var getParamName = function(p) { return getText(getChild(getChild(p, 'parameternamelist'), 'parametername'), 'name'); }
|
|
389
|
+ var paramName = getParamName(param);
|
|
390
|
+ var paramDescription = getText(getChild(param, 'parameterdescription'), 'description');
|
|
391
|
+ if (_.has(spec_js, paramName)) {
|
|
392
|
+ spec_js[paramName].description = paramDescription;
|
|
393
|
+ } else {
|
|
394
|
+ var msg = ' has documentation for an unknown parameter: ' + paramName + '. ';
|
|
395
|
+ var suggestions = _.difference(_.keys(spec_js), _.map(details, getParamName));
|
|
396
|
+ var msgAddendum = (!_.isEmpty(suggestions) ? ('Did you mean ' + suggestions.join(', or ') + '?') : '');
|
|
397
|
+ console.log('Warning: ' + method + msg + msgAddendum);
|
|
398
|
+ }
|
|
399
|
+ });
|
|
400
|
+ return spec_js;
|
|
401
|
+}
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+function getType(type_c) {
|
|
406
|
+ var type_js = type_c;
|
|
407
|
+ _.find(xml2js.TYPEMAPS, function(to, from) {
|
|
408
|
+ var pattern = new RegExp(from, 'i');
|
|
409
|
+ if (type_c.search(pattern) == 0) {
|
|
410
|
+ type_js = to;
|
|
411
|
+ }
|
|
412
|
+ });
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+ if (type_js.search(/\S+\s*\*$/) != -1) {
|
|
417
|
+ type_js = type_js.replace(/\s*\*$/, '*');
|
|
418
|
+ }
|
|
419
|
+ return type_js;
|
|
420
|
+}
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+function hasValidTypes(methodSpec, methodName, parent) {
|
|
425
|
+ var valid = true;
|
|
426
|
+ var msg = (xml2js.opts.strict ? ' is omitted from JS documentation.' : ' has invalid type(s).');
|
|
427
|
+ var printIgnoredMethodOnce = _.once(function() { console.log(methodName + msg); });
|
|
428
|
+ _.each(methodSpec.params, function(paramSpec, paramName) {
|
|
429
|
+ if (!isValidType(paramSpec.type, parent)) {
|
|
430
|
+ valid = false;
|
|
431
|
+ printIgnoredMethodOnce();
|
|
432
|
+ console.log(' Error: parameter ' + paramName + ' has invalid type ' + paramSpec.type);
|
|
433
|
+ }
|
|
434
|
+ });
|
|
435
|
+ if (!_.isEmpty(methodSpec.return) && !isValidType(methodSpec.return.type, parent)) {
|
|
436
|
+ valid = false;
|
|
437
|
+ printIgnoredMethodOnce();
|
|
438
|
+ console.log(' Error: returns invalid type ' + methodSpec.return.type);
|
|
439
|
+ }
|
|
440
|
+ return valid;
|
|
441
|
+}
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+function ofValidType(varSpec, varName, parent) {
|
|
446
|
+ if (isValidType(varSpec.type, parent)) {
|
|
447
|
+ return true;
|
|
448
|
+ } else {
|
|
449
|
+ var msgAddendum = (xml2js.opts.strict ? ' Omitted from JS documentation.' : '');
|
|
450
|
+ console.log('Error: ' + varName + ' is of invalid type ' + varSpec.type + '.' + msgAddendum);
|
|
451
|
+ return false;
|
|
452
|
+ }
|
|
453
|
+}
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+function isValidType(type, parent) {
|
|
459
|
+ return (_.contains(_.values(xml2js.TYPEMAPS), type) ||
|
|
460
|
+ _.has(xml2js.CLASSES, type) ||
|
|
461
|
+ _.has(xml2js.ENUMS_BY_GROUP, type) ||
|
|
462
|
+ _.contains(['Buffer', 'Function', 'mraa_result_t'], type) ||
|
|
463
|
+ _.has((parent ? xml2js.CLASSES[parent].enums_by_group : []), type));
|
|
464
|
+}
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+function isPointer(type) {
|
|
469
|
+ return (type.search(/\w+\s*\*/) != -1);
|
|
470
|
+}
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+function getParamsDetails(spec_c) {
|
|
475
|
+ var paras = getChildren(spec_c, 'para');
|
|
476
|
+ var details = _.find(_.map(paras, function(para) {
|
|
477
|
+ return getChild(para, 'parameterlist');
|
|
478
|
+ }), function(obj) { return (obj != undefined); });
|
|
479
|
+ return (details ? details.children : undefined);
|
|
480
|
+}
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+function getReturnDetails(spec_c) {
|
|
485
|
+ var paras = getChildren(spec_c, 'para');
|
|
486
|
+ return _.find(_.map(paras, function(para) {
|
|
487
|
+ return getChild(para, 'simplesect');
|
|
488
|
+ }), function(obj) { return ((obj != undefined) && (getAttr(obj, 'kind') == 'return')); });
|
|
489
|
+}
|
|
490
|
+
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+function getText(obj, why) {
|
|
494
|
+
|
|
495
|
+ var GENERATE_LINK = function(x) { return x + ' '; }
|
|
496
|
+ return _.reduce(obj.children, function(text, elem) {
|
|
497
|
+ if (_.isString(elem)) {
|
|
498
|
+ return text += elem.trim() + ' ';
|
|
499
|
+ } else if (_.isPlainObject(elem)) {
|
|
500
|
+ switch(elem.name) {
|
|
501
|
+ case 'para':
|
|
502
|
+ return text += getText(elem, why) + ' \n';
|
|
503
|
+ case 'ref':
|
|
504
|
+ return text += GENERATE_LINK(getText(elem, why));
|
|
505
|
+ case 'parameterlist':
|
|
506
|
+ case 'simplesect':
|
|
507
|
+ return text;
|
|
508
|
+ case 'programlisting':
|
|
509
|
+ case 'htmlonly':
|
|
510
|
+ return text;
|
|
511
|
+
|
|
512
|
+ case 'itemizedlist':
|
|
513
|
+ return text += '\n' + getText(elem, why) + '\n';
|
|
514
|
+ case 'listitem':
|
|
515
|
+ return text += '+ ' + getText(elem, why) + '\n';
|
|
516
|
+ case 'bold':
|
|
517
|
+ return text += '__' + getText(elem, why).trim() + '__ ';
|
|
518
|
+ case 'ulink':
|
|
519
|
+ return text += '[' + getText(elem, why).trim() + '](' + getAttr(elem, 'url').trim() + ') ';
|
|
520
|
+ case 'image':
|
|
521
|
+
|
|
522
|
+ var fn = getAttr(elem, 'name');
|
|
523
|
+ return text += ' \n \n ';
|
|
524
|
+ case 'linebreak':
|
|
525
|
+ return text += ' \n';
|
|
526
|
+ case 'ndash':
|
|
527
|
+ return text += '– ';
|
|
528
|
+ default:
|
|
529
|
+
|
|
530
|
+ throw new Error('NYI Unknown Object Type: ' + elem.name);
|
|
531
|
+ }
|
|
532
|
+ } else {
|
|
533
|
+ throw new Error('NYI Unknown Type: ' + (typeof elem));
|
|
534
|
+ }
|
|
535
|
+ }, '').trim();
|
|
536
|
+}
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+function getAttr(obj, name) {
|
|
541
|
+ return _.find(obj.attr, function(item) {
|
|
542
|
+ return item.name == name;
|
|
543
|
+ }).value;
|
|
544
|
+}
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+function getChild(obj, name) {
|
|
549
|
+ return _.find(obj.children, function(child) {
|
|
550
|
+ return child.name == name;
|
|
551
|
+ });
|
|
552
|
+}
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
|
|
556
|
+function getChildren(obj, name) {
|
|
557
|
+ return _.filter(obj.children, function(child) {
|
|
558
|
+ return child.name == name;
|
|
559
|
+ });
|
|
560
|
+}
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+function getClassName(spec_c) {
|
|
565
|
+ return getText(getChild(spec_c, 'compoundname'), 'name').replace(xml2js.opts.module + '::', '');
|
|
566
|
+}
|
|
567
|
+
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+function printObj(obj) {
|
|
571
|
+ console.log(util.inspect(obj, false, null));
|
|
572
|
+}
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+module.exports = xml2js;
|