Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

ClassFactory.cpp

Go to the documentation of this file.
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         //don't forget to clear the documents. Just in case
00016         clearClasDef();
00017         clearCurContext();
00018 }
00019 
00020 void CClassFactory::clearClasDef()
00021 {
00022         //just clear the document
00023         classesDefinitions.Clear();
00024         
00025 }
00026 
00027 void CClassFactory::clearCurContext()
00028 {
00029         //just clear the document
00030         currentContext.Clear(); 
00031 }
00032 
00033 bool CClassFactory::loadContext(const char *filename)
00034 {       
00035         //We load the file, and check Error
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         //We save the file, and check Error
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         //We load the file, and check Error
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         //We save the file, and check Error
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         //we din't find the class
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                 //if document it's empty
00102                 if (root == NULL)
00103                 {
00104                         //create a root node
00105                         currentContext.Parse ("<instances/>");
00106                         root = currentContext.RootElement();
00107                 }
00108                 
00109                 //new node for the instance
00110                 TiXmlElement element("instance");
00111                 
00112                 //add the attributes to the instace
00113                 if (addAttributesToInstance(&element, classNode))
00114                 {                       
00115                         element.SetAttribute("class", className);
00116                         element.SetAttribute("name", instanceName);
00117 
00118                         //we add the instance it self
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         //if the document is empty return NULL
00136         if (root == NULL)
00137         {               
00138                 return NULL;
00139         }
00140         
00141         //first class def
00142         classNode = root->FirstChild("class");
00143         const char* classNodeName;
00144         
00145         //until be found it or they are no more class def
00146         while ((classNode != NULL) && (!found))
00147         {               
00148                 //get the name of the class
00149                 classNodeName = classNode->ToElement()->Attribute("name");
00150                 
00151                 //is the one?
00152                 if (strcmpi(classNodeName , className) == 0)            
00153                         found = true;                   
00154                 else            
00155                         //go to the next
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         //if we donde find it the instance
00167         if (instanceNode == NULL)
00168         {               
00169                 error("Couldn't find instance %s",instanceName);
00170                 return false;           
00171         }
00172         else            
00173                 //remove the instance node
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         //if the document is empty we return NULL
00187         if (root == NULL)
00188         {
00189                 return NULL;
00190         }
00191         
00192         //get first instance node
00193         instanceNode = root->FirstChild("instance");
00194         const char* instanceNodeName;
00195         
00196         //until we found it or they are not more nodes
00197         while ((instanceNode != NULL) && (!found))
00198         {       
00199                 //get instance name
00200                 instanceNodeName = instanceNode->ToElement()->Attribute("name");
00201                 
00202                 //check if its the one
00203                 if (strcmpi(instanceNodeName , instanceName) == 0)
00204                         found = true;
00205                 else
00206                         //go to the next
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         //if we don't find the instance
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                 //if we don't find the attribute
00229                 if (attributeNode == NULL)
00230                 {
00231                         error("Couldn't find attribute %s",attributeName);
00232                         return false;
00233                 }
00234                 else
00235                 {
00236                         //if the attribute have a value (values are childs from attribute)
00237                         if (attributeNode->FirstChild() != NULL)
00238                                 //set the value
00239                                 attributeNode->FirstChild()->SetValue(value);
00240                         else
00241                         {
00242                                 //add the value as children
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         //by default "" value
00255         CString value;  
00256         value = "";
00257         
00258         TiXmlNode* instanceNode = findInstance(instanceName);
00259         
00260         //if we din't found the instance
00261         if (instanceNode == NULL)       
00262                 error("Couldn't find instance %s",instanceName);                
00263         else
00264         {       
00265                 TiXmlNode* attributeNode  = instanceNode->FirstChild(attributeName);
00266                 
00267                 //if we find the attribute
00268                 if (attributeNode == NULL)
00269                         error("Couldn't find attribute %s",attributeName);                      
00270                 else
00271                         //if is have a value (values are childs from attribute)
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         //we get the first attribute
00285         attributeNode = classNode->FirstChild("attribute");
00286         
00287         //until we have not more attributes
00288         while (attributeNode != NULL)
00289         {
00290                 //get the name
00291                 attributeName = attributeNode->ToElement()->Attribute("name");
00292                 
00293                 //add the attribute to the instance
00294                 TiXmlElement attrInstance (attributeName);
00295                 element->InsertEndChild(attrInstance);
00296                 
00297                 //next attribute node
00298                 attributeNode = attributeNode->NextSibling("attribute");
00299         }
00300         
00301         const char* extends = NULL;
00302         
00303         extends = classNode->ToElement()->Attribute("extends");
00304         
00305         //if this class have a parent class
00306         if (extends != NULL)
00307         {
00308                 TiXmlNode* parentClassNode = findClass(extends);
00309 
00310                 //the parent class dosen't exist
00311                 if (parentClassNode == NULL)
00312                 {               
00313                         error("Error couldn't find parent class %s",extends);
00314                         return false;
00315                 }
00316                 else
00317                         //add the attribute of the parent class (recursive)
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         //if we allready have that class
00329         if (classNode != NULL)
00330         {
00331                 error("Class allready exits %s",className);
00332                 return false;   
00333         }
00334         else
00335         {
00336                 //new class def node
00337                 TiXmlElement element("class");
00338                 
00339                 element.SetAttribute("name", className);
00340                 
00341                 //if we set a parent class
00342                 if (extends != NULL)
00343                 {
00344                         //if we set a not empty class name
00345                         if (strcmpi(extends,"") != 0)
00346                         {
00347                                 classNode = findClass(extends);
00348                                 //if we din't find the parent class
00349                                 if (classNode == NULL)
00350                                 {
00351                                         error("Parent class dont found %s","extends");
00352                                         return false;   
00353                                 }
00354                                 else
00355                                         //set the parent as attribute of the node
00356                                         element.SetAttribute("extends", extends);
00357                         }
00358                 }
00359                 
00360                 TiXmlElement* root = classesDefinitions.RootElement();
00361                 
00362                 //if document its empty
00363                 if (root == NULL)
00364                 {       
00365                         //add a root node
00366                         classesDefinitions.Parse("<classes/>");
00367                         root = classesDefinitions.RootElement();
00368                 }
00369                 
00370                 //insert the class def
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         //if we din't find the class
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                 //get the fist attribute node
00395                 TiXmlNode* attributeNode = classNode->FirstChild("attribute");
00396 
00397                 //until be found it or they aren't more attributes nodes
00398                 while ((attributeNode != NULL) && (!found))
00399                 {
00400                         //get the attribute name
00401                         findAttributeName = attributeNode->ToElement()->Attribute("name");
00402                         
00403                         //check if its the one
00404                         if (strcmpi(findAttributeName , attributeName) == 0)                    
00405                                 found = true;                   
00406                         else
00407                                 //go to the next attribute
00408                                 attributeNode = attributeNode->NextSibling("attribute");
00409                 }
00410 
00411                 //if we found it
00412                 if (found)
00413                 {
00414                         error("Attribute allready defined for the class %s",attributeName);
00415                         return false;   
00416                 }
00417                 else
00418                 {
00419                         //add the attribute to the class
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         //if we din't find the class
00436         if (classNode == NULL)
00437         {
00438                 error("Cant find the class %s",className);
00439                 return false;   
00440         }
00441         else
00442                 //we remove the class def node. this olso remove all childrens
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         //if we din't find the class name
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                 //get the first attribute node
00466                 TiXmlNode* attributeNode = classNode->FirstChild("attribute");
00467 
00468                 //until we found it or they aren't more attributes nodes
00469                 while ((attributeNode != NULL) && (!found))
00470                 {       
00471                         //get the name
00472                         findAttributeName = attributeNode->ToElement()->Attribute("name");
00473                         
00474                         //check if its the one
00475                         if (strcmpi(findAttributeName , attributeName) == 0)                    
00476                                 found = true;
00477                         else
00478                                 //go to the next
00479                                 attributeNode = attributeNode->NextSibling("attribute");
00480                 }
00481                 //if we dont found it
00482                 if (!found)
00483                 {
00484                         error("Attribute dosen't exits for this class %s",attributeName);
00485                         return false;   
00486                 }
00487                 else
00488                         //remove the attribute
00489                         classNode->RemoveChild(attributeNode);
00490         }
00491         
00492         return true;
00493 }
00494 
00495 void CClassFactory::error(char message[], ...)
00496 {
00497         //nasty code to display error messages
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 }

Generated on Wed Oct 2 10:33:38 2002 for XML Class Factory by doxygen1.2.15