2007-12-14 00:46:32 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
2011-11-10 14:28:12 +00:00
|
|
|
* Copyright (C) 2011 Tatsuhiro Tsujikawa
|
2007-12-14 00:46:32 +00:00
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-12-14 00:46:32 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
|
|
|
*/
|
|
|
|
/* copyright --> */
|
2011-11-10 14:28:12 +00:00
|
|
|
#include "ExpatXmlParser.h"
|
2010-05-26 12:36:23 +00:00
|
|
|
|
2011-08-07 09:08:16 +00:00
|
|
|
#include <cstdio>
|
2011-11-03 14:09:03 +00:00
|
|
|
#include <cstring>
|
2011-11-10 14:28:12 +00:00
|
|
|
#include <deque>
|
2010-05-26 12:36:23 +00:00
|
|
|
|
2011-11-10 14:28:12 +00:00
|
|
|
#include <expat.h>
|
|
|
|
|
|
|
|
#include "a2io.h"
|
|
|
|
#include "BinaryStream.h"
|
2011-08-07 09:08:16 +00:00
|
|
|
#include "BufferedFile.h"
|
2011-11-10 14:28:12 +00:00
|
|
|
#include "ParserStateMachine.h"
|
|
|
|
#include "A2STR.h"
|
2011-08-07 09:08:16 +00:00
|
|
|
#include "a2functional.h"
|
2011-11-10 14:28:12 +00:00
|
|
|
#include "XmlAttr.h"
|
2008-02-27 15:04:42 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2007-12-14 00:46:32 +00:00
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2011-11-10 14:28:12 +00:00
|
|
|
struct SessionData {
|
2010-06-21 13:51:56 +00:00
|
|
|
std::deque<std::string> charactersStack_;
|
2011-11-10 14:28:12 +00:00
|
|
|
ParserStateMachine* psm_;
|
|
|
|
SessionData(ParserStateMachine* psm)
|
|
|
|
: psm_(psm)
|
|
|
|
{}
|
2007-12-14 00:46:32 +00:00
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2007-12-14 00:46:32 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2012-01-10 17:04:20 +00:00
|
|
|
void splitNsName(const char** localname, const char** nsUri, const char* src)
|
2010-02-25 14:40:18 +00:00
|
|
|
{
|
2012-01-10 17:04:20 +00:00
|
|
|
const char* sep = strchr(src, '\t');
|
|
|
|
if(sep) {
|
2011-11-10 14:28:12 +00:00
|
|
|
*localname = sep+1;
|
2012-01-10 17:04:20 +00:00
|
|
|
size_t nsUriLen = sep-src;
|
2011-11-10 14:28:12 +00:00
|
|
|
char* temp = new char[nsUriLen+1];
|
2012-01-10 17:04:20 +00:00
|
|
|
memcpy(temp, src, nsUriLen);
|
2011-11-10 14:28:12 +00:00
|
|
|
temp[nsUriLen] = '\0';
|
|
|
|
*nsUri = temp;
|
2012-01-10 17:04:20 +00:00
|
|
|
} else {
|
|
|
|
*localname = src;
|
2010-02-25 14:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2010-02-25 14:40:18 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void mlStartElement(void* userData, const char* nsName, const char** attrs)
|
2007-12-14 00:46:32 +00:00
|
|
|
{
|
2008-04-26 07:43:01 +00:00
|
|
|
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
2010-02-25 14:40:18 +00:00
|
|
|
std::vector<XmlAttr> xmlAttrs;
|
2007-12-14 00:46:32 +00:00
|
|
|
if(attrs) {
|
|
|
|
const char** p = attrs;
|
|
|
|
while(*p != 0) {
|
2011-11-10 14:28:12 +00:00
|
|
|
XmlAttr xa;
|
2011-11-03 14:09:03 +00:00
|
|
|
const char* attrNsName = *p++;
|
2007-12-14 00:46:32 +00:00
|
|
|
if(*p == 0) {
|
2010-01-05 16:01:46 +00:00
|
|
|
break;
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2012-01-10 17:04:20 +00:00
|
|
|
splitNsName(&xa.localname, &xa.nsUri, attrNsName);
|
2011-11-03 14:09:03 +00:00
|
|
|
const char* value = *p++;
|
2011-11-10 14:28:12 +00:00
|
|
|
xa.value = value;
|
|
|
|
xa.valueLength = strlen(value);
|
2010-02-25 14:40:18 +00:00
|
|
|
xmlAttrs.push_back(xa);
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-10 14:28:12 +00:00
|
|
|
const char* localname = 0;
|
|
|
|
const char* prefix = 0;
|
|
|
|
const char* nsUri = 0;
|
2012-01-10 17:04:20 +00:00
|
|
|
splitNsName(&localname, &nsUri, nsName);
|
2011-11-10 14:28:12 +00:00
|
|
|
sd->psm_->beginElement(localname, prefix, nsUri, xmlAttrs);
|
|
|
|
delete [] nsUri;
|
|
|
|
for(std::vector<XmlAttr>::iterator i = xmlAttrs.begin(),
|
|
|
|
eoi = xmlAttrs.end(); i != eoi; ++i) {
|
|
|
|
delete [] (*i).nsUri;
|
|
|
|
}
|
|
|
|
if(sd->psm_->needsCharactersBuffering()) {
|
2010-06-21 13:51:56 +00:00
|
|
|
sd->charactersStack_.push_front(A2STR::NIL);
|
2008-04-26 07:43:01 +00:00
|
|
|
}
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2007-12-14 00:46:32 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void mlEndElement(void* userData, const char* nsName)
|
2007-12-14 00:46:32 +00:00
|
|
|
{
|
2011-11-10 14:28:12 +00:00
|
|
|
const char* localname = 0;
|
|
|
|
const char* prefix = 0;
|
|
|
|
const char* nsUri = 0;
|
2012-01-10 17:04:20 +00:00
|
|
|
splitNsName(&localname, &nsUri, nsName);
|
2008-04-26 07:43:01 +00:00
|
|
|
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
|
|
|
std::string characters;
|
2011-11-10 14:28:12 +00:00
|
|
|
if(sd->psm_->needsCharactersBuffering()) {
|
2010-06-21 13:51:56 +00:00
|
|
|
characters = sd->charactersStack_.front();
|
|
|
|
sd->charactersStack_.pop_front();
|
2008-04-26 07:43:01 +00:00
|
|
|
}
|
2011-11-10 14:28:12 +00:00
|
|
|
sd->psm_->endElement(localname, prefix, nsUri, characters);
|
|
|
|
delete [] nsUri;
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2007-12-14 00:46:32 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void mlCharacters(void* userData, const char* ch, int len)
|
2007-12-14 00:46:32 +00:00
|
|
|
{
|
2008-04-26 07:43:01 +00:00
|
|
|
SessionData* sd = reinterpret_cast<SessionData*>(userData);
|
2011-11-10 14:28:12 +00:00
|
|
|
if(sd->psm_->needsCharactersBuffering()) {
|
2011-11-03 09:56:47 +00:00
|
|
|
sd->charactersStack_.front().append(&ch[0], &ch[len]);
|
2008-04-26 07:43:01 +00:00
|
|
|
}
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2007-12-14 00:46:32 +00:00
|
|
|
|
2011-11-10 14:28:12 +00:00
|
|
|
XmlParser::XmlParser(ParserStateMachine* psm)
|
|
|
|
: psm_(psm)
|
|
|
|
{}
|
|
|
|
|
|
|
|
XmlParser::~XmlParser() {}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
2011-11-10 14:28:12 +00:00
|
|
|
XML_Parser createParser(SessionData* sd)
|
2010-05-26 12:36:23 +00:00
|
|
|
{
|
|
|
|
XML_Parser parser = XML_ParserCreateNS(0, static_cast<const XML_Char>('\t'));
|
2011-11-10 14:28:12 +00:00
|
|
|
XML_SetUserData(parser, sd);
|
2010-05-26 12:36:23 +00:00
|
|
|
XML_SetElementHandler(parser, &mlStartElement, &mlEndElement);
|
|
|
|
XML_SetCharacterDataHandler(parser, &mlCharacters);
|
|
|
|
return parser;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2010-05-26 12:36:23 +00:00
|
|
|
|
2011-11-10 14:28:12 +00:00
|
|
|
bool XmlParser::parseFile(const char* filename)
|
2007-12-14 00:46:32 +00:00
|
|
|
{
|
2011-11-10 14:28:12 +00:00
|
|
|
if(strcmp(filename, DEV_STDIN) == 0) {
|
2012-06-26 16:38:27 +00:00
|
|
|
BufferedFile fp(stdin);
|
2011-11-10 14:28:12 +00:00
|
|
|
return parseFile(fp);
|
2010-05-26 12:36:23 +00:00
|
|
|
} else {
|
2012-06-26 16:38:27 +00:00
|
|
|
BufferedFile fp(filename, BufferedFile::READ);
|
2011-11-10 14:28:12 +00:00
|
|
|
return parseFile(fp);
|
2010-05-26 12:36:23 +00:00
|
|
|
}
|
|
|
|
}
|
2007-12-14 00:46:32 +00:00
|
|
|
|
2012-06-26 16:38:27 +00:00
|
|
|
bool XmlParser::parseFile(BufferedFile& fp)
|
2010-05-26 12:36:23 +00:00
|
|
|
{
|
2012-06-26 16:38:27 +00:00
|
|
|
if(!fp) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-26 12:36:23 +00:00
|
|
|
char buf[4096];
|
2011-11-10 14:28:12 +00:00
|
|
|
SessionData sessionData(psm_);
|
|
|
|
XML_Parser parser = createParser(&sessionData);
|
2010-05-26 12:36:23 +00:00
|
|
|
auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
|
2011-08-07 09:08:16 +00:00
|
|
|
while(1) {
|
2012-06-26 16:38:27 +00:00
|
|
|
size_t res = fp.read(buf, sizeof(buf));
|
2011-08-07 09:08:16 +00:00
|
|
|
if(XML_Parse(parser, buf, res, 0) == XML_STATUS_ERROR) {
|
2011-11-10 14:28:12 +00:00
|
|
|
return false;
|
2010-05-26 12:36:23 +00:00
|
|
|
}
|
2011-08-07 09:08:16 +00:00
|
|
|
if(res < sizeof(buf)) {
|
|
|
|
break;
|
|
|
|
}
|
2010-05-26 12:36:23 +00:00
|
|
|
}
|
2011-11-10 14:28:12 +00:00
|
|
|
return XML_Parse(parser, 0, 0, 1) != XML_STATUS_ERROR && psm_->finished();
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2010-01-05 16:01:46 +00:00
|
|
|
|
2011-11-10 14:28:12 +00:00
|
|
|
bool XmlParser::parseBinaryStream(BinaryStream* bs)
|
2007-12-14 00:46:32 +00:00
|
|
|
{
|
2011-11-10 14:28:12 +00:00
|
|
|
const ssize_t bufSize = 4096;
|
2007-12-14 00:46:32 +00:00
|
|
|
unsigned char buf[bufSize];
|
2011-11-10 14:28:12 +00:00
|
|
|
SessionData sessionData(psm_);
|
|
|
|
XML_Parser parser = createParser(&sessionData);
|
2010-11-11 13:38:34 +00:00
|
|
|
auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
|
2012-07-23 12:36:24 +00:00
|
|
|
int64_t readOffset = 0;
|
2010-03-02 14:07:30 +00:00
|
|
|
while(1) {
|
2011-11-10 14:28:12 +00:00
|
|
|
ssize_t res = bs->readData(buf, bufSize, readOffset);
|
2010-03-02 14:07:30 +00:00
|
|
|
if(res == 0) {
|
|
|
|
break;
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2010-03-02 14:07:30 +00:00
|
|
|
if(XML_Parse(parser, reinterpret_cast<const char*>(buf), res, 0) ==
|
|
|
|
XML_STATUS_ERROR) {
|
2011-11-10 14:28:12 +00:00
|
|
|
return false;
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2010-03-02 14:07:30 +00:00
|
|
|
readOffset += res;
|
|
|
|
}
|
2011-11-10 14:28:12 +00:00
|
|
|
return XML_Parse(parser, 0, 0, 1) != XML_STATUS_ERROR && psm_->finished();
|
2007-12-14 00:46:32 +00:00
|
|
|
}
|
2008-02-27 15:04:42 +00:00
|
|
|
|
2011-12-01 14:34:14 +00:00
|
|
|
bool XmlParser::parseMemory(const char* xml, size_t size)
|
|
|
|
{
|
|
|
|
SessionData sessionData(psm_);
|
|
|
|
XML_Parser parser = createParser(&sessionData);
|
|
|
|
auto_delete<XML_Parser> deleter(parser, XML_ParserFree);
|
|
|
|
if(XML_Parse(parser, xml, size, 0) == XML_STATUS_ERROR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return XML_Parse(parser, 0, 0, 1) != XML_STATUS_ERROR && psm_->finished();
|
|
|
|
}
|
|
|
|
|
2008-02-27 15:04:42 +00:00
|
|
|
} // namespace aria2
|