File indexing completed on 2024-04-21 16:29:08

0001 /*
0002  *  SPDX-FileCopyrightText: 2019  Andreas Cord-Landwehr <cordlandwehr@kde.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #ifndef DIRECTORYPARSER_H
0008 #define DIRECTORYPARSER_H
0009 
0010 #include "licenseregistry.h"
0011 #include <QRegularExpression>
0012 
0013 class DirectoryParser
0014 {
0015 public:
0016     enum class LicenseParser { SKIP_PARSER, REGEXP_PARSER };
0017     enum class ConvertOption {
0018         NONE = 0x0,
0019         LICENSE_INFO = 0x1,
0020         COPYRIGHT_TEXT = 0x2,
0021         PRETTY = 0x4
0022     };
0023     Q_DECLARE_FLAGS(ConvertOptions, ConvertOption)
0024 
0025     void setLicenseHeaderParser(LicenseParser parser);
0026     QMap<QString, LicenseRegistry::SpdxExpression> parseAll(const QString &directory, bool convertMode = false, const QString &ignorePattern = QString()) const;
0027     void convertCopyright(const QString &directory, ConvertOptions = ConvertOption::COPYRIGHT_TEXT, const QString &ignorePattern = QString()) const;
0028     QRegularExpression copyrightRegExp() const;
0029     QRegularExpression spdxStatementRegExp() const;
0030     QString unifyCopyrightStatements(const QString &originalText) const;
0031     QString unifyCopyrightCommentHeader(const QString &originalText) const;
0032     QString cleanupSpaceInCopyrightYearList(const QString &originalYearText) const;
0033     /**
0034      * @brief Uses regexp for the SPDX expression and replace matching text
0035      * @param fileContent The input content
0036      * @param spdxExpression The SPDX expression that shall be detected
0037      * @return Converted file content with correct SPDX statement
0038      */
0039     QString replaceHeaderText(const QString &fileContent, const QString &spdxExpression) const;
0040 
0041     /**
0042      * @brief Detect licenses by computing all matches
0043      * @param fileContent the content of the file
0044      * @return the list of detected license matches
0045      */
0046     QVector<LicenseRegistry::SpdxExpression> detectLicenses(const QString &fileContent) const;
0047     LicenseRegistry::SpdxExpression detectSpdxLicenseStatement(const QString &fileContent) const;
0048 
0049     /**
0050      * @brief Take license liste and prune statements
0051      *
0052      * This is a simple algorithm can handle the following cases:
0053      * - license containted twice
0054      * - license detacted singular and detected in OR statement as well
0055      * - license detacted singular and detected in WITH statement as well
0056      *
0057      * @param inputLicenses
0058      * @return
0059      */
0060     QVector<LicenseRegistry::SpdxExpression> pruneLicenseList(const QVector<LicenseRegistry::SpdxExpression> &inputLicenses) const;
0061 
0062 private:
0063     QVector<LicenseRegistry::SpdxExpression> detectLicensesRegexpParser(const QString &fileContent) const;
0064     QVector<LicenseRegistry::SpdxExpression> detectLicensesSkipParser(const QString &fileContent) const;
0065 
0066     LicenseRegistry m_registry;
0067     LicenseParser m_parserType {LicenseParser::REGEXP_PARSER};
0068     static const QStringList s_supportedExtensions;
0069 };
0070 Q_DECLARE_OPERATORS_FOR_FLAGS(DirectoryParser::ConvertOptions)
0071 
0072 #endif // DIRECTORYPARSER_H