ua_xml.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef __UA_XML_H
  2. #define __UA_XML_H
  3. #include <expat.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h> // strlen
  7. #include <ctype.h> // isspace
  8. #include <unistd.h> // read
  9. #include "ua_types.h"
  10. struct XML_Stack;
  11. typedef char const *const XML_Attr;
  12. typedef char const *cstring;
  13. #define XML_STACK_MAX_DEPTH 10
  14. #define XML_STACK_MAX_CHILDREN 40
  15. typedef UA_Int32 (*XML_decoder)(struct XML_Stack *s, XML_Attr *attr, void *dst, UA_Boolean isStart);
  16. /** @brief A readable shortcut for NodeIds. A list of aliases is intensively used in the namespace0-xml-files */
  17. typedef struct UA_NodeSetAlias {
  18. UA_String alias;
  19. UA_String value;
  20. } UA_NodeSetAlias;
  21. //UA_TYPE_PROTOTYPES(UA_NodeSetAlias)
  22. /** @brief UA_NodeSetAliases - a list of aliases */
  23. typedef struct UA_NodeSetAliases {
  24. UA_Int32 size;
  25. UA_NodeSetAlias **aliases;
  26. } UA_NodeSetAliases;
  27. //UA_TYPE_PROTOTYPES(UA_NodeSetAliases)
  28. typedef struct XML_child {
  29. cstring name;
  30. UA_Int32 length;
  31. UA_Int32 type;
  32. XML_decoder elementHandler;
  33. void *obj;
  34. } XML_child;
  35. typedef struct XML_Parent {
  36. cstring name;
  37. int textAttribIdx; // -1 - not set
  38. cstring textAttrib;
  39. int activeChild; // -1 - no active child
  40. int len; // -1 - empty set
  41. XML_child children[XML_STACK_MAX_CHILDREN];
  42. } XML_Parent;
  43. typedef struct XML_Stack {
  44. int depth;
  45. XML_Parent parent[XML_STACK_MAX_DEPTH];
  46. UA_NodeSetAliases *aliases; // shall point to the aliases of the NodeSet after reading
  47. } XML_Stack;
  48. UA_Int32 UA_Boolean_copycstring(cstring src, UA_Boolean *dst);
  49. UA_Int32 UA_Int16_copycstring(cstring src, UA_Int16 *dst);
  50. UA_Int32 UA_UInt16_copycstring(cstring src, UA_UInt16 *dst);
  51. UA_Boolean UA_NodeId_isBuiltinType(UA_NodeId *nodeid);
  52. /** @brief an object to hold a typed array */
  53. typedef struct UA_TypedArray {
  54. UA_Int32 size;
  55. UA_VTable_Entry *vt;
  56. void *elements;
  57. } UA_TypedArray;
  58. /** @brief init typed array with size=-1 and an UA_INVALIDTYPE */
  59. UA_Int32 UA_TypedArray_init(UA_TypedArray *p);
  60. /** @brief allocate memory for the array header only */
  61. UA_Int32 UA_TypedArray_new(UA_TypedArray **p);
  62. UA_Int32 UA_TypedArray_setType(UA_TypedArray *p, UA_Int32 type);
  63. //UA_Int32 UA_TypedArray_decodeXML(XML_Stack *s, XML_Attr *attr, UA_TypedArray *dst, UA_Boolean isStart);
  64. UA_Int32 UA_NodeSetAlias_init(UA_NodeSetAlias* p);
  65. UA_Int32 UA_NodeSetAlias_new(UA_NodeSetAlias** p);
  66. UA_Int32 UA_NodeSetAliases_init(UA_NodeSetAliases* p);
  67. UA_Int32 UA_NodeSetAliases_new(UA_NodeSetAliases** p);
  68. UA_Int32 UA_NodeSetAliases_println(cstring label, UA_NodeSetAliases *p);
  69. UA_Int32 UA_ExpandedNodeId_copycstring(cstring src, UA_ExpandedNodeId* dst, UA_NodeSetAliases* aliases);
  70. void XML_Stack_init(XML_Stack* p, UA_UInt32 nsid, cstring name);
  71. void XML_Stack_print(XML_Stack* s);
  72. /** @brief add a reference to a handler (@see XML_Stack_addChildHandler) for text data
  73. *
  74. * Assume a XML structure such as
  75. * <LocalizedText>
  76. * <Locale></Locale>
  77. * <Text>Server</Text>
  78. * </LocalizedText>
  79. * which might be abbreviated as
  80. * <LocalizedText>Server</LocalizedText>
  81. *
  82. * We would add two (@ref XML_Stack_addChildHandler), one for Locale (index 0) and one for Text (index 1),
  83. * both to be handled by (@ref UA_String_decodeXML) with elements "Data" and "Length". To handle the
  84. * abbreviation we add
  85. * XML_Stack_handleTextAsElementOf(s,"Data",1)
  86. *
  87. * @param[in] s the stack
  88. * @param[in] textAttrib the name of the element of the handler at position textAttribIdx
  89. * @param[in] textAttribIdx the index of the handler
  90. */
  91. void XML_Stack_handleTextAsElementOf(XML_Stack *p, cstring textAttrib, unsigned int textAttribIdx);
  92. /** @brief make a handler known to the XML-stack on the current level
  93. *
  94. * The current level is given by s->depth, the maximum number of children is a predefined constant.
  95. * A combination of type=UA_INVALIDTYPE and dst=UA_NULL is valid for special handlers only
  96. *
  97. * @param[in] s the stack
  98. * @param[in] name the name of the element
  99. * @param[in] nameLength the length of the element name
  100. * @param[in] handler the decoder routine for this element
  101. * @param[in] type the open62541-type of the element, UA_INVALIDTYPE if not in the VTable
  102. * @param[out] dst the address of the object for the data, handlers will allocate object if UA_NULL
  103. */
  104. void XML_Stack_addChildHandler(XML_Stack *p, cstring name, UA_Int32 nameLength, XML_decoder handler, UA_Int32 type,
  105. void *dst);
  106. void XML_Stack_startElement(void *data, const char *el, const char **attr);
  107. UA_Int32 XML_isSpace(cstring s, int len);
  108. void XML_Stack_handleText(void *data, const char *txt, int len);
  109. void XML_Stack_endElement(void *data, const char *el);
  110. UA_Int32 UA_Text_decodeXmlFromStack(XML_Stack *s, XML_Attr *attr, UA_Byte **dst, _Bool isStart);
  111. UA_Int32 UA_NodeId_copycstring(cstring src, UA_NodeId *dst, UA_NodeSetAliases *aliases);
  112. UA_Int32 UA_TypedArray_decodeXmlFromStack(XML_Stack *s, XML_Attr *attr, UA_TypedArray *dst, _Bool isStart);
  113. #endif // __UA_XML_H__