Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

XML.cpp

Go to the documentation of this file.
00001 //
00002 // XML.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "XML.h"
00025 
00026 #ifdef DEBUG_NEW
00027 #undef THIS_FILE
00028 static char THIS_FILE[]=__FILE__;
00029 #define new DEBUG_NEW
00030 #endif
00031 
00032 
00034 // CXMLNode construction
00035 
00036 CXMLNode::CXMLNode(CXMLElement* pParent, LPCTSTR pszName)
00037 {
00038         m_nNode         = xmlNode;
00039         m_pParent       = pParent;
00040         if ( pszName ) m_sName = pszName;
00041 }
00042 
00043 CXMLNode::~CXMLNode()
00044 {
00045 }
00046 
00048 // CXMLNode parsing
00049 
00050 BOOL CXMLNode::ParseMatch(LPCTSTR& pszBase, LPCTSTR pszToken)
00051 {
00052         LPCTSTR pszXML = pszBase;
00053         int nParse = 0;
00054 
00055         for ( ; *pszXML == ' ' || *pszXML == '\t' || *pszXML == '\r' || *pszXML == '\n' ; pszXML++, nParse++ );
00056         if ( ! *pszXML ) return FALSE;
00057 
00058         for ( ; *pszXML && *pszToken ; pszXML++, pszToken++, nParse++ )
00059         {
00060                 if ( *pszXML != *pszToken ) return FALSE;
00061         }
00062 
00063         pszBase += nParse;
00064 
00065         return TRUE;
00066 }
00067 
00068 BOOL CXMLNode::ParseIdentifier(LPCTSTR& pszBase, CString& strIdentifier)
00069 {
00070         LPCTSTR pszXML = pszBase;
00071         int nParse = 0;
00072 
00073         for ( ; *pszXML == ' ' || *pszXML == '\t' || *pszXML == '\r' || *pszXML == '\n' ; pszXML++, nParse++ );
00074         if ( ! *pszXML ) return FALSE;
00075 
00076         int nIdentifier = 0;
00077         for ( ; *pszXML && ( _istalnum( *pszXML ) || *pszXML == ':' || *pszXML == '_' ) ; pszXML++, nIdentifier++ );
00078         if ( ! nIdentifier ) return FALSE;
00079 
00080         pszBase += nParse;
00081         _tcsncpy( strIdentifier.GetBuffer( nIdentifier ), pszBase, nIdentifier );
00082         strIdentifier.ReleaseBuffer( nIdentifier );
00083         pszBase += nIdentifier;
00084 
00085         return TRUE;
00086 }
00087 
00089 // CXMLNode string to value
00090 
00091 CString CXMLNode::StringToValue(LPCTSTR& pszXML, int nLength)
00092 {
00093         CString strValue;
00094 
00095         if ( ! nLength || ! *pszXML ) return strValue;
00096 
00097         LPTSTR pszValue = strValue.GetBuffer( nLength + 4 );
00098         LPTSTR pszOut = pszValue;
00099 
00100         LPTSTR pszNull = (LPTSTR)pszXML + nLength;
00101         TCHAR cNull = *pszNull;
00102         *pszNull = 0;
00103 
00104         while ( *pszXML && pszXML < pszNull )
00105         {
00106                 if ( _istspace( *pszXML ) )
00107                 {
00108                         if ( pszValue != pszOut ) *pszOut++ = ' ';
00109                         pszXML++;
00110                         while ( *pszXML && _istspace( *pszXML ) ) pszXML++;
00111                         if ( ! *pszXML || pszXML >= pszNull ) break;
00112                 }
00113 
00114                 if ( *pszXML == '&' )
00115                 {
00116                         pszXML++;
00117                         if ( ! *pszXML || pszXML >= pszNull ) break;
00118 
00119                         if ( _tcsnicmp( pszXML, _T("amp;"), 4 ) == 0 )
00120                         {
00121                                 *pszOut++ = '&';
00122                                 pszXML += 4;
00123                         }
00124                         else if ( _tcsnicmp( pszXML, _T("lt;"), 3 ) == 0 )
00125                         {
00126                                 *pszOut++ = '<';
00127                                 pszXML += 3;
00128                         }
00129                         else if ( _tcsnicmp( pszXML, _T("gt;"), 3 ) == 0 )
00130                         {
00131                                 *pszOut++ = '>';
00132                                 pszXML += 3;
00133                         }
00134                         else if ( _tcsnicmp( pszXML, _T("quot;"), 5 ) == 0 )
00135                         {
00136                                 *pszOut++ = '\"';
00137                                 pszXML += 5;
00138                         }
00139                         else if ( _tcsnicmp( pszXML, _T("apos;"), 5 ) == 0 )
00140                         {
00141                                 *pszOut++ = '\'';
00142                                 pszXML += 5;
00143                         }
00144                         else if ( _tcsnicmp( pszXML, _T("nbsp;"), 5 ) == 0 )
00145                         {
00146                                 *pszOut++ = ' ';
00147                                 pszXML += 5;
00148                         }
00149                         else if ( *pszXML == '#' )
00150                         {
00151                                 int nChar;
00152                                 pszXML++;
00153                                 if ( ! *pszXML || pszXML >= pszNull || ! _istdigit( *pszXML ) ) break;
00154 
00155                                 if ( _stscanf( pszXML, _T("%lu;"), &nChar ) == 1 )
00156                                 {
00157                                         *pszOut++ = (TCHAR)nChar;
00158                                         while ( *pszXML && *pszXML != ';' ) pszXML++;
00159                                         if ( ! *pszXML || pszXML >= pszNull ) break;
00160                                         pszXML++;
00161                                 }
00162                         }
00163                         else
00164                         {
00165                                 *pszOut++ = '&';
00166                         }
00167                 }
00168                 else
00169                 {
00170                         *pszOut++ = *pszXML++;
00171                 }
00172         }
00173 
00174         ASSERT( pszNull == pszXML );
00175         *pszNull = cNull;
00176 
00177         ASSERT( pszOut - pszValue <= nLength );
00178         strValue.ReleaseBuffer( (int)( pszOut - pszValue ) );
00179 
00180         return strValue;
00181 }
00182 
00184 // CXMLNode value to string
00185 
00186 #define V2S_APPEND(x,y) \
00187         if ( (x) > nOut ) \
00188         { \
00189                 strXML.ReleaseBuffer( nLen + nOut ); \
00190                 nOut += (x) + 16; \
00191                 pszOut = strXML.GetBuffer( nLen + nOut ) + nLen; \
00192         } \
00193         { for ( LPCTSTR pszIn = (y) ; *pszIn ; nOut--, nLen++ ) *pszOut++ = *pszIn++; }
00194 
00195 void CXMLNode::ValueToString(LPCTSTR pszValue, CString& strXML)
00196 {
00197         int nLen = strXML.GetLength();
00198         int nOut = (int)_tcslen( pszValue );
00199         LPTSTR pszOut = strXML.GetBuffer( nLen + nOut ) + nLen;
00200 
00201         for ( ; *pszValue ; pszValue++ )
00202         {
00203 #ifdef UNICODE
00204                 int nChar = (int)(unsigned short)*pszValue;
00205 #else
00206                 int nChar = (int)(unsigned char)*pszValue;
00207 #endif
00208 
00209                 switch ( nChar )
00210                 {
00211                 case '&':
00212                         V2S_APPEND( 5, _T("&amp;") );
00213                         break;
00214                 case '<':
00215                         V2S_APPEND( 4, _T("&lt;") );
00216                         break;
00217                 case '>':
00218                         V2S_APPEND( 4, _T("&gt;") );
00219                         break;
00220                 case '\"':
00221                         V2S_APPEND( 6, _T("&quot;") );
00222                         break;
00223                 case '\'':
00224                         V2S_APPEND( 6, _T("&apos;") );
00225                         break;
00226                 default:
00227                         if ( nChar > 127 )
00228                         {
00229                                 CString strItem;
00230                                 strItem.Format( _T("&#%lu;"), nChar );
00231                                 V2S_APPEND( strItem.GetLength(), strItem );
00232                         }
00233                         else if ( nOut > 0 )
00234                         {
00235                                 *pszOut++ = nChar;
00236                                 nOut--;
00237                                 nLen++;
00238                         }
00239                         else
00240                         {
00241                                 strXML.ReleaseBuffer( nLen + nOut );
00242                                 nOut += 16;
00243                                 pszOut = strXML.GetBuffer( nLen + nOut ) + nLen;
00244                                 *pszOut++ = nChar;
00245                                 nOut--;
00246                                 nLen++;
00247                         }
00248                         break;
00249                 }
00250         }
00251 
00252         strXML.ReleaseBuffer( nLen );
00253 }
00254 
00256 // CXMLNode serialize
00257 
00258 #ifdef _AFX
00259 
00260 void CXMLNode::Serialize(CArchive& ar)
00261 {
00262         if ( ar.IsStoring() )
00263         {
00264                 ar << m_sName;
00265                 ar << m_sValue;
00266         }
00267         else
00268         {
00269                 ar >> m_sName;
00270                 ar >> m_sValue;
00271         }
00272 }
00273 
00274 #endif
00275 
00277 // CXMLNode string helper
00278 
00279 void CXMLNode::UniformString(CString& str)
00280 {
00281         // non-alphanumeric characters which will not be ignored
00282         static LPCTSTR pszOK = _T("'-&/,;#()");
00283 
00284         str.TrimLeft();
00285         str.TrimRight();
00286 
00287         BOOL bSpace = TRUE;
00288 
00289         for ( int nPos = 0 ; nPos < str.GetLength() ; nPos++ )
00290         {
00291 #ifdef UNICODE
00292                 int nChar = (int)(unsigned short)str.GetAt( nPos );
00293 #else
00294                 int nChar = (int)(unsigned char)str.GetAt( nPos );
00295 #endif
00296 
00297                 if ( nChar <= 32 )
00298                 {
00299                         if ( bSpace )
00300                         {
00301                                 str = str.Left( nPos ) + str.Mid( nPos + 1 );
00302                                 nPos--;
00303                         }
00304                         else
00305                         {
00306                                 if ( nChar != 32 ) str.SetAt( nPos, 32 );
00307                                 bSpace = TRUE;
00308                         }
00309                 }
00310                 else if ( ! _istalnum( nChar ) && nChar < 0xC0 && _tcschr( pszOK, nChar ) == NULL )
00311                 {
00312                         str = str.Left( nPos ) + str.Mid( nPos + 1 );
00313                         nPos--;
00314                 }
00315                 else
00316                 {
00317                         bSpace = FALSE;
00318                 }
00319         }
00320 }
00321 
00322 
00324 // CXMLElement construction
00325 
00326 CXMLElement::CXMLElement(CXMLElement* pParent, LPCTSTR pszName) : CXMLNode( pParent, pszName )
00327 {
00328         m_nNode = xmlElement;
00329 }
00330 
00331 CXMLElement::~CXMLElement()
00332 {
00333         DeleteAllElements();
00334         DeleteAllAttributes();
00335 }
00336 
00338 // CXMLElement clone
00339 
00340 CXMLElement* CXMLElement::Clone(CXMLElement* pParent)
00341 {
00342         CXMLElement* pClone = new CXMLElement( pParent, m_sName );
00343 
00344         for ( POSITION pos = GetAttributeIterator() ; pos ; )
00345         {
00346                 CXMLAttribute* pAttribute = GetNextAttribute( pos )->Clone( pClone );
00347                 CString strName( pAttribute->m_sName );
00348 
00349                 CharLower( strName.GetBuffer() );
00350                 strName.ReleaseBuffer();
00351                 pClone->m_pAttributes.SetAt( strName, pAttribute );
00352         }
00353 
00354         for ( POSITION pos = GetElementIterator() ; pos ; )
00355         {
00356                 CXMLElement* pElement = GetNextElement( pos );
00357                 pClone->m_pElements.AddTail( pElement->Clone( pClone ) );
00358         }
00359 
00360         pClone->m_sValue = m_sValue;
00361 
00362         return pClone;
00363 }
00364 
00366 // CXMLElement delete
00367 
00368 void CXMLElement::DeleteAllElements()
00369 {
00370         for ( POSITION pos = m_pElements.GetHeadPosition() ; pos ; )
00371         {
00372                 delete (CXMLElement*)m_pElements.GetNext( pos );
00373         }
00374         m_pElements.RemoveAll();
00375 }
00376 
00377 void CXMLElement::DeleteAllAttributes()
00378 {
00379         for ( POSITION pos = m_pAttributes.GetStartPosition() ; pos ; )
00380         {
00381                 CXMLAttribute* pAttribute = NULL;
00382                 CString strName;
00383 
00384                 m_pAttributes.GetNextAssoc( pos, strName, XMLVOID(pAttribute) );
00385                 delete pAttribute;
00386         }
00387         m_pAttributes.RemoveAll();
00388 }
00389 
00391 // CXMLElement to string
00392 
00393 CString CXMLElement::ToString(BOOL bHeader, BOOL bNewline)
00394 {
00395         CString strXML;
00396         if ( bHeader ) strXML = _T("<?xml version=\"1.0\"?>");
00397         if ( bNewline ) strXML += _T("\r\n");
00398         ToString( strXML, bNewline );
00399         ASSERT( strXML.GetLength() == _tcslen(strXML) );
00400         return strXML;
00401 }
00402 
00403 void CXMLElement::ToString(CString& strXML, BOOL bNewline)
00404 {
00405         strXML += '<' + m_sName;
00406 
00407     POSITION pos = GetAttributeIterator();
00408         for ( ; pos ; )
00409         {
00410                 strXML += ' ';
00411                 CXMLAttribute* pAttribute = GetNextAttribute( pos );
00412                 pAttribute->ToString( strXML );
00413         }
00414 
00415         pos = GetElementIterator();
00416 
00417         if ( pos == NULL && m_sValue.IsEmpty() )
00418         {
00419                 strXML += _T("/>");
00420                 if ( bNewline ) strXML += _T("\r\n");
00421                 return;
00422         }
00423 
00424         strXML += '>';
00425         if ( bNewline && pos ) strXML += _T("\r\n");
00426 
00427         while ( pos )
00428         {
00429                 CXMLElement* pElement = GetNextElement( pos );
00430                 pElement->ToString( strXML, bNewline );
00431         }
00432 
00433         ValueToString( m_sValue, strXML );
00434 
00435         strXML += _T("</") + m_sName + '>';
00436         if ( bNewline ) strXML += _T("\r\n");
00437 }
00438 
00440 // CXMLElement from string
00441 
00442 CXMLElement* CXMLElement::FromString(LPCTSTR pszXML, BOOL bHeader)
00443 {
00444         CXMLElement* pElement   = NULL;
00445         LPCTSTR pszElement              = NULL;
00446 
00447 #ifdef _AFX
00448         try
00449         {
00450 #endif
00451                 if ( ParseMatch( pszXML, _T("<?xml version=\"") ) )
00452                 {
00453                         pszElement = _tcsstr( pszXML, _T("?>") );
00454                         if ( ! pszElement ) return FALSE;
00455                         pszXML = pszElement + 2;
00456                 }
00457                 else if ( bHeader ) return NULL;
00458 
00459                 while ( ParseMatch( pszXML, _T("<!--") ) )
00460                 {
00461                         pszElement = _tcsstr( pszXML, _T("-->") );
00462                         if ( ! pszElement || *pszElement != '-' ) return FALSE;
00463                         pszXML = pszElement + 3;
00464                 }
00465 
00466                 if ( ParseMatch( pszXML, _T("<!DOCTYPE") ) )
00467                 {
00468                         pszElement = _tcsstr( pszXML, _T(">") );
00469                         if ( ! pszElement ) return FALSE;
00470                         pszXML = pszElement + 1;
00471                 }
00472 
00473                 while ( ParseMatch( pszXML, _T("<!--") ) )
00474                 {
00475                         pszElement = _tcsstr( pszXML, _T("-->") );
00476                         if ( ! pszElement || *pszElement != '-' ) return FALSE;
00477                         pszXML = pszElement + 3;
00478                 }
00479 
00480                 pElement = new CXMLElement();
00481 
00482                 if ( ! pElement->ParseString( pszXML ) )
00483                 {
00484                         delete pElement;
00485                         pElement = NULL;
00486                 }
00487 #ifdef _AFX
00488         }
00489         catch ( CException* pException )
00490         {
00491                 pException->Delete();
00492                 delete pElement;
00493                 pElement = NULL;
00494         }
00495 #endif
00496 
00497         return pElement;
00498 }
00499 
00500 BOOL CXMLElement::ParseString(LPCTSTR& strXML)
00501 {
00502         if ( ! ParseMatch( strXML, _T("<") ) ) return FALSE;
00503 
00504         if ( ! ParseIdentifier( strXML, m_sName ) ) return FALSE;
00505 
00506         LPCTSTR pszEnd = strXML + _tcslen( strXML );
00507 
00508         while ( ! ParseMatch( strXML, _T(">") ) )
00509         {
00510                 if ( ParseMatch( strXML, _T("/") ) )
00511                 {
00512                         return ParseMatch( strXML, _T(">") );
00513                 }
00514 
00515                 if ( ! *strXML || strXML >= pszEnd ) return FALSE;
00516 
00517                 CXMLAttribute* pAttribute = new CXMLAttribute( this );
00518 
00519                 if ( pAttribute->ParseString( strXML ) )
00520                 {
00521                         CString strName( pAttribute->m_sName );
00522                         CXMLAttribute* pExisting;
00523                         CharLower( strName.GetBuffer() );
00524                         strName.ReleaseBuffer();
00525                         if ( m_pAttributes.Lookup( strName, XMLVOID(pExisting) ) ) delete pExisting;
00526                         m_pAttributes.SetAt( strName, pAttribute );
00527                 }
00528                 else
00529                 {
00530                         delete pAttribute;
00531                         return FALSE;
00532                 }
00533         }
00534 
00535         CString strClose = _T("</");
00536         strClose += m_sName + '>';
00537 
00538         while ( TRUE )
00539         {
00540                 if ( ! *strXML || strXML >= pszEnd ) return FALSE;
00541 
00542                 LPCTSTR pszElement = _tcschr( strXML, '<' );
00543                 if ( ! pszElement || *pszElement != '<' ) return FALSE;
00544 
00545                 if ( pszElement > strXML )
00546                 {
00547                         if ( m_sValue.GetLength() && m_sValue.Right( 1 ) != ' ' ) m_sValue += ' ';
00548                         m_sValue += StringToValue( strXML, (int)( pszElement - strXML ) );
00549                         ASSERT( strXML == pszElement );
00550                         if ( strXML != pszElement ) return FALSE;
00551                 }
00552 
00553                 if ( ParseMatch( strXML, strClose ) )
00554                 {
00555                         break;
00556                 }
00557                 else if ( ParseMatch( strXML, _T("<!--") ) )
00558                 {
00559                         pszElement = _tcsstr( strXML, _T("-->") );
00560                         if ( ! pszElement || *pszElement != '-' ) return FALSE;
00561                         strXML = pszElement + 3;
00562                 }
00563                 else
00564                 {
00565                         CXMLElement* pElement = new CXMLElement( this );
00566 
00567                         if ( pElement->ParseString( strXML ) )
00568                         {
00569                                 m_pElements.AddTail( pElement );
00570                         }
00571                         else
00572                         {
00573                                 delete pElement;
00574                                 return FALSE;
00575                         }
00576                 }
00577         }
00578 
00579         return TRUE;
00580 }
00581 
00583 // CXMLElement from bytes
00584 
00585 CXMLElement* CXMLElement::FromBytes(BYTE* pByte, DWORD nByte, BOOL bHeader)
00586 {
00587         CString strXML;
00588 
00589         if ( nByte >= 2 && ( ( pByte[0] == 0xFE && pByte[1] == 0xFF ) || ( pByte[0] == 0xFF && pByte[1] == 0xFE ) ) )
00590         {
00591                 nByte = nByte / 2 - 1;
00592 
00593                 if ( pByte[0] == 0xFE && pByte[1] == 0xFF )
00594                 {
00595                         pByte += 2;
00596 
00597                         for ( DWORD nSwap = 0 ; nSwap < nByte ; nSwap ++ )
00598                         {
00599                                 register CHAR nTemp = pByte[ ( nSwap << 1 ) + 0 ];
00600                                 pByte[ ( nSwap << 1 ) + 0 ] = pByte[ ( nSwap << 1 ) + 1 ];
00601                                 pByte[ ( nSwap << 1 ) + 1 ] = nTemp;
00602                         }
00603                 }
00604                 else
00605                 {
00606                         pByte += 2;
00607                 }
00608 
00609                 CopyMemory( strXML.GetBuffer( nByte ), pByte, nByte * sizeof(TCHAR) );
00610                 strXML.ReleaseBuffer( nByte );
00611         }
00612         else
00613         {
00614                 if ( nByte >= 3 && pByte[0] == 0xEF && pByte[1] == 0xBB && pByte[2] == 0xBF )
00615                 {
00616                         pByte += 3; nByte -= 3;
00617                 }
00618 
00619                 DWORD nWide = MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)pByte, nByte, NULL, 0 );
00620 
00621                 MultiByteToWideChar( CP_UTF8, 0, (LPCSTR)pByte, nByte, strXML.GetBuffer( nWide ), nWide );
00622                 strXML.ReleaseBuffer( nWide );
00623         }
00624 
00625         return FromString( strXML, bHeader );
00626 }
00627 
00629 // CXMLElement from file
00630 
00631 CXMLElement* CXMLElement::FromFile(LPCTSTR pszPath, BOOL bHeader)
00632 {
00633         HANDLE hFile = CreateFile(      pszPath, GENERIC_READ, FILE_SHARE_READ, NULL,
00634                                                                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
00635 
00636         if ( hFile == INVALID_HANDLE_VALUE ) return NULL;
00637 
00638         CXMLElement* pXML = FromFile( hFile, bHeader );
00639 
00640         CloseHandle( hFile );
00641 
00642         return pXML;
00643 }
00644 
00645 CXMLElement* CXMLElement::FromFile(HANDLE hFile, BOOL bHeader)
00646 {
00647         DWORD nByte = GetFileSize( hFile, NULL );
00648         if ( nByte > 4096*1024 ) return FALSE;
00649 
00650         BYTE* pByte = new BYTE[ nByte ];
00651         ReadFile( hFile, pByte, nByte, &nByte, NULL );
00652 
00653         CXMLElement* pXML = FromBytes( pByte, nByte, bHeader );
00654 
00655         delete [] pByte;
00656 
00657         return pXML;
00658 }
00659 
00661 // CXMLElement equality
00662 
00663 BOOL CXMLElement::Equals(CXMLElement* pXML) const
00664 {
00665         if ( this == NULL || pXML == NULL ) return FALSE;
00666         if ( pXML == this ) return TRUE;
00667 
00668         if ( m_sName != pXML->m_sName ) return FALSE;
00669         if ( m_sValue != pXML->m_sValue ) return FALSE;
00670 
00671         if ( GetAttributeCount() != pXML->GetAttributeCount() ) return FALSE;
00672         if ( GetElementCount() != pXML->GetElementCount() ) return FALSE;
00673 
00674         for ( POSITION pos = GetAttributeIterator() ; pos ; )
00675         {
00676                 CXMLAttribute* pAttribute1 = GetNextAttribute( pos );
00677                 CXMLAttribute* pAttribute2 = pXML->GetAttribute( pAttribute1->m_sName );
00678                 if ( pAttribute2 == NULL ) return FALSE;
00679                 if ( ! pAttribute1->Equals( pAttribute2 ) ) return FALSE;
00680         }
00681 
00682         POSITION pos1 = GetElementIterator();
00683         POSITION pos2 = pXML->GetElementIterator();
00684 
00685         for ( ; pos1 && pos2 ; )
00686         {
00687                 CXMLElement* pElement1 = GetNextElement( pos1 );
00688                 CXMLElement* pElement2 = pXML->GetNextElement( pos2 );
00689                 if ( pElement1 == NULL || pElement2 == NULL ) return FALSE;
00690                 if ( ! pElement1->Equals( pElement2 ) ) return FALSE;
00691         }
00692 
00693         if ( pos1 != NULL || pos2 != NULL ) return FALSE;
00694 
00695         return TRUE;
00696 }
00697 
00699 // CXMLElement recursive word accumulation
00700 
00701 CString CXMLElement::GetRecursiveWords()
00702 {
00703         CString strWords;
00704 
00705         AddRecursiveWords( strWords );
00706         strWords.TrimLeft();
00707         strWords.TrimRight();
00708 
00709         return strWords;
00710 }
00711 
00712 void CXMLElement::AddRecursiveWords(CString& strWords)
00713 {
00714         for ( POSITION pos = GetAttributeIterator() ; pos ; )
00715         {
00716                 CXMLAttribute* pAttribute = GetNextAttribute( pos );
00717                 CString strText = pAttribute->GetName();
00718 
00719                 if ( strText.Find( ':' ) >= 0 ) continue;
00720                 if ( strText.CompareNoCase( _T("SHA1") ) == 0 ) continue;       // NOTE: Shareaza Specific
00721 
00722                 if ( strWords.GetLength() ) strWords += ' ';
00723                 strWords += pAttribute->GetValue();
00724         }
00725 
00726         for ( POSITION pos = GetElementIterator() ; pos ; )
00727         {
00728                 GetNextElement( pos )->AddRecursiveWords( strWords );
00729         }
00730 
00731         if ( m_sValue.GetLength() )
00732         {
00733                 if ( strWords.GetLength() ) strWords += ' ';
00734                 strWords += m_sValue;
00735         }
00736 }
00737 
00739 // CXMLElement serialize
00740 
00741 #ifdef _AFX
00742 
00743 void CXMLElement::Serialize(CArchive& ar)
00744 {
00745         CXMLNode::Serialize( ar );
00746 
00747         if ( ar.IsStoring() )
00748         {
00749                 ar.WriteCount( GetAttributeCount() );
00750 
00751                 for ( POSITION pos = GetAttributeIterator() ; pos ; )
00752                 {
00753                         GetNextAttribute( pos )->Serialize( ar );
00754                 }
00755 
00756                 ar.WriteCount( GetElementCount() );
00757 
00758                 for ( POSITION pos = GetElementIterator() ; pos ; )
00759                 {
00760                         GetNextElement( pos )->Serialize( ar );
00761                 }
00762         }
00763         else
00764         {
00765                 for ( int nCount = (int)ar.ReadCount() ; nCount > 0 ; nCount-- )
00766                 {
00767                         CXMLAttribute* pAttribute = new CXMLAttribute( this );
00768                         pAttribute->Serialize( ar );
00769 
00770                         CString strName( pAttribute->m_sName );
00771                         CharLower( strName.GetBuffer() );
00772                         strName.ReleaseBuffer();
00773                         m_pAttributes.SetAt( strName, pAttribute );
00774                 }
00775 
00776                 for ( int nCount = (int)ar.ReadCount() ; nCount > 0 ; nCount-- )
00777                 {
00778                         CXMLElement* pElement = new CXMLElement( this );
00779                         pElement->Serialize( ar );
00780                         m_pElements.AddTail( pElement );
00781                 }
00782         }
00783 }
00784 
00785 #endif
00786 
00787 
00789 // CXMLAttribute construction
00790 
00791 LPCTSTR CXMLAttribute::xmlnsSchema              = _T("http://www.w3.org/2001/XMLSchema");
00792 LPCTSTR CXMLAttribute::xmlnsInstance    = _T("http://www.w3.org/2001/XMLSchema-instance");
00793 LPCTSTR CXMLAttribute::schemaName               = _T("xsi:noNamespaceSchemaLocation");
00794 
00795 CXMLAttribute::CXMLAttribute(CXMLElement* pParent, LPCTSTR pszName) : CXMLNode( pParent, pszName )
00796 {
00797         m_nNode = xmlAttribute;
00798 }
00799 
00800 CXMLAttribute::~CXMLAttribute()
00801 {
00802 }
00803 
00805 // CXMLAttribute clone
00806 
00807 CXMLAttribute* CXMLAttribute::Clone(CXMLElement* pParent)
00808 {
00809         CXMLAttribute* pClone = new CXMLAttribute( pParent, m_sName );
00810         pClone->m_sValue = m_sValue;
00811         return pClone;
00812 }
00813 
00815 // CXMLAttribute to string
00816 
00817 void CXMLAttribute::ToString(CString& strXML)
00818 {
00819         strXML += m_sName + _T("=\"");
00820         ValueToString( m_sValue, strXML );
00821         strXML += '\"';
00822 }
00823 
00825 // CXMLAttribute from string
00826 
00827 BOOL CXMLAttribute::ParseString(LPCTSTR& strXML)
00828 {
00829         if ( ! ParseIdentifier( strXML, m_sName ) ) return FALSE;
00830         if ( ! ParseMatch( strXML, _T("=") ) ) return FALSE;
00831 
00832         if ( ParseMatch( strXML, _T("\"") ) )
00833         {
00834                 LPCTSTR pszQuote = _tcschr( strXML,  '\"' );
00835                 if ( ! pszQuote || *pszQuote != '\"' ) return FALSE;
00836 
00837                 m_sValue = StringToValue( strXML, (int)( pszQuote - strXML ) );
00838 
00839                 return ParseMatch( strXML, _T("\"") );
00840         }
00841         else if ( ParseMatch( strXML, _T("'") ) )
00842         {
00843                 LPCTSTR pszQuote = _tcschr( strXML,  '\'' );
00844                 if ( ! pszQuote || *pszQuote != '\'' ) return FALSE;
00845 
00846                 m_sValue = StringToValue( strXML, (int)( pszQuote - strXML ) );
00847 
00848                 return ParseMatch( strXML, _T("\'") );
00849         }
00850         else
00851         {
00852                 return FALSE;
00853         }
00854 }
00855 
00857 // CXMLAttribute equality
00858 
00859 BOOL CXMLAttribute::Equals(CXMLAttribute* pXML) const
00860 {
00861         if ( this == NULL || pXML == NULL ) return FALSE;
00862         if ( pXML == this ) return TRUE;
00863 
00864         if ( m_sName != pXML->m_sName ) return FALSE;
00865         if ( m_sValue != pXML->m_sValue ) return FALSE;
00866 
00867         return TRUE;
00868 }
00869 
00871 // CXMLAttribute serialize
00872 
00873 #ifdef _AFX
00874 
00875 void CXMLAttribute::Serialize(CArchive& ar)
00876 {
00877         CXMLNode::Serialize( ar );
00878 }
00879 
00880 #endif

Generated on Thu Dec 15 10:39:54 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2