📄 schema.cpp
字号:
//
// Schema.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2004.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "StdAfx.h"
#include "Shareaza.h"
#include "Settings.h"
#include "Schema.h"
#include "SchemaMember.h"
#include "SchemaChild.h"
#include "ShellIcons.h"
#include "CoolInterface.h"
#include "XML.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// CSchema construction
CSchema::CSchema()
{
m_nType = stFile;
m_nAvailability = saDefault;
m_bPrivate = FALSE;
m_nIcon16 = m_nIcon32 = m_nIcon48 = -1;
}
CSchema::~CSchema()
{
Clear();
}
//////////////////////////////////////////////////////////////////////
// CSchema member access
POSITION CSchema::GetMemberIterator() const
{
return m_pMembers.GetHeadPosition();
}
CSchemaMember* CSchema::GetNextMember(POSITION& pos) const
{
return (CSchemaMember*)m_pMembers.GetNext( pos );
}
CSchemaMember* CSchema::GetMember(LPCTSTR pszName) const
{
if ( ! pszName || ! *pszName ) return NULL;
for ( POSITION pos = GetMemberIterator() ; pos ; )
{
CSchemaMember* pMember = GetNextMember( pos );
if ( pMember->m_sName.CompareNoCase( pszName ) == 0 ) return pMember;
}
return NULL;
}
int CSchema::GetMemberCount() const
{
return m_pMembers.GetCount();
}
CString CSchema::GetFirstMemberName() const
{
if ( m_pMembers.GetCount() )
{
CSchemaMember* pMember = (CSchemaMember*)m_pMembers.GetHead();
return pMember->m_sName;
}
CString str( _T("title") );
return str;
}
//////////////////////////////////////////////////////////////////////
// CSchema clear
void CSchema::Clear()
{
for ( POSITION pos = GetMemberIterator() ; pos ; )
{
delete GetNextMember( pos );
}
for ( pos = m_pContains.GetHeadPosition() ; pos ; )
{
delete (CSchemaChild*)m_pContains.GetNext( pos );
}
for ( pos = m_pBitziMap.GetHeadPosition() ; pos ; )
{
delete (CSchemaBitzi*)m_pBitziMap.GetNext( pos );
}
m_pMembers.RemoveAll();
m_pContains.RemoveAll();
m_pBitziMap.RemoveAll();
}
//////////////////////////////////////////////////////////////////////
// CSchema load
BOOL CSchema::Load(LPCTSTR pszFile)
{
CString strFile( pszFile );
int nSlash = strFile.Find( '.' );
if ( nSlash >= 0 ) strFile = strFile.Left( nSlash );
if ( ! LoadSchema( strFile + _T(".xsd") ) ) return FALSE;
m_sIcon = strFile + _T(".ico");
LoadDescriptor( strFile + _T(".xml") );
m_sIcon = m_sIcon.Left( m_sIcon.GetLength() - 4 );
m_sIcon += _T("XP.ico");
if ( ! CCoolInterface::IsNewWindows() || ! LoadIcon() )
{
m_sIcon = m_sIcon.Left( m_sIcon.GetLength() - 6 );
m_sIcon += _T(".ico");
LoadIcon();
}
if ( m_sTitle.IsEmpty() )
{
m_sTitle = m_sSingular;
m_sTitle.SetAt( 0, toupper( m_sTitle.GetAt( 0 ) ) );
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// CSchema load schema
BOOL CSchema::LoadSchema(LPCTSTR pszFile)
{
CString strXML;
CXMLElement* pRoot = CXMLElement::FromFile( pszFile );
if ( NULL == pRoot ) return FALSE;
BOOL bResult = FALSE;
m_sURI = pRoot->GetAttributeValue( _T("targetNamespace"), _T("") );
CXMLElement* pPlural = pRoot->GetFirstElement();
if ( pPlural && m_sURI.GetLength() )
{
m_sPlural = pPlural->GetAttributeValue( _T("name") );
CXMLElement* pComplexType = pPlural->GetFirstElement();
if ( pComplexType && pComplexType->IsNamed( _T("complexType") ) && m_sPlural.GetLength() )
{
CXMLElement* pElement = pComplexType->GetFirstElement();
if ( pElement && pElement->IsNamed( _T("element") ) )
{
m_sSingular = pElement->GetAttributeValue( _T("name") );
if ( pElement->GetElementCount() )
{
bResult = LoadPrimary( pRoot, pElement->GetFirstElement() );
}
else
{
CString strType = pElement->GetAttributeValue( _T("type") );
bResult = LoadPrimary( pRoot, GetType( pRoot, strType ) );
}
if ( m_sSingular.IsEmpty() ) bResult = FALSE;
}
}
}
delete pRoot;
return bResult;
}
BOOL CSchema::LoadPrimary(CXMLElement* pRoot, CXMLElement* pType)
{
if ( ! pRoot || ! pType ) return FALSE;
if ( ! pType->IsNamed( _T("complexType") ) &&
! pType->IsNamed( _T("all") ) ) return FALSE;
for ( POSITION pos = pType->GetElementIterator() ; pos ; )
{
CXMLElement* pElement = pType->GetNextElement( pos );
CString strElement = pElement->GetName();
if ( strElement.CompareNoCase( _T("attribute") ) == 0 ||
strElement.CompareNoCase( _T("element") ) == 0 )
{
CSchemaMember* pMember = new CSchemaMember( this );
if ( pMember->LoadSchema( pRoot, pElement ) )
{
m_pMembers.AddTail( pMember );
}
else
{
delete pMember;
return FALSE;
}
}
else if ( strElement.CompareNoCase( _T("all") ) == 0 )
{
if ( ! LoadPrimary( pRoot, pElement ) ) return FALSE;
}
}
return TRUE;
}
CXMLElement* CSchema::GetType(CXMLElement* pRoot, LPCTSTR pszName)
{
if ( ! pszName || ! *pszName ) return NULL;
for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
{
CXMLElement* pElement = pRoot->GetNextElement( pos );
CString strElement = pElement->GetName();
if ( strElement.CompareNoCase( _T("simpleType") ) == 0 ||
strElement.CompareNoCase( _T("complexType") ) == 0 )
{
if ( pElement->GetAttributeValue( _T("name"), _T("?") ).CompareNoCase( pszName ) == 0 )
{
return pElement;
}
}
}
return NULL;
}
//////////////////////////////////////////////////////////////////////
// CSchema load descriptor
BOOL CSchema::LoadDescriptor(LPCTSTR pszFile)
{
CXMLElement* pRoot = CXMLElement::FromFile( pszFile );
if ( NULL == pRoot ) return FALSE;
if ( m_sURI.CompareNoCase( pRoot->GetAttributeValue( _T("location") ) ) ||
! pRoot->IsNamed( _T("schemaDescriptor") ) )
{
delete pRoot;
return FALSE;
}
for ( POSITION pos = pRoot->GetElementIterator() ; pos ; )
{
CXMLElement* pElement = pRoot->GetNextElement( pos );
if ( pElement->IsNamed( _T("object") ) )
{
CString strType = pElement->GetAttributeValue( _T("type") );
strType.MakeLower();
if ( strType == _T("file") )
m_nType = stFile;
else if ( strType == _T("folder") || strType == _T("album") )
m_nType = stFolder;
strType = pElement->GetAttributeValue( _T("availability") );
strType.MakeLower();
if ( strType == _T("system") )
m_nAvailability = saSystem;
else if ( strType == _T("advanced") )
m_nAvailability = saAdvanced;
else
m_nAvailability = saDefault;
if ( pElement->GetAttribute( _T("private") ) ) m_bPrivate = TRUE;
}
else if ( pElement->IsNamed( _T("titles") ) )
{
LoadDescriptorTitles( pElement );
}
else if ( pElement->IsNamed( _T("images") ) )
{
LoadDescriptorIcons( pElement );
}
else if ( pElement->IsNamed( _T("members") ) )
{
LoadDescriptorMembers( pElement );
}
else if ( pElement->IsNamed( _T("extends") ) )
{
LoadDescriptorExtends( pElement );
}
else if ( pElement->IsNamed( _T("contains") ) )
{
LoadDescriptorContains( pElement );
}
else if ( pElement->IsNamed( _T("typeFilter") ) )
{
LoadDescriptorTypeFilter( pElement );
}
else if ( pElement->IsNamed( _T("bitziImport") ) )
{
LoadDescriptorBitziImport( pElement );
}
else if ( pElement->IsNamed( _T("headerContent") ) )
{
LoadDescriptorHeaderContent( pElement );
}
else if ( pElement->IsNamed( _T("viewContent") ) )
{
LoadDescriptorViewContent( pElement );
}
}
delete pRoot;
return TRUE;
}
void CSchema::LoadDescriptorTitles(CXMLElement* pElement)
{
for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
{
CXMLElement* pTitle = pElement->GetNextElement( pos );
if ( pTitle->IsNamed( _T("title") ) )
{
if ( pTitle->GetAttributeValue( _T("language") ).
CompareNoCase( Settings.General.Language ) == 0 )
{
m_sTitle = pTitle->GetValue();
break;
}
else if ( m_sTitle.IsEmpty() )
{
m_sTitle = pTitle->GetValue();
}
}
}
}
void CSchema::LoadDescriptorIcons(CXMLElement* pElement)
{
for ( POSITION pos = pElement->GetElementIterator() ; pos ; )
{
CXMLElement* pIcon = pElement->GetNextElement( pos );
if ( pIcon->IsNamed( _T("icon") ) )
{
int nSlash = m_sIcon.ReverseFind( '\\' );
if ( nSlash >= 0 ) m_sIcon = m_sIcon.Left( nSlash + 1 );
m_sIcon += pIcon->GetAttributeValue( _T("path") );
}
}
}
void CSchema::LoadDescriptorMembers(CXMLElement* pElement)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -