Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATXMLObject.cpp Source File

CATXMLObject.cpp

Go to the documentation of this file.
00001 /// \file    CATXMLObject.cpp
00002 /// \brief   XML Object Base
00003 /// \ingroup CAT
00004 ///
00005 /// Copyright (c) 2003-2008 by Michael Ellison.
00006 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00007 ///
00008 // $Author: mikeellison $
00009 // $Date: 2008-01-21 21:40:31 -0600 (Mon, 21 Jan 2008) $
00010 // $Revision:   $
00011 // $NoKeywords: $
00012 
00013 #include "CATXMLObject.h"
00014 #include "CATStream.h"
00015 
00016 CATXMLObject::CATXMLObject(const CATWChar* type)
00017 {
00018     this->fParent = 0;
00019 
00020     // Have to set a variable and redirect for vs6.0
00021     CATXMLKEYCOMP keyComp = CATXMLObject::CATXMLKeyComp;
00022 
00023     this->fAttribs = new CATXMLAttribs(keyComp);
00024     this->fType = new CATWChar[wcslen(type) + 1];
00025     wcscpy(fType,type);
00026 }
00027 
00028 // On destruction, clean up and delete children
00029 CATXMLObject::~CATXMLObject()
00030 {
00031     std::vector<CATXMLObject*>::iterator iter = fChildren.begin();
00032     while (iter != fChildren.end())
00033     {
00034         delete (*iter);
00035         iter = fChildren.erase(iter);
00036     }
00037 
00038     if (fAttribs)
00039     {
00040         while (fAttribs->size())
00041         {
00042             CATXMLAttribsIter iter = fAttribs->begin();
00043             delete [] (CATWChar*)iter->first;
00044             delete [] (CATWChar*)iter->second;
00045             fAttribs->erase(iter);
00046         }
00047         delete fAttribs;
00048         fAttribs = 0;
00049     }
00050     delete [] fType;
00051     fType = 0;
00052 }
00053 
00054 /// Insert a child object into the theme
00055 void CATXMLObject::AddChild(CATXMLObject* child)
00056 {
00057     child->SetParent(this);
00058     this->fChildren.push_back(child);
00059 }
00060 
00061 /// Retrieve number of child theme objects
00062 CATUInt32 CATXMLObject::GetNumChildren()
00063 {
00064     return (CATUInt32)this->fChildren.size();
00065 }
00066 
00067 // Retrieve a ptr to a specific child (do NOT delete - owned by parent object)
00068 // \param  index               index in child vector (0 <= index < GetNumChildren)
00069 // \return CATXMLObject*       Requested xml object, or 0 if not found
00070 CATXMLObject* CATXMLObject::GetChild(CATUInt32 index) const
00071 {
00072     return this->fChildren[index];
00073 }
00074 
00075 // Set our owner object
00076 void CATXMLObject::SetParent(CATXMLObject* parent)
00077 {
00078     this->fParent = parent;
00079 }
00080 
00081 // Add an attribute to the object
00082 CATResult CATXMLObject::AddAttribute(const CATWChar* key, const CATWChar* value)
00083 {    
00084     if ((key == 0) || (value == 0))
00085         return CAT_ERR_XML_INVALID_ATTRIBUTE;
00086 
00087     CATWChar* newKey   = new CATWChar[wcslen(key)+1];
00088     CATWChar* newValue = new CATWChar[wcslen(value)+1];
00089     if ((newKey == 0) || (newValue == 0))
00090         return CAT_ERR_XML_INVALID_ATTRIBUTE;
00091 
00092     wcscpy(newKey,key);
00093     wcscpy(newValue,value);
00094 
00095     // Clear out attrib if exists already
00096     CATXMLAttribsIter iter = this->fAttribs->find(key);
00097     if (iter != fAttribs->end())
00098     {
00099         delete [] (CATWChar*)iter->first;
00100         delete [] (CATWChar*)iter->second;
00101         this->fAttribs->erase(iter);
00102     }
00103 
00104     // set new attrib
00105     this->fAttribs->insert( std::make_pair(newKey, newValue) );
00106 
00107     return S_OK;
00108 }
00109 
00110 // Retrieve an attribute value
00111 CATString CATXMLObject::GetAttribute(const CATWChar* key)
00112 {    
00113     if (key == 0)
00114         return L"";
00115 
00116     CATXMLAttribs::iterator iter = fAttribs->find(key);
00117 
00118     if (iter != fAttribs->end())
00119     {
00120         return CATString(iter->second);
00121     }
00122     return L"";
00123 }
00124 
00125 // Sets the attributes for the object, takes ownership of the CATXMLAttribs passed in.
00126 void CATXMLObject::SetAttributes(CATXMLAttribs* attribs)
00127 {
00128     if (fAttribs)
00129     {
00130         while (fAttribs->size())
00131         {
00132             CATXMLAttribsIter iter = fAttribs->begin();
00133             delete [] (CATWChar*)iter->first;
00134             delete [] (CATWChar*)iter->second;
00135             fAttribs->erase(iter);
00136         }
00137         delete fAttribs;
00138         fAttribs = 0;
00139     }
00140 
00141     fAttribs = attribs;
00142 
00143     if (fAttribs == 0)
00144     {
00145         CATXMLKEYCOMP keyComp = CATXMLObject::CATXMLKeyComp;
00146         fAttribs = new CATXMLAttribs(keyComp);
00147     }
00148 }
00149 
00150 
00151 // Child objects should parse out attributes here, but we don't need.
00152 CATResult CATXMLObject::ParseAttributes()
00153 {
00154     for (CATUInt32 i = 0; i < this->GetNumChildren(); i++)
00155     {
00156         this->GetChild(i)->ParseAttributes();
00157     }
00158 
00159     return CAT_SUCCESS;
00160 }
00161 
00162 bool CATXMLObject::CATXMLKeyComp( const CATWChar*  g1, const CATWChar* g2)
00163 {
00164     if (wcscmp(g1,g2) < 0)
00165         return true;
00166 
00167     return false;
00168 }
00169 
00170 const CATWChar* CATXMLObject::GetType()
00171 {
00172     return fType;
00173 }
00174 
00175 void CATXMLObject::AppendData(const CATWChar* data, CATInt32 len)
00176 {   
00177     fData.append(data,len);
00178 }
00179 
00180 const CATWChar* CATXMLObject::GetData()
00181 {
00182     return fData.c_str();
00183 }
00184 
00185 void CATXMLObject::SetData(const CATWChar* data)
00186 {
00187     if (data == 0)
00188     {
00189         fData = L"";
00190         return;
00191     }
00192     fData = data;
00193 }
00194 
00195 
00196 int CATXMLObject::GetNumAttributes()
00197 {
00198     return (int)fAttribs->size();
00199 }
00200 
00201 CATString CATXMLObject::GetAttributeKeyByIndex(int index)
00202 {
00203     CATXMLAttribsIter iter = fAttribs->begin();
00204     int i = 0;
00205 
00206     while (iter != fAttribs->end())
00207     {
00208         if (i == index)
00209             return CATString(iter->first);
00210         i++;
00211         iter++;
00212     }
00213 
00214     return L"";     
00215 }
00216 
00217 
00218 CATXMLObject* CATXMLObject::GetParent()
00219 {
00220     return fParent;
00221 }
00222 
00223 CATResult CATXMLObject::WriteToStream(CATStream* stream)
00224 {
00225     CATResult result = CAT_SUCCESS;
00226     if ((stream == 0) || (stream->IsOpen() == false))
00227     {
00228         return CATRESULT(CAT_ERR_STREAM_INVALID);
00229     }
00230 
00231     CATString tmpTxt = L"<";
00232     tmpTxt << ((CATString)fType).Escape();
00233 
00234     if (fAttribs->size() == 0)
00235     {
00236         tmpTxt << L">";     
00237         tmpTxt.SetCodePage(CP_UTF8);
00238         if (CATFAILED(result = stream->Write((const char*)tmpTxt, tmpTxt.Length())))
00239         {
00240             // Bail if we get a write error
00241             return result;
00242         }
00243     }
00244     else
00245     {                    
00246         tmpTxt.SetCodePage(CP_UTF8);
00247         if (CATFAILED(result = stream->Write((const char*)tmpTxt, tmpTxt.Length())))
00248         {
00249             // Bail if we get a write error
00250             return result;
00251         }
00252 
00253         CATXMLAttribsIter iter = fAttribs->begin();
00254         while (iter != fAttribs->end())
00255         {
00256             CATString key = (*iter).first;
00257             CATString val = (*iter).second;
00258 
00259             tmpTxt = " ";
00260             tmpTxt << key.Escape() << L"=\"" << val.Escape() << "\"";
00261 
00262             tmpTxt.SetCodePage(CP_UTF8);
00263             if (CATFAILED(result = stream->Write((const char*)tmpTxt, tmpTxt.Length())))
00264             {
00265                 // Bail if we get a write error
00266                 return result;
00267             }
00268 
00269             ++iter;
00270         }
00271 
00272         // Write out closing text for start tag
00273         tmpTxt = L">";
00274         tmpTxt.SetCodePage(CP_UTF8);
00275         if (CATFAILED(result = stream->Write((const char*)tmpTxt, tmpTxt.Length())))
00276         {
00277             // Bail if we get a write error
00278             return result;
00279         }
00280     }
00281 
00282     // Enumerate through the children and let them write themselves out.
00283     CATUInt32 numChildren = this->GetNumChildren();
00284     CATXMLObject* childObj = 0;         
00285     for (CATUInt32 childIndex = 0; childIndex < numChildren; childIndex++)
00286     {
00287         childObj = GetChild(childIndex);
00288         result = childObj->WriteToStream(stream);
00289         
00290         // break on error
00291         if (CATFAILED(result))
00292             break;
00293     }
00294 
00295     // Write ending tag
00296     tmpTxt = L"</";
00297     tmpTxt << fType << L">";
00298     tmpTxt.SetCodePage(CP_UTF8);
00299     if (CATFAILED(result = stream->Write((const char*)tmpTxt, tmpTxt.Length())))
00300         return result;
00301 
00302     return result;         
00303 }

Generated on Mon Feb 11 04:09:49 2008 for Game Accessibility Suite by doxygen 1.5.4