| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qrcreader.h" |
| 5 | #include "fmt.h" |
| 6 | |
| 7 | #include <QtCore/qcoreapplication.h> |
| 8 | #include <QtCore/qfileinfo.h> |
| 9 | #include <QtCore/qxmlstream.h> |
| 10 | |
| 11 | using namespace Qt::Literals::StringLiterals; |
| 12 | |
| 13 | bool isSupportedExtension(const QString &ext) |
| 14 | { |
| 15 | return ext == "qml"_L1|| ext == "js"_L1|| ext == "qs"_L1|| ext == "ui"_L1|| ext == "jui"_L1; |
| 16 | } |
| 17 | |
| 18 | ReadQrcResult readQrcFile(const QString &resourceFile, const QString &content) |
| 19 | { |
| 20 | ReadQrcResult result; |
| 21 | QString dirPath = QFileInfo(resourceFile).path(); |
| 22 | QXmlStreamReader reader(content); |
| 23 | bool isFileTag = false; |
| 24 | QStringList tagStack{ "RCC"_L1, "qresource"_L1, "file"_L1}; |
| 25 | int curDepth = 0; |
| 26 | while (!reader.atEnd()) { |
| 27 | QXmlStreamReader::TokenType t = reader.readNext(); |
| 28 | switch (t) { |
| 29 | case QXmlStreamReader::StartElement: |
| 30 | if (curDepth >= tagStack.size() || reader.name() != tagStack.at(i: curDepth)) { |
| 31 | result.errorString = FMT::tr(sourceText: "unexpected <%1> tag\n") |
| 32 | .arg(a: reader.name().toString()); |
| 33 | result.line = reader.lineNumber(); |
| 34 | return result; |
| 35 | } |
| 36 | if (++curDepth == tagStack.size()) |
| 37 | isFileTag = true; |
| 38 | break; |
| 39 | |
| 40 | case QXmlStreamReader::EndElement: |
| 41 | isFileTag = false; |
| 42 | if (curDepth == 0 || reader.name() != tagStack.at(i: curDepth - 1)) { |
| 43 | result.errorString = FMT::tr(sourceText: "unexpected closing <%1> tag\n") |
| 44 | .arg(a: reader.name().toString()); |
| 45 | result.line = reader.lineNumber(); |
| 46 | return result; |
| 47 | } |
| 48 | --curDepth; |
| 49 | break; |
| 50 | |
| 51 | case QXmlStreamReader::Characters: |
| 52 | if (isFileTag) { |
| 53 | QString fn = reader.text().toString(); |
| 54 | if (!QFileInfo(fn).isAbsolute()) |
| 55 | fn = dirPath + u'/' + fn; |
| 56 | QFileInfo cfi(fn); |
| 57 | if (isSupportedExtension(ext: cfi.suffix())) |
| 58 | result.files << cfi.filePath(); |
| 59 | } |
| 60 | break; |
| 61 | |
| 62 | default: |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | if (reader.error() != QXmlStreamReader::NoError) { |
| 67 | result.errorString = reader.errorString(); |
| 68 | result.line = reader.lineNumber(); |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 |
