converter routine for odf templates
authorKonrad Rosenbaum <konrad@silmor.de>
Mon, 30 Jan 2012 21:07:21 +0000 (22:07 +0100)
committerKonrad Rosenbaum <konrad@silmor.de>
Mon, 30 Jan 2012 21:07:21 +0000 (22:07 +0100)
src/misc/misc.cpp
src/misc/misc.h
src/templates/odtrender.cpp

index f2c0f98..663d788 100644 (file)
@@ -52,6 +52,21 @@ QString xmlize(QString str,QString newline)
        }
        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+="&lt;";else
+               if(c=='>')out+="&gt;";else
+               if(c=='&')out+="&amp;";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;
index 156b37c..4511d30 100644 (file)
@@ -26,6 +26,8 @@ QString htmlize(QString str);
 
 /**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);
index 655df4c..7938cf2 100644 (file)
@@ -117,13 +117,13 @@ MOdtRendererPrivate::MOdtRendererPrivate(QString file,MOdtRenderer*p)
        }
        //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()
@@ -432,11 +432,70 @@ QByteArray MOdtRenderer::convertV1toV2(const QByteArray& old)
 {
        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;
 }