24#include "open62541/plugin/log_stdout.h"
26#include <libxml2/libxml/tree.h>
27#include <libxml2/libxml/xpath.h>
28#include <libxml2/libxml/xpathInternals.h>
30#include <boost/algorithm/string.hpp>
31#include <boost/tokenizer.hpp>
41 std::string(
"Failed to parse ") + filePath + (
". Check if mapping file exsists and is well formated."));
46 std::vector<xmlNodePtr> nodeVector;
47 while(startNode !=
nullptr) {
48 if((!xmlStrcmp(startNode->name, (
const xmlChar*)nodeName.c_str()))) {
49 nodeVector.push_back(startNode);
51 startNode = startNode->next;
58 auto* xpath = (xmlChar*)xPathString.c_str();
59 xmlXPathContextPtr context;
60 xmlXPathObjectPtr result;
65 context = xmlXPathNewContext(this->doc);
66 if(context ==
nullptr) {
69 if(xmlXPathRegisterNs(context, (xmlChar*)
"csa",
70 (xmlChar*)
"https://github.com/ChimeraTK/ControlSystemAdapter-OPC-UA-Adapter") != 0) {
71 UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
72 "Failed to register xml namespace: https://github.com/ChimeraTK/ControlSystemAdapter-OPC-UA-Adapter");
76 result = xmlXPathEvalExpression(xpath, context);
77 xmlXPathFreeContext(context);
78 if(result ==
nullptr) {
81 if(xmlXPathNodeSetIsEmpty(result->nodesetval)) {
82 xmlXPathFreeObject(result);
89 if(this->doc !=
nullptr) {
96 if(filePath.empty()) {
101 this->doc = xmlParseFile(filePath.c_str());
103 if(this->doc ==
nullptr) {
104 UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Document not parsed successfully.");
112 const std::string& variablePath,
const std::string& seperator) {
113 std::vector<std::string> pathList;
114 boost::char_separator<char> sep(seperator.c_str());
115 boost::tokenizer<boost::char_separator<char>> tokens(variablePath, sep);
117 for(
const auto& t : tokens) {
118 pathList.push_back(t);
124 xmlAttrPtr attr = xmlHasProp(node, (xmlChar*)attributeName.c_str());
125 if(attr !=
nullptr) {
126 std::string merker = (std::string)((
char*)attr->children->content);
133 if(node !=
nullptr) {
134 xmlChar* content = xmlNodeGetContent(node->xmlChildrenNode);
135 if(content !=
nullptr) {
136 std::string maker = (std::string)((
char*)content);
146 xmlFreeDoc(this->doc);
xml_file_handler(const std::string &filePath)
The constructor of the class creates a doc pointer depending on the file path.
static std::vector< xmlNodePtr > getNodesByName(xmlNodePtr startNode, const std::string &nodeName)
This methode return a list of all nodes with the given name nodeName starting by the given startNode.
bool createDoc(const std::string &filePath)
This methode set a document pointer to the file it ist given by the file path.
xmlXPathObjectPtr getNodeSet(const std::string &xPathString)
This methode return a pointer of a xPath element depending of the given xPathString.
~xml_file_handler()
Destructor of the class, frees the document and clean the parser.
static std::string getContentFromNode(xmlNode *node)
This methode returns the value between a xml tag.
static std::vector< std::string > parseVariablePath(const std::string &variablePath, const std::string &seperator="/")
This methode splitt a given string bey the given seperators.
static std::string getAttributeValueFromNode(xmlNode *node, const std::string &attributeName)
This methode returns a value of the given attribute from the given node you want to know.
bool isDocSetted()
This Methode check if a document is currently setted.