Would it be possible to implement the code below to support reading a Lex from a XML file?
https://forum.codejock.com/uploads/20070108_164245_Sample.zip - uploads/20070108_164245_Sample.zip
1. Change CXTPSyntaxEditCtrl::Create to
BOOL Create(CWnd *pParent, BOOL bHorzScroll, BOOL bVertScroll, CXTPSyntaxEditBufferManager *pBuffer = NULL, CCreateContext *lpCS = NULL, bool bXml=false);
2. Change XTPSyntaxEditCtrl.cpp line 555 to
if (!SetConfigFile(csCfgFilePath,bXml))
3. Change CXTPSyntaxEditCtrl::SetConfigFile and CXTPSyntaxEditBufferManager::SetConfigFile to
BOOL SetConfigFile(LPCTSTR szPath, bool bXml=false);
4. Change XTPSyntaxEditCtrl.cpp line 6892 to
m_pBuffer->SetConfigFile(szPath,bXml);
5. Change CXTPSyntaxEditBufferManager.cpp line 1199 to
m_ptrLexConfigurationManager->ReloadConfig(szPath,bXml);
6. Add the lines below to CXTPSyntaxEditConfigurationManager
bool GetXml() const { return m_bXml; }
bool m_bXml; // Indicates whether the configuration file is in a XML
7. Change CXTPSyntaxEditConfigurationManager::ReloadConfig to
void CXTPSyntaxEditConfigurationManager::ReloadConfig(const CString& strConfigFilePath, bool bXml)
{
CSingleLock singleLock(&m_DataLockerCS, TRUE);
m_strMainIniFilename = strConfigFilePath;
m_bXml = bXml;
if( bXml &&
strConfigFilePath.GetLength() > 4 &&
strConfigFilePath.Right( 4 ).CompareNoCase( _T(".ini") ) == 0 )
{
m_strMainIniFilename = m_strMainIniFilename.Mid( 0, m_strMainIniFilename.GetLength() - 4 ) + _T(".xml");
}
if( m_bXml )
ReloadConfigFromXml();
else
ReloadConfig();
}
8. Add class CXTPPropExchangeXMLNode; to XTPSyntaxEditLexColorFileReader.h and XTPSyntaxEditLexCfgFileReader.h
9. Add #include "Common\XTPPropExchange.h" to XTPSyntaxEditLexColorFileReader.cpp and XTPSyntaxEditLexCfgFileReader.cpp
10. Add the following methods to the appropriate files (remember to add the definition to the .h files)
void CXTPSyntaxEditConfigurationManager::ReloadConfigFromXml()
{
ASSERT( m_bXml );
CSingleLock singleLock(&m_DataLockerCS, TRUE);
TRACE(_T("LOAD/reload configuration from XML: %s \n"), (LPCTSTR)m_strMainIniFilename);
// restart folder monitor
m_FolderMonitor.StopMonitoring();
// Remove previous data
m_ColorThemeManager.RemoveAll();
m_TextSchemesManager.RemoveAll();
if (!m_FolderMonitor.AddMonitorFile(m_strMainIniFilename, xtpEditCfgObjMainConfig))
{
m_pConnectMT->SendEvent(xtpEditAllConfigWasChanged, 0, 0);
// start monitoring config files
m_FolderMonitor.StartMonitoring();
return;
}
CString strMainIniFolder;
int nFLs = m_strMainIniFilename.ReverseFind(_T('\\'));
if (nFLs > 0)
{
strMainIniFolder = m_strMainIniFilename.Mid(0, nFLs + 1);
}
m_FolderMonitor.SetDefaultFolder(strMainIniFolder);
CXTPPropExchangeXMLNode* pXml = new CXTPPropExchangeXMLNode( TRUE, 0, _T("SyntaxEdit") );
ASSERT_VALID( pXml );
// Load the XML file
if( !pXml->LoadFromFile( m_strMainIniFilename ) )
{
TRACE0( _T("Failed to load XML file.\n") );
pXml->InternalRelease();
return;
}
pXml->OnBeforeExchange();
pXml->SetCompactMode( TRUE );
// Load the themes
CXTPPropExchangeSection secThemes( pXml->GetSection( _T("Themes") ) );
CXTPPropExchangeEnumeratorPtr ptrXmlEnumThemes( ((CXTPPropExchangeXMLNode*)&secThemes)->GetEnumerator( _T("Theme") ) );
for( POSITION xmlPos = ptrXmlEnumThemes->GetPosition(); xmlPos != NULL; )
{
if( m_hReloadThread && m_pBreakReloadEvent )
{
if( XTPSyntaxEditLexAnalyser::IsEventSet( *m_pBreakReloadEvent ) )
{
TRACE(_T("BREAK config reloading. \n"));
return;
}
}
// Load the current theme
CXTPPropExchangeSection secTheme( ptrXmlEnumThemes->GetNext( xmlPos ) );
m_ColorThemeManager.LoadThemeFromXml( (CXTPPropExchangeXMLNode*)&secTheme );
}
// Load the schemas
CXTPPropExchangeSection secSchemas( pXml->GetSection( _T("Schemas") ) );
CXTPPropExchangeEnumeratorPtr ptrXmlEnumSchemas( ((CXTPPropExchangeXMLNode*)&secSchemas)->GetEnumerator( _T("Schema") ) );
for( POSITION xmlPos = ptrXmlEnumSchemas->GetPosition(); xmlPos != NULL; )
{
if( m_hReloadThread && m_pBreakReloadEvent )
{
if( XTPSyntaxEditLexAnalyser::IsEventSet( *m_pBreakReloadEvent ) )
{
TRACE(_T("BREAK config reloading. \n"));
return;
}
}
// Load the current theme
CXTPPropExchangeSection secSchema( ptrXmlEnumSchemas->GetNext( xmlPos ) );
m_TextSchemesManager.LoadTextSchemeFromXml( (CXTPPropExchangeXMLNode*)&secSchema );
}
pXml->InternalRelease();
// set default theme
SetTheme(m_strCurrentThemeName);
//========================================================================
if (m_hReloadThread && m_pBreakReloadEvent)
{
if (XTPSyntaxEditLexAnalyser::IsEventSet(*m_pBreakReloadEvent))
{
TRACE(_T("BREAK config reloading. \n"));
return;
}
}
//========================================================================
m_pConnectMT->SendEvent(xtpEditAllConfigWasChanged, 0, 0);
// start monitoring config files
m_FolderMonitor.StartMonitoring();
}
void CXTPSyntaxEditColorThemesManager::LoadThemeFromXml( CXTPPropExchangeXMLNode* pXml )
{
// Get the theme name
CString strThemeName;
PX_String( pXml, _T("Name"), strThemeName );
ASSERT( strThemeName.GetLength() > 0 );
if( strThemeName.GetLength() == 0 )
return;
// delete old theme
CXTPSyntaxEditColorTheme* pTheme = NULL;
if( m_mapThemes.Lookup( strThemeName, pTheme ) && pTheme )
{
delete pTheme;
// delete its name
for( int i = 0; i < m_arThemeNames.GetSize(); i++ )
{
if( !m_arThemeNames.GetAt( i ).CompareNoCase(strThemeName) )
{
m_arThemeNames.RemoveAt( i );
break;
}
}
}
// create and load new theme
pTheme = new CXTPSyntaxEditColorTheme( this );
pTheme->LoadFromXml(strThemeFilename);
m_mapThemes[strThemeName] = pTheme;
m_arThemeNames.Add(strThemeName);
}
void CXTPSyntaxEditColorTheme::LoadFromXml( CXTPPropExchangeXMLNode* pXml )
{
// Load the Styles node
CXTPPropExchangeSection secStyles( pXml->GetSection( _T("Styles") ) );
// Get the styles name
CString strStylesName;
PX_String( &secStyles, _T("Name"), strStylesName );
ASSERT( strStylesName.GetLength() > 0 );
if( strStylesName.GetLength() == 0 )
return;
CXTPSyntaxEditColorInfo* pColorInfo = new CXTPSyntaxEditColorInfo( strStylesName, this );
// Load the Style nodes
CXTPPropExchangeEnumeratorPtr ptrXmlEnumStyles( ((CXTPPropExchangeXMLNode*)&secStyles)->GetEnumerator( _T("Style") ) );
for( POSITION xmlPos = ptrXmlEnumStyles->GetPosition(); xmlPos != NULL; )
{
CXTPPropExchangeSection secStyle( ptrXmlEnumStyles->GetNext( xmlPos ) );
// Get the style name and value
CString strStyleName;
PX_String( &secStyle, _T("Name"), strStyleName );
ASSERT( strStyleName.GetLength() > 0 );
if( strStyleName.GetLength() == 0 )
continue;
CString strStyleValue;
PX_String( &secStyle, _T("Value"), strStyleValue );
ASSERT( strStyleValue.GetLength() > 0 );
if( strStyleValue.GetLength() == 0 )
continue;
pColorInfo->AddParam( strStyleName, strStyleValue );
}
m_mapLexColorInfo[ strStylesName ] = pColorInfo;
}
void CXTPSyntaxEditTextSchemesManager::LoadTextSchemeFromXml( CXTPPropExchangeXMLNode* pXml )
{
// Get the schema name
CString strSchemaName;
PX_String( pXml, _T("Name"), strSchemaName );
ASSERT( strSchemaName.GetLength() > 0 );
if( strSchemaName.GetLength() == 0 )
return;
CString strSchemeNameLower( strSchemaName );
strSchemeNameLower.MakeLower();
XTPSyntaxEditLexConfig()->ReadSourceFromXml( pXml );
CXTPSyntaxEditLexClassInfoArray& arLexClassInfo = XTPSyntaxEditLexConfig()->GetLexClassInfoArray();
CXTPSyntaxEditTextSchemaPtr ptrTxtSch( new CXTPSyntaxEditTextSchema( strSchemaName ) );
if (ptrTxtSch)
{
ptrTxtSch->LoadClassSchema( arLexClassInfo );
m_mapSchemes[strSchemeNameLower] = ptrTxtSch;
}
}
void CXTPSyntaxEditLexCfgFileReader::ReadSourceFromXml( CXTPPropExchangeXMLNode* pXml )
{
// Load the LexClass nodes
CXTPPropExchangeEnumeratorPtr ptrXmlEnumLexClass( pXml->GetEnumerator( _T("LexClass") ) );
for( POSITION posLexClass = ptrXmlEnumLexClass->GetPosition(); posLexClass != NULL; )
{
CXTPPropExchangeSection secLexClass( ptrXmlEnumLexClass->GetNext( posLexClass ) );
// Get the LexClass name
CString strLexClassName;
PX_String( &secLexClass, _T("Name"), strLexClassName );
ASSERT( strLexClassName.GetLength() > 0 );
if( strLexClassName.GetLength() == 0 )
return;
XTP_EDIT_LEXCLASSINFO infoClass;
infoClass.csClassName = strLexClassName;
infoClass.nStartLine = -1;
infoClass.nEndLine = -1;
// Load the properties
CXTPPropExchangeEnumeratorPtr ptrXmlEnumProps( ((CXTPPropExchangeXMLNode*)&secLexClass)->GetEnumerator( _T("Property") ) );
for( POSITION posProp = ptrXmlEnumProps->GetPosition(); posProp != NULL; )
{
CXTPPropExchangeSection secProp( ptrXmlEnumProps->GetNext( posProp ) );
// Get the property name and value
CString strPropName;
PX_String( &secProp, _T("Name"), strPropName );
strPropName.Trim();
if( strPropName.GetLength() == 0 )
continue;
CString strPropValueRaw;
PX_String( &secProp, _T("Value"), strPropValueRaw );
CString strPropValue = StrToES( strPropValueRaw, TRUE );
XTP_EDIT_LEXPROPINFO infoProp;
_ParsePropName( strPropName, infoProp );
_ParsePropValue( strPropValue, infoProp );
infoProp.nLine = -1;
infoProp.nOffset = -1;
infoProp.nPropertyLen = strPropName.GetLength();
if( infoProp.arPropName.GetCount() > 0 && infoProp.arPropValue.GetCount() > 0 )
infoClass.arPropertyDesc.Add( infoProp );
}
m_arLexClassInfo.Add( infoClass );
}
}
void CXTPSyntaxEditLexCfgFileReader::_ParsePropName( const CString& sPropName, XTP_EDIT_LEXPROPINFO& infoProp )
{
int nTokenPos = 0;
CString sToken = sPropName.Tokenize( _T(":"), nTokenPos );
while( !sToken.IsEmpty() )
{
ASSERT( !sToken.IsEmpty() );
infoProp.arPropName.Add( sToken );
sToken = sPropName.Tokenize( _T(":"), nTokenPos );
}
}
void CXTPSyntaxEditLexCfgFileReader::_ParsePropValue( const CString& sPropValue, XTP_EDIT_LEXPROPINFO& infoProp )
{
TCHAR tLastChar = '\0';
bool bInQuote = false;
bool bWantComma = false;
int nStart = 0;
int nLength = sPropValue.GetLength() - 1;
for( int nPos = 0; nPos <= nLength; nPos++ )
{
TCHAR tChar = sPropValue.GetAt( nPos );
if( tChar == '/' && tLastChar == '/' )
break;
if( !bWantComma || tChar != ',' )
{
bWantComma = false;
if( bInQuote && tChar == '\'' && tLastChar != '\\' )
{
CString sValue = sPropValue.Mid( nStart, nPos - nStart + 1 );
infoProp.arPropValue.Add( sValue );
bInQuote = false;
bWantComma = true;
nStart = nPos + 1;
}
else if( !bInQuote && tChar == '\'' )
{
bInQuote = true;
nStart = nPos;
}
else if( !bInQuote && tChar == ',' )
{
CString sValue = sPropValue.Mid( nStart, nPos - nStart );
infoProp.arPropValue.Add( sValue.Trim() );
nStart = nPos + 1;
}
}
else if( tChar == ',' )
{
bWantComma = false;
nStart = nPos + 1;
}
tLastChar = tChar;
}
if( !bInQuote && nStart <= nLength )
{
CString sValue = sPropValue.Mid( nStart, nLength - nStart + 1 );
infoProp.arPropValue.Add( sValue.Trim() );
}
}