menu: added new Keywords tag to .desktop files
[barry.git] / src / xmlparser.cc
blob34f041e3dab5dc5afc6e47b0c474bde32bd6a4a9
1 ///
2 /// \file xmlparser.cc
3 /// A simple XML parser (use callback on start, end and data block
4 ///
6 /*
7 Copyright (C) 2010, Nicolas VIVIEN
8 Copyright (C) 2005-2013, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <iostream>
25 #include "xmlparser.h"
28 namespace Barry {
30 namespace XML {
33 XMLParser::XMLParser(std::istream& input, const char *charset)
34 : xmlpp::SaxParser()
35 , input(input)
37 this->depth = 0;
38 this->charset = charset;
42 XMLParser::~XMLParser(void)
47 const unsigned long XMLParser::GetDepth(void) const
49 return depth;
53 bool XMLParser::Run(void)
55 try {
56 set_substitute_entities(true);
57 parse_chunk("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
59 std::string line;
60 while( getline(input, line) ) {
61 parse_chunk(line);
63 finish_chunk_parsing();
65 catch (const xmlpp::exception& ex) {
66 std::cout << "libxml++ exception: " << ex.what() << std::endl;
67 return false;
70 return true;
74 void XMLParser::on_start_document()
76 std::cout << "on_start_document()" << std::endl;
80 void XMLParser::on_end_document()
82 std::cout << "on_end_document()" << std::endl;
86 void XMLParser::on_start_element(const Glib::ustring& name,
87 const xmlpp::SaxParser::AttributeList& attributes)
89 std::cout << "Start:" << name << std::endl;
90 depth++;
92 // Print attributes:
93 for (xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
94 std::cout << " Attribute name=" << iter->name << std::endl;
96 std::cout << " , value= " << iter->value << std::endl;
101 void XMLParser::on_end_element(const Glib::ustring& name)
103 std::cout << "End:" << name << std::endl;
104 depth--;
108 void XMLParser::on_characters(const Glib::ustring& text)
110 std::cout << " Data:" << text << std::endl;
114 void XMLParser::on_comment(const Glib::ustring& text)
116 std::cout << "on_comment(): " << text << std::endl;
120 void XMLParser::on_warning(const Glib::ustring& text)
122 std::cout << "on_warning(): " << text << std::endl;
126 void XMLParser::on_error(const Glib::ustring& text)
128 std::cout << "on_error(): " << text << std::endl;
132 void XMLParser::on_fatal_error(const Glib::ustring& text)
134 std::cout << "on_fatal_error(): " << text << std::endl;
138 } // namespace XML
140 } // namespace Barry