libxcks  0.1.0.1
xcksfilereader.cpp
Go to the documentation of this file.
1 /*
2  * libxcks
3  * Copyright (C) 2022 Julien Couot
4  *
5  * This program is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 //---------------------------------------------------------------------------
25 #include <cassert>
26 #include <boost/locale.hpp>
27 
28 #include "xcksfilereader.hpp"
29 #include "xcksfilereader_1.hpp"
30 #include "libxcks/version.hpp"
31 #include "libxcks/defs.hpp"
32 #include "strutil.hpp"
33 //---------------------------------------------------------------------------
34 
36 using namespace std;
37 
39 using namespace boost::locale;
40 //---------------------------------------------------------------------------
41 
42 
43 namespace libxcks
44 {
52 XCKSFileReader::XCKSFileReader(XCKSReaderHandler& xcksReaderHandler,
53  const std::filesystem::path& baseDir) : XMLParser(),
54  readerHandler(xcksReaderHandler),
55  baseDirectory(baseDir)
56 {
57  assert(baseDir.is_absolute());
58 }
59 //---------------------------------------------------------------------------
60 
61 
68 void XCKSFileReader::startElement(const string& name, const XMLParserAttributes& atts) noexcept(false)
69 {
70  if (getDepth() == 0)
71  {
72  if (name == "checksumsFile")
73  {
74  string xcksVer;
75  if (atts.getValue("version", xcksVer))
76  {
77  if (xcksVer == "1.0.0")
78  {
79  XCKS1FileHandler* handler = new XCKS1FileHandler(readerHandler, baseDirectory);
80  setVersionHandler(handler);
81  readerHandler.onChecksumsFile(xcksVer);
82  }
83  else
84  throw XMLParserException((format(translate("The checksums' file version cannot be read by this release of {1}.").str(libxcks_domain)) % getLibraryName()).str());
85  }
86  else
87  throw XMLParserException(translate("The checksums' file hasn't version information.").str(libxcks_domain));
88  }
89  }
90  else
91  {
92  getVersionHandler()->startElement(name, atts);
93  }
94 }
95 //---------------------------------------------------------------------------
96 
97 
103 void XCKSFileReader::endElement(const string& name) noexcept(false)
104 {
105  if (getDepth() == 0)
106  {
107  if (name != "checksumsFile")
108  throw XMLParserException(translate("Mismatching ending tag.").str(libxcks_domain));
109  }
110  else
111  {
112  getVersionHandler()->endElement(name);
113  }
114 }
115 //---------------------------------------------------------------------------
116 
117 
123 void XCKSFileReader::characters(const string& chars) noexcept(false)
124 {
125  if (getDepth() == 0)
126  {
127  string s = trim_copy(chars);
128  if (!s.empty())
129  throw XMLParserException(translate("The file cannot contains characters between tags.").str(libxcks_domain));
130  }
131  else
132  getVersionHandler()->characters(chars);
133 }
134 //---------------------------------------------------------------------------
135 
136 
145 void XCKSFileReader::fatalError(XML_Error errorCode, const string& errorMessage, int line, int column) noexcept
146 {
147  if (getVersionHandler() != nullptr)
148  getVersionHandler()->fatalError(errorCode, errorMessage, line, column);
149  else
150  readerHandler.onFatalError(errorMessage, line, column);
151 }
152 //---------------------------------------------------------------------------
153 
154 
165 {
166  return versionHandler.get();
167 }
168 //---------------------------------------------------------------------------
169 
170 
177 {
178  this->versionHandler = shared_ptr<XMLParserDefaultHandler>(handler);
179 }
180 //---------------------------------------------------------------------------
181 } // namespace libxcks
182 //---------------------------------------------------------------------------
XCKS file version 1 handler.
void characters(const std::string &chars) noexcept(false) override
Receives notification of character data.
void endElement(const std::string &name) noexcept(false) override
Receives notification of the end of an element.
void setVersionHandler(XMLParserDefaultHandler *handler)
Sets the handler to parse the sums' file.
XMLParserDefaultHandler * getVersionHandler() const
Gets the handler to parse the sums' file.
void startElement(const std::string &name, const XMLParserAttributes &atts) noexcept(false) override
Receives notification of the beginning of an element.
void fatalError(XML_Error errorCode, const std::string &errorMessage, int line, int column) noexcept override
Receive notification of a non-recoverable error.
std::shared_ptr< XMLParserDefaultHandler > versionHandler
The handler for the version of the file.
Handler for reading XCKS files.
Definition: handlers.hpp:50
Manages elements' attributes.
Definition: xmlparser.hpp:84
Default XML parser handler.
Definition: xmlparser.hpp:198
An exception class for the XML parser.
Definition: xmlparser.hpp:42
A very simple XML parser.
Definition: xmlparser.hpp:138
Common definitions for libxcks.
constexpr const char * libxcks_domain
Domain for translations (i18n).
Definition: defs.hpp:47
std::string trim_copy(std::string s)
Trim from both ends (copying).
Definition: strutil.cpp:123
String utilities.
Version of the library.
LIBXCKS_SO_EXPORT const std::string getLibraryName()
Gets the name of the library.
Definition: version.cpp:49
Classes that read a XCKS file.
Classes that read a XCKS file version 1.