summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-03-10 13:53:07 +0000
committerrandomdan <randomdan@localhost>2011-03-10 13:53:07 +0000
commitddf64c4da3152222f64fdce1de320b7f9adb1906 (patch)
tree0a622525e8603f74e7ccfda843f16bf5d8cce578
parentFix surplus local pointers causing random crashes (diff)
downloadproject2-ddf64c4da3152222f64fdce1de320b7f9adb1906.tar.bz2
project2-ddf64c4da3152222f64fdce1de320b7f9adb1906.tar.xz
project2-ddf64c4da3152222f64fdce1de320b7f9adb1906.zip
Allow scripts to propergate messages from parameter checks into the presentation layer
Also allow variables to be constructed from a child element's text
-rw-r--r--project2/appEngine.cpp31
-rw-r--r--project2/appEngine.h18
-rw-r--r--project2/cgi/cgiAppEngine.cpp1
-rw-r--r--project2/cgi/cgiStageInitial.cpp6
-rw-r--r--project2/cgi/cgiStagePresent.cpp1
-rw-r--r--project2/cgi/cgiStageRequest.cpp1
-rw-r--r--project2/console/consoleAppEngine.cpp1
-rw-r--r--project2/exceptions.h5
-rw-r--r--project2/paramChecker.cpp3
-rw-r--r--project2/paramChecker.h8
-rw-r--r--project2/variables.cpp14
11 files changed, 81 insertions, 8 deletions
diff --git a/project2/appEngine.cpp b/project2/appEngine.cpp
index 1489cd1..9abe834 100644
--- a/project2/appEngine.cpp
+++ b/project2/appEngine.cpp
@@ -1,5 +1,7 @@
#include "appEngine.h"
+#include "logger.h"
#include <stdexcept>
+#include <boost/foreach.hpp>
ApplicationEngine * ApplicationEngine::currentEngine = NULL;
@@ -17,3 +19,32 @@ ApplicationEngine::~ApplicationEngine()
currentEngine = NULL;
}
+void
+ApplicationEngine::logMessage(bool writeLog, const Glib::ustring & g, const Glib::ustring & m)
+{
+ if (writeLog) {
+ Logger()->messagef(LOG_NOTICE, "%s: %s: %s", __PRETTY_FUNCTION__, g.c_str(), m.c_str());
+ }
+ appMessages.push_back(new Message(g, m));
+}
+
+void
+ApplicationEngine::addCoreAppData(const Presenter * p) const
+{
+ // Message log
+ p->pushSub("messages", env()->getXmlPrefix());
+ BOOST_FOREACH(const Messages::value_type & m, appMessages) {
+ p->pushSub("message");
+ p->addAttr("group", m->group);
+ p->addAttr("text", m->message);
+ p->popSub();
+ }
+ p->popSub();
+}
+
+ApplicationEngine::Message::Message(const Glib::ustring & g, const Glib::ustring & m) :
+ group(g),
+ message(m)
+{
+}
+
diff --git a/project2/appEngine.h b/project2/appEngine.h
index a9df07e..689f1b5 100644
--- a/project2/appEngine.h
+++ b/project2/appEngine.h
@@ -8,9 +8,21 @@
class ApplicationEngine : public Configuration {
public:
+ class Message : public IntrusivePtrBase {
+ public:
+ Message(const Glib::ustring & g, const Glib::ustring & m);
+
+ const Glib::ustring group;
+ const Glib::ustring message;
+ };
+ typedef boost::intrusive_ptr<Message> MessagePtr;
+ typedef std::list<MessagePtr> Messages;
+
ApplicationEngine(const Glib::ustring & engineKeys);
virtual ~ApplicationEngine() = 0;
+ void logMessage(bool writeLog, const Glib::ustring & g, const Glib::ustring & m);
+
virtual void process() const = 0;
virtual const Environment * env() const = 0;
virtual SessionPtr session() const = 0;
@@ -18,6 +30,12 @@ class ApplicationEngine : public Configuration {
virtual void addEnvData(const Presenter * p) const = 0;
static ApplicationEngine * getCurrent() { return currentEngine; }
+
+ protected:
+ void addCoreAppData(const Presenter * p) const;
+
+ Messages appMessages;
+
private:
static ApplicationEngine * currentEngine;
};
diff --git a/project2/cgi/cgiAppEngine.cpp b/project2/cgi/cgiAppEngine.cpp
index 547506d..52e69cf 100644
--- a/project2/cgi/cgiAppEngine.cpp
+++ b/project2/cgi/cgiAppEngine.cpp
@@ -155,6 +155,7 @@ CgiApplicationEngine::addEnvData(const Presenter * p) const
void
CgiApplicationEngine::addAppData(const Presenter * p) const
{
+ addCoreAppData(p);
// Sessions variables
if (!sessionID.is_nil()) {
p->pushSub("session", env()->getXmlPrefix());
diff --git a/project2/cgi/cgiStageInitial.cpp b/project2/cgi/cgiStageInitial.cpp
index a9315bc..d302971 100644
--- a/project2/cgi/cgiStageInitial.cpp
+++ b/project2/cgi/cgiStageInitial.cpp
@@ -1,5 +1,8 @@
#include "cgiAppEngine.h"
#include "cgiEnvironment.h"
+#include "../exceptions.h"
+
+StaticMessageException(EmptyRequestURL, "Request URL cannot be empty");
CgiApplicationEngine::InitialStage::InitialStage(const CgiEnvironment * env) :
CgiApplicationEngine::Stage(env)
@@ -10,6 +13,9 @@ CgiApplicationEngine::StagePtr
CgiApplicationEngine::InitialStage::run()
{
if (e->getRequestMethod() == "POST") {
+ if (e->elems.empty()) {
+ throw EmptyRequestURL();
+ }
return new RequestStage(e, e->elems[0]);
}
else {
diff --git a/project2/cgi/cgiStagePresent.cpp b/project2/cgi/cgiStagePresent.cpp
index 9dcf124..47db567 100644
--- a/project2/cgi/cgiStagePresent.cpp
+++ b/project2/cgi/cgiStagePresent.cpp
@@ -20,6 +20,7 @@ CgiApplicationEngine::PresentStage::run()
{
BOOST_FOREACH(ParamCheckers::value_type pc, parameterChecks.get<bySOOrder>()) {
if (!pc->performCheck()) {
+ ApplicationEngine::getCurrent()->logMessage(false, pc->group(), pc->message());
return new PresentStage(e, pc->present);
}
}
diff --git a/project2/cgi/cgiStageRequest.cpp b/project2/cgi/cgiStageRequest.cpp
index 6602527..6f877f7 100644
--- a/project2/cgi/cgiStageRequest.cpp
+++ b/project2/cgi/cgiStageRequest.cpp
@@ -25,6 +25,7 @@ CgiApplicationEngine::RequestStage::run()
{
BOOST_FOREACH(const ParamCheckers::value_type & pc, parameterChecks.get<bySOOrder>()) {
if (!pc->performCheck()) {
+ ApplicationEngine::getCurrent()->logMessage(false, pc->group(), pc->message());
return new PresentStage(e, pc->present);
}
}
diff --git a/project2/console/consoleAppEngine.cpp b/project2/console/consoleAppEngine.cpp
index 7a08f69..769add6 100644
--- a/project2/console/consoleAppEngine.cpp
+++ b/project2/console/consoleAppEngine.cpp
@@ -81,6 +81,7 @@ ConsoleApplicationEngine::process() const
{
BOOST_FOREACH(const ParamCheckers::value_type & pc, parameterChecks.get<bySOOrder>()) {
if (!pc->performCheck()) {
+ ApplicationEngine::getCurrent()->logMessage(false, pc->group(), pc->message());
throw std::runtime_error("Check failed");
}
}
diff --git a/project2/exceptions.h b/project2/exceptions.h
index 23faafc..a9d4952 100644
--- a/project2/exceptions.h
+++ b/project2/exceptions.h
@@ -12,6 +12,11 @@ class numeric_error : public std::exception {
int err;
mutable char * buf;
};
+#define StaticMessageException(Name, Text) \
+class Name : public std::runtime_error { \
+ public: \
+ Name() : std::runtime_error(Text) { } \
+}
#define SimpleMessageException(Name) \
class Name : public std::runtime_error { \
public: \
diff --git a/project2/paramChecker.cpp b/project2/paramChecker.cpp
index aa1dbff..0781e90 100644
--- a/project2/paramChecker.cpp
+++ b/project2/paramChecker.cpp
@@ -3,7 +3,8 @@
ParamChecker::ParamChecker(const xmlpp::Element * p) :
SourceObject(p),
- message(xmlChildText(p, "message")),
+ message(p, "message", false, "Check failed"),
+ group(p, "group", false, "default"),
present(p->get_attribute_value("present"))
{
}
diff --git a/project2/paramChecker.h b/project2/paramChecker.h
index 465b8e9..57addbc 100644
--- a/project2/paramChecker.h
+++ b/project2/paramChecker.h
@@ -3,10 +3,7 @@
#include <libxml/tree.h>
#include "sourceObject.h"
-
-class ApplicationEngine;
-class ParamChecker;
-typedef boost::intrusive_ptr<ParamChecker> ParamCheckerPtr;
+#include "variables.h"
/// Base class for Project2 compoments that perform tests/checks
class ParamChecker : public SourceObject {
@@ -16,7 +13,8 @@ class ParamChecker : public SourceObject {
virtual bool performCheck() const = 0;
- const Glib::ustring message;
+ const Variable message;
+ const Variable group;
const std::string present;
};
diff --git a/project2/variables.cpp b/project2/variables.cpp
index 2b2872d..a5cfa0d 100644
--- a/project2/variables.cpp
+++ b/project2/variables.cpp
@@ -1,4 +1,5 @@
#include "variables.h"
+#include "xmlObjectLoader.h"
#include "exceptions.h"
#include "appEngine.h"
#include "session.h"
@@ -389,8 +390,17 @@ Variable::Variable(const xmlpp::Element * e, const Glib::ustring & n, bool requi
var = new VariableParam(c);
else if (source == "config")
var = new VariableConfig(c);
- else if (source == "literal" || source.empty())
- var = new VariableLiteral(c->get_attribute_value("value"), getVariableTypeFromName(c->get_attribute_value("type")));
+ else if (source == "literal" || source.empty()) {
+ if (const xmlpp::Attribute * la = c->get_attribute("value")) {
+ var = new VariableLiteral(la->get_value(), getVariableTypeFromName(c->get_attribute_value("type")));
+ }
+ else if (const xmlpp::TextNode * t = c->get_child_text()) {
+ var = new VariableLiteral(t->get_content(), getVariableTypeFromName(c->get_attribute_value("type")));
+ }
+ else {
+ var = new VariableLiteral(VariableType());
+ }
+ }
else
throw UnknownVariableSource(source);
return;