Changeset - a7a6564630fe
[Not reviewed]
0 1 0
Jan Kaluza - 13 years ago 2013-02-20 12:53:29
hanzz.k@gmail.com
Twitter: print bad xml if we cannot parse it
1 file changed with 14 insertions and 3 deletions:
0 comments (0 inline, 0 general)
backends/twitter/TwitterResponseParser.cpp
Show inline comments
 
@@ -128,145 +128,156 @@ DirectMessage getDirectMessage(const Swift::ParserElement::ref &element, const s
 
	DM.setSenderData( getUser(element->getChild(TwitterReponseTypes::sender, xmlns), xmlns) );
 
	DM.setRecipientData( getUser(element->getChild(TwitterReponseTypes::recipient, xmlns), xmlns) );
 
	return DM;
 
}
 

	
 
std::vector<Status> getTimeline(std::string &xml)
 
{
 
	std::vector<Status> statuses;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement == NULL) {
 
		LOG4CXX_ERROR(logger, "Error while parsing XML")
 
        LOG4CXX_ERROR(logger, xml)
 
		return statuses;
 
	}
 

	
 
	if(rootElement->getName() != "statuses") {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to timeline")
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to timeline:")
 
        LOG4CXX_ERROR(logger, xml)
 
		return statuses;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::status, xmlns);
 

	
 
	for(int i = 0; i <  children.size() ; i++) {
 
		const Swift::ParserElement::ref status = children[i];
 
		statuses.push_back(getStatus(status, xmlns));
 
	}
 
	return statuses;
 
}
 

	
 
std::vector<DirectMessage> getDirectMessages(std::string &xml)
 
{
 
	std::vector<DirectMessage> DMs;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement == NULL) {
 
		LOG4CXX_ERROR(logger, "Error while parsing XML")
 
        LOG4CXX_ERROR(logger, xml)
 
		return DMs;
 
	}
 
	
 
	if(rootElement->getName() != TwitterReponseTypes::directmessages) {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to direct-messages")
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to direct-messages:")
 
        LOG4CXX_ERROR(logger, xml)
 
		return DMs;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::direct_message, xmlns);
 

	
 
	for(int i = 0; i <  children.size() ; i++) {
 
		const Swift::ParserElement::ref dm = children[i];
 
		DMs.push_back(getDirectMessage(dm, xmlns));
 
	}
 
	return DMs;
 
}
 

	
 
std::vector<User> getUsers(std::string &xml)
 
{
 
	std::vector<User> users;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement == NULL) {
 
		LOG4CXX_ERROR(logger, "Error while parsing XML")
 
        LOG4CXX_ERROR(logger, xml)
 
		return users;
 
	}
 

	
 
	if(rootElement->getName() != TwitterReponseTypes::users) {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to user list")
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to user list:")
 
        LOG4CXX_ERROR(logger, xml)
 
		return users;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const std::vector<Swift::ParserElement::ref> children = rootElement->getChildren(TwitterReponseTypes::user, xmlns);
 

	
 
	for(int i = 0 ; i < children.size() ; i++) {
 
		const Swift::ParserElement::ref user = children[i];
 
		users.push_back(getUser(user, xmlns));
 
	}
 
	return users;
 
}
 

	
 
User getUser(std::string &xml)
 
{
 
	User user;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 
	
 
	if(rootElement == NULL) {
 
		LOG4CXX_ERROR(logger, "Error while parsing XML")
 
        LOG4CXX_ERROR(logger, xml)
 
		return user;
 
	}
 

	
 
	if(rootElement->getName() != TwitterReponseTypes::user) {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to user object")
 
        LOG4CXX_ERROR(logger, xml)
 
		return user;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	return user = getUser(rootElement, xmlns);
 
}
 

	
 
std::vector<std::string> getIDs(std::string &xml)
 
{
 
	std::vector<std::string> IDs;
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 

	
 
	if(rootElement == NULL) {
 
		LOG4CXX_ERROR(logger, "Error while parsing XML")
 
        LOG4CXX_ERROR(logger, xml)
 
		return IDs;
 
	}
 

	
 
	if(rootElement->getName() != TwitterReponseTypes::id_list) {
 
		LOG4CXX_ERROR(logger, "XML doesn't correspond to id_list");
 
        LOG4CXX_ERROR(logger, xml)
 
		return IDs;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const std::vector<Swift::ParserElement::ref> ids = rootElement->getChild(TwitterReponseTypes::ids, xmlns)->getChildren(TwitterReponseTypes::id, xmlns);
 
	
 
	for(int i=0 ; i<ids.size() ; i++) {
 
		IDs.push_back(std::string( ids[i]->getText() ));
 
	}
 
	return IDs;
 
}
 

	
 
Error getErrorMessage(std::string &xml)
 
{
 
	std::string error = "";
 
	std::string code = "0";
 
	Error resp;
 

	
 
	Swift::ParserElement::ref rootElement = Swift::StringTreeParser::parse(xml);
 

	
 
	if(rootElement == NULL) {
 
		LOG4CXX_ERROR(logger, "Error while parsing XML");
 
        LOG4CXX_ERROR(logger, xml)
 
		return resp;
 
	}
 

	
 
	const std::string xmlns = rootElement->getNamespace();
 
	const Swift::ParserElement::ref errorElement = rootElement->getChild(TwitterReponseTypes::error, xmlns);
 
	Swift::AttributeMap attributes = errorElement->getAttributes();
 
	
 
	if(errorElement != NULL) {
 
		error = errorElement->getText();
 
		code = (errorElement->getAttributes()).getAttribute("code");
 
	}
 

	
0 comments (0 inline, 0 general)