forgetIt/ParserClass/FileManParser.cpp

136 lines
3.6 KiB
C++
Raw Normal View History

2015-03-11 10:52:05 +04:00
/**
* @file FileManParser.cpp
* @brief FileManParser class definitions
* @author manzerbredes
* @date 11 Mars 2015
*
* Contain all implementations of FileManParser class.
*
*/
2015-03-11 16:12:56 +04:00
#include "FileManParser.hpp"
FileManParser::FileManParser(std::string data){
2015-03-14 18:44:05 +04:00
//String to stringstream
(this->dataStream) << data;
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
//Create parser
//parser.parse_stream(dataStream);
(this->parser).parse_file("Doxygen/doc.xml");
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
//Retrieve document
this->document=(this->parser).get_document();
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
//Init root Node
this->rootNode=(this->document)->get_root_node();
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
//Init container:
this->initWebsites();
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
}
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
void FileManParser::initWebsites(){
this->websites=new std::vector<Website>;
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
std::vector<xmlpp::Node*> websitesNodeSet=this->rootNode->find("//websites");
this->websitesNode=websitesNodeSet.at(0);
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
std::vector<xmlpp::Node*> websiteNodeSet=this->websitesNode->find("*");
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
for(int i=0;i<websiteNodeSet.size();i++){
xmlpp::Node* current=websiteNodeSet.at(i);
xmlpp::Element* currentElement=(xmlpp::Element*)current;
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
Website newWebsite;
newWebsite.setId(currentElement->get_attribute_value("id"));
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
std::vector<xmlpp::Node*> websiteChildren=current->find("*");
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
for(int j=0;j<websiteChildren.size();j++){
xmlpp::Element* currentChild=(xmlpp::Element*)websiteChildren.at(j);
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
std::list<xmlpp::Node*> contentNodes=currentChild->get_children();
xmlpp::CdataNode* cdataNode=(xmlpp::CdataNode*)contentNodes.front();
std::string cdataContent=cdataNode->get_content();
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
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);
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
}
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
}
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
this->websites->push_back(newWebsite);
2015-03-11 16:12:56 +04:00
2015-03-13 15:06:36 +04:00
2015-03-14 18:44:05 +04:00
}
2015-03-13 15:06:36 +04:00
2015-03-14 18:44:05 +04:00
}
2015-03-11 19:53:47 +04:00
2015-03-14 18:44:05 +04:00
std::string FileManParser::getDocument(){
std::string data=(this->document)->write_to_string();
return data;
}
2015-03-11 19:53:47 +04:00
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
std::vector<Website>* FileManParser::getWebsites(){
return this->websites;
2015-03-11 16:12:56 +04:00
}
2015-03-14 18:44:05 +04:00
void FileManParser::updateParser(){
this->rootNode->remove_child(this->websitesNode);
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
xmlpp::Element* websitesNode=this->rootNode->add_child("websites");
this->websitesNode=(xmlpp::Node*)websitesNode;
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
for(int i=0;i<this->websites->size();i++){
xmlpp::Element* current=this->websitesNode->add_child("website");
Website currentWebsite=this->websites->at(i);
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
current->set_attribute("id", currentWebsite.getId());
xmlpp::Element* title=current->add_child("title");
title->add_child_cdata(currentWebsite.getTitle());
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
xmlpp::Element* url=current->add_child("url");
url->add_child_cdata(currentWebsite.getUrl());
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
xmlpp::Element* username=current->add_child("username");
username->add_child_cdata(currentWebsite.getUsername());
2015-03-11 16:12:56 +04:00
2015-03-14 18:44:05 +04:00
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());
}
2015-03-11 16:12:56 +04:00
}