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
|
/*
Copyright (C) 2011 VULTUREIIC
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
// $Id: xsd.c,v 1.14 2007-10-29 12:48:12 cokeman1982 Exp $
#include "quakedef.h"
#include "expat.h"
#include "xsd.h"
typedef xml_t * (*XSD_DocumentLoadType)(vfsfile_t *v, int len);
typedef void (*XSD_DocumentFreeType)(xml_t *);
typedef xml_document_t * (*XSD_DocumentConvertType)(xml_t *);
typedef struct xsd_mapping_s
{
char *document_type;
XSD_DocumentLoadType load_function;
XSD_DocumentFreeType free_function;
XSD_DocumentConvertType convert_function;
}
xsd_mapping_t;
xsd_mapping_t xsd_mappings[] = {
{"variable", XSD_Variable_LoadFromHandle, XSD_Variable_Free, XSD_Variable_Convert},
{"command", XSD_Command_LoadFromHandle, XSD_Command_Free, NULL},
{"document", XSD_Document_LoadFromHandle, XSD_Document_Free, NULL},
{NULL, NULL, NULL}
};
// get value attribute from the array
const char *XSD_GetAttribute(const char **atts, const char *name)
{
while (*atts)
{
if (!strcmp(*atts, name))
return *(atts+1);
atts += 2;
}
return NULL;
}
// add text (strcat) with realocation
char *XSD_AddText(char *dst, const char *src, int src_len)
{
char *buf;
size_t len;
if (dst == NULL)
{
buf = (char *) Q_malloc(src_len + 1);
memcpy(buf, src, src_len);
buf[src_len] = 0;
}
else
{
len = 1 + src_len + strlen(dst);
buf = (char *) Q_malloc(len);
strlcpy (buf, dst, len);
memcpy(buf+strlen(buf), src, src_len);
buf[src_len+strlen(dst)] = 0;
Q_free(dst);
}
return buf;
}
// test if character is valid XML space
int XSD_IsSpace(char c)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
return 1;
return 0;
}
// strip spaces multiple spaces from in-between words
char *XSD_StripSpaces (char *str)
{
char *buf, *ret;
unsigned int p = 0, q = 0;
if (str == NULL)
return str;
buf = (char *) Q_malloc(strlen(str)+1);
for (p=0; p < strlen(str); p++)
{
if (XSD_IsSpace(str[p]))
{
if (q == 0 || XSD_IsSpace(buf[q-1]))
;
else
buf[q++] = ' ';
}
else
buf[q++] = str[p];
}
// strip spaces from the end
while (q > 0 && XSD_IsSpace(buf[q-1]))
q--;
buf[q] = 0;
ret = (char *) Q_strdup(buf);
Q_free(buf);
Q_free(str);
return ret;
}
// check if we are somewhere inside given element
int XSD_IsIn(char *path, char *subPath)
{
size_t sublen = strlen(subPath);
// if path is shorter than subpath
if (strlen(path) < sublen)
return 0;
// if it matches
if (!strncmp(path, subPath, sublen) &&
(path[sublen] == 0 || path[sublen] == '/'))
{
return 1;
}
// no match
return 0;
}
// initialize parser stack
void XSD_InitStack(xml_parser_stack_t *stack)
{
memset(stack, 0, sizeof(xml_parser_stack_t));
}
// restore parser handlers from previous one in stack
void XSD_RestoreStack(xml_parser_stack_t *stack)
{
XML_SetStartElementHandler(stack->parser, stack->oldStartHandler);
XML_SetEndElementHandler(stack->parser, stack->oldEndHandler);
XML_SetCharacterDataHandler(stack->parser, stack->oldCharacterDataHandler);
XML_SetUserData(stack->parser, stack->oldUserData);
}
// call when element starts
void XSD_OnStartElement(xml_parser_stack_t *stack, const XML_Char *name, const XML_Char **atts)
{
strlcat(stack->path, "/", sizeof (stack->path));
strlcat(stack->path, name, sizeof (stack->path));
}
// call when element ends
void XSD_OnEndElement(xml_parser_stack_t *stack, const XML_Char *name)
{
char *t = strrchr(stack->path, '/');
*t = 0;
}
static void XSD_DetectType_OnStartElement(void *userData, const XML_Char *name, const XML_Char **atts)
{
char *type = (char *) userData;
if (type[0] == 0)
strcpy(type, name);
}
// load document, auto-recognizing its type, returns NULL in case of failure
xml_t * XSD_LoadDocument(char *filename)
{
xml_t *ret = NULL;
int i;
vfsfile_t *f;
XML_Parser parser = NULL;
int len;
int filelen;
char buf[XML_READ_BUFSIZE];
char document_type[1024];
// FIXME: D-Kure, does FS_ANY handle both the above cases
if (!(f = FS_OpenVFS(filename, "rb", FS_ANY))) {
return NULL;
}
filelen = VFS_GETLEN(f);
// initialize XML parser
parser = XML_ParserCreate(NULL);
if (parser == NULL) {
Com_Printf("could not open2\n");
goto error;
}
XML_SetStartElementHandler(parser, XSD_DetectType_OnStartElement);
XML_SetUserData(parser, document_type);
document_type[0] = 0;
while (document_type[0] == 0 && (len = VFS_READ(f, buf, XML_READ_BUFSIZE, NULL)) > 0)
{
if (XML_Parse(parser, buf, len, 0) != XML_STATUS_OK) {
Com_Printf("could not open3\n");
goto error;
}
}
if (document_type[0] == 0) {
Com_Printf("could not open4\n");
goto error;
}
// now we know what document type it is...
// parser is no more needed
XML_ParserFree(parser);
parser = NULL;
// fseek to the beginning of the file
VFS_SEEK(f, 0, SEEK_SET);
// execute loading parser
i = 0;
while (xsd_mappings[i].document_type != NULL)
{
if (!strcmp(xsd_mappings[i].document_type, document_type))
{
//???
ret = xsd_mappings[i].load_function(f, filelen);
break;
}
i++;
}
if (ret)
{
VFS_CLOSE(f);
return ret;
}
error:
if (f)
VFS_CLOSE(f);
if (parser)
XML_ParserFree(parser);
return NULL;
}
// load document and convert it to xml_document_t
xml_document_t * XSD_LoadDocumentWithXsl(char *filename)
{
xml_t *doc;
xml_document_t *document;
doc = XSD_LoadDocument(filename);
if (doc == NULL)
return NULL;
if (!strcmp(doc->document_type, "document"))
return (xml_document_t *) doc; // no conversion
// convert
document = XSD_XslConvert(doc);
XSD_FreeDocument(doc);
return document;
}
// free document loaded with XSD_LoadDocument
void XSD_FreeDocument(xml_t *document)
{
int i = 0;
while (xsd_mappings[i].document_type != NULL)
{
if (!strcmp(xsd_mappings[i].document_type, document->document_type))
{
xsd_mappings[i].free_function(document);
return;
}
i++;
}
}
// convert any known xml document to "document" type
xml_document_t * XSD_XslConvert(xml_t *doc)
{
int i = 0;
while (xsd_mappings[i].document_type != NULL)
{
if (!strcmp(xsd_mappings[i].document_type, doc->document_type))
{
if (xsd_mappings[i].convert_function)
return (xml_document_t *)xsd_mappings[i].convert_function(doc);
else
return NULL;
}
i++;
}
return NULL;
}
|