public
Description: A Lua implementation of the XmlReader API using libxml2
Homepage: http://asbradbury.org/projects/lua-xmlreader/
Clone URL: git://github.com/asb/lua-xmlreader.git
lua-xmlreader / error.c
100644 127 lines (117 sloc) 3.154 kb
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
#include "lua.h"
#include "lauxlib.h"
 
#include "libxml/xmlerror.h"
/* TODO: wrap the error object so user code can retrieve and query the last
* error */
/* Mostly ripped from code in parser.c and error.c in libxml. Not ideal, but
* it seems impossible to hook into libxml's error string generation */
int xml_push_error(lua_State *L, xmlErrorPtr err) {
  if (err==NULL)
    luaL_error(L, "a NULL error was raised. Should not happen");
 
  char *file = err->file;
  int line = err->line;
/* int code = err->code; */
  int domain = err->domain;
  xmlErrorLevel level = err->level;
  const xmlChar *name = NULL;
  xmlNodePtr node = err->node;
 
  luaL_Buffer buf;
  luaL_buffinit(L, &buf);
  luaL_Buffer *B = &buf;
 
  if ((node != NULL) && (node->type == XML_ELEMENT_NODE))
    name = node->name;
 
  if (file != NULL) {
    lua_pushfstring(L, "%s:%d: ", file, line);
    luaL_addvalue(B);
  } else if ((line != 0) && (domain == XML_FROM_PARSER)) {
    lua_pushfstring(L, "Entity: line %d: ", line);
    luaL_addvalue(B);
  }
 
  if (name != NULL) {
    lua_pushfstring(L, "element %s: ", name);
    luaL_addvalue(B);
  }
 
  switch (domain) {
    case XML_FROM_PARSER:
      luaL_addstring(B, "parser ");
      break;
    case XML_FROM_NAMESPACE:
      luaL_addstring(B, "namespace ");
      break;
    case XML_FROM_DTD:
    case XML_FROM_VALID:
      luaL_addstring(B, "validity ");
      break;
    case XML_FROM_HTML:
      luaL_addstring(B, "HTML parser ");
      break;
    case XML_FROM_MEMORY:
      luaL_addstring(B, "memory ");
      break;
    case XML_FROM_OUTPUT:
      luaL_addstring(B, "output ");
      break;
    case XML_FROM_IO:
      luaL_addstring(B, "I/O ");
      break;
    case XML_FROM_XINCLUDE:
      luaL_addstring(B, "XInclude ");
      break;
    case XML_FROM_XPATH:
      luaL_addstring(B, "XPath ");
      break;
    case XML_FROM_XPOINTER:
      luaL_addstring(B, "parser ");
      break;
    case XML_FROM_REGEXP:
      luaL_addstring(B, "regexp ");
      break;
    case XML_FROM_MODULE:
      luaL_addstring(B, "module ");
      break;
    case XML_FROM_SCHEMASV:
      luaL_addstring(B, "Schemas validity ");
      break;
    case XML_FROM_SCHEMASP:
      luaL_addstring(B, "Schemas parser ");
      break;
    case XML_FROM_RELAXNGP:
      luaL_addstring(B, "Relax-NG parser ");
      break;
    case XML_FROM_RELAXNGV:
      luaL_addstring(B, "Relax-NG validity ");
      break;
    case XML_FROM_CATALOG:
      luaL_addstring(B, "Catalog ");
      break;
    case XML_FROM_C14N:
      luaL_addstring(B, "C14N ");
      break;
    case XML_FROM_XSLT:
      luaL_addstring(B, "XSLT ");
      break;
    case XML_FROM_I18N:
      luaL_addstring(B, "encoding ");
      break;
    default:
      break;
  }
  switch (level) {
    case XML_ERR_NONE:
      luaL_addstring(B, ": ");
      break;
    case XML_ERR_WARNING:
      luaL_addstring(B, "warning : ");
      break;
    case XML_ERR_ERROR:
      luaL_addstring(B, "error : ");
      break;
    case XML_ERR_FATAL:
      luaL_addstring(B, "error : ");
      break;
  }
 
  if (err->message)
    luaL_addstring(B, err->message);
 
  luaL_pushresult(B);
  return 1;
}