source: appstream-generator/src/asgen/handlers/metainfoparser.d @ 4841

Last change on this file since 4841 was 4841, checked in by Juanma, 2 years ago

Initial release

File size: 2.2 KB
Line 
1/*
2 * Copyright (C) 2016 Matthias Klumpp <matthias@tenstral.net>
3 *
4 * Licensed under the GNU Lesser General Public License Version 3
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the license, or
9 * (at your option) any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20module asgen.handlers.metainfoparser;
21
22import std.path : baseName;
23import std.uni : toLower;
24import std.string : format;
25import std.stdio;
26import appstream.Metadata;
27import appstream.Component;
28
29import asgen.result;
30import asgen.utils;
31
32
33private bool isMetainfoLicense (string license) pure
34{
35    import asgen.bindings.appstream_utils;
36    import std.string : toStringz;
37    return as_license_is_metadata_license (license.toStringz);
38}
39
40Component parseMetaInfoFile (Metadata mdata, GeneratorResult res, const string data)
41{
42    try {
43        mdata.parse (data, FormatKind.XML);
44    } catch (Exception e) {
45        res.addHint ("general", "metainfo-parsing-error", e.msg);
46        return null;
47    }
48
49    auto cpt = mdata.getComponent ();
50    if (cpt is null)
51        return null;
52    res.addComponent (cpt);
53
54    // check if we can actually legally use this metadata
55    if (!isMetainfoLicense (cpt.getMetadataLicense())) {
56        res.addHint (cpt, "metainfo-license-invalid", ["license": cpt.getMetadataLicense()]);
57        return null;
58    }
59
60    // quit immediately if we have an unknown component type
61    if (cpt.getKind () == ComponentKind.UNKNOWN) {
62        res.addHint (cpt, "metainfo-unknown-type");
63        return null;
64    }
65
66    return cpt;
67}
68
69Component parseMetaInfoFile (GeneratorResult res, const string data)
70{
71    auto mdata = new Metadata ();
72    mdata.setLocale ("ALL");
73    mdata.setFormatStyle (FormatStyle.METAINFO);
74
75    return parseMetaInfoFile (mdata, res, data);
76}
Note: See TracBrowser for help on using the repository browser.