Make basic parser
This commit is contained in:
parent
9b99763c2e
commit
1f12bca33f
9 changed files with 191 additions and 188 deletions
|
@ -34,6 +34,6 @@ std::string AbstractIDManager::generateId(){
|
|||
return ss.str();
|
||||
}
|
||||
|
||||
std::string AbstractIDManager::getId(){
|
||||
std::string AbstractIDManager::getId() const{
|
||||
return this->id;
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
|
||||
|
||||
//Getters and setters
|
||||
std::string getId();
|
||||
std::string getId() const;
|
||||
void setId(std::string id);
|
||||
|
||||
|
||||
|
|
|
@ -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<Website> FileManContainer::getWebsites(){
|
||||
return this->websites;
|
||||
}
|
|
@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
//----- 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<Website> getWebsites();
|
||||
|
||||
private:
|
||||
|
||||
std::vector<Website> websites;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<Website>;
|
||||
|
||||
//Get websites élément
|
||||
xercesc::DOMElement* websitesElement=this->getChildByTagName(this->root, "websites");
|
||||
std::vector<xmlpp::Node*> 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<xmlpp::Node*> websiteNodeSet=this->websitesNode->find("*");
|
||||
|
||||
for(int i=0;i<websiteNodeSet.size();i++){
|
||||
xmlpp::Node* current=websiteNodeSet.at(i);
|
||||
xmlpp::Element* currentElement=(xmlpp::Element*)current;
|
||||
|
||||
Website newWebsite;
|
||||
newWebsite.setId(currentElement->get_attribute_value("id"));
|
||||
|
||||
std::vector<xmlpp::Node*> websiteChildren=current->find("*");
|
||||
|
||||
for(int j=0;j<websiteChildren.size();j++){
|
||||
xmlpp::Element* currentChild=(xmlpp::Element*)websiteChildren.at(j);
|
||||
|
||||
std::list<xmlpp::Node*> 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;i<websiteCount;i++){
|
||||
|
||||
xercesc::DOMNode* current=websiteList->item(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;i<nodeCount;i++){
|
||||
|
||||
xercesc::DOMNode* current=nodeList->item(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<Website>* 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;i<this->websites->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());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,23 +8,20 @@
|
|||
*
|
||||
*/
|
||||
|
||||
|
||||
//----- std -----
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
//----- class -----
|
||||
#include "Website.hpp"
|
||||
#include "FileManContainer.hpp"
|
||||
|
||||
//----- xerces -----
|
||||
#include <xercesc/parsers/XercesDOMParser.hpp>
|
||||
#include <xercesc/dom/DOM.hpp>
|
||||
#include <xercesc/framework/MemBufInputSource.hpp>
|
||||
#include <xercesc/sax/HandlerBase.hpp>
|
||||
#include <xercesc/util/XMLString.hpp>
|
||||
#include <xercesc/util/PlatformUtils.hpp>
|
||||
#include <xercesc/util/XercesDefs.hpp>
|
||||
//----- libxml++ -----
|
||||
#include <cstdlib>
|
||||
#include <libxml++/libxml++.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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<Website>* 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<Website> *websites;
|
||||
|
||||
};
|
||||
|
|
59
main.cpp
59
main.cpp
|
@ -20,9 +20,13 @@
|
|||
//----- class -----
|
||||
#include "FileManIOFile.hpp"
|
||||
#include "FileManParser.hpp"
|
||||
#include "FileManContainer.hpp"
|
||||
#include "Website.hpp"
|
||||
|
||||
#include <list>
|
||||
#include <cstdlib>
|
||||
#include <libxml++/nodes/node.h>
|
||||
#include <libxml++/libxml++.h>
|
||||
|
||||
/**
|
||||
* @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="<?xml version=\"1.0\" standalone=\"yes\" ?>\n\
|
||||
std::stringstream xml;
|
||||
xml <<"<?xml version=\"1.0\" standalone=\"yes\" ?>\n\
|
||||
<forgetIt> \n\
|
||||
<websites> \n\
|
||||
\n\
|
||||
|
@ -50,13 +55,55 @@ int main(int argc, char *argv[]){
|
|||
";
|
||||
|
||||
|
||||
FileManParser parser(xml);
|
||||
/*FileManParser parser(xml);
|
||||
|
||||
|
||||
FileManContainer container= parser.getContainer();
|
||||
std::vector<Website> websites= container.getWebsites();
|
||||
std::cout << websites.at(0).getId();
|
||||
std::vector<Website> 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<xmlpp::Node*> 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<Website> *bb=fichier.getWebsites();
|
||||
|
||||
|
||||
Website a;
|
||||
a.setTitle("pierre");
|
||||
|
||||
|
||||
bb->push_back(a);
|
||||
|
||||
|
||||
|
||||
|
||||
fichier.updateParser();
|
||||
|
||||
std::cout << fichier.getDocument();
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue