}
return out;
}
+QByteArray xmlize(QByteArray str,QString newline)
+{
+ QByteArray out;
+ for(int i=0;i<str.size();i++){
+ char c=str[i];
+ if(c=='<')out+="<";else
+ if(c=='>')out+=">";else
+ if(c=='&')out+="&";else
+ if(c=='\n')out+=newline;else
+ if(QChar(c).isSpace()||(c>=32&&c<=0x7f))out+=c;
+ else out+="&#"+QString::number((unsigned char)c)+";";
+ }
+ return out;
+}
+
QString cent2str(qint64 c,bool localize)
{
MLocalFormat mf;
/**converts special XML characters into harmless &-codes, so the text can be included*/
QString xmlize(QString str,QString newline="\n");
+/**converts special XML characters into harmless &-codes, so the text can be included*/
+QByteArray xmlize(QByteArray str,QString newline="\n");
/**converts a cent value into a (localized) string*/
QString cent2str(qint64 cent,bool localize=true);
}
//conversion process
if(!dov1)return;
- buffer.setData(MOdtRenderer::convertV1toV2(buffer.data()));
//try again
- if(!cdoc.setContent(&buffer,true,&err,&errln,&errcl)){
+ if(!cdoc.setContent(MOdtRenderer::convertV1toV2(buffer.data()),true,&err,&errln,&errcl)){
qDebug()<<"Hmm, definitely not XML - even after conversion, aborting...";
qDebug()<<" Info: line ="<<errln<<"column ="<<errcl<<"error ="<<err;
return;
- }
+ }else
+ qDebug()<<"Successfully converted the template.";
}
MOdtRenderer::~MOdtRenderer()
{
QByteArray nba;
QList<QByteArray>olst=old.split('\n');
- foreach(const QByteArray&line,olst){
- nba+=line;
+ bool hstarted=false;
+ foreach(QByteArray line,olst){
+ //is this the start of the file?
+ if(!hstarted){
+ QByteArray st=line.trimmed().left(2);
+ //find the first real tag and insert the new start tag there
+ if(st!="<?" && st!="<!" && st[0]=='<'){
+ nba+="<msmoketpl:template xmlns:msmoketpl=\"";
+ nba+=OdfTemplateNS.toAscii();
+ nba+="\">\n";
+ hstarted=true;
+ }
+ //add the line just scanned...
+ nba+=line;
+ nba+='\n';
+ continue;
+ }
+ //is this a special line?
+ QByteArray lnt=line.trimmed();
+ if(lnt.size()>1 && lnt.at(0)=='#'){
+ //extract command
+ int cnt=lnt.indexOf(':');
+ QByteArray parm;
+ if(cnt>0){
+ parm=lnt.mid(cnt+1);
+ lnt=lnt.left(cnt);
+// qDebug()<<"found command"<<lnt<<"param"<<parm;
+ }
+ //check command and replace
+ if(lnt=="#IF"){
+ nba+="<msmoketpl:if select=\"";
+ nba+=xmlize(parm);
+ nba+="\">";
+ }else if(lnt=="#ELSE")
+ nba+="<msmoketpl:else/>";
+ else if(lnt=="#ENDIF")
+ nba+="</msmoketpl:if>";
+ else if(lnt=="#SETNEWLINE"){
+ nba+="<!-- Warning: the obsolete #SETNEWLINE command has been used here! Please consider using a loop instead:\n";
+ nba+=xmlize(line);
+ nba+="\n-->";
+ }else if(lnt=="#CALC"){
+ nba+="<msmoketpl:calculate exec=\"";
+ nba+=xmlize(parm);
+ nba+="\"/>";
+ }else if(lnt=="#LOOP"){
+ nba+="<msmoketpl:loop variable=\"";
+ nba+=xmlize(parm);
+ nba+="\">";
+ }else if(lnt=="#ENDLOOP"){
+ nba+="</msmoketpl:loop>";
+ }else{
+ nba+="<!-- An unknown command was used here:\n";
+ nba+=xmlize(line);
+ nba+="\n-->";
+ }
+ }else //no, not special: just store it
+ nba+=line;
+ //add a newline anyway
nba+='\n';
}
- qDebug()<<"conversion test"<<nba;
+ //close the template
+ nba+="</msmoketpl:template>";
+// qDebug()<<"conversion test"<<nba;
return nba;
}