2014-05-03 18:51:10 +00:00
|
|
|
var _chr = function(c) { return String.fromCharCode(c); };
|
2014-05-16 00:33:34 +00:00
|
|
|
var _ord = function(c) { return c.charCodeAt(0); };
|
2014-04-03 22:51:54 +00:00
|
|
|
var attregexg=/([\w:]+)=((?:")([^"]*)(?:")|(?:')([^']*)(?:'))/g;
|
|
|
|
var attregex=/([\w:]+)=((?:")(?:[^"]*)(?:")|(?:')(?:[^']*)(?:'))/;
|
2013-05-02 16:59:27 +00:00
|
|
|
function parsexmltag(tag) {
|
|
|
|
var words = tag.split(/\s+/);
|
|
|
|
var z = {'0': words[0]};
|
|
|
|
if(words.length === 1) return z;
|
2014-04-03 22:51:54 +00:00
|
|
|
(tag.match(attregexg) || []).map(function(x){
|
|
|
|
var y=x.match(attregex);
|
|
|
|
y[1] = y[1].replace(/xmlns:/,"xmlns");
|
|
|
|
z[y[1].replace(/^[a-zA-Z]*:/,"")] = y[2].substr(1,y[2].length-2);
|
|
|
|
});
|
2013-05-02 16:59:27 +00:00
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
var encodings = {
|
|
|
|
'"': '"',
|
|
|
|
''': "'",
|
|
|
|
'>': '>',
|
|
|
|
'<': '<',
|
|
|
|
'&': '&'
|
|
|
|
};
|
2014-01-29 06:00:09 +00:00
|
|
|
var rencoding = evert(encodings);
|
2014-01-28 16:38:02 +00:00
|
|
|
var rencstr = "&<>'\"".split("");
|
2014-01-29 06:00:09 +00:00
|
|
|
|
2013-05-02 16:59:27 +00:00
|
|
|
// TODO: CP remap (need to read file version to determine OS)
|
|
|
|
function unescapexml(text){
|
|
|
|
var s = text + '';
|
|
|
|
for(var y in encodings) s = s.replace(new RegExp(y,'g'), encodings[y]);
|
|
|
|
return s.replace(/_x([0-9a-fA-F]*)_/g,function(m,c) {return _chr(parseInt(c,16));});
|
|
|
|
}
|
2014-01-28 16:38:02 +00:00
|
|
|
function escapexml(text){
|
|
|
|
var s = text + '';
|
|
|
|
rencstr.forEach(function(y){s=s.replace(new RegExp(y,'g'), rencoding[y]);});
|
2014-05-16 00:33:34 +00:00
|
|
|
s = s.replace(/[\u0000-\u0007]/g,function(s) { return "_x" + ("0000"+_ord(s).toString(16)).substr(-4) + "_";}); /* TODO: verify range */
|
2014-01-28 16:38:02 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:59:27 +00:00
|
|
|
|
|
|
|
function parsexmlbool(value, tag) {
|
|
|
|
switch(value) {
|
|
|
|
case '0': case 0: case 'false': case 'FALSE': return false;
|
|
|
|
case '1': case 1: case 'true': case 'TRUE': return true;
|
|
|
|
default: throw "bad boolean value " + value + " in "+(tag||"?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var utf8read = function(orig) {
|
2014-02-22 21:36:28 +00:00
|
|
|
var out = [], i = 0, c = 0, c1 = 0, c2 = 0, c3 = 0;
|
2013-05-02 16:59:27 +00:00
|
|
|
while (i < orig.length) {
|
|
|
|
c = orig.charCodeAt(i++);
|
2014-02-22 21:36:28 +00:00
|
|
|
if (c < 128) out.push(_chr(c));
|
2013-05-02 16:59:27 +00:00
|
|
|
else {
|
|
|
|
c2 = orig.charCodeAt(i++);
|
2014-02-22 21:36:28 +00:00
|
|
|
if (c>191 && c<224) out.push(_chr((c & 31) << 6 | c2 & 63));
|
2013-05-02 16:59:27 +00:00
|
|
|
else {
|
|
|
|
c3 = orig.charCodeAt(i++);
|
2014-02-22 21:36:28 +00:00
|
|
|
out.push(_chr((c & 15) << 12 | (c2 & 63) << 6 | c3 & 63));
|
2013-05-02 16:59:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-22 21:36:28 +00:00
|
|
|
return out.join("");
|
2013-05-02 16:59:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// matches <foo>...</foo> extracts content
|
2014-04-15 09:04:03 +00:00
|
|
|
function matchtag(f,g) {return new RegExp('<(?:\\w+:)?'+f+'(?: xml:space="preserve")?(?:[^>]*)>([^\u2603]*)</(?:\\w+:)?'+f+'>',(g||"")+"m");}
|
2013-05-02 16:59:27 +00:00
|
|
|
|
|
|
|
function parseVector(data) {
|
|
|
|
var h = parsexmltag(data);
|
|
|
|
|
2013-11-06 06:05:32 +00:00
|
|
|
var matches = data.match(new RegExp("<vt:" + h.baseType + ">(.*?)</vt:" + h.baseType + ">", 'g'))||[];
|
2013-05-02 16:59:27 +00:00
|
|
|
if(matches.length != h.size) throw "unexpected vector length " + matches.length + " != " + h.size;
|
|
|
|
var res = [];
|
|
|
|
matches.forEach(function(x) {
|
|
|
|
var v = x.replace(/<[/]?vt:variant>/g,"").match(/<vt:([^>]*)>(.*)</);
|
|
|
|
res.push({v:v[2], t:v[1]});
|
|
|
|
});
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-05-16 00:33:34 +00:00
|
|
|
function writetag(f,g) {return '<' + f + (g.match(/(^\s|\s$|\n)/)?' xml:space="preserve"' : "") + '>' + g + '</' + f + '>';}
|
|
|
|
|
|
|
|
/*jshint -W041 */
|
|
|
|
function writextag(f,g,h) { return '<' + f + (h != null ? keys(h).map(function(k) { return " " + k + '="' + h[k] + '"';}).join("") : "") + (g == null ? "/" : (g.match(/(^\s|\s$|\n)/)?' xml:space="preserve"' : "") + '>' + g + '</' + f) + '>';}
|
|
|
|
|
|
|
|
function write_w3cdtf(d, t) { try { return d.toISOString().replace(/\.\d*/,""); } catch(e) { if(t) throw e; } }
|
|
|
|
|
|
|
|
function write_vt(s) {
|
|
|
|
if(typeof s == 'string') return writextag('vt:lpwstr', s);
|
|
|
|
if(typeof s == 'number') return writextag((s|0)==s?'vt:i4':'vt:r8', String(s));
|
|
|
|
if(typeof s == 'boolean') return writextag('vt:bool', s?'true':'false');
|
|
|
|
if(s instanceof Date) return writextag('vt:filetime', write_w3cdtf(s));
|
|
|
|
throw new Error("Unable to serialize " + s);
|
|
|
|
}
|
|
|
|
|
|
|
|
var XML_HEADER = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n';
|
|
|
|
var XMLNS = {
|
|
|
|
'dc': 'http://purl.org/dc/elements/1.1/',
|
|
|
|
'dcterms': 'http://purl.org/dc/terms/',
|
|
|
|
'dcmitype': 'http://purl.org/dc/dcmitype/',
|
|
|
|
'mx': 'http://schemas.microsoft.com/office/mac/excel/2008/main',
|
|
|
|
'r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
|
|
|
|
'sjs': 'http://schemas.openxmlformats.org/package/2006/sheetjs/core-properties',
|
|
|
|
'vt': 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes',
|
|
|
|
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
|
|
|
|
'xsd': 'http://www.w3.org/2001/XMLSchema'
|
|
|
|
};
|
|
|
|
|
|
|
|
XMLNS.main = [
|
|
|
|
'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
|
|
|
|
'http://purl.oclc.org/ooxml/spreadsheetml/main',
|
|
|
|
'http://schemas.microsoft.com/office/excel/2006/main',
|
|
|
|
'http://schemas.microsoft.com/office/excel/2006/2'
|
|
|
|
];
|