2016-04-05 39 views
-1

アップロードされたrtfファイルがあります。アップロードされたら、解析してjsonファイルに変換します。私はこれに対してほとんど角度をつけています。私はアップロードが正常に動作している、私はちょうど解析に問題があります。ファイルはクリーンアップされますが、適切にjsonファイルとして形式化されていません。誰にでも最高のことを提案していますか?javascriptを使用してrtfファイルを解析しようとしています

this.parseFile=function(file, uploadUrl){ 

    reader.onload = function(onLoadEvent){ 

     $rootScope.$apply(function(){ 
      var dataURL = reader.result; 
      setUpData(dataURL); 
     }); 

    }; 
    reader.readAsText(file); 
}; 
function saveFile(file){ 

    var fd = new FormData(); 
    fd.append('save_file', file); 
    $http({ 
     method: 'POST', 
     url: "includes/loadFile.php", 
     data: fd, 
     transformRequest: angular.identity, 
     headers: {'Content-Type': undefined} 

    }) 
    .success(function (data, status, headers, config) { 
     msg = { 
       title: "success", 
       cls: "success", 
       msg: data 
     }; 
     popUp(msg); 
    }) 
    .error(function (data, status, header, config) { 
     msg = { 
       title: "error", 
       cls: "danger", 
       msg: data 
     }; 
     popUp(msg); 
    }); 
}; 
function setUpData(data) 
{ 
    text = data; 

    text = text.replace('/[\p{Z}\s]{2,}/u', ' '); 
    // skip over the heading stuff 
    j= text.indexOf('{',1); // skip ahead to the first part of the header 

    var loc = 1; 
    var t =""; 

    var ansa=""; 
    len = text.length; 

    getpgraph(); // skip by the first paragrap 

    while(j<len) { 
     c = text.substr(j,1); 
     if (c=="\\") { 
      // have a tag 
      var tag = gettag(); 
      if (tag.length > 0) { 
       // process known tags 
       switch (tag) { 
        case 'par': 
         ansa+="\r\n"; 
        break; 
        // ad a list of common tags 
        // parameter tags 
        case 'spriority1': 
        case 'fprq2': 
        case 'author': 
        case 'operator': 
        case 'sqformat': 
        case 'company': 
        case 'xmlns1': 
        case 'wgrffmtfilter': 
        case 'pnhang': 
        case 'themedata': 
        case 'colorschememapping': 
         var tt = gettag(); 
        break; 
        case '*': 
        case 'info': 
        case 'stylesheet': 
         // gets to end of paragraph 
         j--; 
         getpgraph(); 
        default: 
        // ignore the tag 
       } 
      } 
     } else { 
      ansa += c; 
     } 
     j++; 
    }; 

    ansa = ansa.replace(/{|}|"|(|)\r/g,''); 
    ansa = ansa.replace(/Amount After|Amt After/g,''); 
    ansa = ansa.replace(/negetive amount means Refund NOTE: Layaways are not in Sales Report till they're paid off/g,''); 
    ansa = ansa.replace("On Line Vendor Sales Quick Report by Month",''); 
    ansa = ansa.replace(/ () /g,''); 

    ansa.trim(); 
    ansa.split("\n"); 

    console.log(ansa); 

    var newData = new Array(); 
    var vendor = new Array(); 
    var orders = new Array(); 
    var v = 0; 

    //Lets look for the line that has the word vendor 
    vendorLine = ansa.match(/Vendor/gi); 

    //Look for each order for the vendor 
    orderLine = ansa.match(/date/gi); 
    figuresLine = ansa.match(/figures/g); 

    var start; 
    var end; 
    for(arr = 0; arr <=ansa.length; arr++) 
    { 

    } 

    if(vendorLine) 
    { 
     vendorInfo = ansa.split(' ',ansa.replace('/Vendor|\r|\s/','')); 
     vendorFName = vendorInfo[0]; 
     vendorLName = vendorInfo[1]; 
     vendorID = vendorInfo[2]; 
     vendor = {vendorid: vendorID, vendorInfo:{fname: vendorFName,lname:vendorLName} , orders:orders}; 


    } 

    if(orderLine) 
    { 
     start = 1; 
    } 

    if(figuresLine) 
    { 
     end = 0; 
    } 
    if(!end || !start) 
    { 
     if(end < start ) 
     { 
      ordersLine = explode(" ",v); 
      orderDesc = new Array(); 
      //Remove the stuff we dont need. Like Qty ,Subtotal and Inv#. Once we remove them we reindex them 
      unset(ordersLine[0],ordersLine[2],ordersLine[count(ordersLine)]); // Removes Subtotal and Inv# 
      unset(ordersLine[count(ordersLine)]); // Removes QTY 
      ordersLine = array_values(ordersLine); 

      orderDate = ordersLine[0]; 
      orderAmt = end(ordersLine); 

      /*foreach (ordersLine as key=>value) 
      { 
       if(key != 0) 
       { 
        if(key != count(ordersLine)-1) 
        { 
         array_push(orderDesc,value); 
         orderLines = array("vendorid"=>vendorID,"order"=>array("date"=> orderDate,"desc"=> implode(" " ,orderDesc), "amount"=>orderAmt)); 
        } 
       }; 
      }*/ 
     } 

    } 
    vendor['orders'] = orders; 

    saveFile(ansa); 
} 
function gettag() { 
    // gets the text following the/character or gets the param if it there 

    var tag=''; 
    while(true) { 
     j++; 
     if (j>=len) break; 
     c = text.substr(j,1); 
     if (c==';') break; 
     if (c=='}') break; 
     if (c=="\\") { 
      j--; 
      break; 
     } 
     if (c=="{") { 
      //getpgraph(); 
      break; 
     } 
     if (((c>='0')&&(c<='9'))||((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'))||c=="'"||c=="-"||c=="*"){ 
      tag= tag+c; 
     } else { 
      // end of tag 
      j--; 
      break; 
     } 
    } 
    return tag; 
} 

function getpgraph() { 
    // if the first char after a tag is { then throw out the entire paragraph 
    // this has to be nested 

    var nest = 0; 
    while(true) { 
     j++; 
     if (j>=len) break; 
     if (text.substr(j,1)=='}') { 

      if (nest==0) return; 
      nest--; 
     } 
     if (text.substr(j,1)=='{') { 
      nest++; 
     } 
    } 
    return; 
} 

は、私がこれに期待することは基本的には、新たに解析され、アップロードされたデータでページを更新。それは当時、彼らがしているページ上でユーザーに表示することができ、クリーンうまくフォーマットされたJSONファイルを作成することです。

+0

ファイルに関する少しの詳細とその結果がどのように表示されるかについて、あなたが何をしているかについていくつかのコードが必要になります – bwright

答えて

0

JS RTFパーサーが役立つ場合があります。結果をjsonエンコードする必要があります。 https://github.com/lazygyu/RTF-parser

これがあなたの質問に答えているかどうかは不明ですが、もう少し情報があれば、修正プログラムをロックするのに役立つかもしれません。

関連する問題