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