source: branches/vendor/trolltech/qt/current/tools/linguist/shared/qph.cpp @ 2

Last change on this file since 2 was 2, checked in by Dmitry A. Kuminov, 13 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the Qt Linguist of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file.  Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file.  Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "translator.h"
43
44#include <QtCore/QByteArray>
45#include <QtCore/QDebug>
46#include <QtCore/QTextCodec>
47#include <QtCore/QTextStream>
48
49#include <QtXml/QXmlStreamReader>
50#include <QtXml/QXmlStreamAttribute>
51
52QT_BEGIN_NAMESPACE
53
54class QPHReader : public QXmlStreamReader
55{
56public:
57    QPHReader(QIODevice &dev, ConversionData &cd)
58      : QXmlStreamReader(&dev), m_cd(cd)
59    {}
60
61    // the "real thing"
62    bool read(Translator &translator);
63
64private:
65    bool elementStarts(const QString &str) const
66    {
67        return isStartElement() && name() == str;
68    }
69
70    bool isWhiteSpace() const
71    {
72        return isCharacters() && text().toString().trimmed().isEmpty();
73    }
74
75    // needed to expand <byte ... />
76    QString readContents();
77    // needed to join <lengthvariant>s
78    QString readTransContents();
79
80    void handleError();
81
82    ConversionData &m_cd;
83
84    enum DataField { NoField, SourceField, TargetField, DefinitionField };
85    DataField m_currentField;
86    QString m_currentSource;
87    QString m_currentTarget;
88    QString m_currentDefinition;
89};
90
91bool QPHReader::read(Translator &translator)
92{
93    m_currentField = NoField;
94    QString result;
95    while (!atEnd()) {
96        readNext();
97        if (isStartElement()) {
98            if (name() == QLatin1String("source"))
99                m_currentField = SourceField;
100            else if (name() == QLatin1String("target"))
101                m_currentField = TargetField;
102            else if (name() == QLatin1String("definition"))
103                m_currentField = DefinitionField;
104            else
105                m_currentField = NoField;
106        } else if (isWhiteSpace()) {
107            // ignore these
108        } else if (isCharacters()) {
109            if (m_currentField == SourceField)
110                m_currentSource += text();
111            else if (m_currentField == TargetField)
112                m_currentTarget += text();
113            else if (m_currentField == DefinitionField)
114                m_currentDefinition += text();
115        } else if (isEndElement() && name() == QLatin1String("phrase")) {
116            TranslatorMessage msg;
117            msg.setSourceText(m_currentSource);
118            msg.setTranslation(m_currentTarget);
119            msg.setTranslatorComment(m_currentDefinition);
120            translator.append(msg);
121            m_currentSource.clear();
122            m_currentTarget.clear();
123            m_currentDefinition.clear();
124        }
125    }
126    return true;
127}
128
129static bool loadQPH(Translator &translator, QIODevice &dev, ConversionData &cd)
130{
131    translator.setLocationsType(Translator::NoLocations);
132    QPHReader reader(dev, cd);
133    return reader.read(translator);
134}
135
136static QString protect(const QString &str)
137{
138    QString result;
139    result.reserve(str.length() * 12 / 10);
140    for (int i = 0; i != str.size(); ++i) {
141        uint c = str.at(i).unicode();
142        switch (c) {
143        case '\"':
144            result += QLatin1String("&quot;");
145            break;
146        case '&':
147            result += QLatin1String("&amp;");
148            break;
149        case '>':
150            result += QLatin1String("&gt;");
151            break;
152        case '<':
153            result += QLatin1String("&lt;");
154            break;
155        case '\'':
156            result += QLatin1String("&apos;");
157            break;
158        default:
159            if (c < 0x20 && c != '\r' && c != '\n' && c != '\t')
160                result += QString(QLatin1String("&#%1;")).arg(c);
161            else // this also covers surrogates
162                result += QChar(c);
163        }
164    }
165    return result;
166}
167
168static bool saveQPH(const Translator &translator, QIODevice &dev, ConversionData &)
169{
170    QTextStream t(&dev);
171    t.setCodec(QTextCodec::codecForName("UTF-8"));
172    t << "<!DOCTYPE QPH>\n<QPH>\n";
173    foreach (const TranslatorMessage &msg, translator.messages()) {
174        t << "<phrase>\n";
175        t << "    <source>" << protect(msg.sourceText()) << "</source>\n";
176        t << "    <target>" << protect(msg.translations().join(QLatin1String("@")))
177            << "</target>\n";
178        if (!msg.context().isEmpty() || !msg.comment().isEmpty())
179            t << "    <definition>" << msg.context() << msg.comment()
180                << "</definition>\n";
181        t << "</phrase>\n";
182    }
183    t << "</QPH>\n";
184    return true;
185}
186
187int initQPH()
188{
189    Translator::FileFormat format;
190
191    format.extension = QLatin1String("qph");
192    format.description = QObject::tr("Qt Linguist 'Phrase Book'");
193    format.fileType = Translator::FileFormat::TranslationSource;
194    format.priority = 0;
195    format.loader = &loadQPH;
196    format.saver = &saveQPH;
197    Translator::registerFileFormat(format);
198
199    return 1;
200}
201
202Q_CONSTRUCTOR_FUNCTION(initQPH)
203
204QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.