Skip to content

Commit

Permalink
fixed possible issues and bugs with XmlNode
Browse files Browse the repository at this point in the history
  • Loading branch information
FStoeltie committed Jun 10, 2016
1 parent e883cf1 commit 2aead6f
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 123 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ add_executable(fagan_test ${GTEST} ${SOURCES_GTEST})
target_link_libraries(fagan_test gtest gtest_main ${CMAKE_THREAD_LIBS_INIT})

set(BUILD_2 main_2)
set(SOURCE_FILES_2 source/src/XmlNode.cpp) #this file has to be added manually, dont know why
set(SOURCE_FILES_2 source/src/main.cpp) #where main_2.cc is your second main/program
add_executable(${BUILD_2} ${SOURCES})
2 changes: 1 addition & 1 deletion source/include/XmlFileFormat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class XmlFileFormat {
//! @param data
void add_xml_data(std::string data = "", XML_DATA xml_data_type = XML_DATA::DEFAULT);

void add_base_node(std::string xml_node_name, std::string type_name);
//void add_base_node(std::string xml_node_name, std::string type_name);
void inspection_data(std::shared_ptr<XmlNode> node,std::string inspection_name, int errors_in_inspection);
void inspection_data(std::shared_ptr<XmlNode> node,std::string inspection_name, int errors_in_inspection, std::string);
void add_xml_node(std::shared_ptr<XmlNode> node);
Expand Down
9 changes: 0 additions & 9 deletions source/include/XmlNode.cpp

This file was deleted.

97 changes: 24 additions & 73 deletions source/include/XmlNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,38 @@

#ifndef FAGAN_XmlNode_HPP
#define FAGAN_XmlNode_HPP
#include <string>
#include <vector>
#include <memory>

#include <iostream>
struct XmlData{
std::string node_indentation;
std::string begin, end;
std::string node_text;
uint8_t node_depth = 0;
};
class XmlNode : public std::enable_shared_from_this<XmlNode> {
public:
//ToDo tree cannot be removed, should have a clear method.

XmlNode(std::string xml_node_name, std::string node_value);
XmlNode(std::shared_ptr<XmlNode> parent) :
parent{parent} {
}
void fix(std::string xml_node_name, std::string node_value = "") {
parent->add_child_node(shared_from_this());
set_indentation_depth();
add_node_name(xml_node_name, node_value);
}
void initialize() {
parent->add_child_node(shared_from_this());
set_indentation_depth();
}
void add_child_node(std::shared_ptr<XmlNode> child) {
children.push_back(child);
}
void add_attribute(std::string attribute, std::string attribute_value) {
begin = begin.substr(0, begin.size() - 2); // remove closing ">" @ node
begin += " " + attribute + "=" + "\"" + attribute_value + "\">\n";
}
void add_node_name(std::string node_name, std::string value = "") {
std::string temp;
if(!value.empty()) {
temp = "=\"" + value + "\"";
}
begin += node_indentation + "<" + node_name + "" + temp + ">\n";
end += node_indentation + "</" + node_name + ">\n";
}
void add_node_text(std::string text) {
node_text += node_indentation + "\t" + text;
}
int calculate_node_depth() {
if(parent) {
return node_depth += parent->calculate_node_depth() + 1;
}
return node_depth;
}
void set_indentation_depth() {
uint8_t n_depth = calculate_node_depth();
while(n_depth--) {
node_indentation += "\t";
}
}
XmlNode(std::shared_ptr<XmlNode> parent);

void initialize();

void clear_node() {
for(auto child : children) {
child->clear_node();
}
for (std::vector<std::shared_ptr<XmlNode>>::iterator it = parent->children.begin(); it != parent->children.end();) {
if(it->get() == this) {
it->reset();
parent->children.erase(it);
}
else {
++it;
}
}
}
std::string get_all_nodes_data() {
std::string xml_data = begin;
xml_data += node_text;
for(auto child : children) {
xml_data += child->get_all_nodes_data();
}
xml_data += end;
return xml_data;
}
void add_child_node(std::shared_ptr<XmlNode> child);
void add_attribute(std::string attribute, std::string attribute_value);
void add_node_name(std::string node_name, std::string value = "");
void add_node_text(std::string text);

//ToDo method should be renamed to clear_node and clear_node has to be renamed to something else.
void clear_node_data();
std::string get_all_nodes_data();
private:
std::string node_indentation;
std::string begin, end;
std::string node_text;
std::string node_name;
uint8_t node_depth = 0;
std::shared_ptr<XmlNode> parent;
int calculate_node_depth();
void set_indentation_depth();

XmlData node_data;
std::weak_ptr<XmlNode> parent;
std::vector<std::shared_ptr<XmlNode>> children;

};
Expand Down
38 changes: 20 additions & 18 deletions source/src/FaganInspectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,30 @@ void FaganInspectionTest::run_all_inspections(vector<string> fileLocations) {

XmlFileFormat xmlff{};
std::vector<BaseTest *> tests;

bool testbool = false;
//FileSearcher files("E:/Development/HBO/Year2/BlokC/ThemaOpdracht7-8/Fagan-Lite-Tool/test/testfiles");
for (std::string fpath : fileLocations) {
XmlFileFormat xmlff{};
xmlff.add_base_node("file", fpath);
std::vector<BaseTest *> tests;
r2d2::DoxygenCheck dc{xmlff};
tests.push_back(&dc);
XmlFileFormat xmlff;
shared_ptr<XmlNode> file_node = shared_ptr<XmlNode>(new XmlNode("file", fpath));
file_node->initialize();
xmlff.base_node = file_node;
/*xmlff.add_base_node("file", fpath);*/
std::vector<BaseTest *> tests;
r2d2::DoxygenCheck dc{xmlff};
tests.push_back(&dc);

//r2d2::IndentCheck ic{xmlff};
// tests.push_back(&ic);
//r2d2::IndentCheck ic{xmlff};
// tests.push_back(&ic);

LineLength ll(xmlff);
tests.push_back(&ll);
LineLength ll(xmlff);
tests.push_back(&ll);

CommentStyle cs(xmlff);
tests.push_back(&cs);

/* //FileSearcher files("E:/Development/HBO/Year2/BlokC/ThemaOpdracht7-8/Fagan-Lite-Tool/test/testfiles");
for (std::string fpath : fileLocations) {
>>>>>>> 9012e3d3039f0ddb1d00b0994d59d511da72efba*/
CommentStyle cs(xmlff);
tests.push_back(&cs);


xmlff.add_xml_data(fpath);

// get file contents and store in string vector
// vector<string> file_contents = get_file_data(fpath);
string f_content = get_file_contents(fpath.c_str());

for(const auto & test : tests) {
Expand All @@ -61,6 +58,11 @@ void FaganInspectionTest::run_all_inspections(vector<string> fileLocations) {
InclusionGuards IG(xmlff);
IG.inspect(f_content);
}
if(testbool) {
xmlff.base_node->clear_node_data();

}
testbool = true;
std::cout << xmlff.data();
xmlff.add_xml_data("</file>\n");
for (string s : xmlff.get_xml_data()) {
Expand Down
2 changes: 1 addition & 1 deletion source/src/InclusionGuards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bool InclusionGuards::inspect(const std::string &file_contents) {
}
if(error_counter == 0) {

node->clear_node();
node->clear_node_data();
}
node->add_attribute("errors", to_string(error_counter));
xml_output += "</" + inspection_name + ">\n";
Expand Down
2 changes: 1 addition & 1 deletion source/src/LineLength.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool LineLength::inspect(const std::string &file_contents) {

if(error_counter == 0) {

node->clear_node();
node->clear_node_data();
}
node->add_attribute("errors", to_string(error_counter));
xml_output += " errors = \"" + to_string(error_counter) + "\">\n";
Expand Down
19 changes: 0 additions & 19 deletions source/src/XmlFileFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

#include "../include/XmlFileFormat.hpp"
#include <iostream>

void XmlFileFormat::add_xml_data(std::string data, XML_DATA xml_data_type) {
//ToDo Check for XML_DATA type and adjust current xml data about file to insert new data in the correct structure
Expand All @@ -16,7 +15,6 @@ void XmlFileFormat::add_xml_data(std::string data, XML_DATA xml_data_type) {
case XML_DATA::END:
xml_data.push_back("</file>\n");
break;
break;
case XML_DATA::DOXYGEN:
xml_data.push_back(data + "</doxygen>\n");
break;
Expand All @@ -32,23 +30,6 @@ void XmlFileFormat::add_xml_data(std::string data, XML_DATA xml_data_type) {
break;
}
}
void XmlFileFormat::add_base_node(std::string xml_node_name, std::string node_value) {
base_node = std::shared_ptr<XmlNode>(new XmlNode(xml_node_name, node_value));
}
void XmlFileFormat::inspection_data(std::shared_ptr<XmlNode> parent_node,std::string inspection_name, int errors_in_inspection) {
std::shared_ptr<XmlNode> node = std::shared_ptr<XmlNode>(new XmlNode(parent_node));
node->fix(inspection_name);
node->add_attribute("errors", std::to_string(errors_in_inspection));
}
void XmlFileFormat::inspection_data(std::shared_ptr<XmlNode> parent_node,std::string inspection_name, int errors_in_inspection, std::string text) {
std::shared_ptr<XmlNode> node = std::shared_ptr<XmlNode>(new XmlNode(parent_node));
node->fix(inspection_name);
node->add_attribute("errors", std::to_string(errors_in_inspection));
node->add_node_text(text);
}
/*void XmlFileFormat::add_xml_node(std::shared_ptr<XmlNode> node) {
node->fix(inspection_name);
}*/
const std::string XmlFileFormat::data() {
return base_node->get_all_nodes_data();
}
Expand Down
72 changes: 72 additions & 0 deletions source/src/XmlNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Created by Ferdi on 08-Jun-16.
//

#include "../include/XmlNode.hpp"

XmlNode::XmlNode(std::string xml_node_name, std::string node_value) {
add_node_name(xml_node_name, node_value);
node_data.node_depth = 0;
}

XmlNode::XmlNode(std::shared_ptr<XmlNode> parent) :
parent{parent} {}

void XmlNode::initialize() {
std::shared_ptr<XmlNode> p = parent.lock();
if(p) {
p->add_child_node(shared_from_this());
}
set_indentation_depth();
}
void XmlNode::add_child_node(std::shared_ptr<XmlNode> child) {
children.push_back(child);
}
void XmlNode::add_attribute(std::string attribute, std::string attribute_value) {
node_data.begin = node_data.begin.substr(0, node_data.begin.size() - 2); // remove closing ">" @ node
node_data.begin += " " + attribute + "=" + "\"" + attribute_value + "\">\n";
}
void XmlNode::add_node_name(std::string node_name, std::string value) {
std::string temp;
if(!value.empty()) {
temp = "=\"" + value + "\"";
}
node_data.begin += node_data.node_indentation + "<" + node_name + "" + temp + ">\n";
node_data.end += node_data.node_indentation + "</" + node_name + ">\n";
}
void XmlNode::add_node_text(std::string text) {
node_data.node_text += node_data.node_indentation + "\t" + text;
}
int XmlNode::calculate_node_depth() {
std::shared_ptr<XmlNode> p = parent.lock();
if(p) {
return node_data.node_depth += p->calculate_node_depth() + 1;
}
return node_data.node_depth;
}
void XmlNode::set_indentation_depth() {
uint8_t n_depth = calculate_node_depth();
while(n_depth--) {
node_data.node_indentation += "\t";
}
}
//ToDo method should be renamed to clear_node and clear_node has to be renamed to something else.
void XmlNode::clear_node_data() {
for(auto child : children) {
child->clear_node_data();
child->node_data.node_indentation = child->node_data.node_depth = 0;
child->node_data.begin = child->node_data.end = child->node_data.node_text = "";
}
node_data. node_indentation = node_data.node_depth = 0;
node_data.begin = node_data.end = node_data.node_text = "";
}

std::string XmlNode::get_all_nodes_data() {
std::string xml_data = node_data.begin;
xml_data += node_data.node_text;
for(auto child : children) {
xml_data += child->get_all_nodes_data();
}
xml_data += node_data.end;
return xml_data;
}
2 changes: 1 addition & 1 deletion test/testfiles/BaseTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class BaseTest {
};


#endif //FAGAN_BASETEST_HPP
#endif //FAGAN_BASETEST_HPP

0 comments on commit 2aead6f

Please sign in to comment.