mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 06:06:10 -04:00
FeedReader plugin
- reserved service id - reworked error codes - added xpath manipulation and basic gui elements in preview dialog git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-gxs-b1@5514 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
08904bf82f
commit
c7ed9c6df7
23 changed files with 1595 additions and 430 deletions
|
@ -32,7 +32,7 @@ bool HTMLWrapper::readHTML(const char *html, const char *url)
|
|||
{
|
||||
cleanup();
|
||||
|
||||
mDocument = htmlReadMemory(html, strlen(html), url, "", HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING/* | HTML_PARSE_COMPACT*/);
|
||||
mDocument = htmlReadMemory(html, strlen(html), url, "", HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING | HTML_PARSE_COMPACT | HTML_PARSE_NONET | HTML_PARSE_NOBLANKS);
|
||||
if (mDocument) {
|
||||
return true;
|
||||
}
|
||||
|
@ -58,3 +58,18 @@ bool HTMLWrapper::saveHTML(std::string &html)
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool HTMLWrapper::createHTML()
|
||||
{
|
||||
/* easy way */
|
||||
return readHTML("<html><body></body></html>", "");
|
||||
}
|
||||
|
||||
xmlNodePtr HTMLWrapper::getBody()
|
||||
{
|
||||
xmlNodePtr root = getRootElement();
|
||||
if (!root) {
|
||||
return NULL;
|
||||
}
|
||||
return findNode(root->children, "body", false);
|
||||
}
|
||||
|
|
|
@ -31,6 +31,10 @@ public:
|
|||
|
||||
bool readHTML(const char *html, const char *url);
|
||||
bool saveHTML(std::string &html);
|
||||
|
||||
bool createHTML();
|
||||
|
||||
xmlNodePtr getBody();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#include <string.h>
|
||||
|
||||
#include "XMLWrapper.h"
|
||||
#include "XPathWrapper.h"
|
||||
|
||||
XMLWrapper::XMLWrapper()
|
||||
{
|
||||
|
@ -41,6 +42,18 @@ XMLWrapper::~XMLWrapper()
|
|||
xmlCharEncCloseFunc(mCharEncodingHandler);
|
||||
}
|
||||
|
||||
XMLWrapper &XMLWrapper::operator=(const XMLWrapper &xml)
|
||||
{
|
||||
cleanup();
|
||||
|
||||
const xmlDocPtr document = xml.getDocument();
|
||||
if (document) {
|
||||
mDocument = xmlCopyDoc(document, 1);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void XMLWrapper::cleanup()
|
||||
{
|
||||
if (mDocument) {
|
||||
|
@ -73,7 +86,7 @@ bool XMLWrapper::convertFromString(const char *text, xmlChar *&xmlText)
|
|||
|
||||
xmlBufferPtr in = xmlBufferCreateStatic((void*) text, strlen(text));
|
||||
xmlBufferPtr out = xmlBufferCreate();
|
||||
int ret = xmlCharEncOutFunc(mCharEncodingHandler, out, in);
|
||||
int ret = xmlCharEncInFunc(mCharEncodingHandler, out, in);
|
||||
if (ret >= 0) {
|
||||
result = true;
|
||||
xmlText = xmlBufferDetach(out);
|
||||
|
@ -85,7 +98,12 @@ bool XMLWrapper::convertFromString(const char *text, xmlChar *&xmlText)
|
|||
return result;
|
||||
}
|
||||
|
||||
xmlNodePtr XMLWrapper::getRootElement()
|
||||
xmlDocPtr XMLWrapper::getDocument() const
|
||||
{
|
||||
return mDocument;
|
||||
}
|
||||
|
||||
xmlNodePtr XMLWrapper::getRootElement() const
|
||||
{
|
||||
if (mDocument) {
|
||||
return xmlDocGetRootElement(mDocument);
|
||||
|
@ -98,7 +116,7 @@ bool XMLWrapper::readXML(const char *xml)
|
|||
{
|
||||
cleanup();
|
||||
|
||||
mDocument = xmlReadDoc((const xmlChar*) xml, "", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING/* | XML_PARSE_COMPACT | XML_PARSE_NOCDATA*/);
|
||||
mDocument = xmlReadDoc(BAD_CAST xml, "", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_COMPACT | XML_PARSE_NOENT | XML_PARSE_NOCDATA);
|
||||
if (mDocument) {
|
||||
return true;
|
||||
}
|
||||
|
@ -114,7 +132,7 @@ bool XMLWrapper::getContent(xmlNodePtr node, std::string &content)
|
|||
return false;
|
||||
}
|
||||
|
||||
xmlChar *xmlContent = xmlNodeListGetString(mDocument, node, 1);
|
||||
xmlChar *xmlContent = xmlNodeGetContent(node);
|
||||
if (!xmlContent) {
|
||||
return true;
|
||||
}
|
||||
|
@ -125,6 +143,23 @@ bool XMLWrapper::getContent(xmlNodePtr node, std::string &content)
|
|||
return result;
|
||||
}
|
||||
|
||||
bool XMLWrapper::setContent(xmlNodePtr node, const char *content)
|
||||
{
|
||||
if (!node) {
|
||||
return false;
|
||||
}
|
||||
|
||||
xmlChar *xmlContent;
|
||||
if (!convertFromString(content, xmlContent)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
xmlNodeSetContent(node, xmlContent);
|
||||
xmlFree(xmlContent);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string XMLWrapper::nodeName(xmlNodePtr node)
|
||||
{
|
||||
std::string name;
|
||||
|
@ -150,7 +185,7 @@ std::string XMLWrapper::attrName(xmlAttrPtr attr)
|
|||
xmlNodePtr XMLWrapper::findNode(xmlNodePtr node, const char *name, bool children)
|
||||
{
|
||||
if (node->name) {
|
||||
if (xmlStrcasecmp(node->name, (xmlChar*) name) == 0) {
|
||||
if (xmlStrEqual(node->name, BAD_CAST name)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
@ -218,7 +253,7 @@ std::string XMLWrapper::getAttr(xmlNodePtr node, const char *name)
|
|||
|
||||
std::string value;
|
||||
|
||||
xmlChar *xmlValue = xmlGetProp(node, (const xmlChar*) name);
|
||||
xmlChar *xmlValue = xmlGetProp(node, BAD_CAST name);
|
||||
if (xmlValue) {
|
||||
convertToString(xmlValue, value);
|
||||
xmlFree(xmlValue);
|
||||
|
@ -238,8 +273,17 @@ bool XMLWrapper::setAttr(xmlNodePtr node, const char *name, const char *value)
|
|||
return false;
|
||||
}
|
||||
|
||||
xmlAttrPtr xmlAttr = xmlSetProp (node, (const xmlChar*) name, xmlValue);
|
||||
xmlAttrPtr xmlAttr = xmlSetProp (node, BAD_CAST name, xmlValue);
|
||||
xmlFree(xmlValue);
|
||||
|
||||
return xmlAttr != NULL;
|
||||
}
|
||||
|
||||
XPathWrapper *XMLWrapper::createXPath()
|
||||
{
|
||||
if (mDocument) {
|
||||
return new XPathWrapper(*this);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -25,16 +25,23 @@
|
|||
#include <string>
|
||||
#include <libxml/parser.h>
|
||||
|
||||
class XPathWrapper;
|
||||
|
||||
class XMLWrapper
|
||||
{
|
||||
public:
|
||||
XMLWrapper();
|
||||
~XMLWrapper();
|
||||
|
||||
XMLWrapper &operator=(const XMLWrapper &xml);
|
||||
|
||||
void cleanup();
|
||||
|
||||
bool readXML(const char *xml);
|
||||
|
||||
xmlDocPtr getDocument() const;
|
||||
xmlNodePtr getRootElement() const;
|
||||
|
||||
std::string nodeName(xmlNodePtr node);
|
||||
std::string attrName(xmlAttrPtr attr);
|
||||
|
||||
|
@ -42,19 +49,20 @@ public:
|
|||
bool getChildText(xmlNodePtr node, const char *childName, std::string &text);
|
||||
|
||||
bool getContent(xmlNodePtr node, std::string &content);
|
||||
bool setContent(xmlNodePtr node, const char *content);
|
||||
|
||||
std::string getAttr(xmlNodePtr node, xmlAttrPtr attr);
|
||||
std::string getAttr(xmlNodePtr node, const char *name);
|
||||
bool setAttr(xmlNodePtr node, const char *name, const char *value);
|
||||
|
||||
xmlNodePtr getRootElement();
|
||||
XPathWrapper *createXPath();
|
||||
|
||||
bool convertToString(const xmlChar *xmlText, std::string &text);
|
||||
bool convertFromString(const char *text, xmlChar *&xmlText);
|
||||
|
||||
protected:
|
||||
xmlDocPtr mDocument;
|
||||
xmlCharEncodingHandlerPtr mCharEncodingHandler;
|
||||
|
||||
bool convertToString(const xmlChar *xmlText, std::string &text);
|
||||
bool convertFromString(const char *text, xmlChar *&xmlText);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
102
plugins/FeedReader/util/XPathWrapper.cpp
Normal file
102
plugins/FeedReader/util/XPathWrapper.cpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
/****************************************************************
|
||||
* RetroShare GUI is distributed under the following license:
|
||||
*
|
||||
* Copyright (C) 2012 by Thunder
|
||||
*
|
||||
* 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
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
****************************************************************/
|
||||
|
||||
#include "XPathWrapper.h"
|
||||
#include "XMLWrapper.h"
|
||||
|
||||
XPathWrapper::XPathWrapper(XMLWrapper &xmlWrapper) : mXMLWrapper(xmlWrapper)
|
||||
{
|
||||
mContext = NULL;
|
||||
mResult = NULL;
|
||||
}
|
||||
|
||||
XPathWrapper::~XPathWrapper()
|
||||
{
|
||||
cleanup();
|
||||
}
|
||||
|
||||
void XPathWrapper::cleanup()
|
||||
{
|
||||
if (mResult) {
|
||||
xmlXPathFreeObject(mResult);
|
||||
mResult = NULL;
|
||||
}
|
||||
if (mContext) {
|
||||
xmlXPathFreeContext(mContext);
|
||||
mContext = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool XPathWrapper::compile(const char *expression)
|
||||
{
|
||||
cleanup();
|
||||
|
||||
xmlDocPtr document = mXMLWrapper.getDocument();
|
||||
if (!document) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mContext = xmlXPathNewContext(document);
|
||||
if (!mContext) {
|
||||
cleanup();
|
||||
return false;
|
||||
}
|
||||
|
||||
xmlChar *xmlExpression = NULL;
|
||||
if (!mXMLWrapper.convertFromString(expression, xmlExpression)) {
|
||||
cleanup();
|
||||
return false;
|
||||
}
|
||||
mResult = xmlXPathEvalExpression(xmlExpression, mContext);
|
||||
xmlFree(xmlExpression);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned int XPathWrapper::count()
|
||||
{
|
||||
if (!mResult) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return mResult->nodesetval->nodeNr;
|
||||
}
|
||||
|
||||
xmlNodePtr XPathWrapper::node(unsigned int index)
|
||||
{
|
||||
if (!mResult) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (xmlXPathNodeSetIsEmpty(mResult->nodesetval)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (index >= (unsigned int) mResult->nodesetval->nodeNr) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return mResult->nodesetval->nodeTab[index];
|
||||
}
|
51
plugins/FeedReader/util/XPathWrapper.h
Normal file
51
plugins/FeedReader/util/XPathWrapper.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
/****************************************************************
|
||||
* RetroShare GUI is distributed under the following license:
|
||||
*
|
||||
* Copyright (C) 2012 by Thunder
|
||||
*
|
||||
* 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
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
****************************************************************/
|
||||
|
||||
#ifndef XPATHWRAPPER
|
||||
#define XPATHWRAPPER
|
||||
|
||||
#include <libxml/xpath.h>
|
||||
|
||||
class XMLWrapper;
|
||||
|
||||
class XPathWrapper
|
||||
{
|
||||
friend class XMLWrapper;
|
||||
|
||||
public:
|
||||
~XPathWrapper();
|
||||
|
||||
void cleanup();
|
||||
|
||||
bool compile(const char *expression);
|
||||
|
||||
unsigned int count();
|
||||
xmlNodePtr node(unsigned int index);
|
||||
|
||||
protected:
|
||||
XPathWrapper(XMLWrapper &xmlWrapper);
|
||||
|
||||
XMLWrapper &mXMLWrapper;
|
||||
xmlXPathContextPtr mContext;
|
||||
xmlXPathObjectPtr mResult;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue