File: canon_xml.c

package info (click to toggle)
libinklevel 0.9.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,840 kB
  • sloc: sh: 4,488; ansic: 4,422; python: 44; makefile: 26
file content (279 lines) | stat: -rw-r--r-- 5,928 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* canon_xml.c
 *
 * (c) 2022 Markus Heinz
 *
 * This software is licensed under the terms of the GPL.
 * For details see file COPYING.
 */

#include <stdio.h>
#include <stdlib.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>

#include "config.h"
#include "inklevel.h"
#include "canon_xml.h"

static int execute_xpath_expression(const xmlChar* xml,
				    const xmlChar* xpathExpr,
				    const xmlChar* nsList,
				    struct ink_level *level);

static int register_namespaces(xmlXPathContextPtr xpathCtx,
			       const xmlChar* nsList);

static void evaluate_xpath_nodes(xmlNodeSetPtr nodes, struct ink_level *level);

int parse_level_from_canon_xml(const unsigned char *xml,
			       struct ink_level *level) {
  int result = OK;

  xmlInitParser();
  xmlChar *xpath = BAD_CAST XPATH_EXPRESSION;
  xmlChar *ns = BAD_CAST XPATH_NS;

  result = execute_xpath_expression((BAD_CAST xml), xpath, ns, level);

  if (result != OK) {
    result = COULD_NOT_PARSE_RESPONSE_FROM_PRINTER;
  }

  xmlCleanupParser();

  return result;
}

static int execute_xpath_expression(const xmlChar* xml,
				    const xmlChar* xpathExpr,
				    const xmlChar* nsList,
				    struct ink_level *level) {
  xmlDocPtr doc;
  xmlXPathContextPtr xpathCtx;
  xmlXPathObjectPtr xpathObj;

  /* Load XML document */
  doc = xmlReadDoc(xml, NULL, NULL, 0);

  if (doc == NULL) {

#ifdef DEBUG
    printf("Error: unable to parse XML\n");
#endif

    return ERROR;
  }

  /* Create xpath evaluation context */
  xpathCtx = xmlXPathNewContext(doc);

  if(xpathCtx == NULL) {

#ifdef DEBUG
    printf("Error: unable to create new XPath context\n");
#endif

    xmlFreeDoc(doc);
    return ERROR;
  }

  /* Register namespaces from list (if any) */
  if((nsList != NULL) && (register_namespaces(xpathCtx, nsList) < 0)) {

#ifdef DEBUG
    printf("Error: failed to register namespaces list \"%s\"\n", nsList);
#endif

    xmlXPathFreeContext(xpathCtx);
    xmlFreeDoc(doc);
    return ERROR;
  }

  /* Evaluate xpath expression */
  xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);

  if(xpathObj == NULL) {

#ifdef DEBUG
    printf("Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr);
#endif

    xmlXPathFreeContext(xpathCtx);
    xmlFreeDoc(doc);
    return ERROR;
  }

  /* Print results */
  evaluate_xpath_nodes(xpathObj->nodesetval, level);

  /* Cleanup */
  xmlXPathFreeObject(xpathObj);
  xmlXPathFreeContext(xpathCtx);
  xmlFreeDoc(doc);

  return OK;
}

static int register_namespaces(xmlXPathContextPtr xpathCtx,
			       const xmlChar* nsList) {
  xmlChar* nsListDup;
  xmlChar* prefix;
  xmlChar* href;
  xmlChar* next;

  nsListDup = xmlStrdup(nsList);

  if(nsListDup == NULL) {

#ifdef DEBUG
    printf("Error: unable to strdup namespaces list\n");
#endif

    return ERROR;
  }

  next = nsListDup;

  while(next != NULL) {
    /* skip spaces */
    while((*next) == ' ') {
      next++;
    }

    if((*next) == '\0') {
      break;
    }

    /* find prefix */
    prefix = next;
    next = (xmlChar*)xmlStrchr(next, '=');

    if(next == NULL) {

#ifdef DEBUG
      printf("Error: invalid namespaces list format\n");
#endif

      xmlFree(nsListDup);
      return ERROR;
    }

    *(next++) = '\0';

    /* find href */
    href = next;
    next = (xmlChar*)xmlStrchr(next, ' ');

    if(next != NULL) {
      *(next++) = '\0';
    }

    /* do register namespace */
    if(xmlXPathRegisterNs(xpathCtx, prefix, href) != 0) {

#ifdef DEBUG
      printf("Error: unable to register NS with "
	      "prefix=\"%s\" and href=\"%s\"\n", prefix, href);
#endif

      xmlFree(nsListDup);
      return ERROR;
    } else {

#ifdef DEBUG
      printf("namespaces registered\n");
#endif

    }
  }

  xmlFree(nsListDup);
  return OK;
}

static void evaluate_xpath_nodes(xmlNodeSetPtr nodes, struct ink_level *level) {
  xmlNodePtr cur;
  int size;
  int i;
  int colorsAdded = 0;

  size = (nodes) ? nodes->nodeNr : 0;

#ifdef DEBUG
  printf("Result (%d nodes):\n", size);
#endif

  for(i = 0; i < size; ++i) {
    if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL) {
      xmlNsPtr ns;

      ns = (xmlNsPtr)nodes->nodeTab[i];
      cur = (xmlNodePtr)ns->next;

#ifdef DEBUG
      if(cur->ns) {
	printf("= namespace \"%s\"=\"%s\" for node %s:%s\n",
	       ns->prefix, ns->href, cur->ns->href, cur->name);
      } else {
	printf("= namespace \"%s\"=\"%s\" for node %s\n",
	       ns->prefix, ns->href, cur->name);
      }
#endif

    } else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
      cur = nodes->nodeTab[i];

#ifdef DEBUG
      if(cur->ns) {
	printf("= element node \"%s:%s\"\n", cur->ns->href, cur->name);
      } else {
	printf("= element node \"%s\"\n", cur->name);
      }
#endif

      xmlNode *children = nodes->nodeTab[i]->children;
      xmlChar *color = NULL;
      xmlChar *curlevel = NULL;

      while (children != NULL) {
	if (!xmlStrcmp(BAD_CAST "color", children->name)) {
	  color = xmlStrndup(xmlNodeGetContent(children), 80);
	} else if (!xmlStrcmp(BAD_CAST "level", children->name)) {
	  curlevel = xmlStrndup(xmlNodeGetContent(children), 10);
	}

	children = children->next;
      }

#ifdef DEBUG
      printf("color %s level %s\n", color, curlevel);
#endif

      // fill in struct ink_level
      level->status = RESPONSE_VALID;

      if (!xmlStrcasecmp(color, BAD_CAST "Color")) {
	level->levels[colorsAdded][INDEX_TYPE] = CARTRIDGE_COLOR;
	level->levels[colorsAdded][INDEX_LEVEL] = atoi((char*) curlevel);
	colorsAdded++;
      } else if (!xmlStrcasecmp(color, BAD_CAST "Black")) {
	level->levels[colorsAdded][INDEX_TYPE] = CARTRIDGE_BLACK;
	level->levels[colorsAdded][INDEX_LEVEL] = atoi((char *)curlevel);
	colorsAdded++;
      }

      free(color);
      free(curlevel);
    } else {
      cur = nodes->nodeTab[i];

#ifdef DEBUG
      printf("= node \"%s\": type %d\n", cur->name, cur->type);
#endif

    }
  }
}