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:
50 template <typename XmlDocumentType>
51 static DocumentPtr create(const std::vector<char>& xmlBuffer)
52 {
53 static_assert(std::is_base_of<musx::xml::IXmlDocument, XmlDocumentType>::value,
54 "XmlReaderType must derive from IXmlDocument.");
55
56 std::unique_ptr<musx::xml::IXmlDocument> xmlDocument = std::make_unique<XmlDocumentType>();
57 xmlDocument->loadFromString(xmlBuffer);
58
59 auto rootElement = xmlDocument->getRootElement();
60 if (!rootElement || rootElement->getTagName() != "finale") {
61 throw std::invalid_argument("Missing <finale> element.");
62 }
63
64 DocumentPtr document(new Document);
65
66 ElementLinker elementLinker;
67 for (auto element = rootElement->getFirstChildElement(); element; element = element->getNextSibling()) {
68 if (element->getTagName() == "header") {
69 document->getHeader() = musx::factory::HeaderFactory::create(element);
70 } else if (element->getTagName() == "options") {
71 document->getOptions() = musx::factory::OptionsFactory::create(element, document, elementLinker);
72 } else if (element->getTagName() == "others") {
73 document->getOthers() = musx::factory::OthersFactory::create(element, document, elementLinker);
74 } else if (element->getTagName() == "details") {
75 document->getDetails() = musx::factory::DetailsFactory::create(element, document, elementLinker);
76 } else if (element->getTagName() == "entries") {
77 document->getEntries() = musx::factory::EntryFactory::create(element, document, elementLinker);
78 } else if (element->getTagName() == "texts") {
79 document->getTexts() = musx::factory::TextsFactory::create(element, document, elementLinker);
80 }
81 }
82 if (!document->getHeader()) document->getHeader() = std::make_shared<musx::dom::Header>();
83 if (!document->getOptions()) document->getOptions() = std::make_shared<musx::dom::OptionsPool>();
84 if (!document->getOthers()) document->getOthers() = std::make_shared<musx::dom::OthersPool>();
85 if (!document->getDetails()) document->getDetails() = std::make_shared<musx::dom::DetailsPool>();
86 if (!document->getEntries()) document->getEntries() = std::make_shared<musx::dom::EntryPool>();
87 if (!document->getTexts()) document->getTexts() = std::make_shared<musx::dom::TextsPool>();
88
89 elementLinker.resolveAll(document);
90#ifdef MUSX_DISPLAY_NODE_NAMES
92#endif
93 return document;
94 }
95};
96
97} // namespace factory
98} // namespace musx
Represents a document object that encapsulates the entire EnigmaXML structure.
Definition Document.h:51
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 std::vector< char > &xmlBuffer)
Creates a Document object from an XML element.
Definition DocumentFactory.h:51
A utility class for managing deferred relationships between elements during document construction.
Definition FactoryBase.h:70
void resolveAll(const dom::DocumentPtr &document)
Resolves all deferred relationships.
Definition FactoryBase.h:108
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:132
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
object model for musx file (enigmaxml)
Definition BaseClasses.h:32