00001
00002 #include "stdafx.h"
00003
00004 #include "ClassFactory.h"
00005
00006
00007
00008 CClassFactory::CClassFactory()
00009 {
00010
00011 }
00012
00013 CClassFactory::~CClassFactory()
00014 {
00015
00016 clearClasDef();
00017 clearCurContext();
00018 }
00019
00020 void CClassFactory::clearClasDef()
00021 {
00022
00023 classesDefinitions.Clear();
00024
00025 }
00026
00027 void CClassFactory::clearCurContext()
00028 {
00029
00030 currentContext.Clear();
00031 }
00032
00033 bool CClassFactory::loadContext(const char *filename)
00034 {
00035
00036 if (! currentContext.LoadFile(filename))
00037 {
00038 error("Error loading file = \"%s\" : %s.", filename,
00039 currentContext.ErrorDesc());
00040 return false;
00041 }
00042 else
00043 return true;
00044
00045 }
00046
00047 bool CClassFactory::saveContext(const char *filename)
00048 {
00049
00050 if (! currentContext.SaveFile(filename))
00051 {
00052 error("Error saving file = \"%s\" : %s.", filename,
00053 currentContext.ErrorDesc());
00054 return false;
00055 }
00056 else
00057 return true;
00058 }
00059
00060 bool CClassFactory::loadClassDef(const char *filename)
00061 {
00062
00063 if (! classesDefinitions.LoadFile(filename))
00064 {
00065 error("Error loading file = \"%s\" : %s.", filename,
00066 classesDefinitions.ErrorDesc());
00067 return false;
00068 }
00069 else
00070 return true;
00071 }
00072
00073 bool CClassFactory::saveClassDef(const char *filename)
00074 {
00075
00076 if (! classesDefinitions.SaveFile(filename))
00077 {
00078 error("Error saving file = \"%s\" : %s.", filename,
00079 classesDefinitions.ErrorDesc());
00080 return false;
00081 }
00082 else
00083 return true;
00084 }
00085
00086 bool CClassFactory::newInstance(const char * className,
00087 const char* instanceName)
00088 {
00089 TiXmlNode* classNode = findClass(className);
00090
00091
00092 if (classNode == NULL)
00093 {
00094 error("Couldn't find class %s",className);
00095 return false;
00096 }
00097 else
00098 {
00099 TiXmlElement* root = currentContext.RootElement();
00100
00101
00102 if (root == NULL)
00103 {
00104
00105 currentContext.Parse ("<instances/>");
00106 root = currentContext.RootElement();
00107 }
00108
00109
00110 TiXmlElement element("instance");
00111
00112
00113 if (addAttributesToInstance(&element, classNode))
00114 {
00115 element.SetAttribute("class", className);
00116 element.SetAttribute("name", instanceName);
00117
00118
00119 root->InsertEndChild(element);
00120 }
00121 else
00122 return false;
00123 }
00124
00125 return true;
00126 }
00127
00128 TiXmlNode* CClassFactory::findClass(const char *className)
00129 {
00130 TiXmlNode* classNode = NULL;
00131 bool found = false;
00132
00133 TiXmlElement* root = classesDefinitions.RootElement();
00134
00135
00136 if (root == NULL)
00137 {
00138 return NULL;
00139 }
00140
00141
00142 classNode = root->FirstChild("class");
00143 const char* classNodeName;
00144
00145
00146 while ((classNode != NULL) && (!found))
00147 {
00148
00149 classNodeName = classNode->ToElement()->Attribute("name");
00150
00151
00152 if (strcmpi(classNodeName , className) == 0)
00153 found = true;
00154 else
00155
00156 classNode = classNode->NextSibling("class");
00157 }
00158
00159 return ((found)?classNode:NULL);
00160 }
00161
00162 bool CClassFactory::deleteInstace(const char *instanceName)
00163 {
00164 TiXmlNode* instanceNode = findInstance(instanceName);
00165
00166
00167 if (instanceNode == NULL)
00168 {
00169 error("Couldn't find instance %s",instanceName);
00170 return false;
00171 }
00172 else
00173
00174 currentContext.RootElement()->RemoveChild(instanceNode);
00175
00176 return true;
00177 }
00178
00179 TiXmlNode* CClassFactory::findInstance(const char *instanceName)
00180 {
00181 TiXmlNode* instanceNode = NULL;
00182 bool found = false;
00183
00184 TiXmlElement* root = currentContext.RootElement();
00185
00186
00187 if (root == NULL)
00188 {
00189 return NULL;
00190 }
00191
00192
00193 instanceNode = root->FirstChild("instance");
00194 const char* instanceNodeName;
00195
00196
00197 while ((instanceNode != NULL) && (!found))
00198 {
00199
00200 instanceNodeName = instanceNode->ToElement()->Attribute("name");
00201
00202
00203 if (strcmpi(instanceNodeName , instanceName) == 0)
00204 found = true;
00205 else
00206
00207 instanceNode = instanceNode->NextSibling("instance");
00208 }
00209
00210 return (found)?instanceNode:NULL;
00211 }
00212
00213 bool CClassFactory::setValue(const char *instanceName,
00214 const char *attributeName, const char *value)
00215 {
00216 TiXmlNode* instanceNode = findInstance(instanceName);
00217
00218
00219 if (instanceNode == NULL)
00220 {
00221 error("Couldn't find instance %s",instanceName);
00222 return false;
00223 }
00224 else
00225 {
00226 TiXmlNode* attributeNode = instanceNode->FirstChild(attributeName);
00227
00228
00229 if (attributeNode == NULL)
00230 {
00231 error("Couldn't find attribute %s",attributeName);
00232 return false;
00233 }
00234 else
00235 {
00236
00237 if (attributeNode->FirstChild() != NULL)
00238
00239 attributeNode->FirstChild()->SetValue(value);
00240 else
00241 {
00242
00243 TiXmlText text(value);
00244 attributeNode->InsertEndChild(text);
00245 }
00246 }
00247 }
00248 return true;
00249 }
00250
00251 CString CClassFactory::getValue(const char *instanceName,
00252 const char *attributeName)
00253 {
00254
00255 CString value;
00256 value = "";
00257
00258 TiXmlNode* instanceNode = findInstance(instanceName);
00259
00260
00261 if (instanceNode == NULL)
00262 error("Couldn't find instance %s",instanceName);
00263 else
00264 {
00265 TiXmlNode* attributeNode = instanceNode->FirstChild(attributeName);
00266
00267
00268 if (attributeNode == NULL)
00269 error("Couldn't find attribute %s",attributeName);
00270 else
00271
00272 if (attributeNode->FirstChild() != NULL)
00273 value = attributeNode->FirstChild()->Value();
00274 }
00275 return value;
00276 }
00277
00278 bool CClassFactory::addAttributesToInstance(TiXmlElement *element,
00279 TiXmlNode *classNode)
00280 {
00281 TiXmlNode* attributeNode;
00282 const char* attributeName;
00283
00284
00285 attributeNode = classNode->FirstChild("attribute");
00286
00287
00288 while (attributeNode != NULL)
00289 {
00290
00291 attributeName = attributeNode->ToElement()->Attribute("name");
00292
00293
00294 TiXmlElement attrInstance (attributeName);
00295 element->InsertEndChild(attrInstance);
00296
00297
00298 attributeNode = attributeNode->NextSibling("attribute");
00299 }
00300
00301 const char* extends = NULL;
00302
00303 extends = classNode->ToElement()->Attribute("extends");
00304
00305
00306 if (extends != NULL)
00307 {
00308 TiXmlNode* parentClassNode = findClass(extends);
00309
00310
00311 if (parentClassNode == NULL)
00312 {
00313 error("Error couldn't find parent class %s",extends);
00314 return false;
00315 }
00316 else
00317
00318 return addAttributesToInstance(element, parentClassNode);
00319 }
00320
00321 return true;
00322 }
00323
00324 bool CClassFactory::addClass(const char *className, const char *extends)
00325 {
00326 TiXmlNode* classNode = findClass(className);
00327
00328
00329 if (classNode != NULL)
00330 {
00331 error("Class allready exits %s",className);
00332 return false;
00333 }
00334 else
00335 {
00336
00337 TiXmlElement element("class");
00338
00339 element.SetAttribute("name", className);
00340
00341
00342 if (extends != NULL)
00343 {
00344
00345 if (strcmpi(extends,"") != 0)
00346 {
00347 classNode = findClass(extends);
00348
00349 if (classNode == NULL)
00350 {
00351 error("Parent class dont found %s","extends");
00352 return false;
00353 }
00354 else
00355
00356 element.SetAttribute("extends", extends);
00357 }
00358 }
00359
00360 TiXmlElement* root = classesDefinitions.RootElement();
00361
00362
00363 if (root == NULL)
00364 {
00365
00366 classesDefinitions.Parse("<classes/>");
00367 root = classesDefinitions.RootElement();
00368 }
00369
00370
00371 root->InsertEndChild(element);
00372 }
00373
00374 return true;;
00375 }
00376
00377 bool CClassFactory::addAttribute(const char *className,
00378 const char *attributeName)
00379 {
00380 TiXmlNode* classNode = findClass(className);
00381
00382
00383 if (classNode == NULL)
00384 {
00385 error("Class dosen't exits %s",className);
00386 return false;
00387 }
00388 else
00389 {
00390 const char* findAttributeName = NULL;
00391
00392 bool found = false;
00393
00394
00395 TiXmlNode* attributeNode = classNode->FirstChild("attribute");
00396
00397
00398 while ((attributeNode != NULL) && (!found))
00399 {
00400
00401 findAttributeName = attributeNode->ToElement()->Attribute("name");
00402
00403
00404 if (strcmpi(findAttributeName , attributeName) == 0)
00405 found = true;
00406 else
00407
00408 attributeNode = attributeNode->NextSibling("attribute");
00409 }
00410
00411
00412 if (found)
00413 {
00414 error("Attribute allready defined for the class %s",attributeName);
00415 return false;
00416 }
00417 else
00418 {
00419
00420 TiXmlElement element("attribute");
00421
00422 element.SetAttribute("name", attributeName);
00423
00424 classNode->ToElement()->InsertEndChild(element);
00425 }
00426 }
00427
00428 return true;
00429 }
00430
00431 bool CClassFactory::deleteClass(const char *className)
00432 {
00433 TiXmlNode* classNode = findClass(className);
00434
00435
00436 if (classNode == NULL)
00437 {
00438 error("Cant find the class %s",className);
00439 return false;
00440 }
00441 else
00442
00443 classesDefinitions.RootElement()->RemoveChild(classNode);
00444
00445 return true;
00446 }
00447
00448 bool CClassFactory::deleteAttribute(const char *className,
00449 const char *attributeName)
00450 {
00451 TiXmlNode* classNode = findClass(className);
00452
00453
00454 if (classNode == NULL)
00455 {
00456 error("Class dosen't exits %s", className);
00457 return false;
00458 }
00459 else
00460 {
00461 const char* findAttributeName = NULL;
00462
00463 bool found = false;
00464
00465
00466 TiXmlNode* attributeNode = classNode->FirstChild("attribute");
00467
00468
00469 while ((attributeNode != NULL) && (!found))
00470 {
00471
00472 findAttributeName = attributeNode->ToElement()->Attribute("name");
00473
00474
00475 if (strcmpi(findAttributeName , attributeName) == 0)
00476 found = true;
00477 else
00478
00479 attributeNode = attributeNode->NextSibling("attribute");
00480 }
00481
00482 if (!found)
00483 {
00484 error("Attribute dosen't exits for this class %s",attributeName);
00485 return false;
00486 }
00487 else
00488
00489 classNode->RemoveChild(attributeNode);
00490 }
00491
00492 return true;
00493 }
00494
00495 void CClassFactory::error(char message[], ...)
00496 {
00497
00498
00499 static char messageBuffer[8192];
00500
00501 va_list argumentsPointer;
00502
00503 va_start(argumentsPointer, message);
00504 vsprintf(messageBuffer, message, argumentsPointer);
00505 va_end(argumentsPointer);
00506
00507 MessageBox(NULL,messageBuffer,"Error", MB_OK | MB_ICONEXCLAMATION);
00508 }