00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <time.h>
00022 #include <boost/algorithm/string/predicate.hpp>
00023 #include "weiParser.h"
00024
00025 using namespace boost::algorithm;
00026
00027 #ifndef __DOXYGEN__
00028 weCmpMode iweEntity::compareMode = weCmpDefault;
00029 #endif // __DOXYGEN__
00030
00041 const string iweEntity::Attr(string name)
00042 {
00043 WeAttrMap::iterator it;
00044
00045 it = attributes.find(name);
00046 if (it != attributes.end())
00047 {
00048 return attributes.val(it);
00049 }
00050 return(*(new string("")));
00051 }
00052
00061 void iweEntity::Attr(string name, string value)
00062 {
00063 attributes[name] = value;
00064 }
00065
00073 iweEntity* iweEntity::Child(string type)
00074 {
00075 iweEntity* retval = NULL;
00076 WeEntityList::iterator it;
00077
00078 for(it = chldList.begin(); it != chldList.end(); it++) {
00079 if((*it)->Name() == type) {
00080 retval = (*it);
00081 break;
00082 }
00083 }
00084 return retval;
00085 }
00086
00094 iweEntity* iweEntity::Child(int idx)
00095 {
00096 if (idx < 0 || idx >= (int)chldList.size()) {
00097 return(NULL);
00098 }
00099 return chldList[idx];
00100 }
00101
00107 #pragma warning(push)
00108 #pragma warning(disable : 4311)
00109 #pragma warning(disable : 4996)
00110 void iweEntity::GenerateId(void)
00111 {
00112
00113 unsigned int first;
00114 unsigned int second;
00115 unsigned int third;
00116 char buff[40];
00117
00118 m_entityId = "{";
00119
00120 first = (unsigned int)this;
00121 while ((first & 0xF0000000) == 0)
00122 {
00123 first += ((unsigned int)rand() << 16);
00124 }
00125
00126 second = (unsigned int)time(NULL);
00127 while ((second & 0xF0000000) == 0)
00128 {
00129 second += ((unsigned int)rand() << 16);
00130 }
00131
00132 third = ((unsigned int)rand() << 8);
00133 third ^= first;
00134 third ^= second;
00135 sprintf(buff, "%08X-%04X-%04X-9798-A2%08X9F", first, (unsigned short)(second & 0xFFFF), (unsigned short)(second >> 16), third);
00136
00137 m_entityId += buff;
00138 m_entityId += "}";
00139 srand(third);
00140 }
00141 #pragma warning(pop)
00142
00153 iweEntity* iweEntity::FindID(string id)
00154 {
00155 WeEntityList::iterator chld;
00156 iweEntity* retval;
00157
00158 if (id == m_entityId) {
00159 return this;
00160 }
00161 for (chld = chldList.begin(); chld != chldList.end(); chld++) {
00162 retval = (*chld)->FindID(id);
00163 if (retval != NULL) {
00164 return retval;
00165 }
00166 }
00167 return (NULL);
00168 }
00169
00180 WeEntityList& iweEntity::FindTags(string tag)
00181 {
00182 WeEntityList* retval = new WeEntityList;
00183 WeEntityList::iterator chld;
00184
00185 if (iequals(tag, entityName)) {
00186 retval->push_back(this);
00187 }
00188 for (chld = chldList.begin(); chld != chldList.end(); chld++) {
00189 WeEntityList& chlds = (*chld)->FindTags(tag);
00190 while (chlds.size() > 0) {
00191 retval->push_back(chlds.back());
00192 chlds.pop_back();
00193 }
00194 }
00195 return (*retval);
00196 }
00197
00206 void iweEntity::ClearChildren( void )
00207 {
00208 WeEntityList::iterator chld;
00209
00210 for (chld = chldList.begin(); chld != chldList.end(); chld++) {
00211 delete (*chld);
00212 }
00213 chldList.clear();
00214 }
00215
00222 iweDocument* iweEntity::GetRootDocument( void )
00223 {
00224 if (entityName == "#document") {
00225 return dynamic_cast<iweDocument*>(this);
00226 }
00227 if (parent == NULL) {
00228 return (iweDocument*)NULL;
00229 }
00230 return parent->GetRootDocument();
00231 }
00232
00241 bool iweEntity::IsParentTag( string tag )
00242 {
00243 if (iequals(tag, entityName)) {
00244 return true;
00245 }
00246 if (parent == NULL) {
00247 return false;
00248 }
00249 return parent->IsParentTag(tag);
00250 }