From 1f12bca33f6e9ce20181a9d09f62286d11592712 Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Sat, 14 Mar 2015 18:44:05 +0400 Subject: [PATCH] Make basic parser --- ParserClass/AbstractIDManager.cpp | 2 +- ParserClass/AbstractIDManager.hpp | 2 +- .../FileManContainer/FileManContainer.cpp | 25 --- .../FileManContainer/FileManContainer.hpp | 46 ----- ParserClass/FileManContainer/Website.cpp | 9 + ParserClass/FileManContainer/Website.hpp | 2 + ParserClass/FileManParser.cpp | 184 +++++++++--------- ParserClass/FileManParser.hpp | 50 +++-- main.cpp | 59 +++++- 9 files changed, 191 insertions(+), 188 deletions(-) delete mode 100644 ParserClass/FileManContainer/FileManContainer.cpp delete mode 100644 ParserClass/FileManContainer/FileManContainer.hpp diff --git a/ParserClass/AbstractIDManager.cpp b/ParserClass/AbstractIDManager.cpp index 01e3c3d..e1c5e13 100644 --- a/ParserClass/AbstractIDManager.cpp +++ b/ParserClass/AbstractIDManager.cpp @@ -34,6 +34,6 @@ std::string AbstractIDManager::generateId(){ return ss.str(); } -std::string AbstractIDManager::getId(){ +std::string AbstractIDManager::getId() const{ return this->id; } diff --git a/ParserClass/AbstractIDManager.hpp b/ParserClass/AbstractIDManager.hpp index a18d1b2..67d5f6f 100644 --- a/ParserClass/AbstractIDManager.hpp +++ b/ParserClass/AbstractIDManager.hpp @@ -46,7 +46,7 @@ //Getters and setters - std::string getId(); + std::string getId() const; void setId(std::string id); diff --git a/ParserClass/FileManContainer/FileManContainer.cpp b/ParserClass/FileManContainer/FileManContainer.cpp deleted file mode 100644 index 7f04be4..0000000 --- a/ParserClass/FileManContainer/FileManContainer.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/** - * @file FileManContainer.cpp - * @brief FileManContainer class definitions - * @author manzerbredes - * @date 11 Mars 2015 - * - * Contain all implementation of FileManContainer class. - * - */ - - -#include "FileManContainer.hpp" - -FileManContainer::FileManContainer(){ -} - - - -void FileManContainer::addWebsite(Website website){ - this->websites.push_back(website); -} - -std::vector FileManContainer::getWebsites(){ - return this->websites; -} diff --git a/ParserClass/FileManContainer/FileManContainer.hpp b/ParserClass/FileManContainer/FileManContainer.hpp deleted file mode 100644 index d5be276..0000000 --- a/ParserClass/FileManContainer/FileManContainer.hpp +++ /dev/null @@ -1,46 +0,0 @@ -/** - * @file FileManContainer.hpp - * @brief FileManContainer class definitions - * @author manzerbredes - * @date 11 Mars 2015 - * - * Contain all definitions of FileManContainer class. - * - */ - -#ifndef __FileManContainer__ -#define __FileManContainer__ - -//----- std ----- - -#include -#include - -//----- class ----- -#include "Website.hpp" - - -/** - * @class Website Website.hpp "/ParserClass/FileManContainer/Website.hpp" - * @brief Class for manager all FileMan container (websites etc...) - * @author manzerbredes - * - * - * - */ - -class FileManContainer{ - - public: - FileManContainer(); - - void addWebsite(Website website); - std::vector getWebsites(); - - private: - - std::vector websites; -}; - - -#endif diff --git a/ParserClass/FileManContainer/Website.cpp b/ParserClass/FileManContainer/Website.cpp index d54e28a..2810d48 100644 --- a/ParserClass/FileManContainer/Website.cpp +++ b/ParserClass/FileManContainer/Website.cpp @@ -54,3 +54,12 @@ void Website::setPassword(std::string password){ void Website::setDescription(std::string description){ this->description = description; } + + +bool Website::operator==(const Website& website) const{ + if((this->getId()).compare(website.getId())==0){ + return true; + } + return false; +} + diff --git a/ParserClass/FileManContainer/Website.hpp b/ParserClass/FileManContainer/Website.hpp index c370f3b..7d68b91 100644 --- a/ParserClass/FileManContainer/Website.hpp +++ b/ParserClass/FileManContainer/Website.hpp @@ -64,7 +64,9 @@ class Website : public AbstractIDManager { void setPassword(std::string password); void setDescription(std::string description); + //bool operator==(Website const& website1, Website const& website2) const; + bool operator==(const Website& website) const; private: std::string title; ///< Title of the website diff --git a/ParserClass/FileManParser.cpp b/ParserClass/FileManParser.cpp index 66e3c52..1434229 100644 --- a/ParserClass/FileManParser.cpp +++ b/ParserClass/FileManParser.cpp @@ -13,26 +13,21 @@ FileManParser::FileManParser(std::string data){ - this->data=data; - try { - xercesc::XMLPlatformUtils::Initialize(); - } - catch (const xercesc::XMLException& toCatch) { - // Do your failure processing here + //String to stringstream + (this->dataStream) << data; - } + //Create parser + //parser.parse_stream(dataStream); + (this->parser).parse_file("Doxygen/doc.xml"); + //Retrieve document + this->document=(this->parser).get_document(); - xercesc::XercesDOMParser *parser = new xercesc::XercesDOMParser(); - xercesc::MemBufInputSource myxml_buf((const XMLByte*)this->data.c_str(), this->data.size(), "dummy",false); - - parser->parse("Doxygen/doc.xml"); - - - this->document=parser->getDocument(); - this->root=this->document->getDocumentElement(); + //Init root Node + this->rootNode=(this->document)->get_root_node(); + //Init container: this->initWebsites(); @@ -41,93 +36,100 @@ FileManParser::FileManParser(std::string data){ -FileManContainer FileManParser::getContainer(){ - return this->container; -} - - -std::string FileManParser::getData(){ return this->data;}; - - void FileManParser::initWebsites(){ + this->websites=new std::vector; - //Get websites élément - xercesc::DOMElement* websitesElement=this->getChildByTagName(this->root, "websites"); + std::vector websitesNodeSet=this->rootNode->find("//websites"); + this->websitesNode=websitesNodeSet.at(0); - //Make list of website - xercesc::DOMNodeList* websiteList=websitesElement->getChildNodes(); - XMLSize_t websiteCount = websiteList->getLength(); + std::vector websiteNodeSet=this->websitesNode->find("*"); + + for(int i=0;iget_attribute_value("id")); + + std::vector websiteChildren=current->find("*"); + + for(int j=0;j contentNodes=currentChild->get_children(); + xmlpp::CdataNode* cdataNode=(xmlpp::CdataNode*)contentNodes.front(); + std::string cdataContent=cdataNode->get_content(); - //Read the list of website - for(int i=0;iitem(i); - std::string TagName=xercesc::XMLString::transcode(current->getNodeName()); - - if( current->getNodeType() == xercesc::DOMNode::ELEMENT_NODE ) { - Website newWebsite; - - //Get id - XMLCh* idXMLCh=(XMLCh*)((xercesc::DOMElement*)current)->getAttribute((XMLCh*) xercesc::XMLString::transcode("id")); - //Convert id to string from XMLCh - std::string id=xercesc::XMLString::transcode(idXMLCh); - - //Assign id - newWebsite.setId(id); - - //Assign title - newWebsite.setTitle(\ - this->getContentOfChild(dynamic_cast< xercesc::DOMElement* >( current ),"title")); - - //Assign url - newWebsite.setUrl(\ - this->getContentOfChild(dynamic_cast< xercesc::DOMElement* >( current ),"url")); - - //Assign username - newWebsite.setUsername(\ - this->getContentOfChild(dynamic_cast< xercesc::DOMElement* >( current ),"username")); - - //Assign password - newWebsite.setPassword(\ - this->getContentOfChild(dynamic_cast< xercesc::DOMElement* >( current ),"password")); - - //Assign description - newWebsite.setDescription(\ - this->getContentOfChild(dynamic_cast< xercesc::DOMElement* >( current ),"description")); - - //Add website to container - this->container.addWebsite(newWebsite); - - } - } -} - - -xercesc::DOMElement* FileManParser::getChildByTagName(xercesc::DOMElement* node, std::string TagName){ - xercesc::DOMNodeList* nodeList=node->getChildNodes(); - XMLSize_t nodeCount = nodeList->getLength(); - xercesc::DOMElement* returnElement=NULL; - - for(int i=0;iitem(i); - std::string currentTagName=xercesc::XMLString::transcode(current->getNodeName()); - - if( current->getNodeType() == xercesc::DOMNode::ELEMENT_NODE ) { - if(currentTagName.compare(TagName)==0){ - returnElement=dynamic_cast< xercesc::DOMElement* >( current ); - break; + if(currentChild->get_name().compare("title")==0){ + newWebsite.setTitle(cdataContent); } + else if(currentChild->get_name().compare("url")==0){ + newWebsite.setUrl(cdataContent); + } + else if(currentChild->get_name().compare("username")==0){ + newWebsite.setUsername(cdataContent); + } + else if(currentChild->get_name().compare("password")==0){ + newWebsite.setPassword(cdataContent); + } + else if(currentChild->get_name().compare("description")==0){ + newWebsite.setDescription(cdataContent); + + } + + } + + this->websites->push_back(newWebsite); + + } - return returnElement; } -std::string FileManParser::getContentOfChild(xercesc::DOMElement* node,std::string TagName){ - xercesc::DOMElement* child=this->getChildByTagName(node,TagName); - return xercesc::XMLString::transcode(child->getTextContent()); + + +std::string FileManParser::getDocument(){ + std::string data=(this->document)->write_to_string(); + return data; +} + + + +std::vector* FileManParser::getWebsites(){ + return this->websites; + +} + + +void FileManParser::updateParser(){ + this->rootNode->remove_child(this->websitesNode); + + xmlpp::Element* websitesNode=this->rootNode->add_child("websites"); + this->websitesNode=(xmlpp::Node*)websitesNode; + + for(int i=0;iwebsites->size();i++){ + xmlpp::Element* current=this->websitesNode->add_child("website"); + Website currentWebsite=this->websites->at(i); + + current->set_attribute("id", currentWebsite.getId()); + xmlpp::Element* title=current->add_child("title"); + title->add_child_cdata(currentWebsite.getTitle()); + + xmlpp::Element* url=current->add_child("url"); + url->add_child_cdata(currentWebsite.getUrl()); + + xmlpp::Element* username=current->add_child("username"); + username->add_child_cdata(currentWebsite.getUsername()); + + xmlpp::Element* password=current->add_child("password"); + password->add_child_cdata(currentWebsite.getPassword()); + + xmlpp::Element* description=current->add_child("description"); + description->add_child_cdata(currentWebsite.getDescription()); + + } } diff --git a/ParserClass/FileManParser.hpp b/ParserClass/FileManParser.hpp index 6a977db..5c64708 100644 --- a/ParserClass/FileManParser.hpp +++ b/ParserClass/FileManParser.hpp @@ -8,23 +8,20 @@ * */ + //----- std ----- #include +#include #include #include //----- class ----- #include "Website.hpp" -#include "FileManContainer.hpp" -//----- xerces ----- -#include -#include -#include -#include -#include -#include -#include +//----- libxml++ ----- +#include +#include + @@ -32,27 +29,44 @@ class FileManParser{ public: + + //Constructor FileManParser(std::string data); + //Get document in string + std::string getDocument(); - FileManContainer getContainer(); - void initWebsites(); + //Get container vector pointer: + std::vector* getWebsites(); - std::string getData(); - xercesc::DOMElement* getChildByTagName(xercesc::DOMElement* node, std::string TagName); - std::string getContentOfChild(xercesc::DOMElement* node,std::string TagName); + + //Apply change that have made on container + void updateParser(); + private: - xercesc::DOMDocument* document; ///< contain the document - xercesc::DOMElement* root; + //Instaciate all website container + void initWebsites(); - FileManContainer container; ///< contain all container - std::string data; ///< contain data to parse + //Parser attributes + std::stringstream dataStream; + xmlpp::DomParser parser; + + + + //Document attributes + xmlpp::Document* document; + xmlpp::Node* rootNode; + + + //Website attributes + xmlpp::Node* websitesNode; + std::vector *websites; }; diff --git a/main.cpp b/main.cpp index 08aa05d..14c5227 100644 --- a/main.cpp +++ b/main.cpp @@ -20,9 +20,13 @@ //----- class ----- #include "FileManIOFile.hpp" #include "FileManParser.hpp" -#include "FileManContainer.hpp" #include "Website.hpp" +#include +#include +#include +#include + /** * @fn int main(int argc, char *argv[]) * @author manzerbredes @@ -41,7 +45,8 @@ int main(int argc, char *argv[]){ std::cout << fichier.getData();*/ - std::string xml="\n\ + std::stringstream xml; + xml <<"\n\ \n\ \n\ \n\ @@ -50,13 +55,55 @@ int main(int argc, char *argv[]){ "; - FileManParser parser(xml); + /*FileManParser parser(xml); - FileManContainer container= parser.getContainer(); - std::vector websites= container.getWebsites(); - std::cout << websites.at(0).getId(); + std::vector websites= parser.getContainer(); + std::cout << typeid(websites.at(0)).name();*/ + + /*xmlpp::DomParser parser; + parser.parse_file("Doxygen/doc.xml"); + xmlpp::Document* doc=parser.get_document(); + const xmlpp::Node* pNode = doc->get_root_node(); //deleted by DomParser. + const Glib::ustring nom="news"; + const Glib::ustring nm=""; + xmlpp::Element* elem=(xmlpp::Element*)pNode;*/ + + //std::vector websites=pNode->find("//websites/*/attribute::id"); +/* + xmlpp::Element* ell=(xmlpp::Element*)websites.at(0); + ell->set_child_text("56"); + elem->add_child(nom); + + std::cout << doc->write_to_string();*/ + //std::cout << (elem->get_child_text())->get_content(); + + + + + FileManParser fichier("loic"); + + + std::vector *bb=fichier.getWebsites(); + + + Website a; + a.setTitle("pierre"); + + + bb->push_back(a); + + + + + fichier.updateParser(); + + std::cout << fichier.getDocument(); + return 0; } + + +