forked from KDE/licensedigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectoryparser.h
72 lines (63 loc) · 2.91 KB
/
directoryparser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* SPDX-FileCopyrightText: 2019 Andreas Cord-Landwehr <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#ifndef DIRECTORYPARSER_H
#define DIRECTORYPARSER_H
#include "licenseregistry.h"
#include <QRegularExpression>
class DirectoryParser
{
public:
enum class LicenseParser { SKIP_PARSER, REGEXP_PARSER };
enum class ConvertOption {
NONE = 0x0,
LICENSE_INFO = 0x1,
COPYRIGHT_TEXT = 0x2,
PRETTY = 0x4
};
Q_DECLARE_FLAGS(ConvertOptions, ConvertOption)
void setLicenseHeaderParser(LicenseParser parser);
QMap<QString, LicenseRegistry::SpdxExpression> parseAll(const QString &directory, bool convertMode = false, const QString &ignorePattern = QString()) const;
void convertCopyright(const QString &directory, ConvertOptions = ConvertOption::COPYRIGHT_TEXT, const QString &ignorePattern = QString()) const;
QRegularExpression copyrightRegExp() const;
QRegularExpression spdxStatementRegExp() const;
QString unifyCopyrightStatements(const QString &originalText) const;
QString unifyCopyrightCommentHeader(const QString &originalText) const;
QString cleanupSpaceInCopyrightYearList(const QString &originalYearText) const;
/**
* @brief Uses regexp for the SPDX expression and replace matching text
* @param fileContent The input content
* @param spdxExpression The SPDX expression that shall be detected
* @return Converted file content with correct SPDX statement
*/
QString replaceHeaderText(const QString &fileContent, const QString &spdxExpression) const;
/**
* @brief Detect licenses by computing all matches
* @param fileContent the content of the file
* @return the list of detected license matches
*/
QVector<LicenseRegistry::SpdxExpression> detectLicenses(const QString &fileContent) const;
LicenseRegistry::SpdxExpression detectSpdxLicenseStatement(const QString &fileContent) const;
/**
* @brief Take license liste and prune statements
*
* This is a simple algorithm can handle the following cases:
* - license containted twice
* - license detacted singular and detected in OR statement as well
* - license detacted singular and detected in WITH statement as well
*
* @param inputLicenses
* @return
*/
QVector<LicenseRegistry::SpdxExpression> pruneLicenseList(const QVector<LicenseRegistry::SpdxExpression> &inputLicenses) const;
private:
QVector<LicenseRegistry::SpdxExpression> detectLicensesRegexpParser(const QString &fileContent) const;
QVector<LicenseRegistry::SpdxExpression> detectLicensesSkipParser(const QString &fileContent) const;
LicenseRegistry m_registry;
LicenseParser m_parserType {LicenseParser::REGEXP_PARSER};
static const QStringList s_supportedExtensions;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(DirectoryParser::ConvertOptions)
#endif // DIRECTORYPARSER_H