Kitlist  1.1.0
kitparser.cpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of Kitlist, a program to maintain a simple list
4  of items and assign items to one or more categories.
5 
6  Copyright (C) 2008,2009 Frank Dean
7 
8  Kitlist is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Kitlist is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Kitlist. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #include "kitparser.hpp"
24 
25 #include <algorithm>
26 #include <iostream>
27 
30 // std::cout << "on_start_document()" << std::endl;
31 }
32 
33 
36 // std::cout << "on_end_document()" << std::endl;
37 }
38 
39 
45 void KitParser::process_item(const AttributeList& attributes) {
46  m_item = new ModelItem();
47  xmlpp::SaxParser::AttributeList::const_iterator attribute =
48  std::find_if(attributes.begin(), attributes.end(), AttributeHasName("id"));
49  if (attribute != attributes.end()) {
50  m_item->set_id(atol(attribute->value.c_str()));
51  } else {
52  std::cout << "WARNING: Missing id attribute for a item element " << std::endl;
53  }
54  attribute =
55  std::find_if(attributes.begin(), attributes.end(), AttributeHasName("checked"));
56  if (attribute != attributes.end()) {
57  m_item->set_checked(attribute->value.casefold().compare("false") != 0);
58  }
60 }
61 
62 
68 void KitParser::process_category(const AttributeList& attributes) {
69  xmlpp::SaxParser::AttributeList::const_iterator attribute =
70  std::find_if(attributes.begin(), attributes.end(), AttributeHasName("id"));
71  if (attribute != attributes.end()) {
72  m_category = new ModelCategory();
73  m_category->set_id(atol(attribute->value.c_str()));
75  } else {
76  std::cout << "WARNING: Missing id attribute for a category element " << std::endl;
77  }
78 }
79 
80 
87 void KitParser::process_category_item(const AttributeList& attributes) {
88  xmlpp::SaxParser::AttributeList::const_iterator attribute =
89  std::find_if(attributes.begin(), attributes.end(), AttributeHasName("id"));
90  if (attribute != attributes.end()) {
91  if (m_category != NULL) {
92  ModelItem* item = m_model.find_item(atol(attribute->value.c_str()));
93  if (item) {
94  m_category->add_item(item);
95  } else {
96  std::cout << "Unable to find item id: "
97  << attribute->value
98  << "belonging to category id: "
99  << m_category->get_id()
100  << std::endl;
101  }
102  }
103  } else {
104  std::cout << "WARNING: Missing id attribute for a category-item element " << std::endl;
105  }
106 }
107 
108 
115 void KitParser::on_start_element(const Glib::ustring& name, const AttributeList& attributes) {
116  // reset the CDATA store at the start of a new element
117  m_cdata.clear();
118 // std::cout << "on_start_element(): " << name << std::endl;
119  if (name.compare("item") == 0) {
120  process_item(attributes);
121  } else if (name.compare("category") == 0) {
122  process_category(attributes);
123  } else if (name.compare("category-item") == 0) {
124  process_category_item(attributes);
125  }
126  // Attributes
127 // for (xmlpp::SaxParser::AttributeList::const_iterator
128 // iter = attributes.begin(); iter != attributes.end(); ++iter) {
129 // std::cout << " Attribute: " << iter->name << " = \"" << iter->value << "\"" << std::endl;
130 // }
131 }
132 
133 
141 void KitParser::on_end_element(const Glib::ustring& name) {
142 // std::cout << "on_end_element(): " << name << std::endl;
143  if (name.compare("item") == 0 && m_item != NULL && m_cdata.size() > 0) {
144  m_item->set_description(m_cdata.c_str());
145  } else if (name.compare("category-name") == 0 && m_category != NULL && m_cdata.size() > 0) {
146 // std::cout << "Setting category name to: " << name << std::endl;
147  m_category->set_name(m_cdata.c_str());
148  }
149 }
150 
151 
164 void KitParser::on_characters(const Glib::ustring& text) {
165 // std::cout << "on_characters(): " << text << std::endl;
166  m_cdata += text;
167 }
168 
169 
171 void KitParser::on_comment(const Glib::ustring& text) {
172 // std::cout << "on_comment(): " << text << std::endl;
173 }
174 
175 
177 void KitParser::on_warning(const Glib::ustring& text) {
178  std::cout << "on_warning(): " << text << std::endl;
179 }
180 
181 
183 void KitParser::on_error(const Glib::ustring& text) {
184  std::cout << "on_error(): " << text << std::endl;
185 }
186 
187 
189 void KitParser::on_fatal_error(const Glib::ustring& text) {
190  std::cout << "on_fatal_error: " << text << std::endl;
191 }
virtual void on_error(const Glib::ustring &text)
Outputs any errors to STDOUT.
Definition: kitparser.cpp:183
virtual void on_start_element(const Glib::ustring &name, const AttributeList &attributes)
Called for each element.
Definition: kitparser.cpp:115
virtual void on_end_document()
Does nothing. Called at the end of a document.
Definition: kitparser.cpp:35
ModelItem * m_item
The most recently processed item element.
Definition: kitparser.hpp:38
void process_item(const AttributeList &attributes)
<The most recently processed CDATA
Definition: kitparser.cpp:45
Represents a Category combined with GuiState attributes.
Definition: kitmodel.hpp:94
virtual void on_comment(const Glib::ustring &text)
Does nothing. Called for each comment.
Definition: kitparser.cpp:171
virtual void add_category(ModelCategory *category)
Add a category to the model.
Definition: kitmodel.cpp:361
virtual ModelItem * find_item(long id)
Finds an item by it&#39;s unique ID.
Definition: kitmodel.cpp:278
virtual void on_warning(const Glib::ustring &text)
Outputs any warnings to STDOUT.
Definition: kitparser.cpp:177
void set_name(const std::string name)
Definition: category.hpp:46
void set_id(long id)
Definition: category.hpp:44
Glib::ustring m_cdata
Definition: kitparser.hpp:39
void process_category(const AttributeList &attributes)
Reads a category&#39;s attributes from a &#39;category&#39; element.
Definition: kitparser.cpp:68
virtual void on_characters(const Glib::ustring &text)
Called for each piece of CDATA belonging to an element.
Definition: kitparser.cpp:164
void set_id(long id)
Definition: item.hpp:45
virtual void on_end_element(const Glib::ustring &name)
Called at the end of each element.
Definition: kitparser.cpp:141
Represents an Item combined with GuiState attributes.
Definition: kitmodel.hpp:60
virtual void on_fatal_error(const Glib::ustring &text)
Outputs any fatal errors to STDOUT.
Definition: kitparser.cpp:189
virtual void on_start_document()
Does nothing. Called at the start of a document.
Definition: kitparser.cpp:29
long get_id()
Definition: category.hpp:45
virtual void set_checked(bool checked)
Definition: kitmodel.hpp:64
virtual void add_item(Item *)
Adds the passed item to this ModelCategory.
Definition: kitmodel.cpp:88
virtual void add_item(ModelItem *item)
Adds an item to the model.
Definition: kitmodel.cpp:371
ModelCategory * m_category
The most recently processed category element.
Definition: kitparser.hpp:37
void process_category_item(const AttributeList &attributes)
Reads details of an item association with a category from a &#39;category-item&#39; element.
Definition: kitparser.cpp:87
KitModel & m_model
Definition: kitparser.hpp:48
void set_description(const std::string description)
Definition: item.hpp:47

Copyright 2008-2021 Frank Dean