37#include "musx/dom/Document.h"
38#include "musx/factory/HeaderFactory.h"
39#include "musx/factory/PoolFactory.h"
40#include "musx/xml/XmlInterface.h"
51 using DocumentPtr = std::shared_ptr<Document>;
55 template <
typename Container>
57 (
sizeof(std::remove_cv_t<typename Container::value_type>) == 1)
58 && !std::is_same_v<std::remove_cv_t<typename Container::value_type>,
bool>
86 std::vector<char> notationMetadata,
90 m_notationMetadata(std::move(notationMetadata)),
91 m_embeddedGraphics(parseEmbeddedGraphicFiles(std::move(embeddedGraphicFiles))),
92 m_sourcePath(std::move(inputFilepath))
107 const std::optional<std::filesystem::path>&
getSourcePath()
const {
return m_sourcePath; }
117 for (
auto& file : embeddedGraphicFiles) {
118 const auto parsed = parseEmbeddedGraphicFilename(file.filename);
122 auto& entry = embeddedGraphics[parsed->first];
123 entry.extension = std::move(parsed->second);
124 entry.bytes = std::move(file.bytes);
126 return embeddedGraphics;
129 static std::optional<std::pair<Cmper, std::string>> parseEmbeddedGraphicFilename(
const std::string& filename)
131 const auto dotPos = filename.find(
'.');
132 if (dotPos == std::string::npos || dotPos == 0 || dotPos + 1 >= filename.size()) {
136 const std::string_view cmperText(filename.data(), dotPos);
137 if (cmperText.empty() || !std::all_of(cmperText.begin(), cmperText.end(),
138 [](
unsigned char ch) { return std::isdigit(ch) != 0; })) {
142 unsigned long long cmperValue = 0;
144 cmperValue = std::stoull(std::string(cmperText));
148 if (cmperValue >
static_cast<unsigned long long>((std::numeric_limits<Cmper>::max)())) {
152 return std::make_pair(
static_cast<Cmper>(cmperValue), filename.substr(dotPos + 1));
155 std::vector<char> m_notationMetadata;
157 std::optional<std::filesystem::path> m_sourcePath;
168 template <
typename XmlDocumentType>
170 static DocumentPtr
create(
const char* data,
size_t size)
176 template <
typename XmlDocumentType,
typename Container,
typename = IsCharContainer<Container>>
178 static DocumentPtr
create(
const Container& xmlBuffer)
180 return create<XmlDocumentType>(asCharData(xmlBuffer), xmlBuffer.size(),
CreateOptions{});
192 template <
typename XmlDocumentType>
196 static_assert(std::is_base_of<musx::xml::IXmlDocument, XmlDocumentType>::value,
197 "XmlReaderType must derive from IXmlDocument.");
199 std::unique_ptr<musx::xml::IXmlDocument> xmlDocument = std::make_unique<XmlDocumentType>();
200 xmlDocument->loadFromBuffer(data, size);
202 auto rootElement = xmlDocument->getRootElement();
203 if (!rootElement || rootElement->getTagName() !=
"finale") {
204 throw std::invalid_argument(
"Missing <finale> element.");
208 document->m_self = document;
209 document->m_partVoicingPolicy = createOptions.partVoicingPolicy;
210 document->m_scoreDurationSeconds = parseScoreDurationSeconds<XmlDocumentType>(createOptions.getNotationMetadata());
211 document->m_embeddedGraphics = createOptions.takeEmbeddedGraphics();
212 document->m_sourcePath = createOptions.getSourcePath();
215 for (
auto element = rootElement->getFirstChildElement(); element; element = element->getNextSibling()) {
216 if (element->getTagName() ==
"header") {
218 }
else if (element->getTagName() ==
"options") {
220 }
else if (element->getTagName() ==
"others") {
222 }
else if (element->getTagName() ==
"details") {
224 }
else if (element->getTagName() ==
"entries") {
226 }
else if (element->getTagName() ==
"texts") {
230 if (!document->getHeader()) document->getHeader() = std::make_shared<musx::dom::Header>();
231 if (!document->getOptions()) document->getOptions() = std::make_shared<musx::dom::OptionsPool>(document);
232 if (!document->getOthers()) document->getOthers() = std::make_shared<musx::dom::OthersPool>(document);
233 if (!document->getDetails()) document->getDetails() = std::make_shared<musx::dom::DetailsPool>(document);
234 if (!document->getEntries()) document->getEntries() = std::make_shared<musx::dom::EntryPool>(document);
235 if (!document->getTexts()) document->getTexts() = std::make_shared<musx::dom::TextsPool>(document);
237#ifdef MUSX_DISPLAY_NODE_NAMES
241 document->m_instruments = document->createInstrumentMap(
SCORE_PARTID);
243 document->m_maxBlankPages = 0;
244 auto linkedParts = document->getOthers()->getArray<PartDefinition>(
SCORE_PARTID);
245 for (
const auto& part : linkedParts) {
246 auto mutablePart =
const_cast<PartDefinition*
>(part.get());
247 mutablePart->numberOfLeadingBlankPages = 0;
248 auto pages = document->getOthers()->getArray<Page>(part->getCmper());
249 mutablePart->numberOfPages = int(pages.size());
250 for (
const auto& page : pages) {
251 if (!page->isBlank()) {
254 mutablePart->numberOfLeadingBlankPages++;
256 if (mutablePart->numberOfLeadingBlankPages > document->m_maxBlankPages) {
257 document->m_maxBlankPages = part->numberOfLeadingBlankPages;
265 template <
typename XmlDocumentType,
typename Container,
typename = IsCharContainer<Container>>
269 return create<XmlDocumentType>(asCharData(xmlBuffer), xmlBuffer.size(), std::move(createOptions));
273 template <
typename Container>
274 static const char* asCharData(
const Container& buffer)
276 return reinterpret_cast<const char*
>(buffer.data());
279 template <
typename XmlDocumentType>
280 static std::optional<double> parseScoreDurationSeconds(
const std::vector<char>& notationMetadata)
282 if (notationMetadata.empty()) {
286 std::unique_ptr<musx::xml::IXmlDocument> xmlDocument = std::make_unique<XmlDocumentType>();
288 xmlDocument->loadFromBuffer(notationMetadata.data(), notationMetadata.size());
293 auto rootElement = xmlDocument->getRootElement();
294 if (!rootElement || rootElement->getTagName() !=
"metadata") {
297 if (
auto fileInfo = rootElement->getFirstChildElement(
"fileInfo")) {
298 auto creatorString = fileInfo->getFirstChildElement(
"creatorString");
299 if (!creatorString || creatorString->getTextTrimmed().rfind(
"Finale", 0) != 0) {
302 if (
auto scoreDuration = fileInfo->getFirstChildElement(
"scoreDuration")) {
304 return scoreDuration->getTextAs<
double>();
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:107
static std::shared_ptr< PoolType > create(const XmlElementPtr &element, const dom::DocumentPtr &document, ElementLinker &elementLinker)
Creates a OthersPool object from an XML element.
Definition PoolFactory.h:68
Factory class for creating Document objects from XML.
Definition DocumentFactory.h:49
std::enable_if_t<(sizeof(std::remove_cv_t< typename Container::value_type >)==1) &&!std::is_same_v< std::remove_cv_t< typename Container::value_type >, bool > > IsCharContainer
SFINAE helper for containers whose element type is 1 byte (excluding bool).
Definition DocumentFactory.h:59
static DocumentPtr create(const Container &xmlBuffer)
Creates a Document object from a char container (e.g. std::string, std::vector<char>).
Definition DocumentFactory.h:178
static DocumentPtr create(const char *data, size_t size, CreateOptions &&createOptions)
Creates a Document object from an XML buffer.
Definition DocumentFactory.h:194
static DocumentPtr create(const Container &xmlBuffer, CreateOptions &&createOptions)
Creates a Document object from a char container and explicit create options.
Definition DocumentFactory.h:267
static DocumentPtr create(const char *data, size_t size)
Creates a Document object from an XML buffer.
Definition DocumentFactory.h:170
A utility class for managing deferred relationships between elements during document construction.
Definition FactoryBase.h:72
void resolveAll(const dom::DocumentPtr &document)
Resolves all deferred relationships.
Definition FactoryBase.h:110
static std::shared_ptr< PoolType > create(const XmlElementPtr &element, const dom::DocumentPtr &document, ElementLinker &elementLinker)
Creates a OthersPool object from an XML element.
Definition PoolFactory.h:68
Factory base class.
Definition FactoryBase.h:134
static std::shared_ptr< PoolType > create(const XmlElementPtr &element, const dom::DocumentPtr &document, ElementLinker &elementLinker)
Creates a OthersPool object from an XML element.
Definition PoolFactory.h:68
static std::shared_ptr< PoolType > create(const XmlElementPtr &element, const dom::DocumentPtr &document, ElementLinker &elementLinker)
Creates a OthersPool object from an XML element.
Definition PoolFactory.h:68
static std::shared_ptr< PoolType > create(const XmlElementPtr &element, const dom::DocumentPtr &document, ElementLinker &elementLinker)
Creates a OthersPool object from an XML element.
Definition PoolFactory.h:68
@ Verbose
Informational messages that should only displayed when verbose logging is requested.
static void log(LogLevel level, const std::string &message)
Logs a message with a specific severity level.
Definition Logger.h:87
constexpr Cmper SCORE_PARTID
The part id of the score.
Definition Fundamentals.h:79
uint16_t Cmper
Enigma "comperator" key type.
Definition Fundamentals.h:55
PartVoicingPolicy
Controls whether Finale-style part voicing is applied when iterating entries via musx::dom::details::...
Definition Document.h:90
std::vector< uint8_t > EmbeddedGraphicBlob
Raw bytes for one embedded graphic payload from a musx archive.
Definition Document.h:57
std::unordered_map< Cmper, EmbeddedGraphicData > EmbeddedGraphicsMap
Definition Document.h:66
object model for musx file (enigmaxml)
Definition BaseClasses.h:36
Raw embedded graphic file payload from a musx archive.
Definition DocumentFactory.h:66
std::string filename
Archive filename in cmper.ext form.
Definition DocumentFactory.h:67
dom::EmbeddedGraphicBlob bytes
Raw file bytes.
Definition DocumentFactory.h:68
Optional arguments for document creation.
Definition DocumentFactory.h:63
CreateOptions(std::filesystem::path inputFilepath, std::vector< char > notationMetadata, EmbeddedGraphicFiles embeddedGraphicFiles, dom::PartVoicingPolicy policy=dom::PartVoicingPolicy::Ignore)
Construct options with explicit inputs.
Definition DocumentFactory.h:85
const dom::EmbeddedGraphicsMap & getEmbeddedGraphics() const
Embedded graphics keyed by graphic cmper.
Definition DocumentFactory.h:103
dom::EmbeddedGraphicsMap takeEmbeddedGraphics()
Move out embedded graphics.
Definition DocumentFactory.h:111
std::vector< EmbeddedGraphicFile > EmbeddedGraphicFiles
Embedded graphic files provided by caller.
Definition DocumentFactory.h:71
CreateOptions()=default
Default constructor.
const std::optional< std::filesystem::path > & getSourcePath() const
Optional path to the musx (or EnigmaXML) file being loaded.
Definition DocumentFactory.h:107
dom::PartVoicingPolicy partVoicingPolicy
Part voicing behavior for this document.
Definition DocumentFactory.h:95
const std::vector< char > & getNotationMetadata() const
Optional NotationMetadata.xml content.
Definition DocumentFactory.h:99
CreateOptions(dom::PartVoicingPolicy policy)
Construct options with a part-voicing policy.
Definition DocumentFactory.h:78