24 #include "open62541/plugin/log_stdout.h"
25 #include <libxml2/libxml/tree.h>
26 #include <libxml2/libxml/xpath.h>
27 #include <libxml2/libxml/xpathInternals.h>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/tokenizer.hpp>
37 xml_file_handler::xml_file_handler(
const std::string& filePath) {
38 if(!this->createDoc(filePath)) {
40 std::string(
"Failed to parse ") + filePath + (
". Check if mapping file exsists and is well formated."));
44 std::vector<xmlNodePtr> xml_file_handler::getNodesByName(xmlNodePtr startNode,
const std::string& nodeName) {
45 std::vector<xmlNodePtr> nodeVector;
46 while(startNode !=
nullptr) {
47 if((!xmlStrcmp(startNode->name, (
const xmlChar*)nodeName.c_str()))) {
48 nodeVector.push_back(startNode);
50 startNode = startNode->next;
56 xmlXPathObjectPtr xml_file_handler::getNodeSet(
const std::string& xPathString) {
57 auto* xpath = (xmlChar*)xPathString.c_str();
58 xmlXPathContextPtr context;
59 xmlXPathObjectPtr result;
64 context = xmlXPathNewContext(this->doc);
65 if(context ==
nullptr) {
68 if(xmlXPathRegisterNs(context, (xmlChar*)
"csa",
69 (xmlChar*)
"https://github.com/ChimeraTK/ControlSystemAdapter-OPC-UA-Adapter") != 0) {
70 UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
71 "Failed to register xml namespace: https://github.com/ChimeraTK/ControlSystemAdapter-OPC-UA-Adapter");
75 result = xmlXPathEvalExpression(xpath, context);
76 xmlXPathFreeContext(context);
77 if(result ==
nullptr) {
80 if(xmlXPathNodeSetIsEmpty(result->nodesetval)) {
81 xmlXPathFreeObject(result);
87 bool xml_file_handler::isDocSetted() {
88 if(this->doc !=
nullptr) {
94 bool xml_file_handler::createDoc(
const std::string& filePath) {
95 if(filePath.empty()) {
100 this->doc = xmlParseFile(filePath.c_str());
102 if(this->doc ==
nullptr) {
103 UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Document not parsed successfully.");
110 std::vector<std::string> xml_file_handler::parseVariablePath(
111 const std::string& variablePath,
const std::string& seperator) {
112 std::vector<std::string> pathList;
113 boost::char_separator<char> sep(seperator.c_str());
114 boost::tokenizer<boost::char_separator<char>> tokens(variablePath, sep);
116 for(
const auto& t : tokens) {
117 pathList.push_back(t);
122 std::string xml_file_handler::getAttributeValueFromNode(xmlNode* node,
const std::string& attributeName) {
123 xmlAttrPtr attr = xmlHasProp(node, (xmlChar*)attributeName.c_str());
124 if(attr !=
nullptr) {
125 std::string merker = (std::string)((
char*)attr->children->content);
131 std::string xml_file_handler::getContentFromNode(xmlNode* node) {
132 if(node !=
nullptr) {
133 xmlChar* content = xmlNodeGetContent(node->xmlChildrenNode);
134 if(content !=
nullptr) {
135 std::string maker = (std::string)((
char*)content);
144 xml_file_handler::~xml_file_handler() {
145 xmlFreeDoc(this->doc);