Roo.rtf.Parser = function(text) {
this.text = '';
this.parserState = this.parseText;
this.doc = {};
this.groupStack = [];
this.hexStore = [];
this.doc = false;
this.groups = []; for (var ii = 0; ii < text.length; ++ii) {
++this.cpos;
if (text[ii] === '\n') {
++this.row;
this.col = 1;
} else {
++this.col;
}
this.parserState(text[ii]);
}
};
Roo.rtf.Parser.prototype = {
text : '', controlWord : '',
controlWordParam : '',
hexChar : '',
doc : false,
group: false,
groupStack : false,
hexStore : false,
cpos : 0,
row : 1, col : 1, push : function (el)
{
var m = 'cmd'+ el.type;
if (typeof(this[m]) == 'undefined') {
Roo.log('invalid cmd:' + el.type);
return;
}
this[m](el);
},
flushHexStore : function()
{
if (this.hexStore.length < 1) {
return;
}
var hexstr = this.hexStore.map(
function(cmd) {
return cmd.value;
}).join('');
this.group.addContent( new Roo.rtf.Hex( hexstr ));
this.hexStore.splice(0)
},
cmdgroupstart : function()
{
this.flushHexStore();
if (this.group) {
this.groupStack.push(this.group);
}
if (this.doc === false) {
this.group = this.doc = new Roo.rtf.Document();
return;
}
this.group = new Roo.rtf.Group(this.group);
},
cmdignorable : function()
{
this.flushHexStore();
this.group.ignorable = true;
},
cmdendparagraph : function()
{
this.flushHexStore();
this.group.addContent(new Roo.rtf.Paragraph());
},
cmdgroupend : function ()
{
this.flushHexStore();
var endingGroup = this.group;
this.group = this.groupStack.pop();
if (this.group) {
this.group.addChild(endingGroup);
}
var doc = this.group || this.doc;
if (endingGroup.ignorable === false) {
this.groups.push(endingGroup);
}
},
cmdtext : function (cmd)
{
this.flushHexStore();
if (!this.group) { return; }
this.group.addContent(new Roo.rtf.Span(cmd));
},
cmdcontrolword : function (cmd)
{
this.flushHexStore();
if (!this.group.type) {
this.group.type = cmd.value;
return;
}
this.group.addContent(new Roo.rtf.Ctrl(cmd));
return ;
},
cmdhexchar : function(cmd) {
this.hexStore.push(cmd);
},
cmderror : function(cmd) {
throw cmd.value;
},
parseText : function(c)
{
if (c === '\\') {
this.parserState = this.parseEscapes;
} else if (c === '{') {
this.emitStartGroup();
} else if (c === '}') {
this.emitEndGroup();
} else if (c === '\x0A' || c === '\x0D') {
} else {
this.text += c;
}
},
parseEscapes: function (c)
{
if (c === '\\' || c === '{' || c === '}') {
this.text += c;
this.parserState = this.parseText;
} else {
this.parserState = this.parseControlSymbol;
this.parseControlSymbol(c);
}
},
parseControlSymbol: function(c)
{
if (c === '~') {
this.text += '\u00a0'; this.parserState = this.parseText
} else if (c === '-') {
this.text += '\u00ad'; } else if (c === '_') {
this.text += '\u2011'; } else if (c === '*') {
this.emitIgnorable();
this.parserState = this.parseText;
} else if (c === "'") {
this.parserState = this.parseHexChar;
} else if (c === '|') { this.emitFormula();
this.parserState = this.parseText;
} else if (c === ':') { this.emitIndexSubEntry();
this.parserState = this.parseText;
} else if (c === '\x0a') {
this.emitEndParagraph();
this.parserState = this.parseText;
} else if (c === '\x0d') {
this.emitEndParagraph();
this.parserState = this.parseText;
} else {
this.parserState = this.parseControlWord;
this.parseControlWord(c);
}
},
parseHexChar: function (c)
{
if (/^[A-Fa-f0-9]$/.test(c)) {
this.hexChar += c;
if (this.hexChar.length >= 2) {
this.emitHexChar();
this.parserState = this.parseText;
}
return;
}
this.emitError("Invalid character \"" + c + "\" in hex literal.");
this.parserState = this.parseText;
},
parseControlWord : function(c)
{
if (c === ' ') {
this.emitControlWord();
this.parserState = this.parseText;
} else if (/^[-\d]$/.test(c)) {
this.parserState = this.parseControlWordParam;
this.controlWordParam += c;
} else if (/^[A-Za-z]$/.test(c)) {
this.controlWord += c;
} else {
this.emitControlWord();
this.parserState = this.parseText;
this.parseText(c);
}
},
parseControlWordParam : function (c) {
if (/^\d$/.test(c)) {
this.controlWordParam += c;
} else if (c === ' ') {
this.emitControlWord();
this.parserState = this.parseText;
} else {
this.emitControlWord();
this.parserState = this.parseText;
this.parseText(c);
}
},
emitText : function () {
if (this.text === '') {
return;
}
this.push({
type: 'text',
value: this.text,
pos: this.cpos,
row: this.row,
col: this.col
});
this.text = ''
},
emitControlWord : function ()
{
this.emitText();
if (this.controlWord === '') {
} else {
this.push({
type: 'controlword',
value: this.controlWord,
param: this.controlWordParam !== '' && Number(this.controlWordParam),
pos: this.cpos,
row: this.row,
col: this.col
});
}
this.controlWord = '';
this.controlWordParam = '';
},
emitStartGroup : function ()
{
this.emitText();
this.push({
type: 'groupstart',
pos: this.cpos,
row: this.row,
col: this.col
});
},
emitEndGroup : function ()
{
this.emitText();
this.push({
type: 'groupend',
pos: this.cpos,
row: this.row,
col: this.col
});
},
emitIgnorable : function ()
{
this.emitText();
this.push({
type: 'ignorable',
pos: this.cpos,
row: this.row,
col: this.col
});
},
emitHexChar : function ()
{
this.emitText();
this.push({
type: 'hexchar',
value: this.hexChar,
pos: this.cpos,
row: this.row,
col: this.col
});
this.hexChar = ''
},
emitError : function (message)
{
this.emitText();
this.push({
type: 'error',
value: message,
row: this.row,
col: this.col,
char: this.cpos });
},
emitEndParagraph : function () {
this.emitText();
this.push({
type: 'endparagraph',
pos: this.cpos,
row: this.row,
col: this.col
});
}
} ;