1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Linguist of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #include "translator.h"
30 
31 #include <QtCore/QByteArray>
32 #include <QtCore/QDebug>
33 #include <QtCore/QTextCodec>
34 #include <QtCore/QTextStream>
35 
36 #include <QtCore/QXmlStreamReader>
37 
38 QT_BEGIN_NAMESPACE
39 
40 class QPHReader : public QXmlStreamReader
41 {
42 public:
QPHReader(QIODevice & dev)43     QPHReader(QIODevice &dev)
44       : QXmlStreamReader(&dev)
45     {}
46 
47     // the "real thing"
48     bool read(Translator &translator);
49 
50 private:
isWhiteSpace() const51     bool isWhiteSpace() const
52     {
53         return isCharacters() && text().toString().trimmed().isEmpty();
54     }
55 
56     enum DataField { NoField, SourceField, TargetField, DefinitionField };
57     DataField m_currentField;
58     QString m_currentSource;
59     QString m_currentTarget;
60     QString m_currentDefinition;
61 };
62 
read(Translator & translator)63 bool QPHReader::read(Translator &translator)
64 {
65     m_currentField = NoField;
66     while (!atEnd()) {
67         readNext();
68         if (isStartElement()) {
69             if (name() == QLatin1String("source")) {
70                 m_currentField = SourceField;
71             } else if (name() == QLatin1String("target")) {
72                 m_currentField = TargetField;
73             } else if (name() == QLatin1String("definition")) {
74                 m_currentField = DefinitionField;
75             } else {
76                 m_currentField = NoField;
77                 if (name() == QLatin1String("QPH")) {
78                     QXmlStreamAttributes atts = attributes();
79                     translator.setLanguageCode(atts.value(QLatin1String("language")).toString());
80                     translator.setSourceLanguageCode(atts.value(QLatin1String("sourcelanguage")).toString());
81                 }
82             }
83         } else if (isWhiteSpace()) {
84             // ignore these
85         } else if (isCharacters()) {
86             if (m_currentField == SourceField)
87                 m_currentSource += text();
88             else if (m_currentField == TargetField)
89                 m_currentTarget += text();
90             else if (m_currentField == DefinitionField)
91                 m_currentDefinition += text();
92         } else if (isEndElement() && name() == QLatin1String("phrase")) {
93             m_currentTarget.replace(QChar(Translator::TextVariantSeparator),
94                                     QChar(Translator::BinaryVariantSeparator));
95             TranslatorMessage msg;
96             msg.setSourceText(m_currentSource);
97             msg.setTranslation(m_currentTarget);
98             msg.setComment(m_currentDefinition);
99             translator.append(msg);
100             m_currentSource.clear();
101             m_currentTarget.clear();
102             m_currentDefinition.clear();
103         }
104     }
105     return true;
106 }
107 
loadQPH(Translator & translator,QIODevice & dev,ConversionData &)108 static bool loadQPH(Translator &translator, QIODevice &dev, ConversionData &)
109 {
110     translator.setLocationsType(Translator::NoLocations);
111     QPHReader reader(dev);
112     return reader.read(translator);
113 }
114 
protect(const QString & str)115 static QString protect(const QString &str)
116 {
117     QString result;
118     result.reserve(str.length() * 12 / 10);
119     for (int i = 0; i != str.size(); ++i) {
120         uint c = str.at(i).unicode();
121         switch (c) {
122         case '\"':
123             result += QLatin1String("&quot;");
124             break;
125         case '&':
126             result += QLatin1String("&amp;");
127             break;
128         case '>':
129             result += QLatin1String("&gt;");
130             break;
131         case '<':
132             result += QLatin1String("&lt;");
133             break;
134         case '\'':
135             result += QLatin1String("&apos;");
136             break;
137         default:
138             if (c < 0x20 && c != '\r' && c != '\n' && c != '\t')
139                 result += QString(QLatin1String("&#%1;")).arg(c);
140             else // this also covers surrogates
141                 result += QChar(c);
142         }
143     }
144     return result;
145 }
146 
saveQPH(const Translator & translator,QIODevice & dev,ConversionData &)147 static bool saveQPH(const Translator &translator, QIODevice &dev, ConversionData &)
148 {
149     QTextStream t(&dev);
150     t.setCodec(QTextCodec::codecForName("UTF-8"));
151     t << "<!DOCTYPE QPH>\n<QPH";
152     QString languageCode = translator.languageCode();
153     if (!languageCode.isEmpty() && languageCode != QLatin1String("C"))
154         t << " language=\"" << languageCode << "\"";
155     languageCode = translator.sourceLanguageCode();
156     if (!languageCode.isEmpty() && languageCode != QLatin1String("C"))
157         t << " sourcelanguage=\"" << languageCode << "\"";
158     t << ">\n";
159     foreach (const TranslatorMessage &msg, translator.messages()) {
160         t << "<phrase>\n";
161         t << "    <source>" << protect(msg.sourceText()) << "</source>\n";
162         QString str = msg.translations().join(QLatin1Char('@'));
163         str.replace(QChar(Translator::BinaryVariantSeparator),
164                     QChar(Translator::TextVariantSeparator));
165         t << "    <target>" << protect(str)
166             << "</target>\n";
167         if (!msg.comment().isEmpty())
168             t << "    <definition>" << protect(msg.comment()) << "</definition>\n";
169         t << "</phrase>\n";
170     }
171     t << "</QPH>\n";
172     return true;
173 }
174 
initQPH()175 int initQPH()
176 {
177     Translator::FileFormat format;
178 
179     format.extension = QLatin1String("qph");
180     format.untranslatedDescription = QT_TRANSLATE_NOOP("FMT", "Qt Linguist 'Phrase Book'");
181     format.fileType = Translator::FileFormat::TranslationSource;
182     format.priority = 0;
183     format.loader = &loadQPH;
184     format.saver = &saveQPH;
185     Translator::registerFileFormat(format);
186 
187     return 1;
188 }
189 
190 Q_CONSTRUCTOR_FUNCTION(initQPH)
191 
192 QT_END_NAMESPACE
193