注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Computer Science

I Pressed My Words Here. Will You Read ?

 
 
 

日志

 
 

用JavaScript编写、解析及生成 对象框架组件文档(二)文档解析篇  

2012-07-10 21:00:01|  分类: My Projects |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
输出文档程序代码:
  1 var createCompsDocumentJSONData = function(compObj, parentObj){
  2 +-- 72 lines: var privateNameBeginWith = '_';
 74 }
 75
 76 var printDocument =function(v, log) {
 77         function exceptObject(compProp){
 78             var privateNameBeginWith = '_';
 79             var exceptObjectName = ['commonAttributes','commonEvents','commonFunctions','zh_cn','symbol'];
 80             if(compProp.indexOf(privateNameBeginWith) == 0){
 81                 //continue;
 82                 return false;
 83             }
 84             var exceptObjectNameLength = exceptObjectName.length;
 85             var resultBoolean = true;
 86             while(exceptObjectNameLength-->0){
 87                 if(compProp == exceptObjectName[exceptObjectNameLength]){
 88                     resultBoolean = false;
 89                 }
 90             }
 91             return resultBoolean;
 92         };
 93         var text = '';
 94         var language = 'en';
 95         if (typeOf(v) == 'Object'{
 96                 language = v.language || language;
 97                 log = log || v.log || {};
 98         }
 99         var word = function(w) {
100                 var dict = log[language];
101                 if (dict == null || dict[w] == null{
102                         return w;
103                 }
104                 /*
105                 switch(w){
106                     case ',': sIndent = symbol('indent');word = dict['{'];break;//symbol('newLine') +sIndent.plus(dict['{'].count(sIndent))+ sIndent;break;
107                     case '{': sIndent = symbol('indent');word += sIndent;dict['}'] += sIndent;break;
108                     case '}': sIndent = symbol('indent');word = word.removeLastSubString(sIndent);dict['{'] = dict['{'].removeLastSubString(sIndent);break;
109                     default:break;
110                 }
111                 */
112                 return dict[w] || w;
113         }
114         var symbol = function(sAry) {
115                 var dict = log['symbol'];
116                 var result = '';
117                 if (typeOf(sAry) === 'String'{
118                         sAry = [sAry];
119                 }else if(typeOf(sAry)!='Array'){
120                     return sAry;
121                 }
122                 var sAryLength = sAry.length;
123                 while (sAryLength-- > 0) {
124                         sStr = sAry[sAryLength];
125                         if (dict== null || dict[sStr] == null{
126                                 result += sStr;
127                                 continue;
128                         }
129                         var sym = dict[sStr];
130                         var sIndent;
131                             sIndent = dict['indent'];
132                         if(sStr === '{'){
133                             sym += sIndent;
134                             dict['{'] = sym;
135                             //dict['}'] += sIndent;
136                         }else if(sStr === '}'){
137                             //sym = sym.removeLastSubString(sIndent);
138                             dict['{'] = dict['{'].removeLastSubString(sIndent);
139                             //dict['}'] = dict['}'].removeLastSubString(sIndent);
140                         }else if(sStr === ','){
141                             var sIndentCount = dict['{'].count(sIndent);
142                             sym = /*dict['{'];*/symbol('newLine') + sIndent.plus(sIndentCount) + sym;
143                             result += sym;
144                             continue;
145                         }
146                         //dict[sStr] = sym;
147                         result += sym;
148                 }
149                 return result;
150         }
151         var addText = function(w) {
152                 if (typeOf(w) === 'String'{
153                         text = text + w;
154                 }
155         }
156         var thisLog = log;
157         var getText = function() {
158                 addText(getString(log));
159                 //showMe('123abc\tcba'.removeLastSubString('\tc'));
160                 return text;
161         }
162         /*
163         for(var oA in log){
164             var oAObj = log[oA];
165             addText(word(oA) + log[oA] + symbol('newLine'));
166             if(oA == 'components'){
167                 addText(symbol('indent'));
168                 for(var oB in oAObj){
169                     if(oB == 'commonInterface'){
170                         continue;
171                     }
172                     var oBObj = oAObj[oB];
173                     addText(word('name') + symbol('is') + oB + symbol(['newLine','indent','indent']));
174                     for(var oC in oBObj){
175                     }
176                 }
177                 
178             }
179             
180         }
181         */
182
183         var getParentInObject = function(obj, pathDepth, pathAry){
184 +----- 55 lines: var currentLevel = 0;
239         }
240
241         /*
242          * get object string by burning CPU
243          */
244         var getString = function(obj) {
245                 var result = {
246                         'string': ''
247                         ,'hasChild': true
248                         ,'child': null
249                         ,'self': obj
250                         ,'parent': null
251                 };
252                 var pathArray = [0];// record current index in every level/depth
253                 var pathObjAry = [obj];// record  object in each parent node
254                 var typeValue = typeOf(obj);
255                 arguments[1] = arguments[1] || {};
256                 if (typeOf(arguments[1], 'String')) {
257                         var argumentsObj = {};
258                         argumentsObj[arguments[1]] = true;
259                         arguments[1] = argumentsObj;
260                 }
261                 var command = arguments[1] || {};
262                 var currentLevel = 0;
263                 do {
264                         if(currentLevel == 1){
265                             result['string'] += symbol('newParagraph');
266                         }
267                         var length = pathArray.length;
268                         arguments[1]['pathLevel'] = currentLevel;// record current level /depth
269                         arguments[1]['pathNo'] = pathArray[currentLevel];// record item No. in current level
270                         arguments[1]['parent'] = result['parent'];
271                         var rs = analyseTarget(result['self'], arguments[1]) || {};
272                         result['hasChild'] = rs['hasChild'];
273                         result['string'] = result['string'] + word(rs['string']);
274                         if (currentLevel == length - 1) { // while digging deeper
275                                 //alert(result['string'] + '\n\rlength: ' + length + '\n\rcurrentLevel: ' + currentLevel);
276                                 if (rs['hasChild']{
277                                         result['parent'] = rs['self']
278                                         result['self'] = rs['child'];
279                                         result['child'] = null;
280                                         pathArray[length] = 0;// increase pathArray
281                                         pathObjAry[length] = result['self'];
282                                         currentLevel++;
283                                 } else { // reach the end, go back to the top to refind the parent obj.
284                                         var index = 0;
285                                         if (length >= 1) {
286                                                 pathArray.pop();
287                                                 index = pathArray.length - 1;
288                                                 currentLevel = index;
289                                                 pathArray[index] = pathArray[index] + 1;
290                                         } else {
291                                                 break;
292                                         }
293                                         result['parent'] = pathObjAry[index-1]; /*||*/  //getParentInObject(obj,currentLevel, pathArray);
294                                         result['self'] = rs['parent']
295                                         result['child'] = rs['self'];
296                                 }
297                         } else { // while refinding the deepest obj.
298                                 currentLevel++;
299                         }
300                 } while (true)
301                 delete command;
302                 delete typeValue;
303                 delete pathArray;
304                 delete arguments;
305                 return result['string'];
306         }
307         /**
308          * for "getting string by burning CPU", return piece of result string and
309          * state(true/false:keep diging or not)
310          */
311         var analyseTarget = function(obj) {
312                 // preparing
313                 var rs = {
314                         'string': ''
315                         ,'hasChild': false
316                         ,'child': null
317                         ,'self': obj
318                         ,'parent': arguments[1]['parent']
319                 };
320                 var typeValue = typeOf(obj);
321                 var noFunction = arguments[1].noFunction;
322                 if (noFunction && typeValue == 'Function'{
323                         return rs;
324                 }
325                 arguments[1] = arguments[1] || undefined;
326                 if (typeOf(arguments[1], 'String')) {
327                         var argumentsObj = {};
328                         argumentsObj[arguments[1]] = true;
329                         arguments[1] = argumentsObj;
330                 }
331                 arguments[1] = arguments[1] || {};
332                 var pathLevel = arguments[1].pathLevel || 1;
333                 var maxDepth = arguments[1].maxDepth;
334                 if (typeOf(maxDepth, 'number') && pathLevel++ > maxDepth) {
335                         rs['string'] = '[object ' + typeValue + ']';
336                         return rs;
337                 }
338                 var command = arguments[1] || {};
339                 var pathNo = command.pathNo || 0;
340                 // start
341                 switch (typeValue) {
342                 case 'String':
343                         rs['string'] = symbol('"') + word(String(obj)) + symbol('"');
344                         rs['hasChild'] = false;
345                         rs['child'] = null;
346                         break;
347                 case 'Array':
348                         var length = obj.length;
349                         if (length < 1) {
350                                 rs['string'] = symbol('[]');
351                                 rs['hasChild'] = false;
352                                 rs['child'] = null;
353                                 break;
354                         }
355                         if (pathNo < length) {
356                                 if (pathNo == 0) {
357                                         rs['string'] = symbol('[');
358                                 } else if (pathNo > 0) {
359                                         rs['string'] = symbol(' ,');
360                                 }
361                                 rs['hasChild'] = true;
362                                 rs['child'] = obj[pathNo];
363                                 rs['self'] = obj;
364                         } else if (pathNo == length) {
365                                 //if(pathNo == 0){ rs['string'] = '['; }// this situation is already inclued in condition of "if(length<1)"
366                                 rs['string'] = symbol(']');
367                                 rs['hasChild'] = false;
368                                 rs['child'] = null;
369                         }
370                         break;
371                 case 'Error':break;
372                 case 'Object':
373                         var count = - 1;
374                         try {
375                                 for (var prop in obj) { // when count is equal pathNo, we get the child and continue, if count is larger than pathNo, then we will know this is not the last child.
376                                         count++;
377                                         if (count < pathNo) {} else if (count == pathNo) {
378                                                 if (pathNo === 0) {
379                                                         rs['string'] = symbol('{');
380                                                 } else if (pathNo > 0) {
381                                                         rs['string'] = symbol('domEnd') + symbol(',');
382                                                 }
383                                                 if(prop == 'commonAttributes'){
384                                                     rs['string'] = rs['string'] + '匿名' + symbol(':');
385                                                     rs['hasChild'] = true;
386                                                     rs['child'] = '内部使用';
387                                                     rs['self'] = obj;
388                                                 }else
389                                                 if(exceptObject(prop) === false){// 过滤内部接口
390                                                     rs['string'] = rs['string'] + symbol('domStart') + word('匿名') + symbol(':');
391                                                     rs['hasChild'] = true;
392                                                     rs['child'] = word('内部使用');
393                                                     rs['self'] = obj;
394                                                 }else{
395                                                     rs['string'] = rs['string'] + symbol('domStart') + word(prop) + symbol(':');
396                                                     rs['hasChild'] = true;
397                                                     rs['child'] = obj[prop];
398                                                     rs['self'] = obj;
399                                                 }
400                                         } else if (count > pathNo) {
401                                                 break;
402                                         }
403                                 }
404                         } catch(e) {
405                                 if (!jQuery.isPlainObject(obj)) {
406                                         rs['string'] = word('object')
407                                         rs['hasChild'] = false;
408                                         rs['child'] = null;
409                                         break;
410                                 }
411                         }
412                         if (!rs['hasChild'] && count == - 1) { // obj does not have a child at all.
413                                 rs['string'] = symbol('{}');
414                         } else if (!rs['hasChild'] && count < pathNo) { // rs['child'] == obj last child
415                                 rs['string'] = rs['string'] + symbol('domEnd') + symbol('}');
416                         } else if (rs['hasChild']){// not the end, self  need to be record;
417                                 rs['self'] = obj;
418                         }
419                         delete prop;
420                         delete count;
421                         break;
422                 case 'Number':
423                         rs['string'] = word(obj);
424                         rs['hasChild'] = false;
425                         rs['child'] = null;
426                         break;
427                 case 'Function':
428                         if (command == 'functionNameOnly' || command.functionNameOnly) {
429                                 rs['string'] = word('function()');
430                         } else {
431                                 rs['string'] = word(String(obj));
432                         }
433                         rs['hasChild'] = false;
434                         rs['child'] = null;
435                         break;
436                 case 'Boolean':
437                         rs['string'] = word(String(obj));
438                         break;
439                 case 'RegExp':
440                         rs['string'] = word(String(obj));
441                         break;
442                 default:
443                         rs['string'] = '[object ' + typeValue + ']';
444                         break;
445                 }
446                 return rs;
447         }
448         getText();
449         return text;
450 }
451
452
453 if(!String.prototype.removeLastSubString){
454 +--  5 lines: String.prototype.removeLastSubString = function(word){
459 }
460 if(!String.prototype.removeFirstSubString){
461 +--  6 lines: String.prototype.removeFirstSubString = function(word){
467 }
468 if(!String.prototype.plus){
469 +-- 10 lines: String.prototype.plus = function(num){
479 }
480 if(!String.prototype.count){
481 +-- 14 lines: String.prototype.count = function(str){
495 }
496 if(!String.prototype.isEndWidth){
497 +-- 12 lines: String.prototype.isEndWidth = function(str){
509 }
510 if (!Array.prototype.remove) {
511 +-- 10 lines: Array.prototype.remove = function(sortValue) {
521 }
522 if (!Array.prototype.sortLocale) {
523 +-- 32 lines: Array.prototype.sortLocale = function() {
555 }
556 if (!Array.prototype.indexOf) {
557 +-- 14 lines: Array.prototype.indexOf = function(elt
571 }
572
573 /*
574 +-- 18 lines: * typeOf({},['object','string']) == true;
592 if(!typeOf){
593 +-- 36 lines: var typeOf = function(obj, type) {
629 }
630
631 /*
632 +-- 15 lines: * 测试用
647 var g_showMe = g_showMe || true;
648 if(!showMe){
649 +-- 31 lines: var showMe = function(obj) {
680 }
681 // 将字符串 数组 对象 以字符串的形式返回。
682 if(!getStr){
683 +-- 80 lines: var getStr = function(obj) {
763 }
764 /*
765 +--  2 lines: * get object string by burning CPU
767 if(!getString){
768 +-- 50 lines: var getString = function(obj) {
818 }
819 /**
820 +--  3 lines: * for "getting string by burning CPU", return piece of result string and
823 if(!analyseTarget){
824 +--121 lines: var analyseTarget = function(obj) {
945 }
946 

————————————————
  评论这张
 
阅读(1027)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017