00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <string.h>
00021 #include <boost/lexical_cast.hpp>
00022 #include <boost/algorithm/string/predicate.hpp>
00023 #include <curl/curl.h>
00024 #include "weHelper.h"
00025 #include <log4cxx/basicconfigurator.h>
00026 #include <log4cxx/propertyconfigurator.h>
00027
00028 using namespace log4cxx;
00029 using namespace log4cxx::helpers;
00030 using namespace boost::algorithm;
00031
00032 WeHtmlFactory weHtmlFactory;
00033 WeTransportFactory weTransportFactory;
00034 static bool isLibInited = false;
00035
00042 void WeLibInit(void)
00043 {
00044 CURLcode cCode;
00045 FUNCTION;
00046 try {
00047 #ifdef WIN32
00048 PropertyConfigurator::configure(log4cxx::File("trace.config"), LOG4CXX_STR("webEngine"));
00049 #else
00050 PropertyConfigurator::configure(log4cxx::File("trace.config"));
00051 #endif
00052
00053 }
00054 catch (...) {
00055 try {
00056 BasicConfigurator::configure();
00057 }
00058 catch (...) {
00059 throw WeError("Can't initialize logging subsystem!");
00060 }
00061 };
00062 LOG4CXX_TRACE(WeLogger::GetLogger(), "WeLibInit status = " << isLibInited);
00063 if (!isLibInited)
00064 {
00065 weHtmlFactory.Init();
00066 weTransportFactory.Init();
00067 cCode = curl_global_init(CURL_GLOBAL_ALL);
00068 if (cCode != 0) {
00069 throw WeError(string("cURL initialization failed : ") + boost::lexical_cast<std::string>(cCode));
00070 }
00071 }
00072 isLibInited = true;
00073 }
00074
00080 void WeLibClose(void)
00081 {
00082 FUNCTION;
00083 LOG4CXX_TRACE(WeLogger::GetLogger(), "WeLibClose status = " << isLibInited);
00084 if (!isLibInited) {
00085 return;
00086 }
00087 curl_global_cleanup();
00088
00089 isLibInited = false;
00090 }
00091
00092 #ifndef __DOXYGEN__
00094 // HTML elements creation functions
00096 static WeHtmlEntity* weCreateRefObj(iweEntity* prnt)
00097 {
00098 FUNCTION;
00099 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory: create WeRefrenceObject");
00100 return new WeRefrenceObject(prnt);
00101 }
00102
00103 static WeHtmlEntity* weCreateText(iweEntity* prnt)
00104 {
00105 FUNCTION;
00106 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory: create WeInnerText");
00107 return new WeInnerText(prnt);
00108 }
00109
00110 static WeHtmlEntity* weCreateComment(iweEntity* prnt)
00111 {
00112 FUNCTION;
00113 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory: create WeHtmlComment");
00114 return new WeHtmlComment(prnt);
00115 }
00116
00117 static WeHtmlEntity* weCreateCData(iweEntity* prnt)
00118 {
00119 FUNCTION;
00120 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory: create WeCData");
00121 return new WeCData(prnt);
00122 }
00123
00124 static WeHtmlEntity* weCreatePhpInc(iweEntity* prnt)
00125 {
00126 FUNCTION;
00127 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory: create WePhpInclude");
00128 return new WePhpInclude(prnt);
00129 }
00130
00131 static WeHtmlEntity* weCreateDocument(iweEntity* prnt)
00132 {
00133 FUNCTION;
00134 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory: create WeHtmlDocument");
00135 return NULL;
00136 }
00137
00138 static WeHtmlEntity* weCreateScript(iweEntity* prnt)
00139 {
00140 FUNCTION;
00141 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory: create WeScript");
00142 return new WeScript(prnt);
00143 }
00144
00146
00148 static iweTransport* weCreateHttp()
00149 {
00150 FUNCTION;
00151 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeTransportFactory: create WeHTTP");
00152 return NULL;
00153 }
00154
00155 static iweTransport* weCreateFile()
00156 {
00157 FUNCTION;
00158 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeTransportFactory: create WeFile");
00159 return NULL;
00160 }
00161
00162 #endif //__DOXYGEN__
00163
00164
00165 WeHtmlFactory::WeHtmlFactory() :
00166 WeLinkedList<string, EntityFactory>()
00167 {
00168 data = new WeLinkedListElem<string, EntityFactory>;
00169 data->Key("");
00170 data->Value(NULL);
00171 data->Link(NULL);
00172 }
00173
00174 void WeHtmlFactory::Add( string name, EntityFactory func )
00175 {
00176 WeLinkedListElem<string, EntityFactory>* obj;
00177
00178 LOG4CXX_TRACE(WeLogger::GetLogger(), "new EntityFactory added for " << name);
00179 obj = new WeLinkedListElem<string, EntityFactory>();
00180 obj->Key(name);
00181 obj->Value(func);
00182 data->Add(obj);
00183 }
00184
00185 void WeHtmlFactory::Init()
00186 {
00187 Clear();
00188 data = new WeLinkedListElem<string, EntityFactory>;
00189 data->Key("");
00190 data->Value(NULL);
00191 data->Link(NULL);
00192 Add("#document", weCreateDocument);
00193 Add("#text", weCreateText);
00194 Add("#comment", weCreateComment);
00195 Add("#cdata", weCreateCData);
00196 Add("#php", weCreatePhpInc);
00197
00198
00199
00200
00201
00202 }
00203
00212 WeHtmlEntity* WeHtmlFactory::CreateEntity( string tagName, WeHtmlEntity* prnt )
00213 {
00214 EntityFactory func;
00215
00216 FUNCTION;
00217 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeHtmlFactory::CreateEntity => " << tagName);
00218 func = FindFirst(tagName);
00219 if (func == NULL) {
00220 return new WeHtmlEntity(prnt);
00221 }
00222 return func(prnt);
00223 }
00224
00228 WeTransportFactory::WeTransportFactory() :
00229 WeLinkedList<string, TransportFactory>()
00230 {
00231 data = new WeLinkedListElem<string, TransportFactory>;
00232 data->Key("");
00233 data->Value(NULL);
00234 data->Link(NULL);
00235 }
00236
00237 void WeTransportFactory::Add( string name, TransportFactory func )
00238 {
00239 WeLinkedListElem<string, TransportFactory>* obj;
00240
00241 LOG4CXX_TRACE(WeLogger::GetLogger(), "new TransportFactory added for " << name);
00242 obj = new WeLinkedListElem<string, TransportFactory>();
00243 obj->Key(name);
00244 obj->Value(func);
00245 data->Add(obj);
00246 }
00247
00248 void WeTransportFactory::Init()
00249 {
00250 Clear();
00251 data = new WeLinkedListElem<string, TransportFactory>;
00252 data->Key("");
00253 data->Value(NULL);
00254 data->Link(NULL);
00255 Add("http", weCreateHttp);
00256 Add("file", weCreateFile);
00257 }
00265 iweTransport* WeTransportFactory::CreateTransport( string tagName)
00266 {
00267 TransportFactory func;
00268
00269 FUNCTION;
00270 LOG4CXX_DEBUG(WeLogger::GetLogger(), "WeTransportFactory::CreateTransport => " << tagName);
00271 func = FindFirst(tagName);
00272 if (func == NULL) {
00273 return NULL;
00274 }
00275 return func();
00276 }