Mantid
Loading...
Searching...
No Matches
Citation.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <nexus/NeXusFile.hpp>
10#include <stdexcept>
11
12#include <stdexcept>
13
14namespace Mantid::API {
15
16/*
17 There are some strict parameters that the function arguments must adhere to.
18
19 - description is always optional (this isn't needed for citation, but gives
20 insight as to why this citation is relevant)
21 - if bibtex is provided endnote must also be provided, and vice-versa (BibTex
22 and Endnote contain essentially the same information, they can both be
23 created if one can be. BibTex and Endnote do not imply a DOI is minted)
24 - if doi is provided, url, bibtex and endnote must all be provided (BibTex
25 and Endnote can be generated from DOIs)
26 - if none of doi, bibtex or endnote are provided, url must be provided
27 (there must be something there, even if this isn't citable a URL is better
28 than nothing)
29 */
30Citation::Citation(const std::string &doi, const std::string &bibtex, const std::string &endnote,
31 const std::string &url, const std::string &description)
32 : m_doi(doi), m_bibtex(bibtex), m_endnote(endnote), m_url(url), m_description(description) {
33 if (doi.empty() && bibtex.empty() && endnote.empty() && url.empty() && description.empty())
34 throw std::invalid_argument("No arguements were given!");
35
36 // This is an initial implementation that expects it but it should be possible
37 // to generate one from the other
38 if ((!bibtex.empty() && endnote.empty()) || (bibtex.empty() && !endnote.empty()))
39 throw std::invalid_argument("If bibtex is provided, endnote must also be provided and vice-versa");
40
41 if (!doi.empty() && (bibtex.empty() || endnote.empty() || url.empty()))
42 throw std::invalid_argument("If doi is provided then url, bibtex and endnote must be");
43
44 if (doi.empty() && bibtex.empty() && endnote.empty())
45 if (url.empty())
46 throw std::invalid_argument("If none of doi, bibtex, or endnote is provided, then url must be");
47}
48
49Citation::Citation(::NeXus::File *file, const std::string &group) { loadNexus(file, group); }
50
51bool Citation::operator==(const Citation &rhs) const {
52 return m_bibtex == rhs.m_bibtex && m_description == rhs.m_description && m_doi == rhs.m_doi &&
53 m_endnote == rhs.m_endnote && m_url == rhs.m_url;
54}
55
56const std::string &Citation::description() const { return m_description; }
57const std::string &Citation::url() const { return m_url; }
58const std::string &Citation::doi() const { return m_doi; }
59const std::string &Citation::bibtex() const { return m_bibtex; }
60const std::string &Citation::endnote() const { return m_endnote; }
61
62void Citation::loadNexus(::NeXus::File *file, const std::string &group) {
63 file->openGroup(group, "NXCite");
64 file->readData("url", m_url);
65 file->readData("description", m_description);
66 file->readData("doi", m_doi);
67 file->readData("endnote", m_endnote);
68 file->readData("bibtex", m_bibtex);
69 file->closeGroup();
70}
71
72void Citation::saveNexus(::NeXus::File *file, const std::string &group) {
73 file->makeGroup(group, "NXCite", true);
74 file->writeData("url", m_url);
75 file->writeData("description", m_description);
76 file->writeData("doi", m_doi);
77 file->writeData("endnote", m_endnote);
78 file->writeData("bibtex", m_bibtex);
79 file->closeGroup();
80}
81
82} // namespace Mantid::API
const std::vector< double > & rhs
std::string m_description
Definition: Citation.h:69
const std::string & endnote() const
Definition: Citation.cpp:60
const std::string & bibtex() const
Definition: Citation.cpp:59
void loadNexus(::NeXus::File *file, const std::string &group)
Definition: Citation.cpp:62
void saveNexus(::NeXus::File *file, const std::string &group)
Definition: Citation.cpp:72
Citation(::NeXus::File *file, const std::string &group)
This constructor will load the data from the given file using the given group as the NeXus Group in w...
Definition: Citation.cpp:49
const std::string & doi() const
Definition: Citation.cpp:58
const std::string & url() const
Definition: Citation.cpp:57
std::string m_doi
Definition: Citation.h:65
std::string m_bibtex
Definition: Citation.h:66
const std::string & description() const
Definition: Citation.cpp:56
bool operator==(const Citation &rhs) const
Definition: Citation.cpp:51
std::string m_endnote
Definition: Citation.h:67
std::string m_url
Definition: Citation.h:68