-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsrcsax_controller.cpp
More file actions
181 lines (139 loc) · 4.43 KB
/
srcsax_controller.cpp
File metadata and controls
181 lines (139 loc) · 4.43 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
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
// SPDX-License-Identifier: GPL-3.0-only
/**
* @file srcsax_controller.cpp
*
* @copyright Copyright (C) 2013-2024 srcML, LLC. (www.srcML.org)
*/
#include <srcsax.hpp>
#include <sax2_srcsax_handler.hpp>
#include <libxml/parserInternals.h>
#include <functional>
/**
* libxml_error
*
* Silence/catch/default libxml2 errors.
*/
static void libxml_error(void * /*ctx*/, const char* msg, ...) {
va_list vl;
va_start(vl, msg);
vfprintf(stderr, msg, vl);
va_end(vl);
}
/* srcsax_create_parser_context forward declaration */
static xmlParserCtxtPtr srcsax_create_parser_context(xmlParserInputBufferPtr buffer_input, xmlCharEncoding enc);
/**
* srcsax_create_context_parser_input_buffer
* @param srcml_context an opened context for opened srcML document
* @param read_callback a read callback function
* @close_callback a close callback function
* @param encoding the files character encoding
*
* Create a srcsSAX context from a general context and read/close callbacks with the specified encoding.
*
* @returns srcsax_context context to be used for srcML parsing.
*/
srcsax_context* srcsax_create_context_parser_input_buffer(std::unique_ptr<xmlParserInputBuffer> input) {
if (!input)
return 0;
const char* encoding = nullptr;
srcsax_context* context = nullptr;
try {
context = new srcsax_context();
} catch (...) {
return 0;
}
context->input = std::move(input);
xmlParserCtxtPtr libxml2_context = srcsax_create_parser_context(context->input.release(), encoding ? xmlParseCharEncoding(encoding) : XML_CHAR_ENCODING_NONE);
if (libxml2_context == nullptr) {
delete context;
return 0;
}
xmlGenericErrorFunc error_handler = (xmlGenericErrorFunc) libxml_error;
xmlSetGenericErrorFunc(libxml2_context, error_handler);
context->libxml2_context = libxml2_context;
return context;
}
/**
* srcsax_free_context
* @param context a srcSAX context
*
* Free the resources associated with a srcsax_context as created
* by a previous srcsax_create_context_*.
*/
void srcsax_free_context(srcsax_context* context) {
if (context == 0)
return;
if (context->libxml2_context)
xmlFreeParserCtxt(context->libxml2_context);
delete context;
}
/**
* srcsax_parse
* @param context srcSAX context
*
* Parse the context using the provide sax handlers.
* On error calls the error callback function before returning.
*
* @returns 0 on success -1 on error.
*/
int srcsax_parse(srcsax_context* context) {
if (context == 0 || context->handler == 0)
return -1;
xmlSAXHandlerPtr save_sax = context->libxml2_context->sax;
xmlSAXHandler sax = srcsax_sax2_factory();
context->libxml2_context->sax = &sax;
sax2_srcsax_handler state;
state.context = context;
context->libxml2_context->_private = &state;
int status = xmlParseDocument(context->libxml2_context);
context->libxml2_context->sax = save_sax;
if (status != 0 && context->srcsax_error) {
auto ep = xmlCtxtGetLastError(context->libxml2_context);
auto str_length = std::string_view(ep->message).size();
ep->message[str_length - 1] = '\0';
context->srcsax_error((const char *)ep->message, ep->code);
}
return status;
}
/**
* srcsax_create_parser_context
* @param buffer_input a parser input buffer
*
* Create a ctxt from a parser input buffer.
* Modeled after function in libxml2.
*
* @returns xml parser ctxt
*/
xmlParserCtxtPtr srcsax_create_parser_context(xmlParserInputBufferPtr buffer_input, xmlCharEncoding enc) {
if (buffer_input == 0)
return 0;
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
if (ctxt == 0)
return 0;
xmlCtxtUseOptions(ctxt, XML_PARSE_COMPACT | XML_PARSE_HUGE | XML_PARSE_NODICT);
xmlParserInputPtr input = xmlNewIOInputStream(ctxt, buffer_input, enc);
if (input == 0) {
xmlFreeParserCtxt(ctxt);
return 0;
}
inputPush(ctxt, input);
return ctxt;
}
/**
* srcsax_stop_parser
* @param context a srcSAX context
*
* Stop srcSAX parser.
*/
void srcsax_stop_parser(srcsax_context* context) {
xmlParserCtxtPtr ctxt = context->libxml2_context;
ctxt->sax->startDocument = 0;
ctxt->sax->endDocument = 0;
ctxt->sax->startElementNs = 0;
ctxt->sax->endElementNs = 0;
ctxt->sax->characters = 0;
ctxt->sax->cdataBlock = 0;
ctxt->sax->comment = 0;
ctxt->sax->ignorableWhitespace = 0;
xmlStopParser(ctxt);
}