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

Schema.cpp

Go to the documentation of this file.
00001 //
00002 // Schema.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 "Settings.h"
00025 #include "Schema.h"
00026 #include "SchemaMember.h"
00027 #include "SchemaChild.h"
00028 #include "ShellIcons.h"
00029 #include "CoolInterface.h"
00030 #include "XML.h"
00031 
00032 #ifdef _DEBUG
00033 #undef THIS_FILE
00034 static char THIS_FILE[]=__FILE__;
00035 #define new DEBUG_NEW
00036 #endif
00037 
00038 
00040 // CSchema construction
00041 
00042 CSchema::CSchema()
00043 {
00044         m_nType                 = stFile;
00045         m_nAvailability = saDefault;
00046         m_bPrivate              = FALSE;
00047         m_nIcon16               = m_nIcon32 = m_nIcon48 = -1;
00048         m_sDonkeyType.Empty();
00049 }
00050 
00051 CSchema::~CSchema()
00052 {
00053         Clear();
00054 }
00055 
00057 // CSchema member access
00058 
00059 POSITION CSchema::GetMemberIterator() const
00060 {
00061         return m_pMembers.GetHeadPosition();
00062 }
00063 
00064 CSchemaMember* CSchema::GetNextMember(POSITION& pos) const
00065 {
00066         return (CSchemaMember*)m_pMembers.GetNext( pos );
00067 }
00068 
00069 CSchemaMember* CSchema::GetMember(LPCTSTR pszName) const
00070 {
00071         if ( ! pszName || ! *pszName ) return NULL;
00072 
00073         for ( POSITION pos = GetMemberIterator() ; pos ; )
00074         {
00075                 CSchemaMember* pMember = GetNextMember( pos );
00076                 if ( pMember->m_sName.CompareNoCase( pszName ) == 0 ) return pMember;
00077         }
00078         return NULL;
00079 }
00080 
00081 int CSchema::GetMemberCount() const
00082 {
00083         return m_pMembers.GetCount();
00084 }
00085 
00086 CString CSchema::GetFirstMemberName() const
00087 {
00088         if ( m_pMembers.GetCount() )
00089         {
00090                 CSchemaMember* pMember = (CSchemaMember*)m_pMembers.GetHead();
00091                 return pMember->m_sName;
00092         }
00093 
00094         CString str( _T("title") );
00095         return str;
00096 }
00097 
00099 // CSchema clear
00100 
00101 void CSchema::Clear()
00102 {
00103         for ( POSITION pos = GetMemberIterator() ; pos ; )
00104         {
00105                 delete GetNextMember( pos );
00106         }
00107 
00108         for ( POSITION pos = m_pContains.GetHeadPosition() ; pos ; )
00109         {
00110                 delete (CSchemaChild*)m_pContains.GetNext( pos );
00111         }
00112 
00113         for ( POSITION pos = m_pBitziMap.GetHeadPosition() ; pos ; )
00114         {
00115                 delete (CSchemaBitzi*)m_pBitziMap.GetNext( pos );
00116         }
00117 
00118         m_pMembers.RemoveAll();
00119         m_pContains.RemoveAll();
00120         m_pBitziMap.RemoveAll();
00121 }
00122 
00124 // CSchema load
00125 
00126 BOOL CSchema::Load(LPCTSTR pszFile)
00127 {
00128         CString strFile( pszFile );
00129 
00130         int nSlash = strFile.Find( '.' );
00131         if ( nSlash >= 0 ) strFile = strFile.Left( nSlash );
00132 
00133         if ( ! LoadSchema( strFile + _T(".xsd") ) ) return FALSE;
00134         
00135         m_sIcon = strFile + _T(".ico");
00136 
00137         LoadDescriptor( strFile + _T(".xml") );
00138 
00139         m_sIcon = m_sIcon.Left( m_sIcon.GetLength() - 4 );
00140         m_sIcon += _T("XP.ico");
00141 
00142         //LoadIcon() causes bad registry reads
00143         //CCoolInterface::IsNewWindows() causes several reapeat ones.
00144         if ( ! CCoolInterface::IsNewWindows() || ! LoadIcon() )
00145         {
00146                 m_sIcon = m_sIcon.Left( m_sIcon.GetLength() - 6 );
00147                 m_sIcon += _T(".ico");
00148                 LoadIcon();
00149         }
00150 
00151         if ( m_sTitle.IsEmpty() )
00152         {
00153                 m_sTitle = m_sSingular;
00154                 m_sTitle.SetAt( 0, toupper( m_sTitle.GetAt( 0 ) ) );
00155         }
00156 
00157         
00158         if( m_nType == stFile )
00159         {
00160                 //Bit of a hack - Should probably save this info as part of schema. Do that in 2.2
00161                 if ( m_sURI == CSchema::uriAudio )
00162                         m_sDonkeyType = _T("Audio");
00163                 else if ( m_sURI == CSchema::uriVideo)
00164                         m_sDonkeyType = _T("Video");
00165                 else if ( m_sURI == CSchema::uriImage )
00166                         m_sDonkeyType = _T("Image");
00167                 else if ( m_sURI == CSchema::uriApplication )
00168                         m_sDonkeyType = _T("Pro");
00169                 else if ( m_sURI == CSchema::uriBook )
00170                         m_sDonkeyType = _T("Doc");
00171                 else if ( m_sURI == CSchema::uriPresentation )
00172                         m_sDonkeyType = _T("Doc");
00173                 else if ( m_sURI == CSchema::uriSpreadsheet )
00174                         m_sDonkeyType = _T("Doc");
00175                 else if ( m_sURI == CSchema::uriDocument )
00176                         m_sDonkeyType = _T("Doc");
00177                 /*
00178                 //Bit of a hack - Should probably save this info as part of schema. Do that in 2.2
00179                 if ( m_sURI == _T("http://www.limewire.com/schemas/audio.xsd") )
00180                         m_sDonkeyType = _T("Audio");
00181                 else if ( m_sURI == _T("http://www.limewire.com/schemas/video.xsd") )
00182                         m_sDonkeyType = _T("Video");
00183                 else if ( m_sURI == _T("http://www.shareaza.com/schemas/image.xsd") )
00184                         m_sDonkeyType = _T("Image");
00185                 else if ( m_sURI == _T("http://www.shareaza.com/schemas/application.xsd") )
00186                         m_sDonkeyType = _T("Pro");
00187                 else if ( m_sURI == _T("http://www.limewire.com/schemas/book.xsd") )
00188                         m_sDonkeyType = _T("Doc");
00189                 else if ( m_sURI == _T("http://www.shareaza.com/schemas/presentation.xsd") )
00190                         m_sDonkeyType = _T("Doc");
00191                 else if ( m_sURI == _T("http://www.shareaza.com/schemas/spreadsheet.xsd") )
00192                         m_sDonkeyType = _T("Doc");
00193                 else if ( m_sURI == CSchema::uriDocument )
00194                         m_sDonkeyType = _T("Doc");
00195                 */
00196 
00197         }
00198 
00199         return TRUE;
00200 }
00201 
00203 // CSchema load schema
00204 
00205 BOOL CSchema::LoadSchema(LPCTSTR pszFile)
00206 {
00207         CString strXML;
00208 
00209         CXMLElement* pRoot = CXMLElement::FromFile( pszFile );
00210         if ( NULL == pRoot ) return FALSE;
00211 
00212         BOOL bResult = FALSE;
00213 
00214         m_sURI = pRoot->GetAttributeValue( _T("targetNamespace"), _T("") );
00215 
00216         CXMLElement* pPlural = pRoot->GetFirstElement();
00217 
00218         if ( pPlural && m_sURI.GetLength() )
00219         {
00220                 m_sPlural = pPlural->GetAttributeValue( _T("name") );
00221 
00222                 CXMLElement* pComplexType = pPlural->GetFirstElement();
00223                 
00224                 if ( pComplexType && pComplexType->IsNamed( _T("complexType") ) && m_sPlural.GetLength() )
00225                 {
00226                         CXMLElement* pElement = pComplexType->GetFirstElement();
00227 
00228                         if ( pElement && pElement->IsNamed( _T("element") ) )
00229                         {
00230                                 m_sSingular = pElement->GetAttributeValue( _T("name") );
00231 
00232                                 if ( pElement->GetElementCount() )
00233                                 {
00234                                         bResult = LoadPrimary( pRoot, pElement->GetFirstElement() );
00235                                 }
00236                                 else
00237                                 {
00238                                         CString strType = pElement->GetAttributeValue( _T("type") );
00239                                         bResult = LoadPrimary( pRoot, GetType( pRoot, strType ) );
00240                                 }
00241 
00242                                 if ( m_sSingular.IsEmpty() ) bResult = FALSE;
00243                         }
00244                 }
00245         }
00246 
00247         delete pRoot;
00248         
00249         return bResult;
00250 }
00251 
00252 BOOL CSchema::LoadPrimary(CXMLElement* pRoot, CXMLElement* pType)
00253 {
00254         if ( ! pRoot || ! pType ) return FALSE;
00255         
00256         if ( ! pType->IsNamed( _T("complexType") ) &&
00257                  ! pType->IsNamed( _T("all") ) ) return FALSE;
00258 
00259         for ( POSITION pos = pType->GetElementIterator() ; pos ; )
00260         {
00261                 CXMLElement* pElement   = pType->GetNextElement( pos );
00262                 CString strElement              = pElement->GetName();
00263 
00264                 if ( strElement.CompareNoCase( _T("attribute") ) == 0 ||
00265                          strElement.CompareNoCase( _T("element") ) == 0 )
00266                 {
00267                         CSchemaMember* pMember = new CSchemaMember( this );
00268 
00269                         if ( pMember->LoadSchema( pRoot, pElement ) )
00270                         {
00271                                 m_pMembers.AddTail( pMember );
00272                         }
00273                         else
00274                         {
00275                                 delete pMember;
00276                                 return FALSE;
00277                         }
00278                 }
00279                 else if ( strElement.CompareNoCase( _T("all") ) == 0 )
00280                 {
00281                         if ( ! LoadPrimary( pRoot, pElement ) ) return FALSE;
00282                 }
00283         }
00284 
00285         return TRUE;
00286 }
00287 
00288 CXMLElement* CSchema::GetType(CXMLElement* pRoot, LPCTSTR pszName)
00289 {
00290         if ( ! pszName || ! *pszName ) return NULL;
00291 
00292         for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
00293         {
00294                 CXMLElement* pElement = pRoot->GetNextElement( pos );
00295 
00296                 CString strElement = pElement->GetName();
00297 
00298                 if ( strElement.CompareNoCase( _T("simpleType") ) == 0 ||
00299                          strElement.CompareNoCase( _T("complexType") ) == 0 )
00300                 {
00301                         if ( pElement->GetAttributeValue( _T("name"), _T("?") ).CompareNoCase( pszName ) == 0 )
00302                         {
00303                                 return pElement;
00304                         }
00305                 }
00306         }
00307 
00308         return NULL;
00309 }
00310 
00312 // CSchema load descriptor
00313 
00314 BOOL CSchema::LoadDescriptor(LPCTSTR pszFile)
00315 {
00316         CXMLElement* pRoot = CXMLElement::FromFile( pszFile );
00317         if ( NULL == pRoot ) return FALSE;
00318         
00319         if ( m_sURI.CompareNoCase( pRoot->GetAttributeValue( _T("location") ) ) ||
00320                  ! pRoot->IsNamed( _T("schemaDescriptor") ) )
00321         {
00322                 delete pRoot;
00323                 return FALSE;
00324         }
00325         
00326         for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
00327         {
00328                 CXMLElement* pElement = pRoot->GetNextElement( pos );
00329                 
00330                 if ( pElement->IsNamed( _T("object") ) )
00331                 {
00332                         CString strType = pElement->GetAttributeValue( _T("type") );
00333                         CharLower( strType.GetBuffer() );
00334                         strType.ReleaseBuffer();
00335                         
00336                         if ( strType == _T("file") )
00337                                 m_nType = stFile;
00338                         else if ( strType == _T("folder") || strType == _T("album") )
00339                                 m_nType = stFolder;
00340                         
00341                         strType = pElement->GetAttributeValue( _T("availability") );
00342                         CharLower( strType.GetBuffer() );
00343                         strType.ReleaseBuffer();
00344                         
00345                         if ( strType == _T("system") )
00346                                 m_nAvailability = saSystem;
00347                         else if ( strType == _T("advanced") )
00348                                 m_nAvailability = saAdvanced;
00349                         else
00350                                 m_nAvailability = saDefault;
00351                         
00352                         if ( pElement->GetAttribute( _T("private") ) ) m_bPrivate = TRUE;
00353                 }
00354                 else if ( pElement->IsNamed( _T("titles") ) )
00355                 {
00356                         LoadDescriptorTitles( pElement );
00357                 }
00358                 else if ( pElement->IsNamed( _T("images") ) )
00359                 {
00360                         LoadDescriptorIcons( pElement );
00361                 }
00362                 else if ( pElement->IsNamed( _T("members") ) )
00363                 {
00364                         LoadDescriptorMembers( pElement );
00365                 }
00366                 else if ( pElement->IsNamed( _T("extends") ) )
00367                 {
00368                         LoadDescriptorExtends( pElement );
00369                 }
00370                 else if ( pElement->IsNamed( _T("contains") ) )
00371                 {
00372                         LoadDescriptorContains( pElement );
00373                 }
00374                 else if ( pElement->IsNamed( _T("typeFilter") ) )
00375                 {
00376                         LoadDescriptorTypeFilter( pElement );
00377                 }
00378                 else if ( pElement->IsNamed( _T("bitziImport") ) )
00379                 {
00380                         LoadDescriptorBitziImport( pElement );
00381                 }
00382                 else if ( pElement->IsNamed( _T("headerContent") ) )
00383                 {
00384                         LoadDescriptorHeaderContent( pElement );
00385                 }
00386                 else if ( pElement->IsNamed( _T("viewContent") ) )
00387                 {
00388                         LoadDescriptorViewContent( pElement );
00389                 }/* // ToDo: Add this to schemas
00390                 else if ( pElement->IsNamed( _T("donkeyType") ) )
00391                 {
00392                         LoadDescriptorDonkeyType( pElement );
00393                 }*/
00394         }
00395 
00396         delete pRoot;
00397 
00398         return TRUE;
00399 }
00400 
00401 void CSchema::LoadDescriptorTitles(CXMLElement* pElement)
00402 {
00403         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00404         {
00405                 CXMLElement* pTitle = pElement->GetNextElement( pos );
00406 
00407                 if ( pTitle->IsNamed( _T("title") ) )
00408                 {
00409                         if ( pTitle->GetAttributeValue( _T("language") ).
00410                                         CompareNoCase( Settings.General.Language ) == 0 )
00411                         {
00412                                 m_sTitle = pTitle->GetValue();
00413                                 break;
00414                         }
00415                         else if ( m_sTitle.IsEmpty() )
00416                         {
00417                                 m_sTitle = pTitle->GetValue();
00418                         }
00419                 }
00420         }
00421 }
00422 
00423 void CSchema::LoadDescriptorIcons(CXMLElement* pElement)
00424 {
00425         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00426         {
00427                 CXMLElement* pIcon = pElement->GetNextElement( pos );
00428 
00429                 if ( pIcon->IsNamed( _T("icon") ) )
00430                 {
00431                         int nSlash = m_sIcon.ReverseFind( '\\' );
00432                         if ( nSlash >= 0 ) m_sIcon = m_sIcon.Left( nSlash + 1 );
00433                         m_sIcon += pIcon->GetAttributeValue( _T("path") );
00434                 }
00435         }
00436 }
00437 
00438 void CSchema::LoadDescriptorMembers(CXMLElement* pElement)
00439 {
00440         BOOL bPrompt = FALSE;
00441         
00442         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00443         {
00444                 CXMLElement* pDisplay = pElement->GetNextElement( pos );
00445                 
00446                 if ( pDisplay->IsNamed( _T("member") ) )
00447                 {
00448                         CString strMember = pDisplay->GetAttributeValue( _T("name") );
00449                         
00450                         if ( CSchemaMember* pMember = GetMember( strMember ) )
00451                         {
00452                                 pMember->LoadDescriptor( pDisplay );
00453                                 bPrompt |= pMember->m_bPrompt;
00454                         }
00455                 }
00456         }
00457         
00458         if ( bPrompt ) return;
00459         
00460         for ( POSITION pos = GetMemberIterator() ; pos ; )
00461         {
00462                 GetNextMember( pos )->m_bPrompt = TRUE;
00463         }
00464 }
00465 
00466 void CSchema::LoadDescriptorExtends(CXMLElement* pElement)
00467 {
00468         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00469         {
00470                 CXMLElement* pExtend = pElement->GetNextElement( pos );
00471 
00472                 if ( pExtend->IsNamed( _T("schema") ) )
00473                 {
00474                         CString strURI = pExtend->GetAttributeValue( _T("location") );
00475                         if ( strURI.GetLength() ) m_pExtends.AddTail( strURI );
00476                 }
00477         }
00478 }
00479 
00480 void CSchema::LoadDescriptorContains(CXMLElement* pElement)
00481 {
00482         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00483         {
00484                 CXMLElement* pExtend = pElement->GetNextElement( pos );
00485 
00486                 if ( pExtend->IsNamed( _T("object") ) )
00487                 {
00488                         CSchemaChild* pChild = new CSchemaChild( this );
00489 
00490                         if ( pChild->Load( pExtend ) )
00491                         {
00492                                 m_pContains.AddTail( pChild );
00493                         }
00494                         else
00495                         {
00496                                 delete pChild;
00497                         }
00498                 }
00499         }
00500 }
00501 
00502 void CSchema::LoadDescriptorTypeFilter(CXMLElement* pElement)
00503 {
00504         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00505         {
00506                 CXMLElement* pType = pElement->GetNextElement( pos );
00507 
00508                 if ( pType->GetName().CompareNoCase( _T("type") ) == 0 )
00509                 {
00510                         CString strType = pType->GetAttributeValue( _T("extension"), _T("") );
00511                         CharLower( strType.GetBuffer() );
00512                         strType.ReleaseBuffer();
00513 
00514                         m_sTypeFilter += _T("|.");
00515                         m_sTypeFilter += strType;
00516                         m_sTypeFilter += '|';
00517                 }
00518         }
00519 }
00520 
00521 void CSchema::LoadDescriptorBitziImport(CXMLElement* pElement)
00522 {
00523         m_sBitziTest = pElement->GetAttributeValue( _T("testExists"), NULL );
00524 
00525         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00526         {
00527                 CXMLElement* pBitzi = pElement->GetNextElement( pos );
00528 
00529                 if ( pBitzi->GetName().CompareNoCase( _T("mapping") ) == 0 )
00530                 {
00531                         CSchemaBitzi* pMap = new CSchemaBitzi();
00532                         pMap->Load( pBitzi );
00533                         m_pBitziMap.AddTail( pMap );
00534                 }
00535         }
00536 }
00537 
00538 void CSchema::LoadDescriptorHeaderContent(CXMLElement* pElement)
00539 {
00540         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00541         {
00542                 CXMLElement* pXML = pElement->GetNextElement( pos );
00543                 
00544                 BOOL bLanguage = pXML->GetAttributeValue( _T("language") ).
00545                         CompareNoCase( Settings.General.Language ) == 0;
00546 
00547                 if ( pXML->IsNamed( _T("title") ) )
00548                 {
00549                         if ( bLanguage || m_sHeaderTitle.IsEmpty() )
00550                                 m_sHeaderTitle = pXML->GetValue();
00551                 }
00552                 else if ( pXML->IsNamed( _T("subtitle") ) )
00553                 {
00554                         if ( bLanguage || m_sHeaderSubtitle.IsEmpty() )
00555                                 m_sHeaderSubtitle = pXML->GetValue();
00556                 }
00557         }
00558 }
00559 
00560 void CSchema::LoadDescriptorViewContent(CXMLElement* pElement)
00561 {
00562         m_sLibraryView = pElement->GetAttributeValue( _T("preferredView") );
00563         
00564         for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
00565         {
00566                 CXMLElement* pXML = pElement->GetNextElement( pos );
00567 
00568                 BOOL bLanguage = pXML->GetAttributeValue( _T("language") ).
00569                         CompareNoCase( Settings.General.Language ) == 0;
00570                 
00571                 if ( pXML->IsNamed( _T("tileLine1") ) )
00572                 {
00573                         if ( bLanguage || m_sTileLine1.IsEmpty() )
00574                                 m_sTileLine1 = pXML->GetValue();
00575                 }
00576                 else if ( pXML->IsNamed( _T("tileLine2") ) )
00577                 {
00578                         if ( bLanguage || m_sTileLine2.IsEmpty() )
00579                                 m_sTileLine2 = pXML->GetValue();
00580                 }
00581         }
00582 }
00583 
00585 // CSchema load icon
00586 
00587 BOOL CSchema::LoadIcon()
00588 {
00589         HICON hIcon16 = NULL, hIcon32 = NULL, hIcon48 = NULL;
00590 
00591         if ( HINSTANCE hUser = LoadLibrary( _T("User32.dll") ) )
00592         {
00593                 UINT (WINAPI *pfnPrivate)(LPCTSTR, int, int, int, HICON*, UINT*, UINT, UINT);
00594 
00595 #ifdef UNICODE
00596                 (FARPROC&)pfnPrivate = GetProcAddress( hUser, "PrivateExtractIconsW" );
00597 #else
00598                 (FARPROC&)pfnPrivate = GetProcAddress( hUser, "PrivateExtractIconsA" );
00599 #endif
00600 
00601                 if ( pfnPrivate )
00602                 {
00603                         UINT nLoadedID;
00604                         (*pfnPrivate)( m_sIcon, 0, 48, 48, &hIcon48, &nLoadedID, 1, 0 );
00605 
00606                         if ( hIcon48 )
00607                         {
00608                                 if ( theApp.m_bRTL ) hIcon48 = CreateMirroredIcon( hIcon48 );
00609                                 m_nIcon48 = ShellIcons.Add( hIcon48, 48 );
00610                                 DestroyIcon( hIcon48 );
00611                         }
00612                 }
00613                 
00614                 FreeLibrary( hUser );
00615         }
00616 
00617         if ( ExtractIconEx( m_sIcon, 0, &hIcon32, &hIcon16, 1 ) )
00618         {
00619                 if ( hIcon16 )
00620                 {
00621                         if ( theApp.m_bRTL ) hIcon16 = CreateMirroredIcon( hIcon16 );
00622                         m_nIcon16 = ShellIcons.Add( hIcon16, 16 );
00623                         DestroyIcon( hIcon16 );
00624                 }
00625 
00626                 if ( hIcon32 )
00627                 {
00628                         if ( theApp.m_bRTL ) hIcon32 = CreateMirroredIcon( hIcon32 );
00629                         m_nIcon32 = ShellIcons.Add( hIcon32, 32 );
00630                         DestroyIcon( hIcon32 );
00631                 }
00632         }
00633 
00634         return hIcon16 || hIcon32 || hIcon48;
00635 }
00636 
00638 // CSchema contained object helpers
00639 
00640 CSchemaChild* CSchema::GetContained(LPCTSTR pszURI) const
00641 {
00642         for ( POSITION pos = m_pContains.GetHeadPosition() ; pos ; )
00643         {
00644                 CSchemaChild* pChild = (CSchemaChild*)m_pContains.GetNext( pos );
00645                 if ( pChild->m_sURI.CompareNoCase( pszURI ) == 0 ) return pChild;
00646         }
00647         return NULL;
00648 }
00649 
00650 CString CSchema::GetContainedURI(int nType) const
00651 {
00652         for ( POSITION pos = m_pContains.GetHeadPosition() ; pos ; )
00653         {
00654                 CSchemaChild* pChild = (CSchemaChild*)m_pContains.GetNext( pos );
00655 
00656                 if ( pChild->m_nType == nType ) return pChild->m_sURI;
00657         }
00658 
00659         CString strURI;
00660         return strURI;
00661 }
00662 
00664 // CSchema instantiate
00665 
00666 CXMLElement* CSchema::Instantiate(BOOL bNamespace) const
00667 {
00668         CXMLElement* pElement = new CXMLElement( NULL, m_sPlural );
00669         pElement->AddAttribute( CXMLAttribute::schemaName, m_sURI );
00670         if ( bNamespace ) pElement->AddAttribute( _T("xmlns:xsi"), _T("http://www.w3.org/2001/XMLSchema-instance") );
00671         return pElement;
00672 }
00673 
00675 // CSchema validate instance
00676 
00677 BOOL CSchema::Validate(CXMLElement* pXML, BOOL bFix)
00678 {
00679         if ( pXML == NULL ) return FALSE;
00680         
00681         if ( ! pXML->IsNamed( m_sPlural ) ) return FALSE;
00682         if ( pXML->GetAttributeValue( CXMLAttribute::schemaName ) != m_sURI ) return FALSE;
00683         
00684         CXMLElement* pBody = pXML->GetFirstElement();
00685         if ( pBody == NULL ) return FALSE;
00686         if ( ! pBody->IsNamed( m_sSingular ) ) return FALSE;
00687         
00688         for ( POSITION pos = GetMemberIterator() ; pos ; )
00689         {
00690                 CSchemaMember* pMember = GetNextMember( pos );
00691                 
00692                 CString str = pMember->GetValueFrom( pBody, _T("(~np~)"), FALSE );
00693                 if ( str == _T("(~np~)") ) continue;
00694                 
00695                 if ( pMember->m_bNumeric )
00696                 {
00697                         float nNumber;
00698                         if ( str.GetLength() && _stscanf( str, _T("%f"), &nNumber ) != 1 ) return FALSE;
00699                         if ( nNumber < pMember->m_nMinOccurs || nNumber > pMember->m_nMaxOccurs ) return FALSE;
00700                 }
00701                 else if ( pMember->m_nMaxLength > 0 )
00702                 {
00703                         if ( str.GetLength() > pMember->m_nMaxLength )
00704                         {
00705                                 if ( ! bFix ) return FALSE;
00706                                 
00707                                 str = str.Left( pMember->m_nMaxLength );
00708                                 pMember->SetValueTo( pBody, str );
00709                         }
00710                 }
00711         }
00712         
00713         return TRUE;
00714 }
00715 
00717 // CSchema indexed words
00718 
00719 CString CSchema::GetIndexedWords(CXMLElement* pXML) const
00720 {
00721         CString str;
00722         
00723         if ( pXML == NULL ) return str;
00724         
00725         for ( POSITION pos = GetMemberIterator() ; pos ; )
00726         {
00727                 CSchemaMember* pMember = GetNextMember( pos );
00728                 
00729                 if ( pMember->m_bIndexed )
00730                 {
00731                         CString strMember = pMember->GetValueFrom( pXML, NULL, FALSE );
00732                         
00733                         if ( strMember.GetLength() )
00734                         {
00735                                 if ( str.GetLength() ) str += ' ';
00736                                 str += strMember;
00737                         }
00738                 }
00739         }
00740         
00741         return str;
00742 }
00743 
00745 // CSchema member resolution
00746 
00747 void CSchema::ResolveTokens(CString& str, CXMLElement* pXML) const
00748 {
00749         while ( TRUE )
00750         {
00751                 int nOpen = str.Find( '{' );
00752                 if ( nOpen < 0 ) break;
00753                 int nClose = str.Find( '}' );
00754                 if ( nClose <= nOpen ) break;
00755                 
00756                 CString strMember = str.Mid( nOpen + 1, nClose - nOpen - 1 );
00757                 strMember.TrimLeft(); strMember.TrimRight();
00758 
00759                 CString strValue;
00760 
00761                 if ( CSchemaMember* pMember = GetMember( strMember ) )
00762                 {
00763                         strValue = pMember->GetValueFrom( pXML, NULL, TRUE );
00764                 }
00765 
00766                 str = str.Left( nOpen ) + strValue + str.Mid( nClose + 1 );
00767         }
00768 }
00769 
00771 // CSchemaBitzi Bitzi map
00772 
00773 BOOL CSchemaBitzi::Load(CXMLElement* pXML)
00774 {
00775         m_sFrom = pXML->GetAttributeValue( _T("from"), NULL );
00776         m_sTo   = pXML->GetAttributeValue( _T("to"), NULL );
00777 
00778         CString strFactor = pXML->GetAttributeValue( _T("factor"), NULL );
00779 
00780         if ( strFactor.IsEmpty() || _stscanf( strFactor, _T("%lf"), &m_nFactor ) != 1 )
00781                 m_nFactor = 0;
00782 
00783         return m_sFrom.GetLength() && m_sTo.GetLength();
00784 }
00785 
00787 // CSchema common schema URIs
00788 
00789 LPCTSTR CSchema::uriApplication = _T("http://www.shareaza.com/schemas/application.xsd");
00790 LPCTSTR CSchema::uriAudio               = _T("http://www.limewire.com/schemas/audio.xsd");
00791 LPCTSTR CSchema::uriBook                = _T("http://www.limewire.com/schemas/book.xsd");
00792 LPCTSTR CSchema::uriImage               = _T("http://www.shareaza.com/schemas/image.xsd");
00793 LPCTSTR CSchema::uriVideo               = _T("http://www.limewire.com/schemas/video.xsd");
00794 LPCTSTR CSchema::uriROM                 = _T("http://www.shareaza.com/schemas/rom.xsd");
00795 LPCTSTR CSchema::uriDocument     = _T("http://www.shareaza.com/schemas/wordProcessing.xsd");
00796 LPCTSTR CSchema::uriSpreadsheet  = _T("http://www.shareaza.com/schemas/spreadsheet.xsd");
00797 LPCTSTR CSchema::uriPresentation = _T("http://www.shareaza.com/schemas/presentation.xsd");
00798 
00799 LPCTSTR CSchema::uriLibrary                                     = _T("http://www.shareaza.com/schemas/libraryRoot.xsd");
00800 
00801 LPCTSTR CSchema::uriFolder                                      = _T("http://www.shareaza.com/schemas/folder.xsd");
00802 LPCTSTR CSchema::uriCollectionsFolder           = _T("http://www.shareaza.com/schemas/collectionsFolder.xsd");
00803 LPCTSTR CSchema::uriFavouritesFolder            = _T("http://www.shareaza.com/schemas/favouritesFolder.xsd");
00804 LPCTSTR CSchema::uriSearchFolder                        = _T("http://www.shareaza.com/schemas/searchFolder.xsd");
00805 LPCTSTR CSchema::uriAllFiles                            = _T("http://www.shareaza.com/schemas/allFiles.xsd");
00806 
00807 LPCTSTR CSchema::uriApplicationRoot                     = _T("http://www.shareaza.com/schemas/applicationRoot.xsd");
00808 LPCTSTR CSchema::uriApplicationAll                      = _T("http://www.shareaza.com/schemas/applicationAll.xsd");
00809 
00810 LPCTSTR CSchema::uriBookRoot                            = _T("http://www.shareaza.com/schemas/bookRoot.xsd");
00811 LPCTSTR CSchema::uriBookAll                                     = _T("http://www.shareaza.com/schemas/bookAll.xsd");
00812 
00813 LPCTSTR CSchema::uriImageRoot                           = _T("http://www.shareaza.com/schemas/imageRoot.xsd");
00814 LPCTSTR CSchema::uriImageAll                            = _T("http://www.shareaza.com/schemas/imageAll.xsd");
00815 LPCTSTR CSchema::uriImageAlbum                          = _T("http://www.shareaza.com/schemas/imageAlbum.xsd");
00816 
00817 LPCTSTR CSchema::uriMusicRoot                           = _T("http://www.shareaza.com/schemas/musicRoot.xsd");
00818 LPCTSTR CSchema::uriMusicAll                            = _T("http://www.shareaza.com/schemas/musicAll.xsd");
00819 LPCTSTR CSchema::uriMusicAlbumCollection        = _T("http://www.shareaza.com/schemas/musicAlbumCollection.xsd");
00820 LPCTSTR CSchema::uriMusicArtistCollection       = _T("http://www.shareaza.com/schemas/musicArtistCollection.xsd");
00821 LPCTSTR CSchema::uriMusicGenreCollection        = _T("http://www.shareaza.com/schemas/musicGenreCollection.xsd");
00822 LPCTSTR CSchema::uriMusicAlbum                          = _T("http://www.shareaza.com/schemas/musicAlbum.xsd");
00823 LPCTSTR CSchema::uriMusicArtist                         = _T("http://www.shareaza.com/schemas/musicArtist.xsd");
00824 LPCTSTR CSchema::uriMusicGenre                          = _T("http://www.shareaza.com/schemas/musicGenre.xsd");
00825 
00826 LPCTSTR CSchema::uriVideoRoot                           = _T("http://www.shareaza.com/schemas/videoRoot.xsd");
00827 LPCTSTR CSchema::uriVideoAll                            = _T("http://www.shareaza.com/schemas/videoAll.xsd");
00828 LPCTSTR CSchema::uriVideoSeriesCollection       = _T("http://www.shareaza.com/schemas/videoSeriesCollection.xsd");
00829 LPCTSTR CSchema::uriVideoSeries                         = _T("http://www.shareaza.com/schemas/videoSeries.xsd");
00830 LPCTSTR CSchema::uriVideoFilmCollection         = _T("http://www.shareaza.com/schemas/videoFilmCollection.xsd");
00831 LPCTSTR CSchema::uriVideoFilm                           = _T("http://www.shareaza.com/schemas/videoFilm.xsd");
00832 LPCTSTR CSchema::uriVideoMusicCollection        = _T("http://www.shareaza.com/schemas/videoMusicCollection.xsd");
00833 
00834 LPCTSTR CSchema::uriDocumentRoot                        = _T("http://www.shareaza.com/schemas/documentRoot.xsd");
00835 LPCTSTR CSchema::uriDocumentAll                         = _T("http://www.shareaza.com/schemas/documentAll.xsd");

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