From 3f7c91db1d0cbfa70f8e4401a60963f05b0d68cd Mon Sep 17 00:00:00 2001 From: Michael Purcaro Date: Sun, 2 Mar 2014 13:10:54 -0500 Subject: [PATCH] add clang-format and etags scripts; re=indent code --- scripts/clang-format.sh | 5 + scripts/etags.py | 14 +++ src/Curl.cpp | 251 +++++++++++++++++++------------------ src/Curl.hpp | 79 ++++++------ src/Entry.cpp | 152 ++++++++++------------- src/Entry.hpp | 80 ++++++------ src/Feed.cpp | 258 +++++++++++++++++---------------------- src/Feed.hpp | 149 +++++++++++----------- src/FeedConfig.cpp | 21 ++-- src/FeedConfig.hpp | 23 ++-- src/MemParseHandlers.cpp | 24 ++-- src/MemParseHandlers.hpp | 19 ++- src/Utils.cpp | 42 +++---- src/Utils.hpp | 31 +++-- src/Validator.cpp | 211 +++++++++++++++----------------- src/Validator.hpp | 35 +++--- src/XmlGlobalState.cpp | 43 +++---- src/XmlGlobalState.hpp | 20 ++- src/export_cfg.hpp | 32 +++-- 19 files changed, 710 insertions(+), 779 deletions(-) create mode 100755 scripts/clang-format.sh create mode 100755 scripts/etags.py diff --git a/scripts/clang-format.sh b/scripts/clang-format.sh new file mode 100755 index 0000000..3534abe --- /dev/null +++ b/scripts/clang-format.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +DIRS="src" + +find $DIRS -regex ".*\.[cChH]\(pp\)?" | xargs clang-format-3.4 -i --style=Google diff --git a/scripts/etags.py b/scripts/etags.py new file mode 100755 index 0000000..5604d49 --- /dev/null +++ b/scripts/etags.py @@ -0,0 +1,14 @@ +#!/usr/bin/python + +import os + +path = os.path.join(os.path.dirname(__file__), "../") +path = os.path.abspath(path) + +regex = '-regex ".*\.[cChH]\(pp\)?"' +exclude = '-not -path "*/external/*" -not -name "*#*"' +cmd = 'find {p} {r} {e} -print | xargs etags '.format(p=path, e=exclude, r=regex) + +print cmd + +os.system(cmd) diff --git a/src/Curl.cpp b/src/Curl.cpp index 315e987..d38034f 100644 --- a/src/Curl.cpp +++ b/src/Curl.cpp @@ -35,132 +35,127 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -namespace cURL -{ - GlobalState::GlobalState() - { - curl_global_init(CURL_GLOBAL_ALL); - } - - GlobalState::~GlobalState() - { - curl_global_cleanup(); - } - - EasyInterface::EasyInterface(const std::string& url, const std::string& etag, bool compress) : - m_curlHandle(curl_easy_init()), - m_url(url), - m_requestHeaders(nullptr), - m_responseData(), - m_responseHeaders() - { - m_responseHeaders.reserve(20); - - curl_easy_setopt(m_curlHandle, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 1); - curl_easy_setopt(m_curlHandle, CURLOPT_URL, m_url.c_str()); - curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYPEER, 0); // disable certificates - curl_easy_setopt(m_curlHandle, CURLOPT_ERRORBUFFER, m_errorBuffer); // error buffer - curl_easy_setopt(m_curlHandle, CURLOPT_FOLLOWLOCATION, 1); // follow redirects - curl_easy_setopt(m_curlHandle, CURLOPT_MAXREDIRS, CURL_MAX_REDIRECTS); // number of redirects to follow - //curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 1); // VERBROSE - - if (compress) - { - curl_easy_setopt(m_curlHandle, CURLOPT_ENCODING, "deflate"); // request zlib compression - } - - // set up a minimum transfer rate - less then a 100 bytes in 5 minutes is a failure - curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_LIMIT, 100); - curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_TIME , 300); - - if (!etag.empty()) - { - std::string etagHeader("If-None-Match: "); - etagHeader += etag; - m_requestHeaders = curl_slist_append(m_requestHeaders, etagHeader.c_str()); - curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, m_requestHeaders); - } - - curl_easy_setopt(m_curlHandle, CURLOPT_HEADERFUNCTION, &EasyInterface::HeaderFunction); - curl_easy_setopt(m_curlHandle, CURLOPT_HEADERDATA, this); - - curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, &EasyInterface::WriteFunction); - curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this); - } - - EasyInterface::~EasyInterface() - { - if (m_requestHeaders) - curl_slist_free_all(m_requestHeaders); - curl_easy_cleanup(m_curlHandle); - } - - void EasyInterface::SetProgressOn() - { - curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 0); - } - - const std::string& EasyInterface::GetResponseData() const - { - return m_responseData; - } - - const std::vector& EasyInterface::GetResponseHeaders() const - { - return m_responseHeaders; - } - - long EasyInterface::PerformRequest() - { - return curl_easy_perform(m_curlHandle) == CURLE_OK ? GetResponseCode() : 0; - } - - std::string EasyInterface::GetResponseEtag() const - { - for (std::vector::const_iterator i = m_responseHeaders.begin(); i != m_responseHeaders.end(); ++i) - if (boost::algorithm::istarts_with(*i, "etag:")) - return i->substr(6, i->size() - 8); // subtract 2 extra bytes for the trailing CRLF - return std::string(); - } - - size_t EasyInterface::WriteFunction(void* data, size_t mult, size_t size, void* state) - { - const size_t dataSize = size * mult; - if (dataSize && state) - reinterpret_cast(state)->HandleWriteData(reinterpret_cast(data), dataSize); - return dataSize; - } - - size_t EasyInterface::HeaderFunction(void* data, size_t mult, size_t size, void* state) - { - const size_t dataSize = size * mult; - if (dataSize && state) - reinterpret_cast(state)->HandleHeaderData(reinterpret_cast(data), dataSize); - return dataSize; - } - - long EasyInterface::GetResponseCode() const - { - long code = 0; - return curl_easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &code) == CURLE_OK ? code : 0; - } - - void EasyInterface::HandleWriteData(const char* data, const size_t dataSize) - { - m_responseData.append(data, dataSize); - } - - void EasyInterface::HandleHeaderData(const char* data, const size_t dataSize) - { - m_responseHeaders.push_back(std::string(data, dataSize)); - - data = m_responseHeaders.back().c_str(); - if (!boost::algorithm::istarts_with(data, "content-length:")) - return; - - int contentLength = std::atoi(data + 16); - if (contentLength) - m_responseData.reserve(contentLength); - } +namespace cURL { +GlobalState::GlobalState() { curl_global_init(CURL_GLOBAL_ALL); } + +GlobalState::~GlobalState() { curl_global_cleanup(); } + +EasyInterface::EasyInterface(const std::string& url, const std::string& etag, + bool compress) + : m_curlHandle(curl_easy_init()), + m_url(url), + m_requestHeaders(nullptr), + m_responseData(), + m_responseHeaders() { + m_responseHeaders.reserve(20); + + curl_easy_setopt(m_curlHandle, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 1); + curl_easy_setopt(m_curlHandle, CURLOPT_URL, m_url.c_str()); + curl_easy_setopt(m_curlHandle, CURLOPT_SSL_VERIFYPEER, + 0); // disable certificates + curl_easy_setopt(m_curlHandle, CURLOPT_ERRORBUFFER, + m_errorBuffer); // error buffer + curl_easy_setopt(m_curlHandle, CURLOPT_FOLLOWLOCATION, + 1); // follow redirects + curl_easy_setopt(m_curlHandle, CURLOPT_MAXREDIRS, + CURL_MAX_REDIRECTS); // number of redirects to follow + // curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 1); // + // VERBROSE + + if (compress) { + curl_easy_setopt(m_curlHandle, CURLOPT_ENCODING, + "deflate"); // request zlib compression + } + + // set up a minimum transfer rate - less then a 100 bytes in 5 minutes is a + // failure + curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_LIMIT, 100); + curl_easy_setopt(m_curlHandle, CURLOPT_LOW_SPEED_TIME, 300); + + if (!etag.empty()) { + std::string etagHeader("If-None-Match: "); + etagHeader += etag; + m_requestHeaders = curl_slist_append(m_requestHeaders, etagHeader.c_str()); + curl_easy_setopt(m_curlHandle, CURLOPT_HTTPHEADER, m_requestHeaders); + } + + curl_easy_setopt(m_curlHandle, CURLOPT_HEADERFUNCTION, + &EasyInterface::HeaderFunction); + curl_easy_setopt(m_curlHandle, CURLOPT_HEADERDATA, this); + + curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, + &EasyInterface::WriteFunction); + curl_easy_setopt(m_curlHandle, CURLOPT_WRITEDATA, this); +} + +EasyInterface::~EasyInterface() { + if (m_requestHeaders) curl_slist_free_all(m_requestHeaders); + curl_easy_cleanup(m_curlHandle); +} + +void EasyInterface::SetProgressOn() { + curl_easy_setopt(m_curlHandle, CURLOPT_NOPROGRESS, 0); +} + +const std::string& EasyInterface::GetResponseData() const { + return m_responseData; +} + +const std::vector& EasyInterface::GetResponseHeaders() const { + return m_responseHeaders; +} + +long EasyInterface::PerformRequest() { + return curl_easy_perform(m_curlHandle) == CURLE_OK ? GetResponseCode() : 0; +} + +std::string EasyInterface::GetResponseEtag() const { + for (std::vector::const_iterator i = m_responseHeaders.begin(); + i != m_responseHeaders.end(); ++i) + if (boost::algorithm::istarts_with(*i, "etag:")) + return i->substr( + 6, i->size() - 8); // subtract 2 extra bytes for the trailing CRLF + return std::string(); +} + +size_t EasyInterface::WriteFunction(void* data, size_t mult, size_t size, + void* state) { + const size_t dataSize = size * mult; + if (dataSize && state) + reinterpret_cast(state) + ->HandleWriteData(reinterpret_cast(data), dataSize); + return dataSize; +} + +size_t EasyInterface::HeaderFunction(void* data, size_t mult, size_t size, + void* state) { + const size_t dataSize = size * mult; + if (dataSize && state) + reinterpret_cast(state) + ->HandleHeaderData(reinterpret_cast(data), dataSize); + return dataSize; +} + +long EasyInterface::GetResponseCode() const { + long code = 0; + return curl_easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &code) == + CURLE_OK + ? code + : 0; +} + +void EasyInterface::HandleWriteData(const char* data, const size_t dataSize) { + m_responseData.append(data, dataSize); +} + +void EasyInterface::HandleHeaderData(const char* data, const size_t dataSize) { + m_responseHeaders.push_back(std::string(data, dataSize)); + + data = m_responseHeaders.back().c_str(); + if (!boost::algorithm::istarts_with(data, "content-length:")) return; + + int contentLength = std::atoi(data + 16); + if (contentLength) m_responseData.reserve(contentLength); +} } diff --git a/src/Curl.hpp b/src/Curl.hpp index 0dc0d16..19009a8 100644 --- a/src/Curl.hpp +++ b/src/Curl.hpp @@ -40,46 +40,51 @@ POSSIBILITY OF SUCH DAMAGE. #include "export_cfg.hpp" #define nullptr 0 -namespace cURL -{ - static const int CURL_MAX_REDIRECTS = 10; +namespace cURL { +static const int CURL_MAX_REDIRECTS = 10; - class GlobalState - { - public: - GlobalState(); - ~GlobalState(); +class GlobalState { + public: + GlobalState(); + ~GlobalState(); - private: - GlobalState(const GlobalState& globalState) { } // disallow copying - }; + private: + GlobalState(const GlobalState& globalState) {} // disallow copying +}; - class EasyInterface - { - public: - EasyInterface(const std::string& url,const std::string& etag, bool compress = false); - ~EasyInterface(); +class EasyInterface { + public: + EasyInterface(const std::string& url, const std::string& etag, + bool compress = false); + ~EasyInterface(); - const std::string& GetResponseData() const; - const std::vector< std::string >& GetResponseHeaders() const; - long PerformRequest(); - std::string GetResponseEtag() const; - void SetProgressOn(); - const char* GetErrors() {return m_errorBuffer;}; - private: - EasyInterface(const EasyInterface& easy) : m_curlHandle(nullptr) { } // disallow copying - static size_t WriteFunction(void* data, size_t mult, size_t size, void* state); - static size_t HeaderFunction(void* data, size_t mult, size_t size, void* state); - long GetResponseCode() const; - void HandleWriteData(const char* data, const size_t dataSize); - void HandleHeaderData(const char* data, const size_t dataSize); - private: - CURL* const m_curlHandle; - const std::string m_url; - curl_slist* m_requestHeaders; - std::string m_responseData; - std::vector< std::string > m_responseHeaders; - char m_errorBuffer[CURL_ERROR_SIZE]; - }; + const std::string& GetResponseData() const; + const std::vector& GetResponseHeaders() const; + long PerformRequest(); + std::string GetResponseEtag() const; + void SetProgressOn(); + const char* GetErrors() { + return m_errorBuffer; + }; + + private: + EasyInterface(const EasyInterface& easy) + : m_curlHandle(nullptr) {} // disallow copying + static size_t WriteFunction(void* data, size_t mult, size_t size, + void* state); + static size_t HeaderFunction(void* data, size_t mult, size_t size, + void* state); + long GetResponseCode() const; + void HandleWriteData(const char* data, const size_t dataSize); + void HandleHeaderData(const char* data, const size_t dataSize); + + private: + CURL* const m_curlHandle; + const std::string m_url; + curl_slist* m_requestHeaders; + std::string m_responseData; + std::vector m_responseHeaders; + char m_errorBuffer[CURL_ERROR_SIZE]; +}; } #endif diff --git a/src/Entry.cpp b/src/Entry.cpp index 085098a..91e3643 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -41,91 +41,71 @@ POSSIBILITY OF SUCH DAMAGE. #include #include "Feed.hpp" -namespace FeedReader -{ - Entry::Entry(const xercesc_2_8::DOMNode* const entryNode) : - UniqueId(), - Published(boost::date_time::min_date_time), - IsLive(true) - { - ParseNodeData(entryNode); - } - - Entry::Entry(const Entry& other) - { - UniqueId = other.UniqueId; - Published = other.Published; - IsLive = other.IsLive; - m_entryElements = other.m_entryElements; - } - - Entry& Entry::operator= (const Entry& other) - { - if (this != &other) - { - UniqueId = other.UniqueId; - Published = other.Published; - IsLive = other.IsLive; - m_entryElements = other.m_entryElements; - } - return *this; - } - - void Entry::ParseNodeData(const xercesc_2_8::DOMNode* const entryNode) - { - const xercesc_2_8::DOMNodeList* const children = entryNode->getChildNodes(); - for (XMLSize_t i = 0, listLength = children->getLength(); i < listLength; ++i) - { - const xercesc_2_8::DOMNode* const node = children->item(i); - const std::string nodeName = XmlCharsToStdString(node->getNodeName()); - const std::string textContent = XmlCharsToStdString(node->getTextContent()); - - if (nodeName == XERCESC_EMPTY_NODE_NAME) - { - continue; - } - if (xercesc_2_8::XMLString::equals(nodeName.c_str(), "entryUniqueId")) - { - UniqueId = textContent; - m_entryElements["entryUniqueId"] = textContent; - } - else if (xercesc_2_8::XMLString::equals(nodeName.c_str(), "publishDateTime")) - { - Published = boost::posix_time::microsec_clock::universal_time(); - m_entryElements["publishDateTime"] = textContent; - } - else - { - m_entryElements[nodeName] = textContent; - } - } - - if (UniqueId.empty()) - { - UniqueId = m_entryElements["url"]; - } - } - - const std::string& Entry::operator[](const std::string& node) const - { - EntryElements::const_iterator itr = m_entryElements.find(node); - if (itr == m_entryElements.end()) - { - static const std::string empty = ""; - return empty; - } - - return itr->second; - } - - void Entry::Print(std::ostream& os) const - { - for (EntryElements::const_iterator itr = m_entryElements.begin(); - itr != m_entryElements.end(); - itr++) - { - os << itr->first << ": '" << itr->second << "'" << std::endl; - } - } +namespace FeedReader { +Entry::Entry(const xercesc_2_8::DOMNode* const entryNode) + : UniqueId(), Published(boost::date_time::min_date_time), IsLive(true) { + ParseNodeData(entryNode); } +Entry::Entry(const Entry& other) { + UniqueId = other.UniqueId; + Published = other.Published; + IsLive = other.IsLive; + m_entryElements = other.m_entryElements; +} + +Entry& Entry::operator=(const Entry& other) { + if (this != &other) { + UniqueId = other.UniqueId; + Published = other.Published; + IsLive = other.IsLive; + m_entryElements = other.m_entryElements; + } + return *this; +} + +void Entry::ParseNodeData(const xercesc_2_8::DOMNode* const entryNode) { + const xercesc_2_8::DOMNodeList* const children = entryNode->getChildNodes(); + for (XMLSize_t i = 0, listLength = children->getLength(); i < listLength; + ++i) { + const xercesc_2_8::DOMNode* const node = children->item(i); + const std::string nodeName = XmlCharsToStdString(node->getNodeName()); + const std::string textContent = XmlCharsToStdString(node->getTextContent()); + + if (nodeName == XERCESC_EMPTY_NODE_NAME) { + continue; + } + if (xercesc_2_8::XMLString::equals(nodeName.c_str(), "entryUniqueId")) { + UniqueId = textContent; + m_entryElements["entryUniqueId"] = textContent; + } else if (xercesc_2_8::XMLString::equals(nodeName.c_str(), + "publishDateTime")) { + Published = boost::posix_time::microsec_clock::universal_time(); + m_entryElements["publishDateTime"] = textContent; + } else { + m_entryElements[nodeName] = textContent; + } + } + + if (UniqueId.empty()) { + UniqueId = m_entryElements["url"]; + } +} + +const std::string& Entry::operator[](const std::string& node) const { + EntryElements::const_iterator itr = m_entryElements.find(node); + if (itr == m_entryElements.end()) { + static const std::string empty = ""; + return empty; + } + + return itr->second; +} + +void Entry::Print(std::ostream& os) const { + for (EntryElements::const_iterator itr = m_entryElements.begin(); + itr != m_entryElements.end(); itr++) { + os << itr->first << ": '" << itr->second << "'" << std::endl; + } +} +} diff --git a/src/Entry.hpp b/src/Entry.hpp index 6db3187..cd3015b 100644 --- a/src/Entry.hpp +++ b/src/Entry.hpp @@ -40,43 +40,49 @@ POSSIBILITY OF SUCH DAMAGE. #include -namespace FeedReader -{ - static const std::string XERCESC_EMPTY_NODE_NAME = "#text"; - - class FEED_EXPORT Entry - { - private: - typedef std::map< std::string, std::string > EntryElements; - EntryElements m_entryElements; - - public: - std::string UniqueId; - boost::posix_time::ptime Published; - bool IsLive; - - private: - void ParseNodeData(const ::xercesc_2_8::DOMNode* const entryNode); - - public: - Entry(const xercesc_2_8::DOMNode* const entryNode); - Entry(const Entry& other); - Entry& operator= (const Entry& other); - - const std::string& operator[](const std::string& node) const; - void Print(std::ostream& os) const; - - // iterator interface to entry elements - typedef EntryElements::const_iterator entry_element_iterator; - typedef EntryElements::const_reverse_iterator reverse_entry_element_iterator; - - entry_element_iterator begin_entry_elements() const { return m_entryElements.begin(); } - entry_element_iterator end_entry_elements() const { return m_entryElements.end(); } - reverse_entry_element_iterator rbegin_entry_elements() const { return m_entryElements.rbegin(); } - reverse_entry_element_iterator rend_entry_elements() const { return m_entryElements.rend(); } - - int num_entry_elements() const { return (int)m_entryElements.size(); } - }; +namespace FeedReader { +static const std::string XERCESC_EMPTY_NODE_NAME = "#text"; + +class FEED_EXPORT Entry { + private: + typedef std::map EntryElements; + EntryElements m_entryElements; + + public: + std::string UniqueId; + boost::posix_time::ptime Published; + bool IsLive; + + private: + void ParseNodeData(const ::xercesc_2_8::DOMNode* const entryNode); + + public: + Entry(const xercesc_2_8::DOMNode* const entryNode); + Entry(const Entry& other); + Entry& operator=(const Entry& other); + + const std::string& operator[](const std::string& node) const; + void Print(std::ostream& os) const; + + // iterator interface to entry elements + typedef EntryElements::const_iterator entry_element_iterator; + typedef EntryElements::const_reverse_iterator reverse_entry_element_iterator; + + entry_element_iterator begin_entry_elements() const { + return m_entryElements.begin(); + } + entry_element_iterator end_entry_elements() const { + return m_entryElements.end(); + } + reverse_entry_element_iterator rbegin_entry_elements() const { + return m_entryElements.rbegin(); + } + reverse_entry_element_iterator rend_entry_elements() const { + return m_entryElements.rend(); + } + + int num_entry_elements() const { return (int)m_entryElements.size(); } +}; } #endif diff --git a/src/Feed.cpp b/src/Feed.cpp index 4ae2243..fe29df3 100644 --- a/src/Feed.cpp +++ b/src/Feed.cpp @@ -43,153 +43,117 @@ POSSIBILITY OF SUCH DAMAGE. #include "Utils.hpp" #include "Validator.hpp" -namespace FeedReader -{ - bool Feed::m_initialized = false; +namespace FeedReader { +bool Feed::m_initialized = false; boost::recursive_mutex Feed::m_stateMutex; - Feed::Feed(const std::string& url, const FeedConfig& feedConfig) : - m_etag(), - m_url(url), - m_lastChecked(boost::date_time::min_date_time), - m_format(UNKNOWN_FEED_TYPE), - m_valid(false), - m_feedConfig(feedConfig) - { - m_entries.reserve(20); - } - - Feed::~Feed() - { - } - - const std::string& Feed::operator[](const std::string& node) const - { - FeedElements::const_iterator itr = m_feedElements.find(node); - if (itr == m_feedElements.end()) - { - static const std::string empty = ""; - return empty; - } - - return itr->second; - } - - const std::string& Feed::GetUrl() const - { - return m_url; - } - - const Entries& Feed::GetEntries() const - { - return m_entries; - } - - const boost::posix_time::ptime& Feed::GetLastChecked() const - { - return m_lastChecked; - } - - void Feed::CheckFeed() - { - m_lastChecked = boost::posix_time::microsec_clock::universal_time(); - cURL::EasyInterface easy(m_url, m_etag); - switch (easy.PerformRequest()) - { - case 200: - { - m_etag = easy.GetResponseEtag(); - FeedValidator validator(easy.GetResponseData(), *this); - validator.Validate(m_feedData); - m_valid = true; - if (m_valid) - { - CreateEntries(m_feedData); - } - return; - } - case 304: - { - m_valid = true; - return; - } - default: - { - m_entries.clear(); - m_valid = false; - } - } - } - - void Feed::CreateEntries(const std::string& feedData) - { - const xercesc_2_8::MemBufInputSource input(reinterpret_cast( - feedData.c_str()), feedData.size(), ""); - xercesc_2_8::XercesDOMParser parser; - parser.setValidationScheme(xercesc_2_8::XercesDOMParser::Val_Never); - parser.parse(input); - if (!parser.getDocument()->hasChildNodes() || - !parser.getDocument()->getChildNodes()->item(0)->hasChildNodes()) - { - return; - } - - const xercesc_2_8::DOMNodeList* const children = - parser.getDocument()->getChildNodes()->item(0)->getChildNodes(); - for (XMLSize_t i = 0, listLength = children->getLength(); i < listLength; ++i) - { - const xercesc_2_8::DOMNode* const node = children->item(i); - const std::string nodeName = XmlCharsToStdString(node->getNodeName()); - - if (nodeName == XERCESC_EMPTY_NODE_NAME) - { - continue; - } - if (xercesc_2_8::XMLString::equals(nodeName.c_str(), "entry")) - { - AddEntryToList(node); - } - else - { - const std::string textContent = XmlCharsToStdString(node->getTextContent()); - m_feedElements[nodeName] = textContent; - } - } - } - - void Feed::AddEntryToList(const Entry& entry) - { - const Entries::iterator findResult = std::find_if(m_entries.begin(), - m_entries.end(), - EntryUniqueIdPredicate(entry.UniqueId)); - if (findResult == m_entries.end()) - { - m_entries.push_back(entry); - } - else - { - *findResult = entry; - } - } - - void Feed::Initialize() - { - boost::recursive_mutex::scoped_lock lock(m_stateMutex); - if (!Feed::m_initialized) - { - static std::auto_ptr curlGlobal(new cURL::GlobalState()); - static std::auto_ptr xmlGlobal(new XmlGlobalState()); - Feed::m_initialized = true; - } - } - - void Feed::SetValidity(bool valid) - { - m_valid = valid; - } - - void Feed::SetFeedFormat(const FeedFormat& format) - { - m_format = format; - } +Feed::Feed(const std::string& url, const FeedConfig& feedConfig) + : m_etag(), + m_url(url), + m_lastChecked(boost::date_time::min_date_time), + m_format(UNKNOWN_FEED_TYPE), + m_valid(false), + m_feedConfig(feedConfig) { + m_entries.reserve(20); +} + +Feed::~Feed() {} + +const std::string& Feed::operator[](const std::string& node) const { + FeedElements::const_iterator itr = m_feedElements.find(node); + if (itr == m_feedElements.end()) { + static const std::string empty = ""; + return empty; + } + + return itr->second; +} + +const std::string& Feed::GetUrl() const { return m_url; } + +const Entries& Feed::GetEntries() const { return m_entries; } + +const boost::posix_time::ptime& Feed::GetLastChecked() const { + return m_lastChecked; +} + +void Feed::CheckFeed() { + m_lastChecked = boost::posix_time::microsec_clock::universal_time(); + cURL::EasyInterface easy(m_url, m_etag); + switch (easy.PerformRequest()) { + case 200: { + m_etag = easy.GetResponseEtag(); + FeedValidator validator(easy.GetResponseData(), *this); + validator.Validate(m_feedData); + m_valid = true; + if (m_valid) { + CreateEntries(m_feedData); + } + return; + } + case 304: { + m_valid = true; + return; + } + default: { + m_entries.clear(); + m_valid = false; + } + } +} + +void Feed::CreateEntries(const std::string& feedData) { + const xercesc_2_8::MemBufInputSource input( + reinterpret_cast(feedData.c_str()), feedData.size(), ""); + xercesc_2_8::XercesDOMParser parser; + parser.setValidationScheme(xercesc_2_8::XercesDOMParser::Val_Never); + parser.parse(input); + if (!parser.getDocument()->hasChildNodes() || + !parser.getDocument()->getChildNodes()->item(0)->hasChildNodes()) { + return; + } + + const xercesc_2_8::DOMNodeList* const children = + parser.getDocument()->getChildNodes()->item(0)->getChildNodes(); + for (XMLSize_t i = 0, listLength = children->getLength(); i < listLength; + ++i) { + const xercesc_2_8::DOMNode* const node = children->item(i); + const std::string nodeName = XmlCharsToStdString(node->getNodeName()); + + if (nodeName == XERCESC_EMPTY_NODE_NAME) { + continue; + } + if (xercesc_2_8::XMLString::equals(nodeName.c_str(), "entry")) { + AddEntryToList(node); + } else { + const std::string textContent = + XmlCharsToStdString(node->getTextContent()); + m_feedElements[nodeName] = textContent; + } + } +} + +void Feed::AddEntryToList(const Entry& entry) { + const Entries::iterator findResult = + std::find_if(m_entries.begin(), m_entries.end(), + EntryUniqueIdPredicate(entry.UniqueId)); + if (findResult == m_entries.end()) { + m_entries.push_back(entry); + } else { + *findResult = entry; + } +} + +void Feed::Initialize() { + boost::recursive_mutex::scoped_lock lock(m_stateMutex); + if (!Feed::m_initialized) { + static std::auto_ptr curlGlobal(new cURL::GlobalState()); + static std::auto_ptr xmlGlobal(new XmlGlobalState()); + Feed::m_initialized = true; + } +} + +void Feed::SetValidity(bool valid) { m_valid = valid; } + +void Feed::SetFeedFormat(const FeedFormat& format) { m_format = format; } } diff --git a/src/Feed.hpp b/src/Feed.hpp index d5c0788..1071474 100644 --- a/src/Feed.hpp +++ b/src/Feed.hpp @@ -44,77 +44,82 @@ POSSIBILITY OF SUCH DAMAGE. #include "Entry.hpp" #include "FeedConfig.hpp" -namespace FeedReader -{ - typedef std::vector< Entry > Entries; - - // exception - class FEED_EXPORT feed_exception : public std::runtime_error - { - public: - explicit feed_exception(const std::string& what) - : std::runtime_error(what) {} - virtual ~feed_exception() throw() {} - }; - - class FEED_EXPORT Feed - { - public: - typedef std::map< std::string, std::string > FeedElements; - explicit Feed(const std::string& url = std::string(), - const FeedConfig& feedConfig = FeedConfig()); - ~Feed(); - - public: - const std::string& operator[](const std::string& node) const; - const std::string& GetUrl() const; - const Entries& GetEntries() const; - const ::boost::posix_time::ptime& GetLastChecked() const; - void SetValidity(bool valid); - void SetFeedFormat(const FeedFormat& format); - void CheckFeed(); - const FeedConfig& GetFeedConfig() {return m_feedConfig;} - - // iterator interface to entries - typedef Entries::const_iterator entry_iterator; - typedef Entries::const_reverse_iterator reverse_entry_iterator; - - entry_iterator begin_entries() const { return m_entries.begin(); } - entry_iterator end_entries() const { return m_entries.end(); } - reverse_entry_iterator rbegin_entries() const { return m_entries.rbegin(); } - reverse_entry_iterator rend_entries() const { return m_entries.rend(); } - - int num_entries() const { return (int)m_entries.size(); } - - // iterator interface to feed elements - typedef FeedElements::const_iterator feed_element_iterator; - typedef FeedElements::const_reverse_iterator reverse_feed_element_iterator; - - feed_element_iterator begin_feed_elements() const { return m_feedElements.begin(); } - feed_element_iterator end_feed_elements() const { return m_feedElements.end(); } - reverse_feed_element_iterator rbegin_feed_elements() const { return m_feedElements.rbegin(); } - reverse_feed_element_iterator rend_feed_elements() const { return m_feedElements.rend(); } - - int num_feed_elements() const { return (int)m_feedElements.size(); } - - // control - static void Initialize(); - - private: - void CreateEntries(const std::string& feedData); - void AddEntryToList(const Entry& entry); - private: - FeedElements m_feedElements; - std::string m_etag; - std::string m_url; - std::string m_feedData; - Entries m_entries; - boost::posix_time::ptime m_lastChecked; - static bool m_initialized; - static boost::recursive_mutex m_stateMutex; - FeedFormat m_format; - bool m_valid; - FeedConfig m_feedConfig; - }; +namespace FeedReader { +typedef std::vector Entries; + +// exception +class FEED_EXPORT feed_exception : public std::runtime_error { + public: + explicit feed_exception(const std::string& what) : std::runtime_error(what) {} + virtual ~feed_exception() throw() {} +}; + +class FEED_EXPORT Feed { + public: + typedef std::map FeedElements; + explicit Feed(const std::string& url = std::string(), + const FeedConfig& feedConfig = FeedConfig()); + ~Feed(); + + public: + const std::string& operator[](const std::string& node) const; + const std::string& GetUrl() const; + const Entries& GetEntries() const; + const ::boost::posix_time::ptime& GetLastChecked() const; + void SetValidity(bool valid); + void SetFeedFormat(const FeedFormat& format); + void CheckFeed(); + const FeedConfig& GetFeedConfig() { return m_feedConfig; } + + // iterator interface to entries + typedef Entries::const_iterator entry_iterator; + typedef Entries::const_reverse_iterator reverse_entry_iterator; + + entry_iterator begin_entries() const { return m_entries.begin(); } + entry_iterator end_entries() const { return m_entries.end(); } + reverse_entry_iterator rbegin_entries() const { return m_entries.rbegin(); } + reverse_entry_iterator rend_entries() const { return m_entries.rend(); } + + int num_entries() const { return (int)m_entries.size(); } + + // iterator interface to feed elements + typedef FeedElements::const_iterator feed_element_iterator; + typedef FeedElements::const_reverse_iterator reverse_feed_element_iterator; + + feed_element_iterator begin_feed_elements() const { + return m_feedElements.begin(); + } + feed_element_iterator end_feed_elements() const { + return m_feedElements.end(); + } + reverse_feed_element_iterator rbegin_feed_elements() const { + return m_feedElements.rbegin(); + } + reverse_feed_element_iterator rend_feed_elements() const { + return m_feedElements.rend(); + } + + int num_feed_elements() const { return (int)m_feedElements.size(); } + + // control + static void Initialize(); + + private: + void CreateEntries(const std::string& feedData); + void AddEntryToList(const Entry& entry); + + private: + FeedElements m_feedElements; + std::string m_etag; + std::string m_url; + std::string m_feedData; + Entries m_entries; + boost::posix_time::ptime m_lastChecked; + static bool m_initialized; + static boost::recursive_mutex m_stateMutex; + FeedFormat m_format; + bool m_valid; + FeedConfig m_feedConfig; +}; } #endif diff --git a/src/FeedConfig.cpp b/src/FeedConfig.cpp index 0056bef..ed6ccb9 100644 --- a/src/FeedConfig.cpp +++ b/src/FeedConfig.cpp @@ -32,16 +32,13 @@ POSSIBILITY OF SUCH DAMAGE. #include "FeedConfig.hpp" -namespace FeedReader -{ - FeedConfig::FeedConfig(const boost::filesystem::path& config_path) - : m_config_path(config_path) - { - // default feed types - m_feed_types.push_back(std::make_pair("RSS_20_xsl.xml","Rss20")); - m_feed_types.push_back(std::make_pair("Atom_10_xsl.xml","Atom10")); - m_feed_types.push_back(std::make_pair("Atom_03_xsl.xml","Atom03")); - m_feed_types.push_back(std::make_pair("RDF_xsl.xml","RDF")); - } +namespace FeedReader { +FeedConfig::FeedConfig(const boost::filesystem::path& config_path) + : m_config_path(config_path) { + // default feed types + m_feed_types.push_back(std::make_pair("RSS_20_xsl.xml", "Rss20")); + m_feed_types.push_back(std::make_pair("Atom_10_xsl.xml", "Atom10")); + m_feed_types.push_back(std::make_pair("Atom_03_xsl.xml", "Atom03")); + m_feed_types.push_back(std::make_pair("RDF_xsl.xml", "RDF")); +} } - diff --git a/src/FeedConfig.hpp b/src/FeedConfig.hpp index 80a8b46..26c889e 100644 --- a/src/FeedConfig.hpp +++ b/src/FeedConfig.hpp @@ -41,21 +41,20 @@ POSSIBILITY OF SUCH DAMAGE. #include "export_cfg.hpp" -namespace FeedReader -{ - typedef std::string FeedFormat; - typedef std::vector < std::pair > FeedTypes; +namespace FeedReader { +typedef std::string FeedFormat; +typedef std::vector > FeedTypes; - const static FeedFormat UNKNOWN_FEED_TYPE = "Unknown"; +const static FeedFormat UNKNOWN_FEED_TYPE = "Unknown"; - class FEED_EXPORT FeedConfig - { - public: - FeedConfig(const boost::filesystem::path& config_path = boost::filesystem::initial_path()); +class FEED_EXPORT FeedConfig { + public: + FeedConfig(const boost::filesystem::path& config_path = + boost::filesystem::initial_path()); - boost::filesystem::path m_config_path; - FeedTypes m_feed_types; - }; + boost::filesystem::path m_config_path; + FeedTypes m_feed_types; +}; } #endif diff --git a/src/MemParseHandlers.cpp b/src/MemParseHandlers.cpp index f2afe05..119842e 100644 --- a/src/MemParseHandlers.cpp +++ b/src/MemParseHandlers.cpp @@ -33,24 +33,16 @@ POSSIBILITY OF SUCH DAMAGE. #include "StdAfx.hpp" #include "MemParseHandlers.hpp" -MemParseHandlers::MemParseHandlers() : - xercesc_2_8::HandlerBase(), - _success(true) -{ -} +MemParseHandlers::MemParseHandlers() + : xercesc_2_8::HandlerBase(), _success(true) {} -bool MemParseHandlers::GetSuccess() -{ - return _success; -} +bool MemParseHandlers::GetSuccess() { return _success; } -void MemParseHandlers::error(const xercesc_2_8::SAXParseException& ex) -{ - _success = false; +void MemParseHandlers::error(const xercesc_2_8::SAXParseException& ex) { + _success = false; } -void MemParseHandlers::fatalError(const xercesc_2_8::SAXParseException& ex) -{ - const XMLCh* foo = ex.getMessage(); - _success = false; +void MemParseHandlers::fatalError(const xercesc_2_8::SAXParseException& ex) { + const XMLCh* foo = ex.getMessage(); + _success = false; } diff --git a/src/MemParseHandlers.hpp b/src/MemParseHandlers.hpp index 3c6d2f5..1915fd4 100644 --- a/src/MemParseHandlers.hpp +++ b/src/MemParseHandlers.hpp @@ -36,19 +36,18 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -class MemParseHandlers : public ::xercesc_2_8::HandlerBase -{ - bool _success; +class MemParseHandlers : public ::xercesc_2_8::HandlerBase { + bool _success; -public: - MemParseHandlers(); + public: + MemParseHandlers(); -public: - bool GetSuccess(); + public: + bool GetSuccess(); -public: - virtual void error(const ::xercesc_2_8::SAXParseException& ex); - virtual void fatalError(const ::xercesc_2_8::SAXParseException& ex); + public: + virtual void error(const ::xercesc_2_8::SAXParseException& ex); + virtual void fatalError(const ::xercesc_2_8::SAXParseException& ex); }; #endif diff --git a/src/Utils.cpp b/src/Utils.cpp index 7725376..12a43f4 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -39,35 +39,25 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +namespace FeedReader { -namespace FeedReader -{ - - std::string XmlCharsToStdString(const XMLCh* const xmlChars) - { - char* stdChars = xercesc_2_8::XMLString::transcode(xmlChars); - const std::string result = std::string(stdChars); - xercesc_2_8::XMLString::release(&stdChars); - return result; - } +std::string XmlCharsToStdString(const XMLCh* const xmlChars) { + char* stdChars = xercesc_2_8::XMLString::transcode(xmlChars); + const std::string result = std::string(stdChars); + xercesc_2_8::XMLString::release(&stdChars); + return result; +} - FeedUrlPredicate::FeedUrlPredicate(const std::string& url) : - _url(url) - { - } +FeedUrlPredicate::FeedUrlPredicate(const std::string& url) : _url(url) {} - bool FeedUrlPredicate::operator()(const Feed& feed) const - { - return boost::algorithm::iequals(_url, feed.GetUrl()); - } +bool FeedUrlPredicate::operator()(const Feed& feed) const { + return boost::algorithm::iequals(_url, feed.GetUrl()); +} - EntryUniqueIdPredicate::EntryUniqueIdPredicate(const std::string& uniqueId) : - _uniqueId(uniqueId) - { - } +EntryUniqueIdPredicate::EntryUniqueIdPredicate(const std::string& uniqueId) + : _uniqueId(uniqueId) {} - bool EntryUniqueIdPredicate::operator()(const Entry& entry) const - { - return boost::algorithm::iequals(_uniqueId, entry.UniqueId); - } +bool EntryUniqueIdPredicate::operator()(const Entry& entry) const { + return boost::algorithm::iequals(_uniqueId, entry.UniqueId); +} } diff --git a/src/Utils.hpp b/src/Utils.hpp index a438ab6..3286db8 100644 --- a/src/Utils.hpp +++ b/src/Utils.hpp @@ -37,29 +37,26 @@ POSSIBILITY OF SUCH DAMAGE. #include "Feed.hpp" -namespace FeedReader -{ - std::string XmlCharsToStdString(const XMLCh* const xmlChars); +namespace FeedReader { +std::string XmlCharsToStdString(const XMLCh* const xmlChars); - class FeedUrlPredicate - { - const std::string& _url; +class FeedUrlPredicate { + const std::string& _url; - public: - FeedUrlPredicate(const std::string& url); + public: + FeedUrlPredicate(const std::string& url); - bool operator()(const Feed& feed) const; - }; + bool operator()(const Feed& feed) const; +}; - class EntryUniqueIdPredicate - { - const std::string& _uniqueId; +class EntryUniqueIdPredicate { + const std::string& _uniqueId; - public: - EntryUniqueIdPredicate(const std::string& uniqueId); + public: + EntryUniqueIdPredicate(const std::string& uniqueId); - bool operator()(const Entry& entry) const; - }; + bool operator()(const Entry& entry) const; +}; } #endif diff --git a/src/Validator.cpp b/src/Validator.cpp index d028f31..865f3f8 100644 --- a/src/Validator.cpp +++ b/src/Validator.cpp @@ -44,114 +44,105 @@ POSSIBILITY OF SUCH DAMAGE. #include "Validator.hpp" #include "MemParseHandlers.hpp" -namespace FeedReader -{ - FeedValidator::FeedValidator(const std::string& feedData, Feed& feed) : - m_feedData(feedData), - m_feed(feed), - m_validationValid(true) - { - } - - void FeedValidator::Validate(std::string& results) - { - try - { - const FeedConfig& feedConfig = m_feed.GetFeedConfig(); - for (FeedTypes::const_iterator itr = feedConfig.m_feed_types.begin(), - eitr = feedConfig.m_feed_types.end();itr != eitr; ++itr) - { - ValidateFeedFormat(itr->first,itr->second,results); - if (!results.empty()) - { - return; - } - } - } - catch (const xercesc_2_8::XMLException& e) - { - std::stringstream msg; - msg << "An error occurred during feed parsing: '" - << XmlCharsToStdString(e.getMessage()); - throw feed_exception(msg.str()); - } - catch (const xercesc_2_8::DOMException& e) - { - const unsigned int maxChars = 2047; - XMLCh errText[maxChars + 1]; - - std::stringstream msg; - msg << "DOM Error occurred during feed parsing. Code is: " << e.code << "."; - - if (xercesc_2_8::DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars)) - msg << " Message is: " << XmlCharsToStdString(errText) << "."; - throw feed_exception(msg.str()); - } - m_feed.SetValidity(false); - m_feed.SetFeedFormat("Unknown"); - } - - void FeedValidator::ValidateFeedFormat(const std::string& xslPath, FeedFormat feedFormat, - std::string& results) - { - boost::filesystem::path fullFileName(m_feed.GetFeedConfig().m_config_path); - fullFileName /= xslPath; - - std::ifstream xslFile(fullFileName.string().c_str(), std::ios_base::binary | std::ios_base::in); - if (!xslFile) - { - std::stringstream msg; - msg << "FeedValidator: Unable to open XSL file: '" << xslPath << "'"; - throw feed_exception(msg.str()); - } - - std::stringstream xslStream; - xslFile.get(*xslStream.rdbuf(), '\0'); - xslStream.seekg(std::stringstream::beg); - - TransformFeed(xslStream, results); - if (!results.empty() && ValidateFeed(results)) - { - m_feed.SetFeedFormat(feedFormat); - m_feed.SetValidity(true); - return; - } - - return; - } - - void FeedValidator::TransformFeed(std::stringstream& xslStream, std::string& results) - { - std::stringstream result; - xalanc_1_10::XalanTransformer trans; - std::stringstream xmlStream(m_feedData); - const xalanc_1_10::XSLTInputSource xmlIn(xmlStream); - const xalanc_1_10::XSLTInputSource xslIn(xslStream); - const xalanc_1_10::XSLTResultTarget xmlOut(result); - - int theResult = -1; - theResult = trans.transform(xmlIn, xslIn, xmlOut); - if(theResult != 0) - { - std::stringstream msg; - msg << "An error occurred while applying XSLT transformation to feed: " << trans.getLastError(); - throw feed_exception(msg.str()); - } - - results = result.str(); - } - - bool FeedValidator::ValidateFeed(const std::string& feed) const - { - MemParseHandlers handler; - const xercesc_2_8::MemBufInputSource input(reinterpret_cast(feed.c_str()), feed.size(), "c:/"); - - xercesc_2_8::XercesDOMParser parser; - parser.setValidationScheme(xercesc_2_8::XercesDOMParser::Val_Never); - parser.setErrorHandler(&handler); - - parser.parse(input); - - return handler.GetSuccess(); - } +namespace FeedReader { +FeedValidator::FeedValidator(const std::string& feedData, Feed& feed) + : m_feedData(feedData), m_feed(feed), m_validationValid(true) {} + +void FeedValidator::Validate(std::string& results) { + try { + const FeedConfig& feedConfig = m_feed.GetFeedConfig(); + for (FeedTypes::const_iterator itr = feedConfig.m_feed_types.begin(), + eitr = feedConfig.m_feed_types.end(); + itr != eitr; ++itr) { + ValidateFeedFormat(itr->first, itr->second, results); + if (!results.empty()) { + return; + } + } + } + catch (const xercesc_2_8::XMLException& e) { + std::stringstream msg; + msg << "An error occurred during feed parsing: '" + << XmlCharsToStdString(e.getMessage()); + throw feed_exception(msg.str()); + } + catch (const xercesc_2_8::DOMException& e) { + const unsigned int maxChars = 2047; + XMLCh errText[maxChars + 1]; + + std::stringstream msg; + msg << "DOM Error occurred during feed parsing. Code is: " << e.code + << "."; + + if (xercesc_2_8::DOMImplementation::loadDOMExceptionMsg(e.code, errText, + maxChars)) + msg << " Message is: " << XmlCharsToStdString(errText) << "."; + throw feed_exception(msg.str()); + } + m_feed.SetValidity(false); + m_feed.SetFeedFormat("Unknown"); +} + +void FeedValidator::ValidateFeedFormat(const std::string& xslPath, + FeedFormat feedFormat, + std::string& results) { + boost::filesystem::path fullFileName(m_feed.GetFeedConfig().m_config_path); + fullFileName /= xslPath; + + std::ifstream xslFile(fullFileName.string().c_str(), + std::ios_base::binary | std::ios_base::in); + if (!xslFile) { + std::stringstream msg; + msg << "FeedValidator: Unable to open XSL file: '" << xslPath << "'"; + throw feed_exception(msg.str()); + } + + std::stringstream xslStream; + xslFile.get(*xslStream.rdbuf(), '\0'); + xslStream.seekg(std::stringstream::beg); + + TransformFeed(xslStream, results); + if (!results.empty() && ValidateFeed(results)) { + m_feed.SetFeedFormat(feedFormat); + m_feed.SetValidity(true); + return; + } + + return; +} + +void FeedValidator::TransformFeed(std::stringstream& xslStream, + std::string& results) { + std::stringstream result; + xalanc_1_10::XalanTransformer trans; + std::stringstream xmlStream(m_feedData); + const xalanc_1_10::XSLTInputSource xmlIn(xmlStream); + const xalanc_1_10::XSLTInputSource xslIn(xslStream); + const xalanc_1_10::XSLTResultTarget xmlOut(result); + + int theResult = -1; + theResult = trans.transform(xmlIn, xslIn, xmlOut); + if (theResult != 0) { + std::stringstream msg; + msg << "An error occurred while applying XSLT transformation to feed: " + << trans.getLastError(); + throw feed_exception(msg.str()); + } + + results = result.str(); +} + +bool FeedValidator::ValidateFeed(const std::string& feed) const { + MemParseHandlers handler; + const xercesc_2_8::MemBufInputSource input( + reinterpret_cast(feed.c_str()), feed.size(), "c:/"); + + xercesc_2_8::XercesDOMParser parser; + parser.setValidationScheme(xercesc_2_8::XercesDOMParser::Val_Never); + parser.setErrorHandler(&handler); + + parser.parse(input); + + return handler.GetSuccess(); +} } diff --git a/src/Validator.hpp b/src/Validator.hpp index 9f50094..fc6042d 100644 --- a/src/Validator.hpp +++ b/src/Validator.hpp @@ -39,26 +39,27 @@ POSSIBILITY OF SUCH DAMAGE. #include "Feed.hpp" // this is the size of the standard header placed by all xsl templates: -// +// static const int XML_HEADER_SIZE = 42; -namespace FeedReader -{ - class FeedValidator - { - public: - FeedValidator(const std::string& feedData, Feed& feed); +namespace FeedReader { +class FeedValidator { + public: + FeedValidator(const std::string& feedData, Feed& feed); - void Validate(std::string& results); - private: - void ValidateFeedFormat(const std::string& xslPath, FeedFormat feedFormat, std::string& results); - void TransformFeed(std::stringstream& xslStream, std::string& results); - bool ValidateFeed(const std::string& feed) const; - private: - const std::string& m_feedData; - Feed& m_feed; - bool m_validationValid; - }; + void Validate(std::string& results); + + private: + void ValidateFeedFormat(const std::string& xslPath, FeedFormat feedFormat, + std::string& results); + void TransformFeed(std::stringstream& xslStream, std::string& results); + bool ValidateFeed(const std::string& feed) const; + + private: + const std::string& m_feedData; + Feed& m_feed; + bool m_validationValid; +}; } #endif diff --git a/src/XmlGlobalState.cpp b/src/XmlGlobalState.cpp index 7f41538..7406f3a 100644 --- a/src/XmlGlobalState.cpp +++ b/src/XmlGlobalState.cpp @@ -36,28 +36,23 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -namespace FeedReader -{ - XmlGlobalState::XmlGlobalState() - { - try - { - xercesc_2_8::XMLPlatformUtils::Initialize(); - xalanc_1_10::XalanTransformer::initialize(); - } - catch(const xercesc_2_8::XMLException &xe) - { - char* message = xercesc_2_8::XMLString::transcode( xe.getMessage() ) ; - std::cout << "Error during Xerces-c Initialization: " - << message << std::endl; - xercesc_2_8::XMLString::release( &message ) ; - throw; - } - } - - XmlGlobalState::~XmlGlobalState() - { - xalanc_1_10::XalanTransformer::terminate(); - xercesc_2_8::XMLPlatformUtils::Terminate(); - } +namespace FeedReader { +XmlGlobalState::XmlGlobalState() { + try { + xercesc_2_8::XMLPlatformUtils::Initialize(); + xalanc_1_10::XalanTransformer::initialize(); + } + catch (const xercesc_2_8::XMLException& xe) { + char* message = xercesc_2_8::XMLString::transcode(xe.getMessage()); + std::cout << "Error during Xerces-c Initialization: " << message + << std::endl; + xercesc_2_8::XMLString::release(&message); + throw; + } +} + +XmlGlobalState::~XmlGlobalState() { + xalanc_1_10::XalanTransformer::terminate(); + xercesc_2_8::XMLPlatformUtils::Terminate(); +} } diff --git a/src/XmlGlobalState.hpp b/src/XmlGlobalState.hpp index 341be2a..605ebf8 100644 --- a/src/XmlGlobalState.hpp +++ b/src/XmlGlobalState.hpp @@ -35,17 +35,15 @@ POSSIBILITY OF SUCH DAMAGE. #include "export_cfg.hpp" -namespace FeedReader -{ - class FEED_EXPORT XmlGlobalState - { - public: - XmlGlobalState(); - ~XmlGlobalState(); - - private: - XmlGlobalState(const XmlGlobalState& xmlGlobalState) { } // disallow copying - }; +namespace FeedReader { +class FEED_EXPORT XmlGlobalState { + public: + XmlGlobalState(); + ~XmlGlobalState(); + + private: + XmlGlobalState(const XmlGlobalState& xmlGlobalState) {} // disallow copying +}; } #endif diff --git a/src/export_cfg.hpp b/src/export_cfg.hpp index 2ccea2e..a491c7a 100644 --- a/src/export_cfg.hpp +++ b/src/export_cfg.hpp @@ -35,32 +35,30 @@ POSSIBILITY OF SUCH DAMAGE. #include - #if defined(__GNUC__) && __GNUC__ >= 4 -# if defined(SCRAPER_BUILDING_SHARED) || defined(SCRAPER_LINKING_SHARED) -# define FEED_EXPORT __attribute__ ((visibility("default"))) -# else -# define FEED_EXPORT -# endif +#if defined(SCRAPER_BUILDING_SHARED) || defined(SCRAPER_LINKING_SHARED) +#define FEED_EXPORT __attribute__((visibility("default"))) +#else +#define FEED_EXPORT +#endif #elif defined(__GNUC__) -# define FEED_EXPORT +#define FEED_EXPORT #elif defined(BOOST_MSVC) -# if defined(FEEDREADER_BUILDING_SHARED) -# define FEED_EXPORT __declspec(dllexport) -# elif defined(FEEDREADER_LINKING_SHARED) -# define FEED_EXPORT __declspec(dllimport) -# else -# define FEED_EXPORT -# endif - +#if defined(FEEDREADER_BUILDING_SHARED) +#define FEED_EXPORT __declspec(dllexport) +#elif defined(FEEDREADER_LINKING_SHARED) +#define FEED_EXPORT __declspec(dllimport) #else -# define _CONFIG_HPP_INCLUDED +#define FEED_EXPORT #endif -#endif // SCRAPER_CONFIG_HPP_INCLUDED +#else +#define _CONFIG_HPP_INCLUDED +#endif +#endif // SCRAPER_CONFIG_HPP_INCLUDED