forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmldoc.cpp
More file actions
439 lines (386 loc) · 11.2 KB
/
xmldoc.cpp
File metadata and controls
439 lines (386 loc) · 11.2 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "cxml.h"
#if defined _MACOSX || defined ( _LINUX ) || defined (TARGET_SUBPLATFORM_IPHONE) || defined (TARGET_SUBPLATFORM_ANDROID)
#define _vsnprintf vsnprintf
#endif
//---------------------------------------GENERAL UTILITY FUNCTIONS------------------------------------
#define WHOLEMATCHES 1
int util_strnicmp(const char *one, const char *two, int n)
{
const char *optr = (const char *)one;
const char *tptr = (const char *)two;
while (n--) {
if (*optr != *tptr) {
char o = tolower(*optr);
char t = tolower(*tptr);
if (o != t || o == '\0' || t == '\0')
return o - t;
}
optr++;
tptr++;
}
if (!WHOLEMATCHES || !*optr)
return 0;
return 1;
}
int util_strncmp(const char *one, const char *two, int n)
{
const char *optr = (const char *)one;
const char *tptr = (const char *)two;
while (n--) {
if (*optr != *tptr) {
char o = *optr;
char t = *tptr;
if (o != t || o == '\0' || t == '\0')
return o - t;
}
optr++;
tptr++;
}
if (!WHOLEMATCHES || !*optr)
return 0;
return 1;
}
const char *util_strchr(const char *sptr, char target, int l)
{
if (!l) l = strlen(sptr);
const char *eptr = sptr + l;
while (sptr < eptr) {
if (*sptr == target)
return sptr;
sptr++;
}
return NULL;
}
//utility function used to concat two strings..and reallocate string if neccessary
void util_concatstring(const char *s, int slen, char *&d, int &dlen, int &dalloc)
{
int newbufsize = dalloc;
while (dlen + slen + 1 > newbufsize)
newbufsize += BUFSIZEINC;
if (newbufsize != dalloc){
dalloc = newbufsize;
d = (char *)realloc(d, dalloc);
}
memcpy(d + dlen, s, slen);
dlen += slen;
}
//----------------------------------CXMLDOCUMENT STATIC MEMBERS AND METHODS-----------------------------
unsigned int CXMLDocument::idcounter = 0;
Bool CXMLDocument::buildtree = True;
Bool CXMLDocument::allowcallbacks = False;
char CXMLDocument::errorbuf[256] = "";
//lists of callbacks - used to establish custom callbacks for parse errors, other callbacks in future
xmlSAXHandler CXMLDocument::SAXHandlerTable = {
internalSubset,
isStandalone,
hasInternalSubset,
hasExternalSubset,
resolveEntity,
getEntity,
entityDecl,
notationDecl,
attributeDecl,
elementDecl,
unparsedEntityDecl,
setDocumentLocator,
startDocumentCallback,//startDocument
endDocumentCallback,//endDocument
startElementCallback,//startElement
endElementCallback,//endElement
reference,
elementDataCallback, //characters,
ignorableWhitespace,
processingInstruction,
comment,
warningCallback,
errorCallback,
fatalCallback,
getParameterEntity,
elementCDataCallback,//cdataBlock,
externalSubset,
1,
NULL,
NULL,
NULL,
NULL,
};
void CXMLDocument::startDocumentCallback(void *ctx)
{
if (allowcallbacks)
CB_startDocument();
startDocument(ctx);
}
void CXMLDocument::endDocumentCallback(void *ctx)
{
if (allowcallbacks)
CB_endDocument();
endDocument(ctx);
}
void CXMLDocument::startElementCallback(void *ctx,
const xmlChar *fullname,
const xmlChar **atts)
{
extern Bool XML_ProcessNameSpaces;
if (allowcallbacks)
CB_startElement((const char *)fullname,(const char **)atts);
if (buildtree)
{
//HS-2010-10-11: [[ Bug 7586 ]] Reinstate libxml2 to create name spaces. Implement new liveCode commands to suppress name space creation.
if (XML_ProcessNameSpaces)
{
startElement(ctx,fullname,atts);
}
else
{
xmlSAX2StartElementNoNS(ctx,fullname,atts);
}
}
}
void CXMLDocument::endElementCallback(void *ctx,
const xmlChar *name)
{
if (allowcallbacks)
CB_endElement((const char *)name);
if (buildtree)
endElement(ctx,name);
}
void CXMLDocument::elementCDataCallback(void *ctx,const xmlChar *ch,int len)
{
if (allowcallbacks)
CB_elementData((const char *)ch,len);
if (buildtree)
cdataBlock(ctx,ch,len);
}
void CXMLDocument::elementDataCallback(void *ctx,const xmlChar *ch,int len)
{
if (allowcallbacks)
CB_elementData((const char *)ch,len);
if (buildtree)
characters(ctx,ch,len);
}
void CXMLDocument::warningCallback(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
_vsnprintf(errorbuf, 255, msg, args);
va_end(args);
}
void CXMLDocument::errorCallback(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
_vsnprintf(errorbuf, 255, msg, args);
va_end(args);
}
void CXMLDocument::fatalCallback(void *ctx, const char *msg, ...)
{
va_list args;
va_start(args, msg);
_vsnprintf(errorbuf, 255, msg, args);
va_end(args);
}
//--------------------------------CXMLDOCUMENT MEMBER FUNCTIONS--------------------------------
/* New - creates new xml document. */
void CXMLDocument::New()
{
Free(); //free document
doc = xmlNewDoc((xmlChar *) "1.0");
}
/*Read - parse xml data and create xml tree
data - points to xml data to parse
tlength - length of xml data to parse
returns false on parse error
*/
Bool CXMLDocument::Read(char *data, unsigned long tlength, Bool wellformed)
{
xmlKeepBlanksDefault(0);
Free(); //free document
// MW-2014-03-10: [[ Bug 11903 ]] Use modified libXML functions that allow us to
// pass through XML_PARSE_HUGE (so the new 2.9 limits don't apply!).
int options;
options = XML_PARSE_HUGE;
if (!wellformed)
options |= XML_PARSE_RECOVER;
doc = xmlSAXParseMemoryWithDataAndOptions(&SAXHandlerTable, data, tlength, options, NULL);
// OK-2007-12-17 : Bug 5632. If Validate() is called in this context it crashes
// For now we just remove the validation step from here as it proved difficult to
// find a way to make it work.
//if (doc && wellformed && !Validate())
//if (doc && wellformed)
// Free();
return doc != NULL;
}
Bool CXMLDocument::ReadFile(char *filename, Bool wellformed)
{
xmlKeepBlanksDefault(0);
Free(); //free document
// MW-2014-03-10: [[ Bug 11903 ]] Use modified libXML functions that allow us to
// pass through XML_PARSE_HUGE (so the new 2.9 limits don't apply!).
int options;
options = XML_PARSE_HUGE;
if (!wellformed)
options |= XML_PARSE_RECOVER;
doc = xmlSAXParseFileWithDataAndOptions(&SAXHandlerTable, filename, options, NULL);
// OK-2007-12-17 : Bug 5632. If Validate() is called in this context it crashes
// For now we just remove the validation step from here as it proved difficult to
// find a way to make it work.
//if (doc && wellformed && !Validate())
// Free();
return doc != NULL;
}
Bool CXMLDocument::Validate()
{
if (!isinited()) return False;
xmlValidCtxt ctxt;
ctxt.doc = doc;
ctxt.userData = NULL;
//point to dtd error handling function
ctxt.error = errorCallback;
ctxt.warning = warningCallback;
if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
return True;
return xmlValidateDocument(&ctxt, doc) == 1;
}
Bool CXMLDocument::AddDTD(char *data, unsigned long tlength)
{
if (!isinited()) return False;
xmlParserInputBufferPtr dtdInputBufferPtr;
xmlDtdPtr dtd;
dtdInputBufferPtr = xmlParserInputBufferCreateMem(data, tlength, XML_CHAR_ENCODING_UTF8);
dtd = xmlIOParseDTD(NULL, dtdInputBufferPtr, XML_CHAR_ENCODING_UTF8);
if (!dtd) return False;
if (dtd->name != NULL)
xmlFree((char*)dtd->name);
CXMLElement telement;
GetRootElement(&telement);
dtd->name = xmlStrdup((xmlChar *)telement.GetName());
doc->intSubset = dtd;
if (dtd->ExternalID != NULL) {
xmlFree((xmlChar *) dtd->ExternalID);
dtd->ExternalID = NULL;
}
if (dtd->SystemID != NULL) {
xmlFree((xmlChar *) dtd->SystemID);
dtd->SystemID = NULL;
}
dtd->doc = doc;
dtd->parent = doc;
if (doc->children == NULL) xmlAddChild((xmlNodePtr)doc, (xmlNodePtr)dtd);
else xmlAddPrevSibling(doc->children, (xmlNodePtr)dtd);
return Validate();
}
Bool CXMLDocument::ValidateDTD(char *data, unsigned long tlength)
{
if (!isinited()) return False;
xmlParserInputBufferPtr dtdInputBufferPtr;
xmlDtdPtr dtd;
dtdInputBufferPtr = xmlParserInputBufferCreateMem(data, tlength, XML_CHAR_ENCODING_UTF8);
dtd = xmlIOParseDTD(NULL, dtdInputBufferPtr, XML_CHAR_ENCODING_UTF8);
if (!dtd) return False;
xmlValidCtxt ctxt;
ctxt.doc = doc;
ctxt.userData = NULL;
//point to dtd error handling function
ctxt.error = errorCallback;
ctxt.warning = warningCallback;
Bool isvalid = xmlValidateDtd(&ctxt,doc,dtd);
xmlFreeDtd(dtd);
return isvalid;
}
/*Write - writes xml tree to buffer
output:
data - is set to point to buffer containing xml data
tlength - is set to length of xml data
*/
void CXMLDocument::Write(char **data,int *tlength,Bool isformatted)
{
if (!isinited()) return;
xmlDocDumpFormatMemory(doc,(unsigned char**)data,tlength,isformatted);
}
/*Free - frees xml document*/
void CXMLDocument::Free()
{
if (!isinited()) return;
xmlFreeDoc(doc);
doc = NULL;
if (NULL != xpathContext)
xmlXPathFreeContext(xpathContext);
xpathContext = NULL;
// MDW-2013-09-04: [[ RevXmlXslt ]]
if (NULL != xsltID)
xsltFreeStylesheet(xsltID);
xsltID = NULL;
}
/*CopyDocument - copies xml tree of other CXMLDocument
truecopy - if true this is set to point to xmltree of tdocument,
otherwise this is set to point to a copy of xmltree in tdocument
output: tdocument - xml document to copy from
*/
void CXMLDocument::CopyDocument(CXMLDocument *tdocument,Bool truecopy)
{
if (!tdocument->isinited()) return;
Free();
if (truecopy)
doc = xmlCopyDoc(tdocument->GetDocPtr(),0);
else
doc = tdocument->GetDocPtr();
}
/*GetRootElement
telement - is set to point to root element, returns false on error
returns False on error
*/
Bool CXMLDocument::GetRootElement(CXMLElement *telement)
{
if (!isinited()) return False;
xmlNodePtr rootelement = xmlDocGetRootElement(doc);
if (rootelement)
telement->SetNodePtr(rootelement);
return rootelement != NULL;
}
/*GetElementByPath
tpath - path of element in xml tree (ie. /rootelement/parentelement[1]/childelement)
telement - on success, output is set to point to element specified by tpath
return True if element found
*/
Bool CXMLDocument::GetElementByPath(CXMLElement *telement, char *tpath)
{
if (!isinited()) return False;
char *sptr = tpath;
Bool isroot = False;
if (!GetRootElement(telement))
return False;
if (*sptr == '/') sptr++;
const char *nameend;
char *nextname = strchr(sptr, '/');
if (!nextname)
{
if (!*sptr)
return True;
nextname = sptr+strlen(sptr);
isroot = True;
}
const char *numpointer = util_strchr(sptr,'[',nextname-sptr);
if (numpointer)
nameend = numpointer;
else
nameend = nextname;
if (!util_strnicmp(telement->GetName(),sptr,nameend-sptr))
{
if (isroot)
return True;
else
return telement->GoChildByPath(nextname+1);
}
return False;
}