1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module 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 "resourcefilemapper.h"
30
31#include <QFileInfo>
32#include <QDir>
33#include <QXmlStreamReader>
34
35ResourceFileMapper::ResourceFileMapper(const QStringList &resourceFiles)
36{
37 for (const QString &fileName: resourceFiles) {
38 QFile f(fileName);
39 if (!f.open(flags: QIODevice::ReadOnly))
40 continue;
41 populateFromQrcFile(file&: f);
42 }
43}
44
45bool ResourceFileMapper::isEmpty() const
46{
47 return qrcPathToFileSystemPath.isEmpty();
48}
49
50QStringList ResourceFileMapper::resourcePaths(const QString &fileName)
51{
52 const QString absPath = QDir::cleanPath(path: QDir::current().absoluteFilePath(fileName));
53 QStringList resourcePaths;
54 for (auto it = qrcPathToFileSystemPath.cbegin(), end = qrcPathToFileSystemPath.cend(); it != end; ++it) {
55 if (QFileInfo(it.value()) == QFileInfo(absPath))
56 resourcePaths.append(t: it.key());
57 }
58 return resourcePaths;
59}
60
61QStringList ResourceFileMapper::qmlCompilerFiles(FileOutput fo) const
62{
63 QStringList files;
64 for (auto it = qrcPathToFileSystemPath.constBegin(), end = qrcPathToFileSystemPath.constEnd();
65 it != end; ++it) {
66 const QString &qrcPath = it.key();
67 const QString suffix = QFileInfo(qrcPath).suffix();
68 if (suffix != QStringLiteral("qml") && suffix != QStringLiteral("js") && suffix != QStringLiteral("mjs"))
69 continue;
70 if (fo == FileOutput::AbsoluteFilePath)
71 files << it.value();
72 else
73 files << qrcPath;
74 }
75 return files;
76}
77
78void ResourceFileMapper::populateFromQrcFile(QFile &file)
79{
80 enum State {
81 InitialState,
82 InRCC,
83 InResource,
84 InFile
85 };
86 State state = InitialState;
87
88 QDir qrcDir = QFileInfo(file).absoluteDir();
89
90 QString prefix;
91 QString currentFileName;
92 QXmlStreamAttributes currentFileAttributes;
93
94 QXmlStreamReader reader(&file);
95 while (!reader.atEnd()) {
96 switch (reader.readNext()) {
97 case QXmlStreamReader::StartElement:
98 if (reader.name() == QStringLiteral("RCC")) {
99 if (state != InitialState)
100 return;
101 state = InRCC;
102 continue;
103 } else if (reader.name() == QStringLiteral("qresource")) {
104 if (state != InRCC)
105 return;
106 state = InResource;
107 QXmlStreamAttributes attributes = reader.attributes();
108 if (attributes.hasAttribute(QStringLiteral("prefix")))
109 prefix = attributes.value(QStringLiteral("prefix")).toString();
110 if (!prefix.startsWith(c: QLatin1Char('/')))
111 prefix.prepend(c: QLatin1Char('/'));
112 if (!prefix.endsWith(c: QLatin1Char('/')))
113 prefix.append(c: QLatin1Char('/'));
114 continue;
115 } else if (reader.name() == QStringLiteral("file")) {
116 if (state != InResource)
117 return;
118 state = InFile;
119 currentFileAttributes = reader.attributes();
120 continue;
121 }
122 return;
123
124 case QXmlStreamReader::EndElement:
125 if (reader.name() == QStringLiteral("file")) {
126 if (state != InFile)
127 return;
128 state = InResource;
129 continue;
130 } else if (reader.name() == QStringLiteral("qresource")) {
131 if (state != InResource)
132 return;
133 state = InRCC;
134 continue;
135 } else if (reader.name() == QStringLiteral("RCC")) {
136 if (state != InRCC)
137 return;
138 state = InitialState;
139 continue;
140 }
141 return;
142
143 case QXmlStreamReader::Characters: {
144 if (reader.isWhitespace())
145 break;
146 if (state != InFile)
147 return;
148 currentFileName = reader.text().toString();
149 if (currentFileName.isEmpty())
150 continue;
151
152 const QString fsPath = QDir::cleanPath(path: qrcDir.absoluteFilePath(fileName: currentFileName));
153
154 if (currentFileAttributes.hasAttribute(QStringLiteral("alias")))
155 currentFileName = currentFileAttributes.value(QStringLiteral("alias")).toString();
156
157 currentFileName = QDir::cleanPath(path: currentFileName);
158 while (currentFileName.startsWith(s: QLatin1String("../")))
159 currentFileName.remove(i: 0, len: 3);
160
161 const QString qrcPath = prefix + currentFileName;
162 if (QFile::exists(fileName: fsPath))
163 qrcPathToFileSystemPath.insert(akey: qrcPath, avalue: fsPath);
164 continue;
165 }
166
167 default: break;
168 }
169 }
170}
171

source code of qtdeclarative/tools/shared/resourcefilemapper.cpp