change in interpreter
[mdictionary] / src / mdictionary / gui / TranslationWidget.cpp
index 468087b..cf796c7 100644 (file)
@@ -80,10 +80,13 @@ void TranslationWidget::show(QStringList translations) {
         trans += t + "\n";
     }
 
+
     trans=QString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + QString("\n <ar>")
            + trans + QString("\n </ar>");
 
-    qDebug()<<trans;
+    //qDebug()<<"\n\n\n\n\n\n\n\n\n before"<<trans;
+    trans=checkTranslation(trans);
+    //qDebug()<<"\n\n after"<<trans;
     trans=XslConversion(trans);
 
     QString head = "<html><head>";
@@ -112,6 +115,77 @@ QString TranslationWidget::XslConversion(QString translation)
     return result;
 }
 
+QString TranslationWidget::checkTranslation(QString trans){
+    /*check all "&"*/
+    int pos=0;
+    int pos2;
+    while(pos!=-1){
+        pos=trans.indexOf("&",pos+1);
+        pos2=trans.indexOf("amp;",pos);
+        if(pos!=-1 && pos2!=-1 && pos+1==pos2)
+            continue;
+        pos2=trans.indexOf("gt;",pos);
+        if(pos!=-1 && pos2!=-1 && pos+1==pos2)
+            continue;
+        pos2=trans.indexOf("lt;",pos);
+        if(pos!=-1 && pos2!=-1 && pos+1==pos2)
+            continue;
+        pos2=trans.indexOf("apos;",pos);
+        if(pos!=-1 && pos2!=-1 && pos+1==pos2)
+            continue;
+        pos2=trans.indexOf("quot;",pos);
+        if(pos!=-1 && pos2!=-1 && pos+1==pos2)
+            continue;
+        if(pos!=-1)
+            trans.insert(pos+1,"amp;");
+        if(pos>40)
+            while(1);
+    }
+
+    /*check all "<" and ">" */
+    pos=0;
+    int count=0;
+    while(pos!=-1){
+        pos=trans.indexOf("<",pos+1);
+        pos2=trans.indexOf(">",pos+1);
+        if(pos!=-1 && pos2!=-1){
+            if(trans.at(pos2-1)!='/' && trans.at(pos+1)!='/')
+                count++;
+            if(trans.at(pos+1)=='/')
+                count--;
+        }
+ /*     else if(pos==-1 && pos2!=-1){
+            qDebug()<<"pos=-1";
+            trans.remove(pos2,1);
+        }
+        else if(pos2==-1 && pos!=-1){
+            qDebug()<<"pos2=-1";
+            trans.remove(pos,1);
+        }
+        else {
+            qDebug()<<"pos i pos2 = -1";
+            break;
+        }
+        qDebug()<<"count"<<count;
+*/
+    }
+
+    /*check all """ and "'" */
+    pos=0;
+    count=0;
+    while(pos<trans.size()){
+        if(trans.at(pos)=='<')
+            count++;
+        else if(trans.at(pos)=='>')
+            count--;
+        else if(trans.at(pos)=='\"' && count==0)
+            trans.replace(pos,1,"&quot;");
+        else if(trans.at(pos)=='\'' && count==0)
+            trans.replace(pos,1,"&apos;");
+        pos++;
+    }
+    return trans;
+}
 
 #ifdef Q_WS_MAEMO_5
 void TranslationWidget::initButtons() {