MUSX Document Model
Loading...
Searching...
No Matches
DocumentFactory.h
1/*
2 * Copyright (C) 2025, Robert Patterson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22#pragma once
23
24#include <memory>
25
26#include "musx/dom/Document.h"
27#include "musx/factory/HeaderFactory.h"
28#include "musx/factory/PoolFactory.h"
29#include "musx/xml/XmlInterface.h"
30
31namespace musx {
32namespace factory {
33
38{
40 using DocumentPtr = std::shared_ptr<Document>;
41
42public:
52 template <typename XmlDocumentType>
53 [[nodiscard]]
54 static DocumentPtr create(const char* data, size_t size, dom::PartVoicingPolicy partVoicingPolicy = dom::PartVoicingPolicy::Ignore)
55 {
56 static_assert(std::is_base_of<musx::xml::IXmlDocument, XmlDocumentType>::value,
57 "XmlReaderType must derive from IXmlDocument.");
58
59 std::unique_ptr<musx::xml::IXmlDocument> xmlDocument = std::make_unique<XmlDocumentType>();
60 xmlDocument->loadFromBuffer(data, size);
61
62 auto rootElement = xmlDocument->getRootElement();
63 if (!rootElement || rootElement->getTagName() != "finale") {
64 throw std::invalid_argument("Missing <finale> element.");
65 }
66
67 DocumentPtr document(new Document);
68 document->m_self = document;
69 document->m_partVoicingPolicy = partVoicingPolicy;
70
71 ElementLinker elementLinker;
72 for (auto element = rootElement->getFirstChildElement(); element; element = element->getNextSibling()) {
73 if (element->getTagName() == "header") {
74 document->getHeader() = musx::factory::HeaderFactory::create(element);
75 } else if (element->getTagName() == "options") {
76 document->getOptions() = musx::factory::OptionsFactory::create(element, document, elementLinker);
77 } else if (element->getTagName() == "others") {
78 document->getOthers() = musx::factory::OthersFactory::create(element, document, elementLinker);
79 } else if (element->getTagName() == "details") {
80 document->getDetails() = musx::factory::DetailsFactory::create(element, document, elementLinker);
81 } else if (element->getTagName() == "entries") {
82 document->getEntries() = musx::factory::EntryFactory::create(element, document, elementLinker);
83 } else if (element->getTagName() == "texts") {
84 document->getTexts() = musx::factory::TextsFactory::create(element, document, elementLinker);
85 }
86 }
87 if (!document->getHeader()) document->getHeader() = std::make_shared<musx::dom::Header>();
88 if (!document->getOptions()) document->getOptions() = std::make_shared<musx::dom::OptionsPool>(document);
89 if (!document->getOthers()) document->getOthers() = std::make_shared<musx::dom::OthersPool>(document);
90 if (!document->getDetails()) document->getDetails() = std::make_shared<musx::dom::DetailsPool>(document);
91 if (!document->getEntries()) document->getEntries() = std::make_shared<musx::dom::EntryPool>(document);
92 if (!document->getTexts()) document->getTexts() = std::make_shared<musx::dom::TextsPool>(document);
93
94#ifdef MUSX_DISPLAY_NODE_NAMES
96#endif
97 elementLinker.resolveAll(document);
98 document->m_instruments = document->createInstrumentMap(SCORE_PARTID);
99
100 document->m_maxBlankPages = 0;
101 auto linkedParts = document->getOthers()->getArray<PartDefinition>(SCORE_PARTID);
102 for (const auto& part : linkedParts) {
103 auto mutablePart = const_cast<PartDefinition*>(part.get());
104 mutablePart->numberOfLeadingBlankPages = 0;
105 auto pages = document->getOthers()->getArray<Page>(part->getCmper());
106 mutablePart->numberOfPages = int(pages.size());
107 for (const auto& page : pages) {
108 if (!page->isBlank()) {
109 break;
110 }
111 mutablePart->numberOfLeadingBlankPages++;
112 }
113 if (mutablePart->numberOfLeadingBlankPages > document->m_maxBlankPages) {
114 document->m_maxBlankPages = part->numberOfLeadingBlankPages;
115 }
116 }
117
118 return document;
119 }
120
129 template <typename XmlDocumentType>
130 [[nodiscard]]
131 static DocumentPtr create(const std::vector<char>& xmlBuffer, dom::PartVoicingPolicy partVoicingPolicy = dom::PartVoicingPolicy::Ignore)
132 {
133 return create<XmlDocumentType>(xmlBuffer.data(), xmlBuffer.size(), partVoicingPolicy);
134 }
135};
136
137} // namespace factory
138} // namespace musx
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:92
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:38
static DocumentPtr create(const char *data, size_t size, dom::PartVoicingPolicy partVoicingPolicy=dom::PartVoicingPolicy::Ignore)
Creates a Document object from an XML buffer.
Definition DocumentFactory.h:54
static DocumentPtr create(const std::vector< char > &xmlBuffer, dom::PartVoicingPolicy partVoicingPolicy=dom::PartVoicingPolicy::Ignore)
Creates a Document object from an XML buffer.
Definition DocumentFactory.h:131
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 musx::dom::header::HeaderPtr create(const XmlElementPtr &element)
Creates a Header object from an XML element.
Definition HeaderFactory.h:47
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
PartVoicingPolicy
Controls whether Finale-style part voicing is applied when iterating entries via musx::dom::details::...
Definition Document.h:75
object model for musx file (enigmaxml)
Definition BaseClasses.h:36