author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 "lupdate.h" |
|
43 |
||
44 |
#include <translator.h> |
|
45 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
46 |
#include <QtCore/QCoreApplication> |
0 | 47 |
#include <QtCore/QDebug> |
48 |
#include <QtCore/QFile> |
|
49 |
#include <QtCore/QString> |
|
50 |
||
51 |
#include <QtXml/QXmlAttributes> |
|
52 |
#include <QtXml/QXmlDefaultHandler> |
|
53 |
#include <QtXml/QXmlLocator> |
|
54 |
#include <QtXml/QXmlParseException> |
|
55 |
||
56 |
||
57 |
QT_BEGIN_NAMESPACE |
|
58 |
||
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
59 |
class LU { |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
60 |
Q_DECLARE_TR_FUNCTIONS(LUpdate) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
61 |
}; |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
62 |
|
0 | 63 |
class UiReader : public QXmlDefaultHandler |
64 |
{ |
|
65 |
public: |
|
66 |
UiReader(Translator &translator, ConversionData &cd) |
|
67 |
: m_translator(translator), m_cd(cd), m_lineNumber(-1), m_isTrString(false), |
|
68 |
m_needUtf8(translator.codecName() != "UTF-8") |
|
69 |
{} |
|
70 |
||
71 |
bool startElement(const QString &namespaceURI, const QString &localName, |
|
72 |
const QString &qName, const QXmlAttributes &atts); |
|
73 |
bool endElement(const QString &namespaceURI, const QString &localName, |
|
74 |
const QString &qName); |
|
75 |
bool characters(const QString &ch); |
|
76 |
bool fatalError(const QXmlParseException &exception); |
|
77 |
||
78 |
void setDocumentLocator(QXmlLocator *locator) { m_locator = locator; } |
|
79 |
||
80 |
private: |
|
81 |
void flush(); |
|
82 |
||
83 |
Translator &m_translator; |
|
84 |
ConversionData &m_cd; |
|
85 |
QString m_context; |
|
86 |
QString m_source; |
|
87 |
QString m_comment; |
|
88 |
QString m_extracomment; |
|
89 |
QXmlLocator *m_locator; |
|
90 |
||
91 |
QString m_accum; |
|
92 |
int m_lineNumber; |
|
93 |
bool m_isTrString; |
|
94 |
bool m_needUtf8; |
|
95 |
}; |
|
96 |
||
97 |
bool UiReader::startElement(const QString &namespaceURI, |
|
98 |
const QString &localName, const QString &qName, const QXmlAttributes &atts) |
|
99 |
{ |
|
100 |
Q_UNUSED(namespaceURI); |
|
101 |
Q_UNUSED(localName); |
|
102 |
||
103 |
if (qName == QLatin1String("item")) { // UI3 menu entries |
|
104 |
flush(); |
|
105 |
if (!atts.value(QLatin1String("text")).isEmpty()) { |
|
106 |
m_source = atts.value(QLatin1String("text")); |
|
107 |
m_isTrString = true; |
|
108 |
if (!m_cd.m_noUiLines) |
|
109 |
m_lineNumber = m_locator->lineNumber(); |
|
110 |
} |
|
111 |
} else if (qName == QLatin1String("string")) { |
|
112 |
flush(); |
|
113 |
if (atts.value(QLatin1String("notr")).isEmpty() || |
|
114 |
atts.value(QLatin1String("notr")) != QLatin1String("true")) { |
|
115 |
m_isTrString = true; |
|
116 |
m_comment = atts.value(QLatin1String("comment")); |
|
117 |
m_extracomment = atts.value(QLatin1String("extracomment")); |
|
118 |
if (!m_cd.m_noUiLines) |
|
119 |
m_lineNumber = m_locator->lineNumber(); |
|
120 |
} else { |
|
121 |
m_isTrString = false; |
|
122 |
} |
|
123 |
} |
|
124 |
m_accum.clear(); |
|
125 |
return true; |
|
126 |
} |
|
127 |
||
128 |
bool UiReader::endElement(const QString &namespaceURI, |
|
129 |
const QString &localName, const QString &qName) |
|
130 |
{ |
|
131 |
Q_UNUSED(namespaceURI); |
|
132 |
Q_UNUSED(localName); |
|
133 |
||
134 |
m_accum.replace(QLatin1String("\r\n"), QLatin1String("\n")); |
|
135 |
||
136 |
if (qName == QLatin1String("class")) { // UI "header" |
|
137 |
if (m_context.isEmpty()) |
|
138 |
m_context = m_accum; |
|
139 |
} else if (qName == QLatin1String("string") && m_isTrString) { |
|
140 |
m_source = m_accum; |
|
141 |
} else if (qName == QLatin1String("comment")) { // FIXME: what's that? |
|
142 |
m_comment = m_accum; |
|
143 |
flush(); |
|
144 |
} else if (qName == QLatin1String("function")) { // UI3 embedded code |
|
145 |
fetchtrInlinedCpp(m_accum, m_translator, m_context); |
|
146 |
} else { |
|
147 |
flush(); |
|
148 |
} |
|
149 |
return true; |
|
150 |
} |
|
151 |
||
152 |
bool UiReader::characters(const QString &ch) |
|
153 |
{ |
|
154 |
m_accum += ch; |
|
155 |
return true; |
|
156 |
} |
|
157 |
||
158 |
bool UiReader::fatalError(const QXmlParseException &exception) |
|
159 |
{ |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
160 |
QString msg = LU::tr("XML error: Parse error at line %1, column %2 (%3).") |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
161 |
.arg(exception.lineNumber()).arg(exception.columnNumber()) |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
162 |
.arg(exception.message()); |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
163 |
m_cd.appendError(msg); |
0 | 164 |
return false; |
165 |
} |
|
166 |
||
167 |
void UiReader::flush() |
|
168 |
{ |
|
169 |
if (!m_context.isEmpty() && !m_source.isEmpty()) { |
|
170 |
TranslatorMessage msg(m_context, m_source, |
|
171 |
m_comment, QString(), m_cd.m_sourceFileName, |
|
172 |
m_lineNumber, QStringList()); |
|
173 |
msg.setExtraComment(m_extracomment); |
|
174 |
if (m_needUtf8 && msg.needs8Bit()) |
|
175 |
msg.setUtf8(true); |
|
176 |
m_translator.extend(msg); |
|
177 |
} |
|
178 |
m_source.clear(); |
|
179 |
m_comment.clear(); |
|
180 |
m_extracomment.clear(); |
|
181 |
} |
|
182 |
||
183 |
bool loadUI(Translator &translator, const QString &filename, ConversionData &cd) |
|
184 |
{ |
|
185 |
cd.m_sourceFileName = filename; |
|
186 |
QFile file(filename); |
|
187 |
if (!file.open(QIODevice::ReadOnly)) { |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
188 |
cd.appendError(LU::tr("Cannot open %1: %2").arg(filename, file.errorString())); |
0 | 189 |
return false; |
190 |
} |
|
191 |
QXmlInputSource in(&file); |
|
192 |
QXmlSimpleReader reader; |
|
193 |
reader.setFeature(QLatin1String("http://xml.org/sax/features/namespaces"), false); |
|
194 |
reader.setFeature(QLatin1String("http://xml.org/sax/features/namespace-prefixes"), true); |
|
195 |
reader.setFeature(QLatin1String( |
|
196 |
"http://trolltech.com/xml/features/report-whitespace-only-CharData"), false); |
|
197 |
UiReader handler(translator, cd); |
|
198 |
reader.setContentHandler(&handler); |
|
199 |
reader.setErrorHandler(&handler); |
|
200 |
bool result = reader.parse(in); |
|
201 |
if (!result) |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
202 |
cd.appendError(LU::tr("Parse error in UI file")); |
0 | 203 |
reader.setContentHandler(0); |
204 |
reader.setErrorHandler(0); |
|
205 |
return result; |
|
206 |
} |
|
207 |
||
208 |
QT_END_NAMESPACE |