00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "InfoItem.h"
00022
00023 namespace cmlabs {
00024
00025 InfoItem::InfoItem() {
00026 object = NULL;
00027 setID(createUniqueID());
00028 }
00029
00030 InfoItem::InfoItem(const JString& xml) {
00031 object = NULL;
00032 setID(createUniqueID());
00033 if (!Object::fromXML(xml)) {
00034 }
00035 }
00036
00037 InfoItem::InfoItem(XMLNode* node) {
00038 object = NULL;
00039 setID(createUniqueID());
00040 if (!fromXML(node)) {
00041 }
00042 }
00043
00044 InfoItem::InfoItem(const InfoItem &item) {
00045
00046 id = item.id;
00047 createdTime = item.createdTime;
00048
00049 auxDict.copyAll(item.auxDict);
00050 timeDict.copyAll(item.timeDict);
00051
00052 if (item.object != NULL) {
00053 object = item.object->clone();
00054 }
00055 else
00056 object = NULL;
00057 }
00058
00059 JString id;
00060 JTime createdTime;
00061
00062 Dictionary auxDict;
00063
00064 ObjectDictionary timeDict;
00065 Object* object;
00066
00067
00068 unsigned long InfoItem::getPayloadSize() const {
00069 unsigned long payloadSize = id.getPayloadSize() + createdTime.getPayloadSize() + auxDict.getPayloadSize();
00070 if (object != NULL) payloadSize += object->getPayloadSize();
00071 return payloadSize;
00072 }
00073
00074 InfoItem& InfoItem::operator=(const InfoItem &item) {
00075
00076 id = item.id;
00077 createdTime = item.createdTime;
00078
00079 auxDict.copyAll(item.auxDict);
00080 timeDict.copyAll(item.timeDict);
00081
00082 if (item.object != NULL) {
00083 object = item.object->clone();
00084 }
00085 else
00086 object = NULL;
00087 return *this;
00088 }
00089
00090
00091 InfoItem::~InfoItem() {
00092 if (object != NULL)
00093 delete(object);
00094 }
00095
00096 Object* InfoItem::clone() const {
00097 InfoItem* item = new InfoItem();
00098 item->id = id;
00099 item->createdTime = createdTime;
00100
00101 item->auxDict.copyAll(auxDict);
00102 item->timeDict.copyAll(timeDict);
00103
00104 if (object != NULL) {
00105 item->object = object->clone();
00106 }
00107 else
00108 item->object = NULL;
00109 return (Object*) item;
00110 }
00111
00112 InfoItem* InfoItem::cloneDataOnly() const {
00113 InfoItem* item = new InfoItem();
00114
00115 item->auxDict.copyAll(auxDict);
00116 item->timeDict.copyAll(timeDict);
00117
00118 if (object != NULL) {
00119 item->object = object->clone();
00120 }
00121 else
00122 item->object = NULL;
00123 return item;
00124 }
00125
00126 bool InfoItem::fromXML(XMLNode* node) {
00127
00128 if (node == NULL) {
00129 return false;
00130 }
00131
00132 if (!node->getTag().equalsIgnoreCase("InfoItem"))
00133 return false;
00134
00135 id = node->getAttribute("id");
00136
00137 JString tag, value;
00138 XMLNode* xmlNode;
00139 XMLNode* subNode;
00140 ObjectCollection* nodes = node->getChildTags();
00141
00142 if (nodes != NULL) {
00143 for (int n=0; n<nodes->getCount(); n++) {
00144 xmlNode = (XMLNode*) nodes->get(n);
00145 if (xmlNode != NULL) {
00146 tag = xmlNode->getTag();
00147 value = xmlNode->getText();
00148 subNode = (XMLNode*) xmlNode->getFirstChildNode();
00149
00150 if (tag.equalsIgnoreCase("object")) {
00151 setObject(createObjectFromXML(subNode));
00152 }
00153 else if (tag.equalsIgnoreCase("createdtime")) {
00154 createdTime = JTime(xmlNode);
00155 }
00156 else if ( xmlNode->hasAttribute("sec") ) {
00157 setTime(xmlNode->getAttribute("name"), JTime(xmlNode));
00158 }
00159 else if (tag.equalsIgnoreCase("entry")) {
00160 setEntry(xmlNode->getAttribute("name"), value);
00161 }
00162 }
00163 }
00164 }
00165
00166 return true;
00167 }
00168
00169
00170 JString InfoItem::toXML() {
00171
00172 JString xml = createdTime.toXML("createdtime") + "\n";
00173
00174 if (object != NULL) {
00175 xml += JString::format("<object>\n%s</object>\n", (char*)object->toXML().indentXML());
00176 }
00177
00178 JString key, value;
00179 int n;
00180 for (n = 0; n < auxDict.getCount(); n++) {
00181 key = auxDict.getKey(n);
00182 value = auxDict.get(n);
00183 if (key.length() > 0) {
00184 xml += JString::format("<entry name=\"%s\">%s</entry>\n", (char*)key.xmlStringEncode(), (char*)value.xmlStringEncode());
00185 }
00186 }
00187
00188 JTime* time;
00189 for (n = 0; n < timeDict.getCount(); n++) {
00190 key = timeDict.getKey(n);
00191 time = (JTime*) timeDict.get(n);
00192 if ( (time != NULL) && (key.length() > 0)) {
00193 xml += time->toXML("time", JString::format("name=\"%s\"", (char*) key)) + "\n";
00194 }
00195 }
00196
00197 return JString::format("<infoitem id=\"%s\">\n%s</infoitem>", (char*) id, (char*) xml.indentXML());
00198 }
00199
00200
00201
00202 JString InfoItem::print() {
00203 return printShort();
00204 }
00205
00206 JString InfoItem::printShort() {
00207
00208 JString message = JString::format("InfoItem:\n ID: %s\n Created: %s\n",
00209 (char*) id, (char*) createdTime.print());
00210
00211 JString key, value;
00212 int n;
00213 for (n=0; n<auxDict.getCount(); n++) {
00214 key = auxDict.getKey(n);
00215 value = auxDict.get(n);
00216 if (key.length() > 0) {
00217 message = JString::format("%s %s: %s\n",
00218 (char*)message, (char*) key, (char*) value);
00219 }
00220 }
00221
00222 JTime* time;
00223 for (n=0; n<timeDict.getCount(); n++) {
00224 key = timeDict.getKey(n);
00225 time = (JTime*) timeDict.get(n);
00226 if ((key.length() > 0) && (time != NULL)) {
00227 message = JString::format("%s %s: %s\n",
00228 (char*)message, (char*) key, (char*) time->print());
00229 }
00230 }
00231
00232 return message;
00233 }
00234
00235 JString InfoItem::toHTML() {
00236 return printHTML("");
00237 }
00238
00239 JString InfoItem::printHTML(JString htmlTemplate) {
00240 JString html;
00241
00242 if (htmlTemplate.length() == 0) {
00243 htmlTemplate = "\
00244 <table border=\"0\">\
00245 <!-- start:InfoItem -->\
00246 <tr><td class=\"InfoItemKey\">%1</td><td class=\"InfoItemValue\">%2</td></tr>\
00247 <!-- end:InfoItem -->\
00248 </table>";
00249 }
00250
00251 ObjectCollection* col = new ObjectCollection();
00252 Collection* lines;
00253 int n;
00254 JString key, value;
00255 for (n=0; n<auxDict.getCount(); n++) {
00256 key = auxDict.getKey(n);
00257 value = auxDict.get(n);
00258 if (key.length() > 0) {
00259 lines = new Collection();
00260 lines->add(key.toHTML());
00261 lines->add(value.toHTML());
00262 col->add(lines);
00263 }
00264 }
00265
00266 JTime* time;
00267 for (n=0; n<timeDict.getCount(); n++) {
00268 key = timeDict.getKey(n);
00269 time = (JTime*) timeDict.get(n);
00270 if ((key.length() > 0) && (time != NULL)) {
00271 lines = new Collection();
00272 lines->add(key.toHTML());
00273 if (time->isValid())
00274 lines->add(time->print().toHTML());
00275 else
00276 lines->add("n/a");
00277 col->add(lines);
00278 }
00279 }
00280
00281 if (object != NULL) {
00282 lines = new Collection();
00283 lines->add("Object");
00284 lines->add(object->print().toHTML());
00285 col->add(lines);
00286 }
00287
00288 htmlTemplate.scriptReplace("InfoItem", col);
00289 delete(col);
00290
00291 return htmlTemplate;
00292 }
00293
00294
00295 bool InfoItem::setEntry(const JString& name, const JString& value) {
00296
00297
00298 return auxDict.put(name, value);
00299 }
00300
00301
00302 JString InfoItem::getEntry(const JString& name) {
00303
00304
00305 return auxDict.getIgnoreCase(name);
00306 }
00307
00308 bool InfoItem::setTime(const JString& name, const JTime& t) {
00309
00310 JTime* tt = new JTime();
00311 *tt = t;
00312 timeDict.put(name, tt);
00313 return true;
00314 }
00315
00316 JTime InfoItem::getTime(const JString& name) {
00317
00318 JTime* t = (JTime*) timeDict.getIgnoreCase(name);
00319 if (t != NULL)
00320 return *t;
00321 JTime tt;
00322 tt.setInvalid();
00323 return tt;
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 bool InfoItem::setObject(Object* obj) {
00337 deleteObject();
00338 object = obj;
00339 return true;
00340 }
00341
00342 Object* InfoItem::getObject() {
00343 return object;
00344 }
00345
00346 JString InfoItem::getObjectType() {
00347 if (object != NULL)
00348 return object->getClass();
00349 return "";
00350 }
00351
00352 bool InfoItem::deleteObject() {
00353 if (object == NULL)
00354 return true;
00355 delete(object);
00356 object = NULL;
00357 return true;
00358 }
00359
00360 Object* InfoItem::takeObject() {
00361 Object* obj = object;
00362 object = NULL;
00363 return obj;
00364 }
00365
00366 JString InfoItem::getID() {
00367 return id;
00368 }
00369
00370 JTime InfoItem::getCreatedTime() {
00371 return createdTime;
00372 }
00373
00374 bool InfoItem::setID(const JString& iD) {
00375 id = iD;
00376 return true;
00377 }
00378
00379 bool InfoItem::setCreatedTime(const JTime& t) {
00380 createdTime = t;
00381 return true;
00382 }
00383
00384
00385
00386
00387
00388
00389
00390
00391 long InfoItem::getBinarySize(int chunk) {
00392 if (object == NULL)
00393 return 0;
00394 return object->getBinarySize(chunk);
00395 }
00396
00397
00398 int InfoItem::getBinaryChunkCount() {
00399 if (object == NULL)
00400 return 0;
00401 return object->getBinaryChunkCount();
00402 }
00403
00404
00405 long InfoItem::toBinaryBuffer(int chunk, char* buffer, int maxlen) {
00406 if (object == NULL)
00407 return false;
00408 return object->toBinaryBuffer(chunk, buffer, maxlen);
00409 }
00410
00411
00412 bool InfoItem::fromBinaryBuffer(int chunk, char* buffer, long len) {
00413 if (object == NULL)
00414 return false;
00415 return object->fromBinaryBuffer(chunk, buffer, len);
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 bool InfoItem::loadFromFile(const JString& filename) {
00427 JString xml = JFile::readAFileASCII(filename);
00428 if (xml.length() == 0)
00429 return false;
00430 if (!Object::fromXML(xml))
00431 return false;
00432
00433 JString binfile;
00434 long binsize = 0;
00435 char* buffer = NULL;
00436 int bufferlen = 0;
00437
00438 JString binaryFilesXML = this->getEntry("BinaryFiles");
00439 if (binaryFilesXML.length() == 0)
00440 return true;
00441
00442 Dictionary binaryFiles;
00443 if (!binaryFiles.fromXML(binaryFilesXML))
00444 return false;
00445
00446 if (binaryFiles.getCount() > 0) {
00447 for (int n=0; n<binaryFiles.getCount(); n++) {
00448 if ( (binfile = binaryFiles.getKey(n)).length() > 0) {
00449 if ( (binsize = binaryFiles.get(n).toLong()) > 0 ) {
00450 buffer = JFile::readAFileBinary(binfile, bufferlen);
00451 if (buffer != NULL) {
00452 if (bufferlen != binsize) {
00453
00454 }
00455 this->fromBinaryBuffer(n, buffer, bufferlen);
00456 delete [] buffer;
00457 buffer = NULL;
00458 bufferlen = 0;
00459 }
00460 }
00461 }
00462 }
00463 }
00464 return true;
00465 }
00466
00467 bool InfoItem::writeToFile(const JString& filename) {
00468
00469 JString binfile;
00470 char* buffer = NULL;
00471 long bufferlen = 0;
00472 int bincount = this->getBinaryChunkCount();
00473
00474 Collection files;
00475 files.add(filename);
00476 Dictionary binaryFiles;
00477 if (bincount > 0) {
00478 for (int n=0; n<bincount; n++) {
00479 binfile = JString::format("%s.%.6d.bin", (char*) filename, n);
00480 bufferlen = this->getBinarySize(n);
00481 if (bufferlen <= 0) {
00482 bufferlen = 0;
00483 }
00484 else {
00485 buffer = new char[bufferlen];
00486 this->toBinaryBuffer(n, buffer, bufferlen);
00487 if (JFile::writeAFileBinary(binfile, buffer, bufferlen))
00488 files.add(binfile);
00489 delete [] buffer;
00490 }
00491 binaryFiles.put(binfile, JString(bufferlen));
00492 }
00493 this->setEntry("BinaryFiles", binaryFiles.toXML());
00494 }
00495
00496 this->setEntry("OutputFilesWrittenToFile", files.printListLine(","));
00497 JString xml = this->toXML();
00498 bool res = JFile::writeAFileASCII(filename, xml);
00499 return res;
00500 }
00501
00502
00503 bool InfoItem::unitTest() {
00504
00505
00506
00507
00508
00509
00510 InfoItem* i = new InfoItem();
00511 i->setEntry("Test", "bla\nbla");
00512 JString xml = i->toXML();
00513 delete(i);
00514 i = new InfoItem(xml);
00515 JString test = i->getEntry("Test");
00516 if (!test.equals("bla\nbla"))
00517 return false;
00518 delete(i);
00519 return true;
00520 }
00521
00522
00523 }