00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "Message.h"
00027
00028 #include "ObjectDictionary.h"
00029 #include "Dictionary.h"
00030 #include "ObjectCollection.h"
00031 #include "Collection.h"
00032 #include "TCPLocation.h"
00033 #include "HTML.h"
00034
00035 namespace cmlabs {
00036
00037 Message::Message() {
00038 object = NULL;
00039 auxDict = NULL;
00040 timeDict = NULL;
00041 history = NULL;
00042 htmlCol = NULL;
00043 inReplyTo = NULL;
00044 priority = 0;
00045 timeToLive = 0;
00046 postedTime.setInvalid();
00047 receivedTime.setInvalid();
00048 setID(createUniqueID());
00049 attachedMsgs = NULL;
00050 }
00051
00052 Message::Message(const JString& xml) {
00053 object = NULL;
00054 auxDict = NULL;
00055 timeDict = NULL;
00056 history = NULL;
00057 htmlCol = NULL;
00058 inReplyTo = NULL;
00059 priority = 0;
00060 timeToLive = 0;
00061 postedTime.setInvalid();
00062 receivedTime.setInvalid();
00063 setID(createUniqueID());
00064 attachedMsgs = NULL;
00065 if (!Object::fromXML(xml)) {
00066 }
00067 }
00068
00069 Message::Message(XMLNode* node) {
00070 object = NULL;
00071 auxDict = NULL;
00072 timeDict = NULL;
00073 history = NULL;
00074 htmlCol = NULL;
00075 inReplyTo = NULL;
00076 priority = 0;
00077 timeToLive = 0;
00078 postedTime.setInvalid();
00079 receivedTime.setInvalid();
00080 setID(createUniqueID());
00081 attachedMsgs = NULL;
00082 if (!fromXML(node)) {
00083 }
00084 }
00085
00086 Message::Message(const Message &msg) {
00087
00088 object = NULL;
00089 auxDict = NULL;
00090 timeDict = NULL;
00091 history = NULL;
00092 htmlCol = NULL;
00093 inReplyTo = NULL;
00094 priority = 0;
00095 timeToLive = 0;
00096 setID(createUniqueID());
00097 attachedMsgs = NULL;
00098
00099 from = msg.from;
00100 to = msg.to;
00101 type = msg.type;
00102 id = msg.id;
00103 cc = msg.cc;
00104 origin = msg.origin;
00105 comment = msg.comment;
00106 priority = msg.priority;
00107 timeToLive = msg.timeToLive;
00108 stored = msg.stored;
00109 noreply = msg.noreply;
00110 if (msg.inReplyTo != NULL) {
00111 inReplyTo = (Reference*) msg.inReplyTo->clone();
00112 }
00113 isResponse = msg.isResponse;
00114
00115
00116 postedTime = msg.postedTime;
00117 receivedTime = msg.receivedTime;
00118
00119 if (msg.auxDict != NULL)
00120 auxDict = (Dictionary*) msg.auxDict->clone();
00121 if (msg.timeDict != NULL)
00122 timeDict = (ObjectDictionary*) msg.timeDict->clone();
00123 if (msg.history != NULL)
00124 history = (ObjectDictionary*) msg.history->clone();
00125
00126 language = msg.language;
00127 content = msg.content;
00128
00129 if (msg.object != NULL) {
00130
00131 object = msg.object->clone();
00132
00133
00134
00135
00136 }
00137 else
00138 object = NULL;
00139 attachedMsgs = NULL;
00140 }
00141
00142 Message& Message::operator=(const Message &msg) {
00143
00144 object = NULL;
00145 auxDict = NULL;
00146 timeDict = NULL;
00147 history = NULL;
00148 htmlCol = NULL;
00149 inReplyTo = NULL;
00150 priority = 0;
00151 timeToLive = 0;
00152 setID(createUniqueID());
00153 attachedMsgs = NULL;
00154
00155 from = msg.from;
00156 to = msg.to;
00157 type = msg.type;
00158 id = msg.id;
00159 cc = msg.cc;
00160 origin = msg.origin;
00161 comment = msg.comment;
00162 priority = msg.priority;
00163 timeToLive = msg.timeToLive;
00164 stored = msg.stored;
00165 noreply = msg.noreply;
00166 if (msg.inReplyTo != NULL) {
00167 delete(inReplyTo);
00168 inReplyTo = (Reference*) msg.inReplyTo->clone();
00169 }
00170 isResponse = msg.isResponse;
00171
00172
00173 postedTime = msg.postedTime;
00174 receivedTime = msg.receivedTime;
00175
00176 if (msg.auxDict != NULL)
00177 auxDict = (Dictionary*) msg.auxDict->clone();
00178 if (msg.timeDict != NULL)
00179 timeDict = (ObjectDictionary*) msg.timeDict->clone();
00180 if (msg.history != NULL)
00181 history = (ObjectDictionary*) msg.history->clone();
00182
00183 language = msg.language;
00184 content = msg.content;
00185
00186 if (msg.object != NULL) {
00187
00188 object = msg.object->clone();
00189
00190
00191
00192
00193 }
00194 else
00195 object = NULL;
00196
00197 attachedMsgs = NULL;
00198 return *this;
00199 }
00200
00201
00202 Message::Message(
00203 const JString& frm,
00204 const JString& t,
00205 const JString& typ
00206 ) {
00207
00208 object = NULL;
00209 auxDict = NULL;
00210 timeDict = NULL;
00211 history = NULL;
00212 htmlCol = NULL;
00213 inReplyTo = NULL;
00214 priority = 0;
00215 timeToLive = 0;
00216 setID(createUniqueID());
00217 attachedMsgs = NULL;
00218 postedTime.setInvalid();
00219 receivedTime.setInvalid();
00220
00221 setFrom(frm);
00222 setTo(t);
00223 setType(typ);
00224 }
00225
00226 Message::Message(
00227 const JString& frm,
00228 const JString& t,
00229 const JString& typ,
00230 const JString& cont,
00231 const JString& lang
00232 ) {
00233 object = NULL;
00234 auxDict = NULL;
00235 timeDict = NULL;
00236 history = NULL;
00237 htmlCol = NULL;
00238 inReplyTo = NULL;
00239 priority = 0;
00240 timeToLive = 0;
00241 setID(createUniqueID());
00242 attachedMsgs = NULL;
00243 postedTime.setInvalid();
00244 receivedTime.setInvalid();
00245
00246 setFrom(frm);
00247 setTo(t);
00248 setType(typ);
00249 setContentNoParse(cont, lang);
00250 }
00251
00252 Message::Message(
00253 const JString& frm,
00254 const JString& t,
00255 const JString& typ,
00256 Object* obj
00257 ) {
00258 object = NULL;
00259 auxDict = NULL;
00260 timeDict = NULL;
00261 history = NULL;
00262 htmlCol = NULL;
00263 inReplyTo = NULL;
00264 priority = 0;
00265 timeToLive = 0;
00266 setID(createUniqueID());
00267 attachedMsgs = NULL;
00268 postedTime.setInvalid();
00269 receivedTime.setInvalid();
00270
00271 setFrom(frm);
00272 setTo(t);
00273 setType(typ);
00274 setObject(obj);
00275 }
00276
00277 Message::~Message() {
00278 if (object != NULL)
00279 delete(object);
00280 object = NULL;
00281 if (attachedMsgs != NULL)
00282 delete(attachedMsgs);
00283 attachedMsgs = NULL;
00284 if (auxDict != NULL)
00285 delete(auxDict);
00286 auxDict = NULL;
00287 if (timeDict != NULL)
00288 delete(timeDict);
00289 timeDict = NULL;
00290 if (history != NULL)
00291 delete(history);
00292 history = NULL;
00293 if (inReplyTo != NULL)
00294 delete(inReplyTo);
00295 inReplyTo = NULL;
00296 if (htmlCol != NULL)
00297 delete(htmlCol);
00298 htmlCol = NULL;
00299 }
00300
00301 bool Message::equals(const Object* o2) const {
00302 if (!isSameClass(o2))
00303 return false;
00304 return (id.equalsIgnoreCase(((Message*)o2)->id));
00305 }
00306
00307 int Message::compare(const Object* o2) const {
00308 if (!isSameClass(o2))
00309 return 0;
00310 if (priority == ((Message*)o2)->priority)
00311 return 0;
00312 else if (priority > ((Message*)o2)->priority)
00313 return 1;
00314 else
00315 return -1;
00316 }
00317
00318
00319
00320 bool Message::init() {
00321 return true;
00322 }
00323
00324 Message* Message::tinyClone() const {
00325 Message* msg;
00326 msg = new Message();
00327
00328 msg->from = from;
00329 msg->to = to;
00330 msg->type = type;
00331 msg->id = id;
00332 msg->cc = cc;
00333 msg->origin = origin;
00334 msg->comment = comment;
00335 msg->priority = priority;
00336 msg->timeToLive = timeToLive;
00337 msg->stored = stored;
00338 msg->isResponse = isResponse;
00339 msg->noreply = noreply;
00340
00341
00342 msg->postedTime = postedTime;
00343 msg->receivedTime = receivedTime;
00344
00345 return msg;
00346 }
00347
00348 Message* Message::shallowClone() const {
00349 Message* msg;
00350 msg = new Message();
00351
00352 msg->from = from;
00353 msg->to = to;
00354 msg->type = type;
00355 msg->id = id;
00356 msg->cc = cc;
00357 msg->origin = origin;
00358 msg->comment = comment;
00359 msg->priority = priority;
00360 msg->timeToLive = timeToLive;
00361 msg->stored = stored;
00362 msg->noreply = noreply;
00363 if (inReplyTo != NULL) {
00364
00365
00366
00367
00368
00369
00370
00371 msg->setInReplyTo((Reference*) inReplyTo->clone());
00372 }
00373 msg->isResponse = isResponse;
00374
00375
00376 msg->postedTime = postedTime;
00377 msg->receivedTime = receivedTime;
00378
00379 if (auxDict != NULL)
00380 msg->auxDict = (Dictionary*) auxDict->clone();
00381 if (timeDict != NULL)
00382 msg->timeDict = (ObjectDictionary*) timeDict->clone();
00383
00384
00385
00386 msg->setContentNoParse(content, language);
00387
00388 return msg;
00389 }
00390
00391
00392 Object* Message::clone() const {
00393 Message* msg;
00394 msg = new Message();
00395
00396 msg->from = from;
00397 msg->to = to;
00398 msg->type = type;
00399 msg->id = id;
00400 msg->cc = cc;
00401 msg->origin = origin;
00402 msg->comment = comment;
00403 msg->priority = priority;
00404 msg->timeToLive = timeToLive;
00405 msg->stored = stored;
00406 msg->noreply = noreply;
00407 if (inReplyTo != NULL)
00408 msg->setInReplyTo((Reference*) inReplyTo->clone());
00409 msg->isResponse = isResponse;
00410
00411
00412 msg->postedTime = postedTime;
00413 msg->receivedTime = receivedTime;
00414
00415 if (auxDict != NULL)
00416 msg->auxDict = (Dictionary*) auxDict->clone();
00417 if (timeDict != NULL)
00418 msg->timeDict = (ObjectDictionary*) timeDict->clone();
00419 if (history != NULL)
00420 msg->history = (ObjectDictionary*) history->clone();
00421
00422 msg->setContentNoParse(content, language);
00423
00424 if (object != NULL) {
00425
00426 msg->object = object->clone();
00427
00428
00429
00430
00431 }
00432 return (Object*) msg;
00433 }
00434
00435 bool Message::fromXML(const JString& xml) {
00436 if (xml.length() == 0)
00437 return false;
00438 XMLParser* xmlParser = new XMLParser();
00439 xmlParser->parse(xml);
00440 bool ret = fromXML(xmlParser->getRootNode());
00441 delete(xmlParser);
00442 return ret;
00443 }
00444
00445 bool Message::fromXML(XMLNode* node) {
00446
00447 if (node == NULL) {
00448 return false;
00449 }
00450
00451 if (!node->getTag().equalsIgnoreCase("message"))
00452 return false;
00453
00454 if (node->hasAttribute("priority"))
00455 priority = node->getAttribute("priority").toDouble();
00456 if (node->hasAttribute("timetolive"))
00457 timeToLive = node->getAttribute("timetolive").toDouble();
00458 if (node->hasAttribute("noreply"))
00459 noreply = node->getAttribute("noreply");
00460
00461 JString tag, value;
00462 XMLNode* xmlNode;
00463 XMLNode* subNode;
00464 XMLNode* refNode;
00465 ObjectCollection* nodes = node->getChildTags();
00466
00467 if (nodes != NULL) {
00468 for (int n=0; n<nodes->getCount(); n++) {
00469 xmlNode = (XMLNode*) nodes->get(n);
00470 if (xmlNode != NULL) {
00471 tag = xmlNode->getTag();
00472 value = xmlNode->getText();
00473 subNode = (XMLNode*) xmlNode->getFirstChildNode();
00474
00475 if (tag.equalsIgnoreCase("content")) {
00476 language = xmlNode->getAttribute("language");
00477 if ( (language.equalsIgnoreCase("xml")) && (subNode != NULL) ) {
00478 setObject(createObjectFromXML(subNode));
00479
00480 }
00481 else {
00482 setContentNoParse(value, language);
00483
00484 }
00485 }
00486 else if ( (subNode != NULL) && (tag.equalsIgnoreCase("history")) ) {
00487 if (history == NULL)
00488 history = new ObjectDictionary();
00489 JString subtag;
00490 JString author;
00491 ObjectCollection* refCol;
00492 ObjectCollection* groups = xmlNode->getChildTags();
00493 if (groups != NULL) {
00494 for (int m=0; m<groups->getCount(); m++) {
00495 subNode = (XMLNode*) groups->get(m);
00496
00497 if ((subNode != NULL) && (subNode->getTag().equalsIgnoreCase("group"))) {
00498 author = subNode->getAttribute("author");
00499 refCol = new ObjectCollection();
00500 history->put(author, refCol);
00501 ObjectCollection* refs = xmlNode->getChildTags();
00502 if (refs != NULL) {
00503 for (int k=0; k<refs->getCount(); k++) {
00504 refNode = (XMLNode*) refs->get(k);
00505 if ((refNode != NULL) && (refNode->getTag().equalsIgnoreCase("reference"))) {
00506 refCol->add(new Reference(refNode));
00507 }
00508 }
00509 }
00510 }
00511 }
00512 }
00513 }
00514 else if ((tag.equalsIgnoreCase("inreplyto")) && (subNode != NULL) && (subNode->getTag().equalsIgnoreCase("reference")) ) {
00515 delete(inReplyTo);
00516 inReplyTo = new Reference(subNode);
00517 }
00518 else if ( xmlNode->hasAttribute("sec") ) {
00519 setTime(tag, JTime(xmlNode));
00520 }
00521 else {
00522 set(tag, value);
00523 }
00524 }
00525 }
00526 }
00527
00528
00529
00530
00531
00532 return true;
00533 }
00534
00535
00536 JString Message::toXML() {
00537
00538 int n;
00539 JString params;
00540 JString key, value;
00541
00542 JString xml;
00543
00544 if (inReplyTo != NULL)
00545 xml = JString::format("<type>%s</type>\n<id>%s</id>\n<from>%s</from>\n<to>%s</to>\n<cc>%s</cc>\n<stored>%s</stored>\n<inreplyto>\n%s</inreplyto>\n<isresponse>%s</isresponse>\n",
00546 (char*) type.xmlStringEncode(), (char*) id.xmlStringEncode(), (char*) from.xmlStringEncode(), (char*) to.xmlStringEncode(), (char*) cc.xmlStringEncode(), (char*) stored.xmlStringEncode(), (char*) inReplyTo->toXML(), (char*) isResponse.xmlStringEncode());
00547 else
00548 xml = JString::format("<type>%s</type>\n<id>%s</id>\n<from>%s</from>\n<to>%s</to>\n<cc>%s</cc>\n<stored>%s</stored>\n<isresponse>%s</isresponse>\n",
00549 (char*) type.xmlStringEncode(), (char*) id.xmlStringEncode(), (char*) from.xmlStringEncode(), (char*) to.xmlStringEncode(), (char*) cc.xmlStringEncode(), (char*) stored.xmlStringEncode(), (char*) isResponse.xmlStringEncode());
00550
00551 if (this->priority != 0)
00552 params += JString::format(" priority=\"%.2f\"", priority);
00553
00554 if (this->timeToLive != 0)
00555 params += JString::format(" timetolive=\"%.2f\"", timeToLive);
00556
00557 if (noreply.length() > 0)
00558 params += JString::format(" noreply=\"%s\"", (char*) noreply);
00559
00560 if (comment.length() > 0)
00561 xml += JString::format("<comment>%s</comment>\n", (char*) comment.xmlStringEncode());
00562
00563 if (auxDict != NULL) {
00564 for (n=0; n<auxDict->getCount(); n++) {
00565 key = auxDict->getKey(n);
00566 value = auxDict->get(key);
00567 if (value.length() > 0)
00568 xml += JString::format("<%s>%s</%s>\n", (char*) key.xmlStringEncode(), (char*) value.xmlStringEncode(), (char*) key.xmlStringEncode());
00569 }
00570 }
00571
00572 if (!postedTime.isValid())
00573 postedTime = JTime();
00574 xml += postedTime.toXML("postedTime") + "\n";
00575 if ((receivedTime.isValid()) && (receivedTime != postedTime))
00576 xml += receivedTime.toXML("receivedTime") + "\n";
00577
00578 if (timeDict != NULL) {
00579 JTime t;
00580 for (n=0; n<timeDict->getCount(); n++) {
00581 if ( (t = *(JTime*) timeDict->get(n)).isValid()) {
00582 xml += t.toXML(timeDict->getKey(n)) + "\n";
00583 }
00584 }
00585 }
00586
00587 JString histXML;
00588 JString str;
00589
00590 if (history != NULL) {
00591 ObjectCollection* col;
00592 JString author;
00593 Reference* ref;
00594 for (n=0; n<history->getCount(); n++) {
00595 author = history->getKey(n);
00596 if ( (col = (ObjectCollection*) history->get(n)) != NULL) {
00597 str = "";
00598 for (int m=0; m<col->getCount(); m++) {
00599 if ( (ref = (Reference*) col->get(m)) != NULL) {
00600 str += ref->toXML().indentXML();
00601 }
00602 }
00603 if (str.length() > 0) {
00604 histXML += JString::format("<group author=\"%s\">\n%s</group>", (char*) author.xmlStringEncode(), (char*) str.xmlStringEncode());
00605 }
00606 }
00607 }
00608 if (histXML.length() > 0) {
00609 xml += JString::format("<history>\n%s</history>", (char*) histXML.indentXML());
00610 }
00611 }
00612
00613 if (object != NULL)
00614 xml += JString::format("<content language=\"xml\">\n%s</content>\n", (char*) object->toXML().indentXML());
00615 else if (language.equalsIgnoreCase("xml"))
00616 xml += JString::format("<content language=\"%s\">%s</content>\n", (char*) language.xmlStringEncode(), (char*) getContent());
00617 else {
00618 JString strcontent = getContent().xmlStringEncode();
00619 if ((strcontent.length() > 0) || (language.length() > 0))
00620 xml += JString::format("<content language=\"%s\">%s</content>\n", (char*) language.xmlStringEncode(), (char*) strcontent);
00621 }
00622
00623 JString ss = JString::format("<message%s>\n%s</message>", (char*) params, (char*) xml.indentXML());
00624
00625 return ss;
00626 }
00627
00628
00629 JString Message::getHistoryHTML(const JString& urlformat) {
00630
00631 JString histHeadFont = "<strong><font color=\"#0066CC\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
00632 JString histFont = "<strong><font color=\"#CC6600\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
00633
00634 JString html;
00635 JString url;
00636 JString author;
00637
00638 html += "<table bgcolor=\"#FFE6E6\">";
00639
00640 if (history != NULL) {
00641 ObjectCollection* group;
00642 Reference* ref;
00643 for (int m=0; m<history->getCount(); m++) {
00644 if ( (group = (ObjectCollection*) history->get(m)) != NULL) {
00645 author = history->getKey(m);
00646 html += JString::format("<tr><td><table bgcolor=\"#FFE6E6\"><tr><td>%s</td><td>\n", (char*) author);
00647 for (int k=0; k<group->getCount(); k++) {
00648 if ( (ref = (Reference*) group->get(k)) != NULL) {
00649 html += ref->toHTML(urlformat);
00650 }
00651 }
00652 html += "</td></table></td></tr>\n";
00653 }
00654 }
00655 }
00656 html += "</table>";
00657 return html;
00658 }
00659
00660 JString Message::toHTMLTable(const JString& tags) {
00661
00662
00663
00664 JString html;
00665
00666 JString key, value;
00667 JTime t;
00668 int width = 40;
00669 int c=0;
00670 int n;
00671
00672 htmlCol = new Collection();
00673 if (tags.length() > 0) {
00674 *htmlCol = tags.split(",");
00675 }
00676 else if (htmlCol->getCount() == 0) {
00677 JString values = "Type,ID,From,To,cc,PostedTime,ReceivedTime,Content,InReplyTo,Stored,History,Comment";
00678 *htmlCol = values.split(",");
00679 }
00680
00681 JString titleFont = "<strong><font color=\"#0066CC\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
00682 JString textFont = "<strong><font color=\"#CC6600\" size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
00683
00684
00685
00686
00687 for (n=0; n<htmlCol->getCount(); n++) {
00688 key = htmlCol->get(n);
00689 value = "";
00690
00691 if (key.equalsIgnoreCase("content")) {
00692 if (language.length() > 0)
00693 key = JString::format("Content (%s)\n", (char*) language);
00694 else
00695 key = JString::format("Content\n");
00696 if (object != NULL)
00697 value = JString::format("%s (see details below)", (char*) object->print());
00698 else
00699 value = getContent();
00700 key = key.toHTML();
00701 value = value.toHTML();
00702 }
00703 else if (key.equalsIgnoreCase("history")) {
00704 value = getHistoryHTML("");
00705 key = key.toHTML();
00706 }
00707 else if (key.equalsIgnoreCase("inreplyto")) {
00708 if (inReplyTo != NULL)
00709 value = inReplyTo->toHTML("");
00710 key = key.toHTML();
00711 }
00712 else {
00713 value = get(key);
00714 if (value.length() == 0) {
00715 t = getTime(key);
00716 if (t.isValid())
00717 value = t.print();
00718 }
00719 key = key.toHTML();
00720 value = value.toHTML();
00721 }
00722
00723 if (c%2 == 0)
00724 html += " <tr bgcolor=\"#E6E6E6\">\n";
00725 else
00726 html += " <tr bgcolor=\"#FFFFFF\">\n";
00727 html += JString::format(" <td width=\"%d\" height=\"21\" align=\"left\" valign=\"middle\">%s", width, (char*) titleFont);
00728 html += JString::format("<nobr>%s</nobr></font></strong></td>\n", (char*) key);
00729 html += JString::format(" <td height=\"21\" align=\"left\" valign=\"middle\">%s", (char*) textFont);
00730 html += value;
00731 html += "</font></strong></td>\n";
00732 html += " </tr>\n";
00733 c++;
00734 }
00735
00736 if (object != NULL) {
00737 html += " <tr height=\"10\" bgcolor=\"#FFFFFF\"><td colspan=\"10\"></td></tr>\n";
00738 c = 0;
00739
00740 if (c%2 == 0)
00741 html += " <tr bgcolor=\"#E6E6E6\">\n";
00742 else
00743 html += " <tr bgcolor=\"#FFFFFF\">\n";
00744 html += JString::format(" <td width=\"%d\" height=\"21\" align=\"left\" valign=\"middle\">%s", width, (char*) titleFont);
00745 html += JString::format("Detailed Content Information</font></strong></td>\n");
00746 html += JString::format(" <td height=\"21\" align=\"left\" valign=\"middle\">%s", (char*) textFont);
00747 html += JString::format("%s", (char*) object->print().toHTML());
00748 html += "</font></strong></td>\n";
00749 html += " </tr>\n";
00750
00751 if (c%2 == 0)
00752 html += " <tr bgcolor=\"#E6E6E6\">\n";
00753 else
00754 html += " <tr bgcolor=\"#FFFFFF\">\n";
00755 html += JString::format(" <td width=\"%d\" height=\"21\" align=\"left\" valign=\"middle\">%s", width, (char*) titleFont);
00756 html += JString::format("Content XML</font></strong></td>\n");
00757 html += JString::format(" <td height=\"21\" align=\"left\" valign=\"middle\">%s", (char*) textFont);
00758 JString str = object->toXML().toHTML();
00759 str.replace("\t", " ");
00760
00761 html += "<font size=\"1\">";
00762 html += str;
00763 html += "</font></font></strong></td>\n";
00764 html += " </tr>\n";
00765 }
00766
00767 return html;
00768 }
00769
00770
00771 JString Message::print() {
00772
00773 JString str;
00774 if (object != NULL)
00775 str = JString::format("Content: Object: %s XML: %d bytes", (char*) object->getClass(), object->toXML().length());
00776 else {
00777 if (getContent().length() > 25)
00778 str = JString("Content: ") + getContent().getFirst(22) + "...";
00779 else
00780 str = JString("Content: ") + getContent().getFirst(25);
00781 }
00782
00783 return JString::format("[%s] From: '%s' To: '%s' Type: '%s' Posted: %s\n%s", (char*) getID(), (char*) getFrom(), (char*) getTo(), (char*) getType(), (char*) getPostedTime().printTimeMS(), (char*) str);
00784 }
00785
00786 JString Message::printShort() {
00787
00788 JString str;
00789 if (object != NULL)
00790 str = JString::format("Content: Object: %s XML: %d bytes", (char*) object->getClass(), object->toXML().length());
00791 else {
00792 if (getContent().length() > 25)
00793 str = JString("Content: ") + getContent().getFirst(22) + "...";
00794 else
00795 str = JString("Content: ") + getContent().getFirst(25);
00796 }
00797
00798 return JString::format("From: '%s' To: '%s' Type: '%s' %s", (char*) getFrom(), (char*) getTo(), (char*) getType(), (char*) str);
00799 }
00800
00801 bool Message::set(const JString& name, const JString& value) {
00802 JString lname = name.toLowerCase();
00803
00804 if (auxDict != NULL)
00805 if (auxDict->contains(lname))
00806 return auxDict->put(lname, value);
00807
00808 if (lname.equals("from"))
00809 from = value;
00810 else if (lname.equals("to"))
00811 to = value;
00812 else if (lname.equals("type"))
00813 type = value;
00814 else if (lname.equals("id"))
00815 id = value;
00816 else if (lname.equals("cc"))
00817 cc = value;
00818 else if (lname.equals("origin"))
00819 origin = value;
00820 else if (lname.equals("comment"))
00821 comment = value;
00822 else if (lname.equals("priority"))
00823 priority = value.toDouble();
00824 else if (lname.equals("timetolive"))
00825 timeToLive = value.toDouble();
00826 else if (lname.equals("stored"))
00827 stored = value;
00828 else if (lname.equals("language"))
00829 language = value;
00830
00831
00832 else if (lname.equals("isresponse"))
00833 isResponse = value;
00834 else if (lname.equals("content"))
00835 return false;
00836 else {
00837 if (auxDict == NULL)
00838 auxDict = new Dictionary();
00839 return auxDict->put(lname, value);
00840 }
00841 return true;
00842 }
00843
00844
00845 JString Message::get(const JString& name) {
00846 JString lname = name.toLowerCase();
00847
00848 if (auxDict != NULL)
00849 if (auxDict->contains(lname))
00850 return auxDict->get(lname);
00851
00852 if (lname.equals("from"))
00853 return from;
00854 else if (lname.equals("to"))
00855 return to;
00856 else if (lname.equals("type"))
00857 return type;
00858 else if (lname.equals("id"))
00859 return id;
00860 else if (lname.equals("cc"))
00861 return cc;
00862 else if (lname.equals("origin"))
00863 return origin;
00864 else if (lname.equals("comment"))
00865 return comment;
00866 else if (lname.equals("priority"))
00867 return JString(priority);
00868 else if (lname.equals("timetolive"))
00869 return JString(timeToLive);
00870 else if (lname.equals("stored"))
00871 return stored;
00872 else if (lname.equals("language"))
00873 return language;
00874
00875
00876 else if (lname.equals("isresponse"))
00877 return isResponse;
00878 else if (lname.equals("content"))
00879 return getContent();
00880 else
00881 return "";
00882 }
00883
00884
00885 bool Message::setAux(const JString& name, const JString& value) {
00886 if (auxDict == NULL)
00887 auxDict = new Dictionary();
00888 return auxDict->put(name.toLowerCase(), value);
00889 }
00890
00891
00892 JString Message::getAux(const JString& name) {
00893 if (auxDict != NULL)
00894 return auxDict->get(name.toLowerCase());
00895 else
00896 return "";
00897 }
00898
00899
00900 bool Message::setTime(const JString& name, const JTime& t) {
00901 JString lname = name.toLowerCase();
00902 if (lname.equals("createdtime"))
00903 postedTime = t;
00904 else if (lname.equals("postedtime"))
00905 postedTime = t;
00906 else if (lname.equals("receivedtime"))
00907 receivedTime = t;
00908 else {
00909 if (timeDict == NULL)
00910 timeDict = new ObjectDictionary();
00911 JTime* tt = new JTime();
00912 *tt = t;
00913 timeDict->put(lname, tt);
00914 }
00915 return true;
00916 }
00917
00918 JTime Message::getTime(const JString& name) {
00919 JString lname = name.toLowerCase();
00920 if (lname.equals("createdtime"))
00921 return postedTime;
00922 else if (lname.equals("postedtime"))
00923 return postedTime;
00924 else if (lname.equals("receivedtime"))
00925 return receivedTime;
00926 else if (timeDict != NULL) {
00927 JTime* t = (JTime*) timeDict->get(lname);
00928 if (t != NULL)
00929 return *t;
00930 }
00931 JTime tt;
00932 tt.setInvalid();
00933 return tt;
00934 }
00935
00936 bool Message::addReferenceToHistory(Message* msg, const JString& author) {
00937 if (msg == NULL)
00938 return false;
00939 return addReferenceToHistory(new Reference(msg), author);
00940 }
00941
00942 bool Message::addReferenceToHistory(Reference* ref, const JString& author) {
00943 if (ref == NULL)
00944 return false;
00945 if (history == NULL)
00946 history = new ObjectDictionary();
00947 ObjectCollection* col = (ObjectCollection*) history->get(author);
00948 if (col == NULL) {
00949 col = new ObjectCollection();
00950 history->put(author, col);
00951 }
00952 col->add(ref);
00953 return true;
00954 }
00955
00956 ObjectDictionary* Message::getAllHistory() {
00957 if (history == NULL)
00958 return NULL;
00959 return history;
00960 }
00961
00962 ObjectCollection* Message::getHistoryFromAuthor(const JString& author) {
00963 if (history == NULL)
00964 return NULL;
00965 return (ObjectCollection*) history->get(author);
00966 }
00967
00968 bool Message::deleteHistory() {
00969 if (history != NULL)
00970 delete(history);
00971 history = NULL;
00972 return true;
00973 }
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986 bool Message::setObject(Object* obj) {
00987 deleteObject();
00988 object = obj;
00989 content = "";
00990 language = "xml";
00991 return true;
00992 }
00993
00994 Object* Message::getObject() {
00995 return object;
00996 }
00997
00998 JString Message::getObjectType() {
00999 if (object != NULL)
01000 return object->getClass();
01001 return "";
01002
01003 }
01004
01005 bool Message::deleteObject() {
01006 if (object == NULL)
01007 return true;
01008 delete(object);
01009 object = NULL;
01010 return true;
01011 }
01012
01013 Object* Message::takeObject() {
01014 Object* obj = object;
01015 object = NULL;
01016 language = "";
01017 return obj;
01018 }
01019
01020
01021 bool Message::doesTypeMatch(const JString& match) {
01022 return type.matchesIgnoreCase(match);
01023 }
01024
01025
01026
01027
01028
01029
01030 JString Message::getID() {
01031 return id;
01032 }
01033
01034 Reference* Message::getInReplyTo() {
01035 return inReplyTo;
01036 }
01037
01038 JString Message::getFrom() {
01039 return from;
01040 }
01041
01042 JString Message::getTo() {
01043 return to;
01044 }
01045
01046 JString Message::getCC() {
01047 return cc;
01048 }
01049
01050 JString Message::getType() {
01051 return type;
01052 }
01053
01054 JString Message::getContent() {
01055
01056 if (content.length() > 0)
01057 return content;
01058
01059 if (object != NULL)
01060 return object->toXML();
01061
01062 return content;
01063 }
01064
01065 JString Message::getOrigin() {
01066 return origin;
01067 }
01068
01069 JString Message::getComment() {
01070 return comment;
01071 }
01072
01073 double Message::getPriority() {
01074 return priority;
01075 }
01076
01077 double Message::getTimeToLive() {
01078 return timeToLive;
01079 }
01080
01081 JString Message::getLanguage() {
01082 return language;
01083 }
01084
01085 JString Message::getStored() {
01086 return stored;
01087 }
01088
01089
01090 JTime Message::getTimestamp() {
01091 return postedTime;
01092 }
01093
01094 JTime Message::getPostedTime() {
01095 return postedTime;
01096 }
01097
01098 JTime Message::getReceivedTime() {
01099 return receivedTime;
01100 }
01101
01102
01103
01104
01105
01106
01107 bool Message::setID(const JString& iD) {
01108 id = iD;
01109 return true;
01110 }
01111
01112 bool Message::setInReplyTo(Reference* ref) {
01113 delete(inReplyTo);
01114 inReplyTo = ref;
01115 return true;
01116 }
01117
01118 bool Message::setFrom(const JString& frm) {
01119 from = frm;
01120 return true;
01121 }
01122
01123 bool Message::setTo(const JString& t) {
01124 to = t;
01125 return true;
01126 }
01127
01128 bool Message::setCC(const JString& c) {
01129 cc = c;
01130 return true;
01131 }
01132
01133 bool Message::setType(const JString& t) {
01134 type = t;
01135 return true;
01136 }
01137
01138 bool Message::setContentNoParse(const JString& cont, const JString& cont_language) {
01139
01140 if (cont.length() > 0)
01141 deleteObject();
01142 language = cont_language;
01143 content = cont;
01144 return true;
01145 }
01146
01147 bool Message::setContent(const JString& cont, const JString& cont_language) {
01148
01149 language = cont_language;
01150
01151 if (cont.length() > 0)
01152 deleteObject();
01153
01154 if ((language.equalsIgnoreCase("xml")) && (cont.looksLikeXML())) {
01155 Object* obj = createObjectFromXML(cont);
01156 setObject(obj);
01157 content = "";
01158 }
01159 else {
01160 content = cont;
01161 }
01162
01163 return true;
01164 }
01165
01166 bool Message::setOrigin(const JString& orig) {
01167 origin = orig;
01168 return true;
01169 }
01170
01171 bool Message::setComment(const JString& str) {
01172 comment = str;
01173 return true;
01174 }
01175
01176 bool Message::setPriority(double pri) {
01177 priority = pri;
01178 return true;
01179 }
01180
01181 bool Message::setTimeToLive(double ms) {
01182 timeToLive = ms;
01183 return true;
01184 }
01185
01186 bool Message::setLanguage(const JString& lang) {
01187 language = lang;
01188 return true;
01189 }
01190
01191 bool Message::setStored(const JString& store) {
01192 stored = store;
01193 return true;
01194 }
01195
01196
01197 bool Message::setTimestamp(const JTime& t) {
01198 postedTime = t;
01199 return true;
01200 }
01201
01202 bool Message::setPostedTime(const JTime& t) {
01203 postedTime = t;
01204 return true;
01205 }
01206
01207 bool Message::setReceivedTime(const JTime& t) {
01208 receivedTime = t;
01209 return true;
01210 }
01211
01212
01213
01214
01215 unsigned long Message::getPayloadSize() const {
01216 long payloadSize = from.length() + to.length() + type.length() + cc.length() + id.length() + origin.length() +
01217 comment.length() + stored.length() + language.length() + content.length() +
01218 postedTime.getPayloadSize() + receivedTime.getPayloadSize();
01219
01220 if (attachedMsgs != NULL) payloadSize += attachedMsgs->getPayloadSize();
01221 if (object != NULL) payloadSize += object->getPayloadSize();
01222 if (auxDict != NULL) payloadSize += auxDict->getPayloadSize();
01223 if (timeDict != NULL) payloadSize += timeDict->getPayloadSize();
01224
01225 return payloadSize;
01226 }
01227
01228
01229
01230
01231
01232
01233
01234
01235 long Message::getBinarySize(int chunk) {
01236 if (object == NULL)
01237 return 0;
01238 return object->getBinarySize(chunk);
01239 }
01240
01241
01242 int Message::getBinaryChunkCount() {
01243 if (object == NULL)
01244 return 0;
01245 return object->getBinaryChunkCount();
01246 }
01247
01248
01249 long Message::toBinaryBuffer(int chunk, char* buffer, int maxlen) {
01250 if (object == NULL)
01251 return false;
01252 return object->toBinaryBuffer(chunk, buffer, maxlen);
01253 }
01254
01255
01256 bool Message::fromBinaryBuffer(int chunk, char* buffer, long len) {
01257 if (object == NULL)
01258 return false;
01259 return object->fromBinaryBuffer(chunk, buffer, len);
01260 }
01261
01262
01263
01264
01265
01266
01267 bool Message::unitTest() {
01268 Message* m;
01269
01270 JTime t1;
01271 int n;
01272 JString s;
01273
01274 m = new Message("From", "To", "Type", "Content", "Language");
01275 s = m->toXML();
01276
01277 Message* m2 = new Message(s);
01278 if ((!m->equals(m2)) &&
01279 (m->from != m2->from) &&
01280 (m->to != m2->to) &&
01281 (m->type != m2->type) )
01282 {
01283 addUnitTestLog("Message from/toXML not equal");
01284 return false;
01285 }
01286
01287 delete(m);
01288 delete(m2);
01289
01290 t1 = JTime();
01291 for (n=0; n<100; n++) {
01292 m = new Message("From", "To", "Type", "Content", "Language");
01293 delete(m);
01294 }
01295 printf("\n %s %s\n", (char*) JString("Create/destroy", 20, 0), (char*) JString(JString::format("%8.3fms", t1.getMicroAge()/(n*1000.0)), 20, 1));
01296
01297 m = new Message("From", "To", "Type", "Content", "Language");
01298 t1 = JTime();
01299 for (n=0; n<100; n++) {
01300 s = m->toXML();
01301 }
01302 printf(" %s %s\n", (char*) JString("toXML", 20, 0), (char*) JString(JString::format("%8.3fms", t1.getMicroAge()/(n*1000.0)), 20, 1));
01303
01304 s = m->toXML();
01305 delete(m);
01306
01307 t1 = JTime();
01308 for (n=0; n<100; n++) {
01309 m = new Message(s);
01310 delete(m);
01311 }
01312 printf(" %s %s\n", (char*) JString("Create from XML/destroy", 20, 0), (char*) JString(JString::format("%8.3fms", t1.getMicroAge()/(n*1000.0)), 20, 1));
01313
01314 m = new Message();
01315 XMLParser parser;
01316 t1 = JTime();
01317 for (n=0; n<100; n++) {
01318 parser.parseXML(s);
01319 }
01320 printf(" %s %s\n", (char*) JString("Parse XML", 20, 0), (char*) JString(JString::format("%8.3fms", t1.getMicroAge()/(n*1000.0)), 20, 1));
01321
01322 parser.parseXML(s);
01323 XMLNode* node = parser.getRootNode();
01324
01325 t1 = JTime();
01326 for (n=0; n<100; n++) {
01327 m->fromXML(node);
01328 }
01329 printf(" %s %s ", (char*) JString("fromXML .1k:", 20, 0), (char*) JString(JString::format("%8.3fms", t1.getMicroAge()/(n*1000.0)), 20, 1));
01330 fflush(stdout);
01331 delete(m);
01332 return true;
01333 }
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353 Reference::Reference() {
01354 postedTime.setInvalid();
01355 receivedTime.setInvalid();
01356 }
01357
01358
01359 Reference::Reference(XMLNode* node) {
01360 postedTime.setInvalid();
01361 receivedTime.setInvalid();
01362 if (!fromXML(node)) {}
01363 }
01364
01365
01366 Reference::Reference(Message* msg) {
01367 if (msg == NULL)
01368 return;
01369 id = msg->getID();
01370 stored = msg->getStored();
01371 type = msg->getType();
01372 from = msg->getFrom();
01373 to = msg->getTo();
01374 cc = msg->getCC();
01375 postedTime = msg->getPostedTime();
01376 receivedTime = msg->getReceivedTime();
01377 }
01378
01379 Object* Reference::clone() const {
01380 Reference* ref = new Reference();
01381 ref->id = id;
01382 ref->stored = stored;
01383 ref->type = type;
01384 ref->from = from;
01385 ref->to = to;
01386 ref->cc = cc;
01387 ref->postedTime = postedTime;
01388 ref->receivedTime = receivedTime;
01389 return ref;
01390 }
01391
01392 bool Reference::fromXML(XMLNode* node) {
01393 if (node == NULL)
01394 return false;
01395 if (!node->getTag().equalsIgnoreCase("reference"))
01396 return false;
01397 id = node->getAttribute("id");
01398 stored = node->getAttribute("stored");
01399 type = node->getAttribute("type");
01400 from = node->getAttribute("from");
01401 to = node->getAttribute("to");
01402 cc = node->getAttribute("cc");
01403
01404 XMLNode* xmlNode;
01405 ObjectCollection* nodes = node->getChildTags();
01406 if (nodes != NULL) {
01407 for (int n=0; n<nodes->getCount(); n++) {
01408 xmlNode = (XMLNode*) nodes->get(n);
01409 if (xmlNode != NULL) {
01410 if (xmlNode->getTag().equalsIgnoreCase("postedtime"))
01411 postedTime.fromXML(xmlNode);
01412 else if (xmlNode->getTag().equalsIgnoreCase("receivedtime"))
01413 receivedTime.fromXML(xmlNode);
01414 }
01415 }
01416 }
01417
01418 return true;
01419 }
01420
01421 JString Reference::toXML() {
01422 if (!postedTime.isValid())
01423 return "<reference />";
01424
01425 JString xml = postedTime.toXML("postedTime") + "\n";
01426 if (receivedTime.isValid())
01427 xml += receivedTime.toXML("receivedTime") + "\n";
01428
01429 return JString::format("<reference id=\"%s\" stored=\"%s\" type=\"%s\" from=\"%s\" to=\"%s\" cc=\"%s\">\n%s</reference>",
01430 (char*) id.xmlStringEncode(), (char*) stored.xmlStringEncode(), (char*) type.xmlStringEncode(),
01431 (char*) from.xmlStringEncode(), (char*) to.xmlStringEncode(), (char*) cc.xmlStringEncode(), (char*) xml.indentXML());
01432 }
01433
01434 JString Reference::toHTML(const JString& urlformat) {
01435 JString url = "";
01436 if (urlformat.length() > 0)
01437 url = JString::format((char*)urlformat, (char*) stored, (char*) id);
01438 return JString::format("<table class=\"MessageShowTable\" width=\"100%%\">\
01439 <tr><td height=\"9\" class=\"MessageShowDataName\" align=\"right\">Type</td><td colspan=\"3\"><a href=\"%s\" target=\"_blank\" class=\"MessageShowDataValue\">%s</a></td></tr>\
01440 <tr><td height=\"9\" class=\"MessageShowDataName\" align=\"right\">ID</td><td colspan=\"3\" class=\"MessageShowDataValue\">%s</td></tr>\
01441 <tr><td height=\"9\" class=\"MessageShowDataName\" align=\"right\">From</td><td colspan=\"3\" class=\"MessageShowDataValue\">%s</td></tr>\
01442 <tr><td height=\"9\" class=\"MessageShowDataName\" align=\"right\">To</td><td class=\"MessageShowDataValue\">%s</td>\
01443 <td height=\"9\" class=\"MessageShowDataName\" align=\"right\">CC</td><td class=\"MessageShowDataValue\">%s</td></tr>\
01444 <tr><td height=\"9\" class=\"MessageShowDataName\" align=\"right\">PostedTime</td><td colspan=\"3\" class=\"MessageShowDataValue\">%s</td></tr>\
01445 <tr><td height=\"9\" class=\"MessageShowDataName\" align=\"right\">ReceivedTime</td><td colspan=\"3\" class=\"MessageShowDataValue\">%s</td></tr>\
01446 <tr><td height=\"9\" class=\"MessageShowDataName\" align=\"right\">Stored</td><td colspan=\"3\" class=\"MessageShowDataValue\">%s</td></tr>\
01447 </table>",(char*) url, (char*) type, (char*) id,
01448 (char*) from, (char*) to, (char*) cc,
01449 (char*) postedTime.print(),
01450 (char*) receivedTime.print(),
01451 (char*) stored
01452 );
01453 }
01454
01455 JString Reference::toHTML() {
01456 return toHTML("/wb=%s&&message=%s");
01457 }
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467
01468
01469
01470 }