XmlReader.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-10-10
  6. * Changed: 2023-05-17
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  11. #include <ls-std/io/FileReader.hpp>
  12. #include <ls-std/io/xml/XmlParser.hpp>
  13. #include <ls-std/io/xml/XmlReader.hpp>
  14. using ls::std::core::Class;
  15. using ls::std::core::IllegalArgumentException;
  16. using ls::std::core::NullPointerArgumentEvaluator;
  17. using ls::std::core::type::byte_field;
  18. using ls::std::io::File;
  19. using ls::std::io::FileReader;
  20. using ls::std::io::XmlDocument;
  21. using ls::std::io::XmlParser;
  22. using ls::std::io::XmlReader;
  23. using std::shared_ptr;
  24. using std::string;
  25. XmlReader::XmlReader(const shared_ptr<XmlDocument> &_document, const string &_absolutePath) : Class("XmlReader")
  26. {
  27. this->_assignDocument(_document);
  28. this->_assignFile(File{_absolutePath});
  29. }
  30. XmlReader::~XmlReader() noexcept = default;
  31. byte_field XmlReader::read()
  32. {
  33. byte_field data = FileReader{this->xmlFile}.read();
  34. XmlParser{this->document}.parse(data);
  35. return data;
  36. }
  37. shared_ptr<XmlDocument> XmlReader::getDocument() const
  38. {
  39. return this->document;
  40. }
  41. void XmlReader::setDocument(const shared_ptr<XmlDocument> &_document)
  42. {
  43. this->_assignDocument(_document);
  44. }
  45. void XmlReader::setFile(const File &_xmlFile)
  46. {
  47. this->_assignFile(_xmlFile);
  48. }
  49. void XmlReader::_assignDocument(const shared_ptr<XmlDocument> &_document)
  50. {
  51. NullPointerArgumentEvaluator{_document, "xml document reference is null!"}.evaluate();
  52. this->document = _document;
  53. }
  54. void XmlReader::_assignFile(const File &_xmlFile)
  55. {
  56. if (!_xmlFile.exists())
  57. {
  58. throw IllegalArgumentException{"file does not exist: " + _xmlFile.getAbsoluteFilePath()};
  59. }
  60. this->xmlFile = _xmlFile;
  61. }