XMLTags.cpp
上传用户:kj0090
上传日期:2007-03-02
资源大小:39k
文件大小:22k
- /***********************************************************************
- *
- * This module is part of the XMLGUI system
- *
- * File name: XMLTags.cpp
- *
- * Creation date: [15 AUGUST 2002]
- *
- * Author(s): [Kolosenko Ruslan]
- *
- * Description: Implements the class XMLTags and descendants
- *
- **********************************************************************/
- #include "stdafx.h"
- #include "XMLTags.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // CXMLTag methods
- //////////////////////////////////////////////////////////////////////
- /**************************************************************************
- * Function : CXMLTag::CXMLTag
- * Description : Constructs CXMLTag object representing root tag
- * : of the XML document
- * Arguments : szXMLFile - pointer to a string specifying full name
- * : of the XML file, whose root tag is encapsulated
- * : in the CXMLTag object
- ***************************************************************************/
- CXMLTag::CXMLTag(LPCTSTR szXMLFile)
- {
- HRESULT hr; // result descriptor for COM operations
- // initialize COM if necessary
- static HRESULT hCOMInitialized = ::CoInitialize(NULL);
- // set initial values for members - COM interface pointers
- m_pXMLDocument = NULL;
- m_pXMLElement = NULL;
-
- // create COM object representing XML document
- hr = CoCreateInstance(CLSID_XMLDocument,NULL,CLSCTX_SERVER,IID_IXMLDocument,
- (void**)&m_pXMLDocument);
- WATCH_XML_EXCEPTION(hr);
- // initialize the created COM object from the specified XML file
- _bstr_t strXMLFile(szXMLFile);
- hr = m_pXMLDocument->put_URL(strXMLFile.copy());
- WATCH_XML_EXCEPTION(hr);
- // retrieve the root tag of the document and save its pointer
- hr = m_pXMLDocument->get_root(&m_pXMLElement);
- WATCH_XML_EXCEPTION(hr);
- }
- /**************************************************************************
- * Function : CXMLTag::CXMLTag
- * Description : Constructs CXMLTag object representing any XML tag node
- * Arguments : pXMLElement - pointer to the COM interface of the XML element
- * : corresponding to the node being encapsulated
- ***************************************************************************/
- CXMLTag::CXMLTag(IXMLElement* pXMLElement)
- {
- // set initial values for members - COM interface pointers
- HRESULT hr;
- m_pXMLDocument = NULL;
- m_pXMLElement = NULL;
- // retrieve IXMLElement interface pointer of the specified XML node
- // this also increments reference count for the XML element COM object
- hr = pXMLElement->QueryInterface(IID_IXMLElement,(void**)&m_pXMLElement);
- WATCH_XML_EXCEPTION(hr);
- }
- /**************************************************************************
- * Function : CXMLTag::~CXMLTag
- * Description : Destructor
- ***************************************************************************/
- CXMLTag::~CXMLTag()
- {
- // release all used COM interfaces
- if ( m_pXMLDocument ) m_pXMLDocument->Release();
- if ( m_pXMLElement ) m_pXMLElement->Release();
- }
- /**************************************************************************
- * Function : CXMLTag::GetTitle
- * Description : Returns name of the XML tag
- * Return value : string containing the name of the XML tag
- ***************************************************************************/
- _bstr_t CXMLTag::GetTitle()
- {
- HRESULT hr;
- BSTR bszTagName;
- // retrieve tag name from the encapsulated COM object
- hr = m_pXMLElement->get_tagName(&bszTagName);
- _bstr_t strTagName(bszTagName);
- // return obtained COM string converted to _bstr_t object
- return strTagName;
- }
- /**************************************************************************
- * Function : CXMLTag::GetText
- * Description : Returns text inside the XML tag
- * Return value : string containing the text inside the XML tag,
- * : if the tag doesn't contain any text - empty string is returned
- ***************************************************************************/
- _bstr_t CXMLTag::GetText()
- {
- HRESULT hr;
- BSTR bszTagText;
- // retrieve text from the encapsulated COM object
- hr = m_pXMLElement->get_text(&bszTagText);
- _bstr_t strTagText(bszTagText);
- // return obtained COM string converted to _bstr_t object
- return strTagText;
- }
- /**************************************************************************
- * Function : CXMLTag::GetAttributeInt
- * Description : Returns value of the specified tag's attribute converted
- * : to numeric representation
- * Return value : numeric representation of the attribute value,
- * : if the tag doesn't contain such attribute or its value
- * : cannot be converted to number - zero is returned
- * Arguments : szAttributeName - pointer to a string containing the name
- * : of the attribute whose value is retrieved
- ***************************************************************************/
- int CXMLTag::GetAttributeInt(LPCTSTR szAttributeName)
- {
- HRESULT hr;
- VARIANT attributeValue;
- int nAttributeValue = 0; // default value, it is returned if attribute not present
- // build COM string wrapper with the attribute name
- _bstr_t strAttributeName(szAttributeName);
- // retrieve attribute value from the encapsulated COM object
- hr = m_pXMLElement->getAttribute(strAttributeName.copy(),&attributeValue);
- if ( SUCCEEDED(hr) )
- {
- // if value obtained - convert in to numeric representation
- nAttributeValue = GetIntegerValue(attributeValue);
- }
- // return result of conversion or zero if attribute not found
- return nAttributeValue;
- }
- /**************************************************************************
- * Function : CXMLTag::GetAttributeString
- * Description : Returns value of the specified tag's attribute converted
- * : to string representation
- * Return value : string representation the attribute value,
- * : if the tag doesn't contain such attribute - empty string
- * : is returned
- * Arguments : szAttributeName - pointer to a string containing the name
- * : of the attribute whose value is retrieved
- ***************************************************************************/
- _bstr_t CXMLTag::GetAttributeString(LPCTSTR szAttributeName)
- {
- HRESULT hr;
- VARIANT attributeValue;
- _bstr_t strAttributeValue; // default value is empty string,
- // it is returned if attribute not present
- // build COM string wrapper with the attribute name
- _bstr_t strAttributeName(szAttributeName);
- // retrieve attribute value from the encapsulated COM object
- hr = m_pXMLElement->getAttribute(strAttributeName.copy(),&attributeValue);
- if ( SUCCEEDED(hr) )
- {
- // if value obtained - convert in to string representation
- strAttributeValue = GetStringValue(attributeValue);
- }
- // return resulting string
- return strAttributeValue;
- }
- /**************************************************************************
- * Function : CXMLTag::GetIntegerValue
- * Description : Converts value of VARIANT object to integer number
- * Return value : numberic representation of the VARIANT object's value,
- * : if the value cannot be converted to an integer number -
- * : zero is returned
- * Arguments : var - VARIANT object whose value is converted
- ***************************************************************************/
- /*static*/ int CXMLTag::GetIntegerValue(VARIANT var)
- {
- // build COM wrappers for VARIANT structure and its string value
- _variant_t vart(var);
- _bstr_t strt = (_bstr_t)vart;
- // return string value of the VARIANT object converted to int type
- return atoi((const char*)strt);
- }
- /**************************************************************************
- * Function : CXMLTag::GetStringValue
- * Description : Converts value of VARIANT object to CString object
- * Return value : string representation of the VARIANT object's value,
- * : if the value cannot be converted to a string -
- * : empty string is returned
- * Arguments : var - VARIANT object whose value is converted
- ***************************************************************************/
- /*static*/ _bstr_t CXMLTag::GetStringValue(VARIANT var)
- {
- // build COM wrappers for VARIANT structure and its string value
- _variant_t vart(var);
- _bstr_t strt = (_bstr_t)vart;
- // return string value of retrieved from the COM string wrapper
- return strt;
- }
- //////////////////////////////////////////////////////////////////////
- // CXMLStylableTag methods
- //////////////////////////////////////////////////////////////////////
- /**************************************************************************
- * Function : CXMLStylableTag::CXMLStylableTag
- * Description : Constructs CXMLStylableTag object representing root tag
- * : of the XML document
- * Arguments : szXMLFile - pointer to a string specifying full name
- * : of the XML file, whose root tag is encapsulated
- * : in the CXMLStylableTag object
- ***************************************************************************/
- CXMLStylableTag::CXMLStylableTag(LPCTSTR szXMLFile) : CXMLTag(szXMLFile)
- {
- // initialize member variables with empty style values
- m_nStyles = 0;
- m_nExStyles = 0;
- }
- /**************************************************************************
- * Function : CXMLStylableTag::CXMLStylableTag
- * Description : Constructs CXMLStylableTag object representing any XML tag node
- * Arguments : pXMLElement - pointer to the COM interface of the XML element
- * : corresponding to the node being encapsulated
- ***************************************************************************/
- CXMLStylableTag::CXMLStylableTag(IXMLElement* pXMLElement) : CXMLTag(pXMLElement)
- {
- // initialize member variables with empty style values
- m_nStyles = 0;
- m_nExStyles = 0;
- }
- /**************************************************************************
- * Function : CXMLStylableTag::ParseStylesAttribute
- * Description : Parses string specified in the "style" attribute and fills
- * : member variables with window style and extended style values
- * Arguments : pTagSpecificStyleSyntax - pointer to the syntax structure
- * : describing mapping of allowed styles in string representaion
- * : to Win32 style constants
- * : pTagSpecificExStyleSyntax - pointer to the syntax structure
- * : describing mapping of allowed extended styles
- ***************************************************************************/
- void CXMLStylableTag::ParseStylesAttribute(
- LPXMLGUIStylesValues pTagSpecificStyleSyntax /*= NULL*/,
- LPXMLGUIStylesValues pTagSpecificExStyleSyntax /*= NULL*/)
- {
- // retrieve styles in string representation
- CString szStyles = (LPCTSTR)GetAttributeString(ATTRIBUTE_CONTROL_STYLE);
- int nCurrPos = 0; // counter for the current position in the string
- int nStylesStringLen = szStyles.GetLength(); // total styles string length
-
- while ( nCurrPos < nStylesStringLen ) // lookup through all styles string
- {
- // find next delimiter ( comma by default )
- int nNextPos = szStyles.Find(ATTVALUE_CONTROL_STYLE_DELIM,nCurrPos);
- // calculate length of the next word describing one style
- int nNextWordLen = nNextPos != -1 ? nNextPos - nCurrPos
- : nStylesStringLen - nCurrPos;
- // and obtain the word with one style name
- CString szOneStyle = szStyles.Mid(nCurrPos,nNextWordLen);
- nCurrPos += nNextWordLen+1; // increase current position counter
- if ( szOneStyle.IsEmpty() )
- {
- continue; // if the found word is empty - go to the next one
- }
- // the meaning of the next variable is whether we have to set or to remove
- // the corresponding style bit, true means to set, false - to remove
- // bSetStyle will be assigned false if the style word is prefixed with "not "
- bool bSetStyle = true;
- if ( !_tcsncmp(szOneStyle,ATTVALUE_CONTROL_STYLE_PREFIX_NOT,
- _tcslen(ATTVALUE_CONTROL_STYLE_PREFIX_NOT)) )
- {
- // if prefix "not " found - remove it from the style word
- // and set bSetStyle to false
- szOneStyle = szOneStyle.Right(szOneStyle.GetLength() -
- _tcslen(ATTVALUE_CONTROL_STYLE_PREFIX_NOT));
- bSetStyle = false;
- }
- // now try to identify a Windows style constant corresponding
- // to the current style word in string representation
- // first - we look through common window styles list
- DWORD nWin32Style = LookupStylesSet(g_CommonWindowStyles,
- (LPCTSTR)szOneStyle);
- // if the style is not found in the common window styles list -
- // search control-specific styles list if any
- if ( !nWin32Style && pTagSpecificStyleSyntax )
- {
- nWin32Style = LookupStylesSet(pTagSpecificStyleSyntax,(LPCTSTR)szOneStyle);
- }
- // if the style constant is found among simple window styles -
- // set or clear the corresponding bit in m_nStyles variable
- if ( nWin32Style )
- {
- if ( bSetStyle )
- {
- m_nStyles |= nWin32Style;
- }
- else
- {
- m_nStyles &= ~nWin32Style;
- }
- continue; // the style is identified - procede to the next one
- }
- // the style was not found among simple window styles -
- // now we search the constant in the list of extended styles
- nWin32Style = LookupStylesSet(g_CommonWindowExStyles,
- (LPCTSTR)szOneStyle);
- // if the extended style wasn't found among common window ex.styles
- // try to lookup control-specific extended styles list
- if ( !nWin32Style && pTagSpecificExStyleSyntax )
- {
- nWin32Style = LookupStylesSet(pTagSpecificExStyleSyntax,(LPCTSTR)szOneStyle);
- }
- // if the style value is found among extended styles -
- // set or clear the corresponding bit in m_nExStyles
- if ( nWin32Style )
- {
- if ( bSetStyle )
- {
- m_nExStyles |= nWin32Style;
- }
- else
- {
- m_nExStyles &= ~nWin32Style;
- }
- }
- }
- }
- /**************************************************************************
- * Function : CXMLStylableTag::LookupStylesSet
- * Description : Searches the specified syntax structure for the Windows
- * : style constant corresponding to the given string representation
- * Return value : double word corresponding to the found Win32 window style or
- * : zero if matching style was not found in the styles syntax
- * Arguments : pStylesSet - pointer to the syntax structure
- * : describing mapping of allowed styles in string representaion
- * : to Win32 style constants
- * : szStyle - string representation of a style whose value is searched
- ***************************************************************************/
- DWORD CXMLStylableTag::LookupStylesSet(LPXMLGUIStylesValues pStylesSet, LPCTSTR szStyle)
- {
- // enumerate all available styles to identify the given string
- int i = 0;
- while ( _tcscmp(pStylesSet[i].szXMLGUIStyle,_T("")) )
- {
- if ( !_tcscmp(pStylesSet[i].szXMLGUIStyle,szStyle) )
- {
- // if style name found in the syntax structure -
- // return the corresponding style bit
- return pStylesSet[i].nWin32Style;
- }
- i++; // if style name mismatches - try next
- }
- return 0; // corresponding style string was not found - returning zero
- }
- //////////////////////////////////////////////////////////////////////
- // CXMLDialogTag methods
- //////////////////////////////////////////////////////////////////////
- /**************************************************************************
- * Function : CXMLDialogTag::CXMLDialogTag
- * Description : Constructs CXMLDialogTag object representing root <DIALOG> tag
- * : of the XML document
- * Arguments : szXMLFile - pointer to a string specifying full name
- * : of the XML file, whose root <DIALOG> tag is encapsulated
- * : in the CXMLDialogTag object
- ***************************************************************************/
- CXMLDialogTag::CXMLDialogTag(LPCTSTR szXMLFile) : CXMLStylableTag(szXMLFile)
- {
- // parse string of dialog's styles and fill member variables
- ParseStylesAttribute(g_DialogStyles,NULL);
- }
- /**************************************************************************
- * Function : CXMLDialogTag::CXMLDialogTag
- * Description : Constructs CXMLDialogTag object representing <DIALOG> tag node
- * Arguments : pXMLElement - pointer to the COM interface of the XML element
- * : corresponding to the node being encapsulated
- ***************************************************************************/
- CXMLDialogTag::CXMLDialogTag(IXMLElement* pXMLElement) : CXMLStylableTag(pXMLElement)
- {
- // parse string of dialog's styles and fill member variables
- ParseStylesAttribute(g_DialogStyles,NULL);
- }
- //////////////////////////////////////////////////////////////////////
- // CXMLControlTag methods
- //////////////////////////////////////////////////////////////////////
- /**************************************************************************
- * Function : CXMLControlTag::CXMLControlTag
- * Description : Constructs CXMLControlTag object representing <CONTROL>
- * : XML tag node
- * Arguments : pXMLElement - pointer to the COM interface of the XML element
- * : corresponding to the node being encapsulated
- ***************************************************************************/
- CXMLControlTag::CXMLControlTag(IXMLElement* pXMLElement) : CXMLStylableTag(pXMLElement)
- {
- // set initial values for class variables
- m_pControlSyntax = NULL;
- m_bActiveX = FALSE;
- // identify control's class
- _bstr_t strXMLGUIClass = GetAttributeString(ATTRIBUTE_CONTROL_CLASS);
- if ( !strXMLGUIClass )
- {
- throw CXMLParsingException(
- _T("Invalid GUI description file - control with no class attribute"));
- }
- int i = 0;
- if ( wcscmp((const wchar_t*)strXMLGUIClass,ATTVALUE_CONTROL_ACTIVEX) )
- {
- // for standard Windows controls (not OLE controls) try to identify
- // predefined window class using XMLGUI syntax mapping structure
- while ( wcscmp(g_ControlsSyntax[i].szXMLGUIName,L"") )
- {
- if ( !wcscmp(g_ControlsSyntax[i].szXMLGUIName,(const wchar_t*)strXMLGUIClass) )
- {
- // if class name found in the syntax structure - remember
- // pointer to the control's syntax
- m_pControlSyntax = &g_ControlsSyntax[i];
- break;
- }
- i++; // if class name mismatches try next syntax structure
- }
- }
- else
- {
- // for OLE controls there's no predefined window class,
- // we just set the ActiveX control flag
- m_bActiveX = TRUE;
- }
- // default styles for any control is "child" and "visible"
- m_nStyles = WS_CHILD|WS_VISIBLE;
- if ( m_pControlSyntax )
- {
- // if the control syntax structure found - add the corresponding
- // default styles mask for the control's window class
- m_nStyles |= m_pControlSyntax->nDefWindowStyles;
- }
- // parse string of control's styles and fill member variables
- ParseStylesAttribute(m_pControlSyntax ? m_pControlSyntax->controlStylesSet : NULL,
- m_pControlSyntax ? m_pControlSyntax->controlExStylesSet : NULL);
- }
- /**************************************************************************
- * Function : CXMLControlTag::GetWindowClass
- * Description : Returns string describing window class for the control
- * Return value : string containing the window class for the control
- ***************************************************************************/
- _bstr_t CXMLControlTag::GetWindowClass()
- {
- if ( m_bActiveX )
- {
- // OLE controls should store CLSID in the DLGITEMTEMPLATE structure
- // instead of window class name - return value of "clsid" attribute
- return GetAttributeString(ATTRIBUTE_CONTROL_CLSID);
- }
- else if ( m_pControlSyntax )
- {
- // if the control syntax structure found - return the corresponding
- // Windows class name
- return _bstr_t(m_pControlSyntax->szWindowClass);
- }
- else
- {
- // otherwise treat the value of the "class" attribute as a registered
- // Windows class name - return it
- return GetAttributeString(ATTRIBUTE_CONTROL_CLASS);
- }
- }
- /**************************************************************************
- * Function : CXMLControlTag::GetWindowClassOrdinal
- * Description : Returns ordinal number of the registered window class
- * : for the control
- * Return value : ordinal number of the control's window class
- ***************************************************************************/
- WORD CXMLControlTag::GetWindowClassOrdinal()
- {
- if ( m_pControlSyntax )
- {
- // if the control syntax structure found - return the corresponding
- // ordinal value for Windows class
- return m_pControlSyntax->nClassOrdinal;
- }
- else
- {
- // otherwise the control is not a standard windows control and there's
- // no ordinal value for its class - class name should be used
- return 0;
- }
- }