home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////////////
- //
- // Node.cpp
- //
- // This file was generated by XMLSPY 5 Enterprise Edition.
- //
- // YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE
- // OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.
- //
- // Refer to the XMLSPY Documentation for further details.
- // http://www.altova.com/xmlspy
- //
- ////////////////////////////////////////////////////////////////////////
-
-
- #include "StdAfx.h"
- #include "SchemaTypes.h"
- #include "XmlException.h"
- #include "Doc.h"
- #include "Node.h"
-
- namespace altova {
-
-
- CNode::CNode()
- {
- m_spDocument = CDoc::GetDocument();
- m_spNode = CDoc::CreateFragment();
- }
-
-
- CNode::CNode(CNode& rParentNode, MSXML2::IXMLDOMNodePtr spThisNode)
- {
- m_spDocument = rParentNode.m_spDocument;
- m_spNode = spThisNode;
- }
-
-
- CNode::CNode(MSXML2::IXMLDOMDocument2Ptr& rspDocument)
- {
- m_spDocument = rspDocument;
- m_spNode = rspDocument->documentElement;
- }
-
-
- CNode::~CNode()
- {
- if ( m_spDocument )
- m_spDocument.Release();
-
- CDoc::CheckDocumentCounter();
- }
-
-
- tstring CNode::Transform(const tstring& sXSLTFilename)
- {
- MSXML2::IXMLDOMDocument2Ptr spXSLTDocument;
- spXSLTDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
- spXSLTDocument->async = VARIANT_FALSE;
- spXSLTDocument->setProperty(_T("NewParser"), true);
-
- if (!spXSLTDocument->load(_variant_t(sXSLTFilename.c_str())))
- {
- MSXML2::IXMLDOMParseErrorPtr spError = spXSLTDocument->parseError;
- throw CXmlException(CXmlException::eError1, (LPCTSTR)spError->reason);
- }
-
- return (LPCTSTR)m_spNode->transformNode(spXSLTDocument);
- }
-
-
- tstring CNode::GetNodeName() const
- {
- return (LPCTSTR)m_spNode->nodeName;
- }
-
-
- tstring CNode::GetNodeText() const
- {
- return (LPCTSTR)m_spNode->text;
- }
-
-
- tstring CNode::ToXMLString() const
- {
- return (LPCTSTR)m_spNode->xml;
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::GetDOMNode()
- {
- return m_spNode;
- }
-
-
- tstring CNode::InternalGetElementValue()
- {
- return m_spNode->text;
- }
-
-
- void CNode::InternalSetElementValue(tstring sValue)
- {
- m_spNode->text = sValue.c_str();
- }
-
-
- void CNode::DeclareNamespace(const tstring& sPrefix, const tstring& sURI)
- {
- if (sURI.empty())
- return;
-
- MSXML2::IXMLDOMElementPtr spRootElement = m_spDocument->documentElement;
-
- if (sPrefix.empty())
- spRootElement->setAttribute(_T("xmlns"), sURI.c_str());
- else
- spRootElement->setAttribute((_T("xmlns:") + sPrefix).c_str(), sURI.c_str());
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::CreateNode(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName)
- {
- _variant_t varNodeType(eNodeType == Element ? (short)MSXML2::NODE_ELEMENT : (short)MSXML2::NODE_ATTRIBUTE);
- return m_spDocument->createNode(varNodeType, AdjustQualifiedName(sNamespaceURI, sName).c_str(), sNamespaceURI.c_str());
- }
-
-
- bool CNode::CompareChildName(MSXML2::IXMLDOMNodePtr spChild, const tstring& sNamespaceURI, const tstring& sName)
- {
- tstring::size_type nPos = sName.find(_T(':'));
- tstring sLocalName;
- if (nPos == tstring::npos)
- sLocalName = sName;
- else
- sLocalName = sName.substr(nPos + 1);
-
- return (!spChild->namespaceURI && sNamespaceURI.empty() || spChild->namespaceURI == _bstr_t(sNamespaceURI.c_str()))
- && spChild->baseName == _bstr_t(sLocalName.c_str());
- }
-
-
- tstring CNode::AdjustQualifiedName(const tstring& sNamespaceURI, const tstring& sName)
- {
- tstring sPrefix = LookupPrefix(m_spNode, sNamespaceURI);
- if (sPrefix.empty())
- {
- return sName;
- }
- else
- {
- tstring::size_type nPos = sName.find(_T(':'));
- if (nPos == tstring::npos)
- return sPrefix + _T(":") + sName;
- else
- return sPrefix + sName.substr(nPos);
- }
- }
-
-
- int CNode::ChildCountInternal(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName)
- {
- if (eNodeType == Element)
- {
- int nCount = 0;
-
- for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
- if (CompareChildName(spChild, sNamespaceURI, sName))
- nCount++;
- return nCount;
- }
- else // eNodeType == Attribute
- {
- return m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str()) ? 1 : 0;
- }
- }
-
-
- bool CNode::InternalHasChild(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName)
- {
- if (eNodeType == Element)
- {
- for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
- if (CompareChildName(spChild, sNamespaceURI, sName))
- return true;
- return false;
- }
- else // eNodeType == Attribute
- {
- return m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str()) ? true : false;
- }
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalAppend(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, const tstring& sValue)
- {
- if (eNodeType == Element)
- {
- MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
- spElement->text = sValue.c_str();
- m_spNode->appendChild(spElement);
- return spElement;
- }
- else // eNodeType == Attribute
- {
- MSXML2::IXMLDOMAttributePtr spAttribute = CreateNode(Attribute, sNamespaceURI, sName);
- spAttribute->text = sValue.c_str();
- m_spNode->attributes->setNamedItem(spAttribute);
- return spAttribute;
- }
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalAppendNode(const tstring& sNamespaceURI, const tstring& sElement, CNode& rNode, MSXML2::IXMLDOMNodePtr spHostNode/* = MSXML2::IXMLDOMNodePtr()*/)
- {
- MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sElement);
-
- while (rNode.m_spNode->firstChild != 0)
- {
- spElement->appendChild(rNode.m_spNode->removeChild(rNode.m_spNode->firstChild));
- }
-
- MSXML2::IXMLDOMNamedNodeMapPtr spAttributeMap = rNode.m_spNode->attributes;
- while (spAttributeMap->length > 0)
- {
- spElement->attributes->setNamedItem(spAttributeMap->removeNamedItem(spAttributeMap->item\[0\]->nodeName));
- }
-
- rNode.m_spNode = spElement;
- rNode.m_spDocument = m_spDocument;
-
- if (spHostNode == NULL)
- m_spNode->appendChild(spElement);
- else
- spHostNode->appendChild(spElement);
-
- rNode.m_spNode = spElement;
-
- return spElement;
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalRemoveAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex)
- {
- if (eNodeType == Element)
- {
- int nCount = 0;
- for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
- if (CompareChildName(spChild, sNamespaceURI, sName))
- if (nCount++ == nIndex)
- return m_spNode->removeChild(spChild);
- if (nCount > 0)
- throw CXmlException(CXmlException::eError1, _T("Index out of range"));
- else
- throw CXmlException(CXmlException::eError1, _T("Not found"));
- }
- else // eNodeType == Attribute
- {
- return m_spNode->attributes->removeNamedItem(sName.c_str());
- }
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalInsertAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex, const tstring& sValue)
- {
- if (eNodeType == Element)
- {
- MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
- spElement->text = sValue.c_str();
- return m_spNode->insertBefore(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex).GetInterfacePtr());
-
- }
- else // eNodeType == Attribute
- {
- MSXML2::IXMLDOMAttributePtr spAttribute = CreateNode(Attribute, sNamespaceURI, sName);
- spAttribute->text = sValue.c_str();
- m_spNode->attributes->setNamedItem(spAttribute);
- return spAttribute;
- }
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalInsertNodeAt(const tstring& sNamespaceURI, const tstring& sName, int nIndex, CNode& rNode)
- {
- MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
-
- while (rNode.m_spNode->firstChild != 0)
- {
- spElement->appendChild(rNode.m_spNode->removeChild(rNode.m_spNode->firstChild));
- }
-
- MSXML2::IXMLDOMNamedNodeMapPtr spAttributeMap = rNode.m_spNode->attributes;
- while (spAttributeMap->length > 0)
- {
- spElement->attributes->setNamedItem(spAttributeMap->removeNamedItem(spAttributeMap->item\[0\]->nodeName));
- }
-
- rNode.m_spNode = spElement;
- rNode.m_spDocument = m_spDocument;
- return m_spNode->insertBefore(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex).GetInterfacePtr());
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalReplaceAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex, const tstring& sValue)
- {
- if (eNodeType == Element)
- {
- MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
- spElement->text = sValue.c_str();
- return m_spNode->replaceChild(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex));
-
- }
- else // eNodeType == Attribute
- {
- if (nIndex > 0)
- throw CXmlException(CXmlException::eError1, _T("Index must be zero"));
- MSXML2::IXMLDOMAttributePtr spAttribute = m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str());
- if (spAttribute)
- {
- spAttribute->text = sValue.c_str();
- return spAttribute;
- }
- else
- throw CXmlException(CXmlException::eError1, _T("Not found"));
- }
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalReplaceNodeAt(const tstring& sNamespaceURI, const tstring& sName, int nIndex, CNode& rNode)
- {
- MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
-
- while (rNode.m_spNode->firstChild != 0)
- {
- spElement->appendChild(rNode.m_spNode->removeChild(rNode.m_spNode->firstChild));
- }
-
- MSXML2::IXMLDOMNamedNodeMapPtr spAttributeMap = rNode.m_spNode->attributes;
- while (spAttributeMap->length > 0)
- {
- spElement->attributes->setNamedItem(spAttributeMap->removeNamedItem(spAttributeMap->item\[0\]->nodeName));
- }
-
- rNode.m_spNode = spElement;
- rNode.m_spDocument = m_spDocument;
- return m_spNode->replaceChild(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex));
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalGetAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex)
- {
- if (eNodeType == Element)
- {
- int nCount = 0;
- for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
- if (CompareChildName(spChild, sNamespaceURI, sName))
- if (nCount++ == nIndex)
- return spChild;
- if (nCount > 0)
- throw CXmlException(CXmlException::eError1, _T("Index out of range"));
- else
- throw CXmlException(CXmlException::eError1, _T("Not found"));
- }
- else // eNodeType == Attribute
- {
- if (nIndex > 0)
- throw CXmlException(CXmlException::eError1, _T("Index must be zero"));
- MSXML2::IXMLDOMNodePtr spAttribute = m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str());
- if (spAttribute)
- return spAttribute;
- else
- throw CXmlException(CXmlException::eError1, _T("Index out of range"));
- }
- }
-
-
- MSXML2::IXMLDOMNodePtr CNode::InternalSet(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, const tstring& sValue)
- {
- if (ChildCountInternal(eNodeType, sNamespaceURI, sName) == 0)
- return InternalAppend(eNodeType, sNamespaceURI, sName, sValue);
- else
- return InternalReplaceAt(eNodeType, sNamespaceURI, sName, 0, sValue);
- }
-
-
- tstring CNode::LookupPrefix(MSXML2::IXMLDOMNodePtr spNode, const tstring& sURI)
- {
- if (!(bool)spNode || sURI.empty())
- return _T("");
-
- if (spNode->nodeTypeString == _bstr_t(L"element"))
- {
- MSXML2::IXMLDOMNamedNodeMapPtr spAttrs = spNode->attributes;
- if (spAttrs)
- {
- long nLength = spAttrs->length;
- for (int i = 0; i < nLength; i++)
- {
- MSXML2::IXMLDOMAttributePtr spAttr = spAttrs->item\[i\];
- _bstr_t bsValue = spAttr->nodeValue;
- if ((LPCTSTR)bsValue == sURI)
- {
- tstring sName = (LPCTSTR)spAttr->nodeName;
- if (sName.length() >= 6 && sName.substr(0, 6) == _T("xmlns:"))
- return sName.substr(6);
- else
- return _T("");
- }
- }
- }
- return LookupPrefix(spNode->parentNode, sURI);
- }
- else if (spNode->nodeTypeString == _bstr_t(L"attribute"))
- {
- return LookupPrefix(spNode->parentNode, sURI);
- }
- else
- {
- return _T("");
- }
- }
-
-
- } // namespace altova
-