1 // -*- coding: utf-8 -*-
2 //
3 // AddonMetadataParser.hxx --- Parser for FlightGear add-on metadata files
4 // Copyright (C) 2018  Florent Rougon
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program 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 General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20 #ifndef FG_ADDON_METADATA_PARSER_HXX
21 #define FG_ADDON_METADATA_PARSER_HXX
22 
23 #include <string>
24 #include <tuple>
25 #include <vector>
26 
27 #include <simgear/misc/sg_path.hxx>
28 
29 #include "addon_fwd.hxx"
30 #include "Addon.hxx"
31 #include "AddonVersion.hxx"
32 #include "contacts.hxx"
33 
34 class SGPropertyNode;
35 
36 namespace flightgear
37 {
38 
39 namespace addons
40 {
41 
42 class Addon::Metadata
43 {
44 public:
45   // Comments about these fields can be found in Addon.hxx
46   std::string id;
47   std::string name;
48   AddonVersion version;
49 
50   std::vector<AuthorRef> authors;
51   std::vector<MaintainerRef> maintainers;
52 
53   std::string shortDescription;
54   std::string longDescription;
55 
56   std::string licenseDesignation;
57   SGPath licenseFile;
58   std::string licenseUrl;
59 
60   std::vector<std::string> tags;
61 
62   std::string minFGVersionRequired;
63   std::string maxFGVersionRequired;
64 
65   std::string homePage;
66   std::string downloadUrl;
67   std::string supportUrl;
68   std::string codeRepositoryUrl;
69 };
70 
71 class Addon::MetadataParser
72 {
73 public:
74   // “Compute” a path to the metadata file from the add-on base path
75   static SGPath getMetadataFile(const SGPath& addonPath);
76 
77   // Parse the add-on metadata file inside 'addonPath' (as defined by
78   // getMetadataFile()) and return the corresponding Addon::Metadata instance.
79   static Addon::Metadata parseMetadataFile(const SGPath& addonPath);
80 
81 private:
82   static std::tuple<string, SGPath, string>
83   parseLicenseNode(const SGPath& addonPath, SGPropertyNode* addonNode);
84 
85   // Parse an addon-metadata.xml node such as <authors> or <maintainers>.
86   // Return the corresponding vector<AuthorRef> or vector<MaintainerRef>. If
87   // the 'mainNode' argument is nullptr, return an empty vector.
88   template <class T>
89   static std::vector<typename contact_traits<T>::strong_ref>
90   parseContactsNode(const SGPath& metadataFile, SGPropertyNode* mainNode);
91 };
92 
93 } // of namespace addons
94 
95 } // of namespace flightgear
96 
97 #endif  // of FG_ADDON_METADATA_PARSER_HXX
98