00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "kitparser.hpp"
00024
00025 #include <algorithm>
00026 #include <iostream>
00027
00029 void KitParser::on_start_document() {
00030
00031 }
00032
00033
00035 void KitParser::on_end_document() {
00036
00037 }
00038
00039
00045 void KitParser::process_item(const AttributeList& attributes) {
00046 m_item = new ModelItem();
00047 xmlpp::SaxParser::AttributeList::const_iterator attribute =
00048 std::find_if(attributes.begin(), attributes.end(), AttributeHasName("id"));
00049 if (attribute != attributes.end()) {
00050 m_item->set_id(atol(attribute->value.c_str()));
00051 } else {
00052 std::cout << "WARNING: Missing id attribute for a item element " << std::endl;
00053 }
00054 attribute =
00055 std::find_if(attributes.begin(), attributes.end(), AttributeHasName("checked"));
00056 if (attribute != attributes.end()) {
00057 m_item->set_checked(attribute->value.casefold().compare("false") != 0);
00058 }
00059 m_model.add_item(m_item);
00060 }
00061
00062
00068 void KitParser::process_category(const AttributeList& attributes) {
00069 xmlpp::SaxParser::AttributeList::const_iterator attribute =
00070 std::find_if(attributes.begin(), attributes.end(), AttributeHasName("id"));
00071 if (attribute != attributes.end()) {
00072 m_category = new ModelCategory();
00073 m_category->set_id(atol(attribute->value.c_str()));
00074 m_model.add_category(m_category);
00075 } else {
00076 std::cout << "WARNING: Missing id attribute for a category element " << std::endl;
00077 }
00078 }
00079
00080
00087 void KitParser::process_category_item(const AttributeList& attributes) {
00088 xmlpp::SaxParser::AttributeList::const_iterator attribute =
00089 std::find_if(attributes.begin(), attributes.end(), AttributeHasName("id"));
00090 if (attribute != attributes.end()) {
00091 if (m_category != NULL) {
00092 ModelItem* item = m_model.find_item(atol(attribute->value.c_str()));
00093 if (item) {
00094 m_category->add_item(item);
00095 } else {
00096 std::cout << "Unable to find item id: "
00097 << attribute->value
00098 << "belonging to category id: "
00099 << m_category->get_id()
00100 << std::endl;
00101 }
00102 }
00103 } else {
00104 std::cout << "WARNING: Missing id attribute for a category-item element " << std::endl;
00105 }
00106 }
00107
00108
00115 void KitParser::on_start_element(const Glib::ustring& name, const AttributeList& attributes) {
00116
00117 m_cdata.clear();
00118
00119 if (name.compare("item") == 0) {
00120 process_item(attributes);
00121 } else if (name.compare("category") == 0) {
00122 process_category(attributes);
00123 } else if (name.compare("category-item") == 0) {
00124 process_category_item(attributes);
00125 }
00126
00127
00128
00129
00130
00131 }
00132
00133
00141 void KitParser::on_end_element(const Glib::ustring& name) {
00142
00143 if (name.compare("item") == 0 && m_item != NULL && m_cdata.size() > 0) {
00144 m_item->set_description(m_cdata.c_str());
00145 } else if (name.compare("category-name") == 0 && m_category != NULL && m_cdata.size() > 0) {
00146
00147 m_category->set_name(m_cdata.c_str());
00148 }
00149 }
00150
00151
00164 void KitParser::on_characters(const Glib::ustring& text) {
00165
00166 m_cdata += text;
00167 }
00168
00169
00171 void KitParser::on_comment(const Glib::ustring& text) {
00172
00173 }
00174
00175
00177 void KitParser::on_warning(const Glib::ustring& text) {
00178 std::cout << "on_warning(): " << text << std::endl;
00179 }
00180
00181
00183 void KitParser::on_error(const Glib::ustring& text) {
00184 std::cout << "on_error(): " << text << std::endl;
00185 }
00186
00187
00189 void KitParser::on_fatal_error(const Glib::ustring& text) {
00190 std::cout << "on_fatal_error: " << text << std::endl;
00191 }