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.";
50 fb = dynamic_cast<Fallback*>(fbNew(fbClass, attrs));
52 throw string("Fallback widget '") + fbClass + " not found!";
55 throw fbClass + " is not a Fallback Widget!";
62 void Parser::on_end_element(const string& name) {
64 cerr << "In Parser::on_end_element('" << name << "');" << endl;
66 Widget* cur = stack.top();
68 // If is the last widget, it's the root widget.
71 // If is not the last widget, we add it as content of his parent.
73 Container* par = dynamic_cast<Container*>(stack.top());
74 // If the parent is a Container, we add curent widget as content.
77 // If not, we raise an exception TODO
79 throw "Trying to add content to a non-container widget.";
84 void Parser::on_characters(const string& chars) {
86 cerr << "In Parser::on_characters('" << chars << "');" << endl;
89 Container* cur = dynamic_cast<Container*>(stack.top());
90 // If we are in a Container, we add curent string widget as content.
92 cur->append(new String(chars));
93 // If not, we raise an exception TODO
95 throw "Trying to add content to a non-container widget.";
98 // FIXME - investigar si tiene sentido.
99 throw "Characters with no tags!!!?!?!?!";
103 void Parser::on_comment(const string& text) {
105 cerr << "In Parser::on_comment('" << text << "');" << endl;
109 void Parser::on_warning(const string& warn) {
111 cerr << "In Parser::on_warning('" << warn << "');" << endl;
115 void Parser::on_error(const string& error) {
117 cerr << "In Parser::on_error('" << error << "');" << endl;
121 void Parser::on_fatal_error(const string& error) {
123 cerr << "In Parser::on_fatal_error('" << error << "');" << endl;
127 void Parser::on_validity_error(const string& error) {
129 cerr << "In Parser::on_validity_error('" << error << "');" << endl;
133 void Parser::on_validity_warning(const string& warn) {
135 cerr << "In Parser::on_validity_warning('" << warn << "');" << endl;
140 Parser::Parser(void): fallbackConstructor(NULL), fallbackDestructor(NULL), root(NULL) {
142 cerr << "In Parser::Parser();" << endl;
144 void* fb = dlopen("./translate.so", RTLD_LAZY); // XXX - asà anda: RTLD_NOW | RTLD_GLOBAL);
146 throw string("No se puede cargar el plug-in: ") + dlerror();
148 fb_create = (create_t*)dlsym(fb, "create");
149 fb_destroy = (destroy_t*)dlsym(fb, "destroy");
150 if (!fb_create || !fb_destroy) {
151 throw string("No se puede cargar el creador del plug-in: ") + dlerror();
156 Parser::Parser(const string& fallback): fbNew(NULL), fbDel(NULL), root(NULL) {
158 cerr << "In Parser::Parser(fallback = '" << fallback << "');" << endl;
160 if (!fallback.empty()) {
161 string::size_type pos = fallback.find(".");
162 if (pos == string::npos) {
163 throw string("Fallback module not specified in fallback name: ") + fallback;
165 fbClass = fallback.substr(pos + 1, fallback.length() - 1);
166 // Opens the fallback module.
167 string modules_dir = "translate";
168 string fb_module = modules_dir + "/" + fallback.substr(0, pos) + ".so";
169 void* dlh = dlopen(fb_module.c_str(), RTLD_LAZY);
171 throw string("No se puede cargar el plug-in: ") + dlerror();
173 fbNew = (Widget::Constructor*)dlsym(dlh, "bife_widget_constructor");
174 fbDel = (Widget::Destructor*)dlsym(dlh, "bife_widget_destructor");
175 if (!fbNew || !fbDel) {
176 throw string("No se puede cargar el creador del plug-in: ") + dlerror();
178 // TODO - CLOSE dl handler, destroy objects.
182 Parser::~Parser(void) {
184 cerr << "In Parser destructor." << endl;
188 Parser::operator string(void) const {