summaryrefslogtreecommitdiff
path: root/src/ParserClass/FileManParser.cpp
blob: 57a485a04c5508704ef46cc3dd4ea622bb423adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
 * @file FileManParser.cpp
 * @brief FileManParser class definitions
 * @author manzerbredes
 * @date 11 Mars 2015
 *
 * Contain all implementations of FileManParser class.
 *
 */


#include "FileManParser.hpp"


FileManParser::FileManParser(std::string data){

    //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();

    //Init root Node
    this->rootNode=(this->document)->get_root_node();

    //Init container:
    this->initWebsites();



}


std::string FileManParser::getDocument(){
    std::string data=(this->document)->write_to_string();
    return data;
}


std::vector<Website>* FileManParser::getWebsites(){
    return this->websites;
}




//----------------Container part------------------------
void FileManParser::initWebsites(){
    this->websites=new std::vector<Website>;

    std::vector<xmlpp::Node*> websitesNodeSet=this->rootNode->find("//websites");
    this->websitesNode=websitesNodeSet.at(0);

    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();


            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);
    }
}









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());

    }
}