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:
51 template <typename XmlDocumentType>
52 static DocumentPtr create(const char * data, size_t size)
53 {
54 static_assert(std::is_base_of<musx::xml::IXmlDocument, XmlDocumentType>::value,
55 "XmlReaderType must derive from IXmlDocument.");
56
57 std::unique_ptr<musx::xml::IXmlDocument> xmlDocument = std::make_unique<XmlDocumentType>();
58 xmlDocument->loadFromBuffer(data, size);
59
60 auto rootElement = xmlDocument->getRootElement();
61 if (!rootElement || rootElement->getTagName() != "finale") {
62 throw std::invalid_argument("Missing <finale> element.");
63 }
64
65 DocumentPtr document(new Document);
66
67 ElementLinker elementLinker;
68 for (auto element = rootElement->getFirstChildElement(); element; element = element->getNextSibling()) {
69 if (element->getTagName() == "header") {
70 document->getHeader() = musx::factory::HeaderFactory::create(element);
71 } else if (element->getTagName() == "options") {
72 document->getOptions() = musx::factory::OptionsFactory::create(element, document, elementLinker);
73 } else if (element->getTagName() == "others") {
74 document->getOthers() = musx::factory::OthersFactory::create(element, document, elementLinker);
75 } else if (element->getTagName() == "details") {
76 document->getDetails() = musx::factory::DetailsFactory::create(element, document, elementLinker);
77 } else if (element->getTagName() == "entries") {
78 document->getEntries() = musx::factory::EntryFactory::create(element, document, elementLinker);
79 } else if (element->getTagName() == "texts") {
80 document->getTexts() = musx::factory::TextsFactory::create(element, document, elementLinker);
81 }
82 }
83 if (!document->getHeader()) document->getHeader() = std::make_shared<musx::dom::Header>();
84 if (!document->getOptions()) document->getOptions() = std::make_shared<musx::dom::OptionsPool>(document);
85 if (!document->getOthers()) document->getOthers() = std::make_shared<musx::dom::OthersPool>(document);
86 if (!document->getDetails()) document->getDetails() = std::make_shared<musx::dom::DetailsPool>(document);
87 if (!document->getEntries()) document->getEntries() = std::make_shared<musx::dom::EntryPool>(document);
88 if (!document->getTexts()) document->getTexts() = std::make_shared<musx::dom::TextsPool>(document);
89
90#ifdef MUSX_DISPLAY_NODE_NAMES
92#endif
93 elementLinker.resolveAll(document);
94 document->m_instruments = document->createInstrumentMap(SCORE_PARTID);
95
96 document->m_maxBlankPages = 0;
97 auto linkedParts = document->getOthers()->getArray<PartDefinition>(SCORE_PARTID);
98 for (const auto& part : linkedParts) {
99 auto mutablePart = const_cast<PartDefinition*>(part.get());
100 mutablePart->numberOfLeadingBlankPages = 0;
101 auto pages = document->getOthers()->getArray<Page>(part->getCmper());
102 mutablePart->numberOfPages = int(pages.size());
103 for (const auto& page : pages) {
104 if (!page->isBlank()) {
105 break;
106 }
107 mutablePart->numberOfLeadingBlankPages++;
108 }
109 if (mutablePart->numberOfLeadingBlankPages > document->m_maxBlankPages) {
110 document->m_maxBlankPages = part->numberOfLeadingBlankPages;
111 }
112 }
113
114 return document;
115 }
116
124 template <typename XmlDocumentType>
125 static DocumentPtr create(const std::vector<char>& xmlBuffer)
126 {
127 return create<XmlDocumentType>(xmlBuffer.data(), xmlBuffer.size());
128 }
129};
130
131} // namespace factory
132} // namespace musx
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:67
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)
Creates a Document object from an XML buffer.
Definition DocumentFactory.h:52
static DocumentPtr create(const std::vector< char > &xmlBuffer)
Creates a Document object from an XML buffer.
Definition DocumentFactory.h:125
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
object model for musx file (enigmaxml)
Definition BaseClasses.h:36