| 1 | /**************************************************************************** |
|---|---|
| 2 | ** |
| 3 | ** Copyright (C) 2018 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 "qrcreader.h" |
| 30 | |
| 31 | #include <QtCore/qcoreapplication.h> |
| 32 | #include <QtCore/qfileinfo.h> |
| 33 | #include <QtCore/qxmlstream.h> |
| 34 | |
| 35 | class FMT { |
| 36 | Q_DECLARE_TR_FUNCTIONS(Linguist) |
| 37 | }; |
| 38 | |
| 39 | static bool isSupportedExtension(const QString &ext) |
| 40 | { |
| 41 | return ext == QLatin1String("qml") |
| 42 | || ext == QLatin1String("js") || ext == QLatin1String( "qs") |
| 43 | || ext == QLatin1String("ui") || ext == QLatin1String( "jui"); |
| 44 | } |
| 45 | |
| 46 | ReadQrcResult readQrcFile(const QString &resourceFile, const QString &content) |
| 47 | { |
| 48 | ReadQrcResult result; |
| 49 | QString dirPath = QFileInfo(resourceFile).path(); |
| 50 | QXmlStreamReader reader(content); |
| 51 | bool isFileTag = false; |
| 52 | QStringList tagStack; |
| 53 | tagStack << QLatin1String("RCC") << QLatin1String( "qresource") << QLatin1String( "file"); |
| 54 | int curDepth = 0; |
| 55 | while (!reader.atEnd()) { |
| 56 | QXmlStreamReader::TokenType t = reader.readNext(); |
| 57 | switch (t) { |
| 58 | case QXmlStreamReader::StartElement: |
| 59 | if (curDepth >= tagStack.count() || reader.name() != tagStack.at(i: curDepth)) { |
| 60 | result.errorString = FMT::tr(sourceText: "unexpected <%1> tag\n") |
| 61 | .arg(a: reader.name().toString()); |
| 62 | result.line = reader.lineNumber(); |
| 63 | return result; |
| 64 | } |
| 65 | if (++curDepth == tagStack.count()) |
| 66 | isFileTag = true; |
| 67 | break; |
| 68 | |
| 69 | case QXmlStreamReader::EndElement: |
| 70 | isFileTag = false; |
| 71 | if (curDepth == 0 || reader.name() != tagStack.at(i: curDepth - 1)) { |
| 72 | result.errorString = FMT::tr(sourceText: "unexpected closing <%1> tag\n") |
| 73 | .arg(a: reader.name().toString()); |
| 74 | result.line = reader.lineNumber(); |
| 75 | return result; |
| 76 | } |
| 77 | --curDepth; |
| 78 | break; |
| 79 | |
| 80 | case QXmlStreamReader::Characters: |
| 81 | if (isFileTag) { |
| 82 | QString fn = reader.text().toString(); |
| 83 | if (!QFileInfo(fn).isAbsolute()) |
| 84 | fn = dirPath + QLatin1Char('/') + fn; |
| 85 | QFileInfo cfi(fn); |
| 86 | if (isSupportedExtension(ext: cfi.suffix())) |
| 87 | result.files << cfi.filePath(); |
| 88 | } |
| 89 | break; |
| 90 | |
| 91 | default: |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | if (reader.error() != QXmlStreamReader::NoError) { |
| 96 | result.errorString = reader.errorString(); |
| 97 | result.line = reader.lineNumber(); |
| 98 | } |
| 99 | return result; |
| 100 | } |
| 101 |
