3
MediaTomb - http://www.mediatomb.cc/
5
parser_expat.cc - this file is part of MediaTomb.
7
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
10
Copyright (C) 2006-2008 Gena Batyan <bgeradz@mediatomb.cc>,
11
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
Leonhard Wimmer <leo@mediatomb.cc>
14
MediaTomb is free software; you can redistribute it and/or modify
15
it under the terms of the GNU General Public License version 2
16
as published by the Free Software Foundation.
18
MediaTomb is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
GNU General Public License for more details.
23
You should have received a copy of the GNU General Public License
24
version 2 along with MediaTomb; if not, write to the Free Software
25
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
27
$Id: parser_expat.cc 1698 2008-02-23 20:48:30Z lww $
30
/// \file parser_expat.cc
33
#include "autoconfig.h"
46
void XMLCALL Parser::element_start(void *userdata, const char *name, const char **attrs)
48
Parser *parser = (Parser *)userdata;
49
Ref<Element> el(new Element(String(name)));
50
for (int i = 0; attrs[i]; i += 2)
52
el->addAttribute(String(attrs[i]), String(attrs[i + 1]));
55
if (parser->root == nil)
59
parser->curEl->appendElementChild(el);
60
parser->elements->push(parser->curEl);
65
void XMLCALL Parser::element_end(void *userdata, const char *name)
67
Parser *parser = (Parser *)userdata;
68
parser->curEl = parser->elements->pop();
71
void XMLCALL Parser::character_data(void *userdata, const XML_Char *s, int len)
73
Parser *parser = (Parser *)userdata;
74
String text = String(s, len);
77
Ref<Text> textEl(new Text(text));
78
parser->curEl->appendChild(RefCast(textEl, Node));
82
void XMLCALL Parser::comment_callback(void *userdata, const XML_Char *s)
84
Parser *parser = (Parser *)userdata;
85
String text = String(s);
88
Ref<Comment> cm(new Comment(text));
89
parser->curEl->appendChild(RefCast(cm, Node));
98
Ref<Element> Parser::parseFile(String filename)
100
Ref<Context> ctx(new Context(filename));
101
return parse(ctx, read_text_file(filename));
104
Ref<Element> Parser::parseString(String str)
106
Ref<Context> ctx(new Context(_("")));
107
return parse(ctx, str);
110
Ref<Element> Parser::parse(Ref<Context> ctx, String input)
112
XML_Parser parser = XML_ParserCreate(NULL);
114
throw Exception(_("Unable to allocate XML parser"));
116
XML_SetUserData(parser, this);
117
XML_SetElementHandler(parser, Parser::element_start, Parser::element_end);
118
XML_SetCharacterDataHandler(parser, Parser::character_data);
119
XML_SetCommentHandler(parser, Parser::comment_callback);
121
elements = Ref<ObjectStack<Element> >(new ObjectStack<Element>(8));
123
if (XML_Parse(parser, input.c_str(), input.length(), 1) != XML_STATUS_OK)
125
ctx->line = XML_GetCurrentLineNumber(parser);
126
ctx->col = XML_GetCurrentColumnNumber(parser);
127
String message = String(XML_ErrorString(XML_GetErrorCode(parser)));
128
XML_ParserFree(parser);
129
throw ParseException(message, ctx);
132
XML_ParserFree(parser);