1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include <qmljs/qmljs_global.h>
29 #include <qmljs/persistenttrie.h>
30 #include <qmljs/qmljsdialect.h>
31 
32 #include <QString>
33 #include <QHash>
34 
QT_FORWARD_DECLARE_CLASS(QTextStream)35 QT_FORWARD_DECLARE_CLASS(QTextStream)
36 
37 namespace Utils { class JsonObjectValue; }
38 
39 namespace QmlJS {
40 
41 /* !
42  \class QmlJS::QmlBundle
43 
44  A Qmlbundle represents a set of qml libraries, with a list of their exports
45 
46  Note that searchPaths, installPaths and implicitImports are PersistentTries
47  and not QStringLists.
48  This makes merging easier, and the order is not important for our use case.
49 */
50 class QMLJS_EXPORT QmlBundle
51 {
52     typedef PersistentTrie::Trie Trie;
53 public:
54     QmlBundle(const QmlBundle &o);
55     QmlBundle();
56     QmlBundle(const QString &name,
57               const Trie &searchPaths,
58               const Trie &installPaths,
59               const Trie &supportedImports,
60               const Trie &implicitImports);
61 
62     QString name() const;
63     Trie installPaths() const;
64     Trie searchPaths() const;
65     Trie implicitImports() const;
66     Trie supportedImports() const;
67 
68     void merge(const QmlBundle &o);
69     void intersect(const QmlBundle &o);
70     QmlBundle mergeF(const QmlBundle &o) const;
71     QmlBundle intersectF(const QmlBundle &o) const;
72     bool isEmpty() const;
73     void replaceVars(const QHash<QString, QString> &replacements);
74     QmlBundle replaceVarsF(const QHash<QString, QString> &replacements) const;
75 
76     bool writeTo(const QString &path) const;
77     bool writeTo(QTextStream &stream, const QString &indent = QString()) const;
78     QString toString(const QString &indent = QString());
79     bool readFrom(QString path, QStringList *errors);
80     bool operator==(const QmlBundle &o) const;
81     bool operator!=(const QmlBundle &o) const;
82 private:
83     static void printEscaped(QTextStream &s, const QString &str);
84     static void writeTrie(QTextStream &stream, const Trie &t, const QString &indent);
85     QStringList maybeReadTrie(Trie &trie, Utils::JsonObjectValue *config, const QString &path,
86                           const QString &propertyName, bool required = false);
87 
88     QString m_name;
89     Trie m_searchPaths;
90     Trie m_installPaths;
91     Trie m_supportedImports;
92     Trie m_implicitImports;
93 };
94 
95 class QMLJS_EXPORT QmlLanguageBundles
96 {
97 public:
98     QmlBundle bundleForLanguage(Dialect l) const;
99     void mergeBundleForLanguage(Dialect l, const QmlBundle &bundle);
100     QList<Dialect> languages() const;
101     void mergeLanguageBundles(const QmlLanguageBundles &);
102 private:
103     QHash<Dialect,QmlBundle> m_bundles;
104 };
105 } // namespace QmlJS
106