* Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
* ubs-engine is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
#ifndef UBSE_XML_H
#define UBSE_XML_H
#include <memory>
#include <stdexcept>
#include <string>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlsave.h>
namespace ubse::utils {
enum class UbseXmlError
{
OK = 0,
NULLPTR,
NOT_FOUND,
ERROR_XML_PARSE,
};
class ListNode {
public:
xmlNode* node{};
std::shared_ptr<ListNode> next;
int depth{};
};
class UbseXml : public std::enable_shared_from_this<UbseXml> {
public:
static std::shared_ptr<UbseXml> Create(const std::string& xml);
static std::shared_ptr<UbseXml> Create();
~UbseXml();
UbseXmlError Parse();
void Printer(std::string& outputString, bool includeXmlDeclaration = true);
std::shared_ptr<UbseXml> Next(const std::string& name, int num = 0);
UbseXmlError Previous();
void Back();
std::shared_ptr<UbseXml> Child(const std::string& name, int num = 0);
std::string Text();
void Text(const std::string& text);
std::string Name();
void Name(const std::string& name);
void SetXmlns(const std::string& uri);
std::string Attr(const std::string& key);
void Attr(const std::string& key, const std::string& value);
void AddNode(const std::string& name, bool isFirst = false);
UbseXmlError DeleteNode(const std::string& name, int num = 0);
int GetDeepth();
bool IsValid() const;
private:
explicit UbseXml(const std::string& xml);
UbseXml();
xmlNode* FindNthChildByName(xmlNode* child, const std::string& name, int num) const;
std::shared_ptr<UbseXml> UpdateToNode(xmlNode* nodePtr);
xmlDoc* doc{};
std::string xmlString;
std::shared_ptr<ListNode> listNode{};
xmlNode* rootNode{};
xmlNode* curNode{};
xmlNode* node{};
};
}
#endif