XmlReader.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-10-10
  6. * Changed: 2023-02-08
  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. ls::std::io::XmlReader::XmlReader(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document, const ::std::string &_absolutePath) : ls::std::core::Class("XmlReader"), xmlFile(ls::std::io::File{""})
  15. {
  16. this->_assignDocument(_document);
  17. this->_assignFile(ls::std::io::File{_absolutePath});
  18. }
  19. ls::std::io::XmlReader::~XmlReader() = default;
  20. ls::std::core::type::byte_field ls::std::io::XmlReader::read()
  21. {
  22. ls::std::core::type::byte_field data = ls::std::io::FileReader{this->xmlFile}.read();
  23. ls::std::io::XmlParser{this->document}.parse(data);
  24. return data;
  25. }
  26. ::std::shared_ptr<ls::std::io::XmlDocument> ls::std::io::XmlReader::getDocument()
  27. {
  28. return this->document;
  29. }
  30. void ls::std::io::XmlReader::setDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document)
  31. {
  32. this->_assignDocument(_document);
  33. }
  34. void ls::std::io::XmlReader::setFile(const ls::std::io::File &_xmlFile)
  35. {
  36. this->_assignFile(_xmlFile);
  37. }
  38. void ls::std::io::XmlReader::_assignDocument(const ::std::shared_ptr<ls::std::io::XmlDocument> &_document)
  39. {
  40. ls::std::core::NullPointerArgumentEvaluator{_document, "xml document reference is null!"}.evaluate();
  41. this->document = _document;
  42. }
  43. void ls::std::io::XmlReader::_assignFile(ls::std::io::File _xmlFile)
  44. {
  45. if (!_xmlFile.exists())
  46. {
  47. throw ls::std::core::IllegalArgumentException{"file does not exist: " + _xmlFile.getAbsoluteFilePath()};
  48. }
  49. this->xmlFile = _xmlFile;
  50. }