| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** 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 have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "lupdate.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include <translator.h> | 
|---|
| 45 |  | 
|---|
| 46 | #include <QtCore/QDebug> | 
|---|
| 47 | #include <QtCore/QFile> | 
|---|
| 48 | #include <QtCore/QString> | 
|---|
| 49 |  | 
|---|
| 50 | #include <QtXml/QXmlAttributes> | 
|---|
| 51 | #include <QtXml/QXmlDefaultHandler> | 
|---|
| 52 | #include <QtXml/QXmlLocator> | 
|---|
| 53 | #include <QtXml/QXmlParseException> | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | QT_BEGIN_NAMESPACE | 
|---|
| 57 |  | 
|---|
| 58 | class UiReader : public QXmlDefaultHandler | 
|---|
| 59 | { | 
|---|
| 60 | public: | 
|---|
| 61 | UiReader(Translator &translator, ConversionData &cd) | 
|---|
| 62 | : m_translator(translator), m_cd(cd), m_lineNumber(-1), m_isTrString(false), | 
|---|
| 63 | m_needUtf8(translator.codecName() != "UTF-8") | 
|---|
| 64 | {} | 
|---|
| 65 |  | 
|---|
| 66 | bool startElement(const QString &namespaceURI, const QString &localName, | 
|---|
| 67 | const QString &qName, const QXmlAttributes &atts); | 
|---|
| 68 | bool endElement(const QString &namespaceURI, const QString &localName, | 
|---|
| 69 | const QString &qName); | 
|---|
| 70 | bool characters(const QString &ch); | 
|---|
| 71 | bool fatalError(const QXmlParseException &exception); | 
|---|
| 72 |  | 
|---|
| 73 | void setDocumentLocator(QXmlLocator *locator) { m_locator = locator; } | 
|---|
| 74 |  | 
|---|
| 75 | private: | 
|---|
| 76 | void flush(); | 
|---|
| 77 |  | 
|---|
| 78 | Translator &m_translator; | 
|---|
| 79 | ConversionData &m_cd; | 
|---|
| 80 | QString m_context; | 
|---|
| 81 | QString m_source; | 
|---|
| 82 | QString m_comment; | 
|---|
| 83 | QString m_extracomment; | 
|---|
| 84 | QXmlLocator *m_locator; | 
|---|
| 85 |  | 
|---|
| 86 | QString m_accum; | 
|---|
| 87 | int m_lineNumber; | 
|---|
| 88 | bool m_isTrString; | 
|---|
| 89 | bool m_needUtf8; | 
|---|
| 90 | }; | 
|---|
| 91 |  | 
|---|
| 92 | bool UiReader::startElement(const QString &namespaceURI, | 
|---|
| 93 | const QString &localName, const QString &qName, const QXmlAttributes &atts) | 
|---|
| 94 | { | 
|---|
| 95 | Q_UNUSED(namespaceURI); | 
|---|
| 96 | Q_UNUSED(localName); | 
|---|
| 97 |  | 
|---|
| 98 | if (qName == QLatin1String("item")) { // UI3 menu entries | 
|---|
| 99 | flush(); | 
|---|
| 100 | if (!atts.value(QLatin1String("text")).isEmpty()) { | 
|---|
| 101 | m_source = atts.value(QLatin1String("text")); | 
|---|
| 102 | m_isTrString = true; | 
|---|
| 103 | if (!m_cd.m_noUiLines) | 
|---|
| 104 | m_lineNumber = m_locator->lineNumber(); | 
|---|
| 105 | } | 
|---|
| 106 | } else if (qName == QLatin1String("string")) { | 
|---|
| 107 | flush(); | 
|---|
| 108 | if (atts.value(QLatin1String("notr")).isEmpty() || | 
|---|
| 109 | atts.value(QLatin1String("notr")) != QLatin1String("true")) { | 
|---|
| 110 | m_isTrString = true; | 
|---|
| 111 | m_comment = atts.value(QLatin1String("comment")); | 
|---|
| 112 | m_extracomment = atts.value(QLatin1String("extracomment")); | 
|---|
| 113 | if (!m_cd.m_noUiLines) | 
|---|
| 114 | m_lineNumber = m_locator->lineNumber(); | 
|---|
| 115 | } else { | 
|---|
| 116 | m_isTrString = false; | 
|---|
| 117 | } | 
|---|
| 118 | } | 
|---|
| 119 | m_accum.clear(); | 
|---|
| 120 | return true; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | bool UiReader::endElement(const QString &namespaceURI, | 
|---|
| 124 | const QString &localName, const QString &qName) | 
|---|
| 125 | { | 
|---|
| 126 | Q_UNUSED(namespaceURI); | 
|---|
| 127 | Q_UNUSED(localName); | 
|---|
| 128 |  | 
|---|
| 129 | m_accum.replace(QLatin1String("\r\n"), QLatin1String("\n")); | 
|---|
| 130 |  | 
|---|
| 131 | if (qName == QLatin1String("class")) { // UI "header" | 
|---|
| 132 | if (m_context.isEmpty()) | 
|---|
| 133 | m_context = m_accum; | 
|---|
| 134 | } else if (qName == QLatin1String("string") && m_isTrString) { | 
|---|
| 135 | m_source = m_accum; | 
|---|
| 136 | } else if (qName == QLatin1String("comment")) { // FIXME: what's that? | 
|---|
| 137 | m_comment = m_accum; | 
|---|
| 138 | flush(); | 
|---|
| 139 | } else if (qName == QLatin1String("function")) { // UI3 embedded code | 
|---|
| 140 | fetchtrInlinedCpp(m_accum, m_translator, m_context); | 
|---|
| 141 | } else { | 
|---|
| 142 | flush(); | 
|---|
| 143 | } | 
|---|
| 144 | return true; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | bool UiReader::characters(const QString &ch) | 
|---|
| 148 | { | 
|---|
| 149 | m_accum += ch; | 
|---|
| 150 | return true; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | bool UiReader::fatalError(const QXmlParseException &exception) | 
|---|
| 154 | { | 
|---|
| 155 | QString msg; | 
|---|
| 156 | msg.sprintf("XML error: Parse error at line %d, column %d (%s).", | 
|---|
| 157 | exception.lineNumber(), exception.columnNumber(), | 
|---|
| 158 | exception.message().toLatin1().data()); | 
|---|
| 159 | m_cd.appendError(msg); | 
|---|
| 160 | return false; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void UiReader::flush() | 
|---|
| 164 | { | 
|---|
| 165 | if (!m_context.isEmpty() && !m_source.isEmpty()) { | 
|---|
| 166 | TranslatorMessage msg(m_context, m_source, | 
|---|
| 167 | m_comment, QString(), m_cd.m_sourceFileName, | 
|---|
| 168 | m_lineNumber, QStringList()); | 
|---|
| 169 | msg.setExtraComment(m_extracomment); | 
|---|
| 170 | if (m_needUtf8 && msg.needs8Bit()) | 
|---|
| 171 | msg.setUtf8(true); | 
|---|
| 172 | m_translator.extend(msg); | 
|---|
| 173 | } | 
|---|
| 174 | m_source.clear(); | 
|---|
| 175 | m_comment.clear(); | 
|---|
| 176 | m_extracomment.clear(); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | bool loadUI(Translator &translator, const QString &filename, ConversionData &cd) | 
|---|
| 180 | { | 
|---|
| 181 | cd.m_sourceFileName = filename; | 
|---|
| 182 | QFile file(filename); | 
|---|
| 183 | if (!file.open(QIODevice::ReadOnly)) { | 
|---|
| 184 | cd.appendError(QString::fromLatin1("Cannot open %1: %2") | 
|---|
| 185 | .arg(filename, file.errorString())); | 
|---|
| 186 | return false; | 
|---|
| 187 | } | 
|---|
| 188 | QXmlInputSource in(&file); | 
|---|
| 189 | QXmlSimpleReader reader; | 
|---|
| 190 | reader.setFeature(QLatin1String("http://xml.org/sax/features/namespaces"), false); | 
|---|
| 191 | reader.setFeature(QLatin1String("http://xml.org/sax/features/namespace-prefixes"), true); | 
|---|
| 192 | reader.setFeature(QLatin1String( | 
|---|
| 193 | "http://trolltech.com/xml/features/report-whitespace-only-CharData"), false); | 
|---|
| 194 | UiReader handler(translator, cd); | 
|---|
| 195 | reader.setContentHandler(&handler); | 
|---|
| 196 | reader.setErrorHandler(&handler); | 
|---|
| 197 | bool result = reader.parse(in); | 
|---|
| 198 | if (!result) | 
|---|
| 199 | cd.appendError(QLatin1String("Parse error in UI file")); | 
|---|
| 200 | reader.setContentHandler(0); | 
|---|
| 201 | reader.setErrorHandler(0); | 
|---|
| 202 | return result; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | QT_END_NAMESPACE | 
|---|