1 // vim: set expandtab tabstop=4 shiftwidth=4:
3 #include "libbife/widget.h"
4 #include "libbife/container.h"
5 #include "libbife/fallback.h"
6 #include "libbife/string.h"
11 //using std::stringstream;
20 void Parser::on_start_document(void) {
22 cerr << "In Parser::on_start_document();" << endl;
26 void Parser::on_end_document(void) {
28 cerr << "In Parser::on_end_document();" << endl;
32 void Parser::on_start_element(const string& name, const AttributeMap& attrs) {
34 cerr << "In Parser::on_start_element(name = '" << name << "', attrs = [";
36 Hash::const_iterator last = attrs.end();
38 for (Hash::const_iterator i = attrs.begin(); i != last; i++) { //last; i++) {
39 cerr << i->first << ": '" << i->second << "', ";
41 cerr << last->first << ": '" << last->second << "'";
43 cerr << "]);" << endl;
45 if (fbClass.empty()) {
46 throw string("Widget '") + name + "' not found and now using a fallback class.";
48 stack.push(fbNew(fbClass, name, attrs));
52 void Parser::on_end_element(const string& name) {
54 cerr << "In Parser::on_end_element('" << name << "');" << endl;
56 Widget* cur = stack.top();
58 // If is the last widget, it's the root widget.
61 // If is not the last widget, we add it as content of his parent.
63 Container* par = dynamic_cast<Container*>(stack.top());
64 // If the parent is a Container, we add curent widget as content.
67 // If not, we raise an exception TODO
69 throw "Trying to add content to a non-container widget.";
74 void Parser::on_characters(const string& chars) {
76 cerr << "In Parser::on_characters('" << chars << "');" << endl;
79 Container* cur = dynamic_cast<Container*>(stack.top());
80 // If we are in a Container, we add curent string widget as content.
82 cur->append(new String(chars));
83 // If not, we raise an exception TODO
85 throw "Trying to add content to a non-container widget.";
88 // FIXME - investigar si tiene sentido.
89 throw "Characters with no tags!!!?!?!?!";
93 void Parser::on_comment(const string& text) {
95 cerr << "In Parser::on_comment('" << text << "');" << endl;
99 void Parser::on_warning(const string& warn) {
101 cerr << "In Parser::on_warning('" << warn << "');" << endl;
105 void Parser::on_error(const string& error) {
107 cerr << "In Parser::on_error('" << error << "');" << endl;
111 void Parser::on_fatal_error(const string& error) {
113 cerr << "In Parser::on_fatal_error('" << error << "');" << endl;
117 void Parser::on_validity_error(const string& error) {
119 cerr << "In Parser::on_validity_error('" << error << "');" << endl;
123 void Parser::on_validity_warning(const string& warn) {
125 cerr << "In Parser::on_validity_warning('" << warn << "');" << endl;
130 Parser::Parser(void): fallbackConstructor(NULL), fallbackDestructor(NULL), root(NULL) {
132 cerr << "In Parser::Parser();" << endl;
134 void* fb = dlopen("./translate.so", RTLD_LAZY); // XXX - asà anda: RTLD_NOW | RTLD_GLOBAL);
136 throw string("No se puede cargar el plug-in: ") + dlerror();
138 fb_create = (create_t*)dlsym(fb, "create");
139 fb_destroy = (destroy_t*)dlsym(fb, "destroy");
140 if (!fb_create || !fb_destroy) {
141 throw string("No se puede cargar el creador del plug-in: ") + dlerror();
146 Parser::Parser(const string& fallback): fbNew(NULL), fbDel(NULL), root(NULL) {
148 cerr << "In Parser::Parser(fallback = '" << fallback << "');" << endl;
150 if (!fallback.empty()) {
151 string::size_type pos = fallback.find(".");
152 if (pos == string::npos) {
153 throw string("Fallback module not specified in fallback name: ") + fallback;
155 fbClass = fallback.substr(pos + 1, fallback.length() - 1);
156 // Opens the fallback module.
157 string modules_dir = "translate";
158 string fb_module = modules_dir + "/" + fallback.substr(0, pos) + ".so";
159 void* dlh = dlopen(fb_module.c_str(), RTLD_LAZY);
161 throw string("No se puede cargar el plug-in: ") + dlerror();
163 fbNew = (Fallback::Constructor*)dlsym(dlh, "fallback_constructor");
164 fbDel = (Widget::Destructor*)dlsym(dlh, "widget_destructor");
165 if (!fbNew || !fbDel) {
166 throw string("No se puede cargar el creador del plug-in: ") + dlerror();
168 // TODO - CLOSE dl handler, destroy objects.
172 Parser::~Parser(void) {
174 cerr << "In Parser destructor." << endl;
178 Parser::operator string(void) const {