Juno_okyo

Vocalbulary Highlighter Export Function

Mar 9th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var VHExportProgress =
  2. {
  3.     onLoad: function()
  4.     {
  5.         var dbConn = VHDatabase.getDbConn();
  6.         if (!dbConn) return;
  7.        
  8.         // the list contains all rows
  9.         var list = [];
  10.        
  11.         var statement = dbConn.createStatement("SELECT word,note,htmlnote,matchcase,nothighlight,penname,style,leftboundary,rightboundary,findderivatives FROM vocabulary LEFT JOIN pens ON vocabulary.penid = pens.id");
  12.         statement.executeAsync(
  13.         {
  14.             handleResult: function(aResultSet)
  15.             {
  16.                 for (var row = aResultSet.getNextRow(); row; row = aResultSet.getNextRow())
  17.                 {
  18.                     var theWord = row.getResultByName("word");
  19.                     var note = row.getResultByName("note");
  20.                     var htmlnote = row.getResultByName("htmlnote");
  21.                     var matchCase = row.getResultByName("matchcase");
  22.                     var nothighlight = row.getResultByName("nothighlight");
  23.                     var penName = row.getResultByName("penname");
  24.                     var style = row.getResultByName("style");
  25.                     var leftBoundary = row.getResultByName("leftboundary");
  26.                     var rightBoundary = row.getResultByName("rightboundary");
  27.                     var findDerivatives = row.getResultByName("findderivatives");
  28.                    
  29.                     list.push({theWord: theWord, note: note, htmlnote: htmlnote, matchCase: matchCase, nothighlight: nothighlight, penName:penName, style:style, leftBoundary: leftBoundary, rightBoundary: rightBoundary, findDerivatives: findDerivatives});
  30.                 }
  31.             },
  32.            
  33.             handleError: VHDatabase.handleDBError,
  34.             handleCompletion: function(aReason)
  35.             {
  36.                 VHDatabase.handleDBCompletion(aReason);
  37.                
  38.                 var file = window.arguments[0].file;
  39.                
  40.                 // saves the directory
  41.                 var parentDir = file.parent;
  42.                 VHPrefs.setFilePref("exportDirectory", parentDir);
  43.                
  44.                 // file output stream
  45.                 var fileOutputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].  
  46.                                     createInstance(Components.interfaces.nsIFileOutputStream);
  47.                 // write only, create if not exist, empty file if exist
  48.                 fileOutputStream.init(file, 0x02 | 0x08 | 0x20, -1, 0);
  49.                
  50.                 var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
  51.                                 createInstance(Components.interfaces.nsIConverterOutputStream);
  52.                 converter.init(fileOutputStream, "UTF-8", 0, 0);
  53.                
  54.                 VHExportProgress.converter = converter;
  55.                
  56.                 VHExportProgress.writeLine(0, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>');
  57.                 VHExportProgress.writeLine(0, '<' + VHDatabase.databaseFileName + ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">');
  58.                
  59.                 writtenPens = {};
  60.                 var defaultPenName = VH.getString("defaultPen");
  61.                 var defaultPenStyle = VHPrefs.getCharPref("defaultpenstyle");
  62.                 var defaultPenLeftBoundary = VHPrefs.getBoolPref("defaultpenleftboundary");
  63.                 var defaultPenRightBoundary = VHPrefs.getBoolPref("defaultpenrightboundary");
  64.                 var defaultPenFindDerivatives = VHPrefs.getBoolPref("defaultpenfindderivatives");
  65.                
  66.                 var penName, style, leftBoundary, rightBoundary, findDerivatives;
  67.                
  68.                 for (var i = 0; i < list.length; i++)
  69.                 {
  70.                     VHExportProgress.writeLine(1, '<vocabulary>');
  71.                     VHExportProgress.writeLine(2, '<word>' + VH.getSearchText(list[i].theWord) + '</word>');
  72.                     if (!list[i].htmlnote)
  73.                     {
  74.                         if (list[i].note)
  75.                             VHExportProgress.writeLine(2, '<note>' + VH.getSearchText(list[i].note) + '</note>');
  76.                         else
  77.                             VHExportProgress.writeLine(2, '<note/>');
  78.                         VHExportProgress.writeLine(2, '<htmlnote/>');
  79.                     }
  80.                     else
  81.                     {
  82.                         VHExportProgress.writeLine(2, '<note/>');
  83.                         VHExportProgress.writeLine(2, '<htmlnote>' + VH.getSearchText(list[i].htmlnote) + '</htmlnote>');
  84.                     }
  85.                     VHExportProgress.writeLine(2, '<matchcase>' + list[i].matchCase + '</matchcase>');
  86.                     VHExportProgress.writeLine(2, '<nothighlight>' + list[i].nothighlight + '</nothighlight>');
  87.                     if (list[i].penName)
  88.                     {
  89.                         penName = VH.getSearchText(list[i].penName);
  90.                         style = list[i].style;
  91.                         leftBoundary = list[i].leftBoundary;
  92.                         rightBoundary = list[i].rightBoundary;
  93.                         findDerivatives = list[i].findDerivatives;
  94.                        
  95.                         if (writtenPens[penName])
  96.                             VHExportProgress.writeLine(2, '<pen penname="' + penName + '"/>');
  97.                         else
  98.                             VHExportProgress.writeLine(2, '<pen penname="' + penName + '">');
  99.                     }
  100.                     else // default pen
  101.                     {
  102.                         penName = defaultPenName;
  103.                         style = defaultPenStyle;
  104.                         leftBoundary = defaultPenLeftBoundary?1:0;
  105.                         rightBoundary = defaultPenRightBoundary?1:0;
  106.                         findDerivatives = defaultPenFindDerivatives?1:0;
  107.                        
  108.                         if (writtenPens[penName])
  109.                             VHExportProgress.writeLine(2, '<pen/>');
  110.                         else
  111.                             VHExportProgress.writeLine(2, '<pen penname="">');
  112.                     }
  113.                     if (!writtenPens[penName])
  114.                     {
  115.                         VHExportProgress.writeLine(3, '<style>' + style + '</style>');
  116.                         VHExportProgress.writeLine(3, '<leftboundary>' + leftBoundary + '</leftboundary>');
  117.                         VHExportProgress.writeLine(3, '<rightboundary>' + rightBoundary + '</rightboundary>');
  118.                         VHExportProgress.writeLine(3, '<findderivatives>' + findDerivatives + '</findderivatives>');
  119.                         VHExportProgress.writeLine(2, '</pen>');
  120.                         writtenPens[penName] = true;
  121.                     }
  122.                     VHExportProgress.writeLine(1, '</vocabulary>');
  123.                 }
  124.                
  125.                 // makes sure there are always at least two rows
  126.                 if (list.length == 0)
  127.                 {
  128.                     VHExportProgress.writeLine(1, '<vocabulary>');
  129.                     VHExportProgress.writeLine(2, '<word/>');
  130.                     VHExportProgress.writeLine(2, '<note/>');
  131.                     VHExportProgress.writeLine(2, '<htmlnote/>');
  132.                     VHExportProgress.writeLine(2, '<matchcase/>');
  133.                     VHExportProgress.writeLine(2, '<nothighlight/>');
  134.                     VHExportProgress.writeLine(2, '<pen penname="">');
  135.                     VHExportProgress.writeLine(3, '<style/>');
  136.                     VHExportProgress.writeLine(3, '<leftboundary/>');
  137.                     VHExportProgress.writeLine(3, '<rightboundary/>');
  138.                     VHExportProgress.writeLine(3, '<findderivatives/>');
  139.                     VHExportProgress.writeLine(2, '</pen>');
  140.                     VHExportProgress.writeLine(1, '</vocabulary>');
  141.                 }
  142.                
  143.                 if (list.length <= 1)
  144.                     VHExportProgress.writeLine(1, '<vocabulary/>');
  145.                
  146.                 VHExportProgress.writeLine(0, '</' + VHDatabase.databaseFileName + '>');
  147.                 converter.close(); // closes output stream
  148.                
  149.                 // closes the dialog
  150.                 document.documentElement.acceptDialog();
  151.             }
  152.         });
  153.     },
  154.    
  155.     converter: null,
  156.    
  157.     writeLine: function(tabCount, str)
  158.     {
  159.         var tabStr = "";
  160.         for (var i = 0; i < tabCount; i++)
  161.             tabStr += "\t";
  162.            
  163.         // uses CRLF for human-reading in Windows
  164.         VHExportProgress.converter.writeString(tabStr + str.replace(/\\/g, "\\") + "\r\n"); // converts '\' to '\\'
  165.     }
  166. }
Add Comment
Please, Sign In to add comment