xml2ns0.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * xml2ns0.c
  3. *
  4. * Created on: 21.04.2014
  5. * Author: mrt
  6. */
  7. #include <expat.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h> // strlen
  11. #include <ctype.h> // isspace
  12. #include <unistd.h> // read
  13. #include "opcua.h"
  14. #include "ua_namespace.h"
  15. typedef char const * const XML_Attr_t;
  16. typedef char const * cstring_t;
  17. struct XML_Stack;
  18. typedef UA_Int32 (*XML_decoder)(struct XML_Stack* s, XML_Attr_t* attr, void* dst, _Bool isStart);
  19. typedef struct XML_child {
  20. cstring_t name;
  21. UA_Int32 type;
  22. XML_decoder elementHandler;
  23. void* obj;
  24. } XML_child_t;
  25. typedef struct XML_Parent {
  26. cstring_t name;
  27. int textAttribIdx; // -1 - not set
  28. cstring_t textAttrib;
  29. int activeChild; // -1 - no active child
  30. int len; // -1 - empty set
  31. XML_child_t children[20];
  32. } XML_Parent_t;
  33. typedef struct XML_Stack {
  34. int depth;
  35. XML_Parent_t parent[10];
  36. } XML_Stack_t;
  37. void XML_Stack_init(XML_Stack_t* p, cstring_t name) {
  38. unsigned int i,j;
  39. p->depth = 0;
  40. for (i=0;i<10;i++) {
  41. p->parent[i].name = UA_NULL;
  42. p->parent[i].len = 0;
  43. p->parent[i].activeChild = -1;
  44. p->parent[i].textAttrib = UA_NULL;
  45. p->parent[i].textAttribIdx = -1;
  46. for (j=0;j<20;j++) {
  47. p->parent[i].children[j].name = UA_NULL;
  48. p->parent[i].children[j].elementHandler = UA_NULL;
  49. p->parent[i].children[j].type = UA_INVALIDTYPE;
  50. p->parent[i].children[j].obj = UA_NULL;
  51. }
  52. }
  53. p->parent[0].name = name;
  54. }
  55. void XML_Stack_print(XML_Stack_t* s) {
  56. UA_Int32 i;
  57. for (i=0;i<=s->depth;i++) {
  58. printf("%s.",s->parent[i].name);
  59. }
  60. }
  61. // FIXME: we might want to calculate textAttribIdx
  62. void XML_Stack_handleTextAs(XML_Stack_t* p,cstring_t textAttrib, unsigned int textAttribIdx) {
  63. p->parent[p->depth].textAttrib = textAttrib;
  64. p->parent[p->depth].textAttribIdx = textAttribIdx;
  65. }
  66. void XML_Stack_addChildHandler(XML_Stack_t* p,cstring_t name,XML_decoder handler, UA_Int32 type, void* dst) {
  67. unsigned int len = p->parent[p->depth].len;
  68. p->parent[p->depth].children[len].name = name;
  69. p->parent[p->depth].children[len].elementHandler = handler;
  70. p->parent[p->depth].children[len].type = type;
  71. p->parent[p->depth].children[len].obj = dst;
  72. p->parent[p->depth].len++;
  73. }
  74. UA_Int32 UA_NodeId_copycstring(cstring_t src,UA_NodeId* dst) {
  75. dst->encodingByte = UA_NODEIDTYPE_FOURBYTE;
  76. dst->namespace = 0;
  77. // FIXME: assumes i=nnnn, does not care for aliases as of now
  78. dst->identifier.numeric = atoi(&src[2]);
  79. return UA_SUCCESS;
  80. }
  81. typedef struct T_UA_NodeSet { int dummy; } UA_NodeSet;
  82. UA_Int32 UA_Array_decodeXML(XML_Stack_t* s, XML_Attr_t* attr, void* dst, _Bool isStart) {
  83. // FIXME: Implement
  84. return UA_SUCCESS;
  85. }
  86. UA_Int32 UA_Int32_decodeXML(XML_Stack_t* s, XML_Attr_t* attr, UA_Int32* dst, _Bool isStart) {
  87. if (isStart) {
  88. if (dst == UA_NULL) { UA_Int32_new(&dst); }
  89. *dst = atoi(attr[1]);
  90. }
  91. return UA_SUCCESS;
  92. }
  93. UA_Int32 UA_String_decodeXML(XML_Stack_t* s, XML_Attr_t* attr, UA_String* dst, _Bool isStart) {
  94. UA_UInt32 i;
  95. if (isStart) {
  96. if (dst == UA_NULL) { UA_String_new(&dst); }
  97. if (s->parent[s->depth].len == 0 ) {
  98. XML_Stack_addChildHandler(s,"Data",(XML_decoder)UA_Array_decodeXML, UA_BYTE, &(dst->data));
  99. XML_Stack_addChildHandler(s,"Length",(XML_decoder)UA_Int32_decodeXML, UA_INT32, &(dst->length));
  100. XML_Stack_handleTextAs(s,"Data",0);
  101. }
  102. // set attributes
  103. for (i = 0; attr[i]; i += 2) {
  104. if (0==strncmp("Data",attr[i],strlen("Data"))) {
  105. UA_String_copycstring(attr[i+1],dst);
  106. } else {
  107. perror("Unknown attribute");
  108. }
  109. }
  110. }
  111. return UA_SUCCESS;
  112. }
  113. UA_Int32 UA_LocalizedText_decodeXML(XML_Stack_t* s, XML_Attr_t* attr, UA_LocalizedText* dst, _Bool isStart) {
  114. UA_UInt32 i;
  115. if (isStart) {
  116. if (dst == UA_NULL) {
  117. UA_LocalizedText_new(&dst);
  118. }
  119. if (s->parent[s->depth].len == 0 ) {
  120. XML_Stack_addChildHandler(s,"Text",(XML_decoder)UA_String_decodeXML, UA_STRING, &(dst->text));
  121. XML_Stack_addChildHandler(s,"Locale",(XML_decoder)UA_String_decodeXML, UA_STRING, &(dst->locale));
  122. XML_Stack_handleTextAs(s,"Data",0);
  123. }
  124. // set attributes
  125. for (i = 0; attr[i]; i += 2) {
  126. if (0==strncmp("Text",attr[i],strlen("Text"))) {
  127. UA_String_copycstring(attr[i+1],&(dst->text));
  128. dst->encodingMask |= UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  129. } else if (0==strncmp("Locale",attr[i],strlen("Locale"))) {
  130. UA_String_copycstring(attr[i+1],&(dst->locale));
  131. dst->encodingMask |= UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE;
  132. } else {
  133. perror("Unknown attribute");
  134. }
  135. }
  136. } else {
  137. switch (s->parent[s->depth].activeChild) {
  138. case 0:
  139. dst->encodingMask |= UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  140. break;
  141. case 1:
  142. dst->encodingMask |= UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE;
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. return UA_SUCCESS;
  149. }
  150. UA_Int32 UA_DataTypeNode_decodeXML(XML_Stack_t* s, XML_Attr_t* attr, UA_DataTypeNode* dst, _Bool isStart) {
  151. UA_UInt32 i;
  152. if (isStart) {
  153. // create a new object if called with UA_NULL
  154. if (dst == UA_NULL) {
  155. UA_DataTypeNode_new(&dst);
  156. s->parent[s->depth-1].children[s->parent[s->depth-1].activeChild].obj = (void*) dst;
  157. }
  158. // add the handlers for the child objects
  159. if (s->parent[s->depth].len == 0 ) {
  160. XML_Stack_addChildHandler(s,"DisplayName",(XML_decoder)UA_LocalizedText_decodeXML, UA_LOCALIZEDTEXT, &(dst->displayName));
  161. XML_Stack_addChildHandler(s,"Description",(XML_decoder)UA_LocalizedText_decodeXML, UA_LOCALIZEDTEXT, &(dst->description));
  162. }
  163. // set missing default attributes
  164. dst->nodeClass = UA_NODECLASS_DATATYPE;
  165. // set attributes
  166. for (i = 0; attr[i]; i += 2) {
  167. if (0==strncmp("NodeId",attr[i],strlen("NodeId"))) {
  168. UA_NodeId_copycstring(attr[i+1],&(dst->nodeId));
  169. } else if (0==strncmp("BrowseName",attr[i],strlen("BrowseName"))) {
  170. UA_String_copycstring(attr[i+1],&(dst->browseName.name));
  171. dst->browseName.namespaceIndex = 0;
  172. } else if (0==strncmp("DisplayName",attr[i],strlen("DisplayName"))) {
  173. UA_String_copycstring(attr[i+1],&(dst->displayName.text));
  174. dst->displayName.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  175. } else if (0==strncmp("Description",attr[i],strlen("Description"))) {
  176. UA_String_copycstring(attr[i+1],&(dst->description.text));
  177. dst->description.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  178. } else {
  179. DBG_ERR(XML_Stack_print(s));
  180. DBG_ERR(printf("%s - unknown attribute\n",attr[i]));
  181. }
  182. }
  183. }
  184. return UA_SUCCESS;
  185. }
  186. UA_Int32 UA_NodeSet_decodeXML(XML_Stack_t* s, XML_Attr_t* attr, UA_NodeSet* dst, _Bool isStart) {
  187. if (isStart) {
  188. if (s->parent[s->depth].len == 0 ) {
  189. // FIXME: add the other node types as well
  190. XML_Stack_addChildHandler(s,"UADataType",(XML_decoder)UA_DataTypeNode_decodeXML, UA_DATATYPENODE, UA_NULL);
  191. }
  192. } else {
  193. // FIXME: mockup code for debugging, should actually add node to namespace
  194. if (attr != UA_NULL) {
  195. UA_DataTypeNode* dtn = (UA_DataTypeNode*) attr;
  196. printf("node.browseName=%.*s\n", dtn->browseName.name.length, dtn->browseName.name.data);
  197. } else {
  198. DBG_ERR(printf("nodeset endElement called with null-ptr\n"));
  199. }
  200. }
  201. return UA_SUCCESS;
  202. }
  203. /** lookup if element is a known child of parent, if yes go for it otherwise ignore */
  204. void startElement(void * data, const char *el, const char **attr) {
  205. XML_Stack_t* s = (XML_Stack_t*) data;
  206. int i;
  207. // scan expected children
  208. XML_Parent_t* cp = &s->parent[s->depth];
  209. for (i = 0; i < cp->len; i++) {
  210. if (0 == strncmp(cp->children[i].name,el,strlen(cp->children[i].name))) {
  211. DBG_VERBOSE(XML_Stack_print(s));
  212. DBG_VERBOSE(printf("%s - processing\n",el));
  213. cp->activeChild = i;
  214. s->depth++;
  215. s->parent[s->depth].name = el;
  216. s->parent[s->depth].len = 0;
  217. s->parent[s->depth].textAttribIdx = -1;
  218. // finally call the elementHandler and return
  219. cp->children[i].elementHandler(data,attr,cp->children[i].obj, TRUE);
  220. return;
  221. }
  222. }
  223. // if we come here we rejected the processing of el
  224. DBG_VERBOSE(XML_Stack_print(s));
  225. DBG_VERBOSE(printf("%s - rejected\n",el));
  226. s->depth++;
  227. s->parent[s->depth].name = el;
  228. // this should be sufficient to reject the children as well
  229. s->parent[s->depth].len = 0;
  230. }
  231. UA_Int32 XML_isSpace(cstring_t s, int len) {
  232. int i;
  233. for (i=0; i<len; i++) {
  234. if (! isspace(s[i])) {
  235. return UA_FALSE;
  236. }
  237. }
  238. return UA_TRUE;
  239. }
  240. /* simulates startElement, endElement behaviour */
  241. void handleText(void * data, const char *txt, int len) {
  242. XML_Stack_t* s = (XML_Stack_t*) data;
  243. if (len > 0 && ! XML_isSpace(txt,len)) {
  244. XML_Parent_t* cp = &(s->parent[s->depth]);
  245. if (cp->textAttribIdx >= 0) {
  246. cp->activeChild = cp->textAttribIdx;
  247. char* buf; // need to copy txt to add 0 as string terminator
  248. UA_alloc((void**)&buf,len+1);
  249. strncpy(buf,txt,len);
  250. buf[len] = 0;
  251. XML_Attr_t attr[3] = { cp->textAttrib, buf, UA_NULL };
  252. cp->children[cp->activeChild].elementHandler(s,attr,cp->children[cp->activeChild].obj, TRUE);
  253. cp->children[cp->activeChild].elementHandler(s,UA_NULL,cp->children[cp->activeChild].obj, FALSE);
  254. UA_free(buf);
  255. } else {
  256. DBG_ERR(XML_Stack_print(s));
  257. DBG_ERR(printf("textData - ignore text data '%.*s'\n",len,txt));
  258. }
  259. }
  260. }
  261. /** if we are an activeChild of a parent we call the child-handler */
  262. void endElement(void *data, const char *el) {
  263. XML_Stack_t* s = (XML_Stack_t*) data;
  264. // the parent knows the elementHandler, therefore depth-1 !
  265. if (s->depth>1) {
  266. // inform parents elementHandler that everything is done
  267. XML_Parent_t* cp = &(s->parent[s->depth-1]);
  268. XML_Parent_t* cpp = &(s->parent[s->depth-2]);
  269. if (cpp->activeChild >= 0 && cp->activeChild >= 0) {
  270. DBG_VERBOSE(XML_Stack_print(s));
  271. DBG_VERBOSE(printf(" - inform parent %s\n", cpp->children[cpp->activeChild].name));
  272. cpp->children[cpp->activeChild].elementHandler(s,(XML_Attr_t*)cp->children[cp->activeChild].obj,cpp->children[cpp->activeChild].obj, FALSE);
  273. }
  274. cp->activeChild = -1;
  275. }
  276. s->depth--;
  277. }
  278. int main()
  279. {
  280. char buf[1024];
  281. int len; /* len is the number of bytes in the current bufferful of data */
  282. XML_Stack_t s;
  283. XML_Stack_init(&s, "ROOT");
  284. XML_Stack_addChildHandler(&s,"UANodeSet", (XML_decoder) UA_NodeSet_decodeXML, UA_INVALIDTYPE, UA_NULL);
  285. XML_Parser parser = XML_ParserCreate(NULL);
  286. XML_SetUserData(parser, &s);
  287. XML_SetElementHandler(parser, startElement, endElement);
  288. XML_SetCharacterDataHandler(parser, handleText);
  289. while ((len = read(0,buf,1024)) > 0) {
  290. if (!XML_Parse(parser, buf, len, (len<1024))) {
  291. return 1;
  292. }
  293. }
  294. XML_ParserFree(parser);
  295. return 0;
  296. }