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 <algorithm>
25#include <cstddef>
26#include <cctype>
27#include <filesystem>
28#include <limits>
29#include <memory>
30#include <optional>
31#include <string>
32#include <string_view>
33#include <type_traits>
34#include <utility>
35#include <vector>
36
37#include "musx/dom/Document.h"
38#include "musx/factory/HeaderFactory.h"
39#include "musx/factory/PoolFactory.h"
40#include "musx/xml/XmlInterface.h"
41
42namespace musx {
43namespace factory {
44
49{
51 using DocumentPtr = std::shared_ptr<Document>;
52
53public:
55 template <typename Container>
56 using IsCharContainer = std::enable_if_t<
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>
59 >;
60
63 {
70
71 using EmbeddedGraphicFiles = std::vector<EmbeddedGraphicFile>;
72
74 CreateOptions() = default;
75
79
85 CreateOptions(std::filesystem::path inputFilepath,
86 std::vector<char> notationMetadata,
87 EmbeddedGraphicFiles embeddedGraphicFiles,
89 : partVoicingPolicy(policy),
90 m_notationMetadata(std::move(notationMetadata)),
91 m_embeddedGraphics(parseEmbeddedGraphicFiles(std::move(embeddedGraphicFiles))),
92 m_sourcePath(std::move(inputFilepath))
93 {}
94
96
98 [[nodiscard]]
99 const std::vector<char>& getNotationMetadata() const { return m_notationMetadata; }
100
102 [[nodiscard]]
103 const dom::EmbeddedGraphicsMap& getEmbeddedGraphics() const { return m_embeddedGraphics; }
104
106 [[nodiscard]]
107 const std::optional<std::filesystem::path>& getSourcePath() const { return m_sourcePath; }
108
110 [[nodiscard]]
111 dom::EmbeddedGraphicsMap takeEmbeddedGraphics() { return std::move(m_embeddedGraphics); }
112
113 private:
114 static dom::EmbeddedGraphicsMap parseEmbeddedGraphicFiles(EmbeddedGraphicFiles&& embeddedGraphicFiles)
115 {
116 dom::EmbeddedGraphicsMap embeddedGraphics;
117 for (auto& file : embeddedGraphicFiles) {
118 const auto parsed = parseEmbeddedGraphicFilename(file.filename);
119 if (!parsed) {
120 continue;
121 }
122 auto& entry = embeddedGraphics[parsed->first];
123 entry.extension = std::move(parsed->second);
124 entry.bytes = std::move(file.bytes);
125 }
126 return embeddedGraphics;
127 }
128
129 static std::optional<std::pair<Cmper, std::string>> parseEmbeddedGraphicFilename(const std::string& filename)
130 {
131 const auto dotPos = filename.find('.');
132 if (dotPos == std::string::npos || dotPos == 0 || dotPos + 1 >= filename.size()) {
133 return std::nullopt;
134 }
135
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; })) {
139 return std::nullopt;
140 }
141
142 unsigned long long cmperValue = 0;
143 try {
144 cmperValue = std::stoull(std::string(cmperText));
145 } catch (...) {
146 return std::nullopt;
147 }
148 if (cmperValue > static_cast<unsigned long long>((std::numeric_limits<Cmper>::max)())) {
149 return std::nullopt;
150 }
151
152 return std::make_pair(static_cast<Cmper>(cmperValue), filename.substr(dotPos + 1));
153 }
154
155 std::vector<char> m_notationMetadata;
156 dom::EmbeddedGraphicsMap m_embeddedGraphics;
157 std::optional<std::filesystem::path> m_sourcePath;
158 };
159
168 template <typename XmlDocumentType>
169 [[nodiscard]]
170 static DocumentPtr create(const char* data, size_t size)
171 {
172 return create<XmlDocumentType>(data, size, CreateOptions{});
173 }
174
176 template <typename XmlDocumentType, typename Container, typename = IsCharContainer<Container>>
177 [[nodiscard]]
178 static DocumentPtr create(const Container& xmlBuffer)
179 {
180 return create<XmlDocumentType>(asCharData(xmlBuffer), xmlBuffer.size(), CreateOptions{});
181 }
182
192 template <typename XmlDocumentType>
193 [[nodiscard]]
194 static DocumentPtr create(const char* data, size_t size, CreateOptions&& createOptions)
195 {
196 static_assert(std::is_base_of<musx::xml::IXmlDocument, XmlDocumentType>::value,
197 "XmlReaderType must derive from IXmlDocument.");
198
199 std::unique_ptr<musx::xml::IXmlDocument> xmlDocument = std::make_unique<XmlDocumentType>();
200 xmlDocument->loadFromBuffer(data, size);
201
202 auto rootElement = xmlDocument->getRootElement();
203 if (!rootElement || rootElement->getTagName() != "finale") {
204 throw std::invalid_argument("Missing <finale> element.");
205 }
206
207 DocumentPtr document(new Document);
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();
213
214 ElementLinker elementLinker;
215 for (auto element = rootElement->getFirstChildElement(); element; element = element->getNextSibling()) {
216 if (element->getTagName() == "header") {
217 document->getHeader() = musx::factory::HeaderFactory::create(element);
218 } else if (element->getTagName() == "options") {
219 document->getOptions() = musx::factory::OptionsFactory::create(element, document, elementLinker);
220 } else if (element->getTagName() == "others") {
221 document->getOthers() = musx::factory::OthersFactory::create(element, document, elementLinker);
222 } else if (element->getTagName() == "details") {
223 document->getDetails() = musx::factory::DetailsFactory::create(element, document, elementLinker);
224 } else if (element->getTagName() == "entries") {
225 document->getEntries() = musx::factory::EntryFactory::create(element, document, elementLinker);
226 } else if (element->getTagName() == "texts") {
227 document->getTexts() = musx::factory::TextsFactory::create(element, document, elementLinker);
228 }
229 }
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);
236
237#ifdef MUSX_DISPLAY_NODE_NAMES
239#endif
240 elementLinker.resolveAll(document);
241 document->m_instruments = document->createInstrumentMap(SCORE_PARTID);
242
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()) {
252 break;
253 }
254 mutablePart->numberOfLeadingBlankPages++;
255 }
256 if (mutablePart->numberOfLeadingBlankPages > document->m_maxBlankPages) {
257 document->m_maxBlankPages = part->numberOfLeadingBlankPages;
258 }
259 }
260
261 return document;
262 }
263
265 template <typename XmlDocumentType, typename Container, typename = IsCharContainer<Container>>
266 [[nodiscard]]
267 static DocumentPtr create(const Container& xmlBuffer, CreateOptions&& createOptions)
268 {
269 return create<XmlDocumentType>(asCharData(xmlBuffer), xmlBuffer.size(), std::move(createOptions));
270 }
271
272private:
273 template <typename Container>
274 static const char* asCharData(const Container& buffer)
275 {
276 return reinterpret_cast<const char*>(buffer.data());
277 }
278
279 template <typename XmlDocumentType>
280 static std::optional<double> parseScoreDurationSeconds(const std::vector<char>& notationMetadata)
281 {
282 if (notationMetadata.empty()) {
283 return std::nullopt;
284 }
285
286 std::unique_ptr<musx::xml::IXmlDocument> xmlDocument = std::make_unique<XmlDocumentType>();
287 try {
288 xmlDocument->loadFromBuffer(notationMetadata.data(), notationMetadata.size());
289 } catch (...) {
290 return std::nullopt;
291 }
292
293 auto rootElement = xmlDocument->getRootElement();
294 if (!rootElement || rootElement->getTagName() != "metadata") {
295 return std::nullopt;
296 }
297 if (auto fileInfo = rootElement->getFirstChildElement("fileInfo")) {
298 auto creatorString = fileInfo->getFirstChildElement("creatorString");
299 if (!creatorString || creatorString->getTextTrimmed().rfind("Finale", 0) != 0) {
300 return std::nullopt;
301 }
302 if (auto scoreDuration = fileInfo->getFirstChildElement("scoreDuration")) {
303 try {
304 return scoreDuration->getTextAs<double>();
305 } catch (...) {
306 return std::nullopt;
307 }
308 }
309 }
310 return std::nullopt;
311 }
312};
313
314} // namespace factory
315} // namespace musx
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 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
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