home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 October / INTERNET108.ISO / pc / software / windows / building / xmlspy / xmlspyentcomplete5.exe / Data1.cab / _B4EEC9A6E4694FC388352CAD9285BD19 < prev    next >
Encoding:
Text File  |  2003-05-21  |  11.9 KB  |  425 lines

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Node.cpp
  4. //
  5. // This file was generated by XMLSPY 5 Enterprise Edition.
  6. //
  7. // YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE
  8. // OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.
  9. //
  10. // Refer to the XMLSPY Documentation for further details.
  11. // http://www.altova.com/xmlspy
  12. //
  13. ////////////////////////////////////////////////////////////////////////
  14.  
  15.  
  16. #include "StdAfx.h"
  17. #include "SchemaTypes.h"
  18. #include "XmlException.h"
  19. #include "Doc.h"
  20. #include "Node.h"
  21.  
  22. namespace altova {
  23.  
  24.  
  25. CNode::CNode()
  26. {
  27.     m_spDocument    = CDoc::GetDocument();
  28.     m_spNode        = CDoc::CreateFragment();
  29. }
  30.  
  31.  
  32. CNode::CNode(CNode& rParentNode, MSXML2::IXMLDOMNodePtr spThisNode)
  33. {
  34.     m_spDocument    = rParentNode.m_spDocument;
  35.     m_spNode        = spThisNode;
  36. }
  37.  
  38.  
  39. CNode::CNode(MSXML2::IXMLDOMDocument2Ptr& rspDocument)
  40. {
  41.     m_spDocument    = rspDocument;
  42.     m_spNode        = rspDocument->documentElement;
  43. }
  44.  
  45.  
  46. CNode::~CNode()
  47. {
  48.     if ( m_spDocument )
  49.         m_spDocument.Release();
  50.  
  51.     CDoc::CheckDocumentCounter();
  52. }
  53.  
  54.  
  55. tstring CNode::Transform(const tstring& sXSLTFilename)
  56. {
  57.     MSXML2::IXMLDOMDocument2Ptr spXSLTDocument;
  58.     spXSLTDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
  59.     spXSLTDocument->async = VARIANT_FALSE;
  60.     spXSLTDocument->setProperty(_T("NewParser"), true);
  61.  
  62.     if (!spXSLTDocument->load(_variant_t(sXSLTFilename.c_str())))
  63.     {
  64.         MSXML2::IXMLDOMParseErrorPtr spError = spXSLTDocument->parseError;
  65.         throw CXmlException(CXmlException::eError1, (LPCTSTR)spError->reason);
  66.     }
  67.  
  68.     return (LPCTSTR)m_spNode->transformNode(spXSLTDocument);
  69. }
  70.  
  71.  
  72. tstring CNode::GetNodeName() const
  73. {
  74.     return (LPCTSTR)m_spNode->nodeName;
  75. }
  76.  
  77.  
  78. tstring CNode::GetNodeText() const
  79. {
  80.     return (LPCTSTR)m_spNode->text;
  81. }
  82.  
  83.  
  84. tstring CNode::ToXMLString() const
  85. {
  86.     return (LPCTSTR)m_spNode->xml;
  87. }
  88.  
  89.  
  90. MSXML2::IXMLDOMNodePtr CNode::GetDOMNode()
  91. {
  92.     return m_spNode;
  93. }
  94.  
  95.  
  96. tstring CNode::InternalGetElementValue()
  97. {
  98.     return m_spNode->text;
  99. }
  100.  
  101.  
  102. void CNode::InternalSetElementValue(tstring sValue)
  103. {
  104.     m_spNode->text = sValue.c_str();
  105. }
  106.  
  107.  
  108. void CNode::DeclareNamespace(const tstring& sPrefix, const tstring& sURI)
  109. {
  110.     if (sURI.empty())
  111.         return;
  112.  
  113.     MSXML2::IXMLDOMElementPtr spRootElement = m_spDocument->documentElement;
  114.  
  115.     if (sPrefix.empty())
  116.         spRootElement->setAttribute(_T("xmlns"), sURI.c_str());
  117.     else
  118.         spRootElement->setAttribute((_T("xmlns:") + sPrefix).c_str(), sURI.c_str());
  119. }
  120.  
  121.  
  122. MSXML2::IXMLDOMNodePtr CNode::CreateNode(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName)
  123. {
  124.     _variant_t varNodeType(eNodeType == Element ? (short)MSXML2::NODE_ELEMENT : (short)MSXML2::NODE_ATTRIBUTE);
  125.     return m_spDocument->createNode(varNodeType, AdjustQualifiedName(sNamespaceURI, sName).c_str(), sNamespaceURI.c_str());
  126. }
  127.  
  128.  
  129. bool CNode::CompareChildName(MSXML2::IXMLDOMNodePtr spChild, const tstring& sNamespaceURI, const tstring& sName)
  130. {
  131.     tstring::size_type nPos = sName.find(_T(':'));
  132.     tstring sLocalName;
  133.     if (nPos == tstring::npos)
  134.         sLocalName = sName;
  135.     else
  136.         sLocalName = sName.substr(nPos + 1);
  137.  
  138.     return (!spChild->namespaceURI && sNamespaceURI.empty() || spChild->namespaceURI == _bstr_t(sNamespaceURI.c_str()))
  139.         && spChild->baseName == _bstr_t(sLocalName.c_str());
  140. }
  141.  
  142.  
  143. tstring CNode::AdjustQualifiedName(const tstring& sNamespaceURI, const tstring& sName)
  144. {
  145.     tstring sPrefix = LookupPrefix(m_spNode, sNamespaceURI);
  146.     if (sPrefix.empty())
  147.     {
  148.         return sName;
  149.     }
  150.     else
  151.     {
  152.         tstring::size_type nPos = sName.find(_T(':'));
  153.         if (nPos == tstring::npos)
  154.             return sPrefix + _T(":") + sName;
  155.         else
  156.             return sPrefix + sName.substr(nPos);
  157.     }
  158. }
  159.  
  160.  
  161. int CNode::ChildCountInternal(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName)
  162. {
  163.     if (eNodeType == Element)
  164.     {
  165.         int nCount = 0;
  166.  
  167.         for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
  168.             if (CompareChildName(spChild, sNamespaceURI, sName))
  169.                 nCount++;
  170.         return nCount;
  171.     }
  172.     else // eNodeType == Attribute
  173.     {
  174.         return m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str()) ? 1 : 0;
  175.     }
  176. }
  177.  
  178.  
  179. bool CNode::InternalHasChild(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName)
  180. {
  181.     if (eNodeType == Element)
  182.     {
  183.         for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
  184.             if (CompareChildName(spChild, sNamespaceURI, sName))
  185.                 return true;
  186.         return false;
  187.     }
  188.     else // eNodeType == Attribute
  189.     {
  190.         return m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str()) ? true : false;
  191.     }
  192. }
  193.  
  194.  
  195. MSXML2::IXMLDOMNodePtr CNode::InternalAppend(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, const tstring& sValue)
  196. {
  197.     if (eNodeType == Element)
  198.     {
  199.         MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
  200.         spElement->text = sValue.c_str();
  201.         m_spNode->appendChild(spElement);
  202.         return spElement;
  203.     }
  204.     else // eNodeType == Attribute
  205.     {
  206.         MSXML2::IXMLDOMAttributePtr spAttribute = CreateNode(Attribute, sNamespaceURI, sName);
  207.         spAttribute->text = sValue.c_str();
  208.         m_spNode->attributes->setNamedItem(spAttribute);
  209.         return spAttribute;
  210.     }
  211. }
  212.  
  213.  
  214. MSXML2::IXMLDOMNodePtr CNode::InternalAppendNode(const tstring& sNamespaceURI, const tstring& sElement, CNode& rNode, MSXML2::IXMLDOMNodePtr spHostNode/* = MSXML2::IXMLDOMNodePtr()*/)
  215. {
  216.     MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sElement);
  217.  
  218.     while (rNode.m_spNode->firstChild != 0)
  219.     {
  220.         spElement->appendChild(rNode.m_spNode->removeChild(rNode.m_spNode->firstChild));
  221.     }
  222.  
  223.     MSXML2::IXMLDOMNamedNodeMapPtr spAttributeMap = rNode.m_spNode->attributes;
  224.     while (spAttributeMap->length > 0)
  225.     {
  226.         spElement->attributes->setNamedItem(spAttributeMap->removeNamedItem(spAttributeMap->item\[0\]->nodeName));
  227.     }
  228.  
  229.     rNode.m_spNode = spElement;
  230.     rNode.m_spDocument = m_spDocument;
  231.  
  232.     if (spHostNode == NULL)
  233.         m_spNode->appendChild(spElement);
  234.     else
  235.         spHostNode->appendChild(spElement);
  236.  
  237.     rNode.m_spNode = spElement;
  238.  
  239.     return spElement;
  240. }
  241.  
  242.  
  243. MSXML2::IXMLDOMNodePtr CNode::InternalRemoveAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex)
  244. {
  245.     if (eNodeType == Element)
  246.     {
  247.         int nCount = 0;
  248.         for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
  249.             if (CompareChildName(spChild, sNamespaceURI, sName))
  250.                 if (nCount++ == nIndex)
  251.                     return m_spNode->removeChild(spChild);
  252.         if (nCount > 0)
  253.             throw CXmlException(CXmlException::eError1, _T("Index out of range"));
  254.         else
  255.             throw CXmlException(CXmlException::eError1, _T("Not found"));
  256.     }
  257.     else // eNodeType == Attribute
  258.     {
  259.         return m_spNode->attributes->removeNamedItem(sName.c_str());
  260.     }
  261. }
  262.  
  263.  
  264. MSXML2::IXMLDOMNodePtr CNode::InternalInsertAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex, const tstring& sValue)
  265. {
  266.     if (eNodeType == Element)
  267.     {
  268.         MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
  269.         spElement->text = sValue.c_str();
  270.         return m_spNode->insertBefore(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex).GetInterfacePtr());
  271.  
  272.     }
  273.     else // eNodeType == Attribute
  274.     {
  275.         MSXML2::IXMLDOMAttributePtr spAttribute = CreateNode(Attribute, sNamespaceURI, sName);
  276.         spAttribute->text = sValue.c_str();
  277.         m_spNode->attributes->setNamedItem(spAttribute);
  278.         return spAttribute;
  279.     }
  280. }
  281.  
  282.  
  283. MSXML2::IXMLDOMNodePtr CNode::InternalInsertNodeAt(const tstring& sNamespaceURI, const tstring& sName, int nIndex, CNode& rNode)
  284. {
  285.     MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
  286.  
  287.     while (rNode.m_spNode->firstChild != 0)
  288.     {
  289.         spElement->appendChild(rNode.m_spNode->removeChild(rNode.m_spNode->firstChild));
  290.     }
  291.  
  292.     MSXML2::IXMLDOMNamedNodeMapPtr spAttributeMap = rNode.m_spNode->attributes;
  293.     while (spAttributeMap->length > 0)
  294.     {
  295.         spElement->attributes->setNamedItem(spAttributeMap->removeNamedItem(spAttributeMap->item\[0\]->nodeName));
  296.     }
  297.  
  298.     rNode.m_spNode = spElement;
  299.     rNode.m_spDocument = m_spDocument;
  300.     return m_spNode->insertBefore(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex).GetInterfacePtr());
  301. }
  302.  
  303.  
  304. MSXML2::IXMLDOMNodePtr CNode::InternalReplaceAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex, const tstring& sValue)
  305. {
  306.     if (eNodeType == Element)
  307.     {
  308.         MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
  309.         spElement->text = sValue.c_str();
  310.         return m_spNode->replaceChild(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex));
  311.  
  312.     }
  313.     else // eNodeType == Attribute
  314.     {
  315.         if (nIndex > 0)
  316.             throw CXmlException(CXmlException::eError1, _T("Index must be zero"));
  317.         MSXML2::IXMLDOMAttributePtr spAttribute = m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str());
  318.         if (spAttribute)
  319.         {
  320.             spAttribute->text = sValue.c_str();
  321.             return spAttribute;
  322.         }
  323.         else
  324.             throw CXmlException(CXmlException::eError1, _T("Not found"));
  325.     }
  326. }
  327.  
  328.  
  329. MSXML2::IXMLDOMNodePtr CNode::InternalReplaceNodeAt(const tstring& sNamespaceURI, const tstring& sName, int nIndex, CNode& rNode)
  330. {
  331.     MSXML2::IXMLDOMElementPtr spElement = CreateNode(Element, sNamespaceURI, sName);
  332.  
  333.     while (rNode.m_spNode->firstChild != 0)
  334.     {
  335.         spElement->appendChild(rNode.m_spNode->removeChild(rNode.m_spNode->firstChild));
  336.     }
  337.  
  338.     MSXML2::IXMLDOMNamedNodeMapPtr spAttributeMap = rNode.m_spNode->attributes;
  339.     while (spAttributeMap->length > 0)
  340.     {
  341.         spElement->attributes->setNamedItem(spAttributeMap->removeNamedItem(spAttributeMap->item\[0\]->nodeName));
  342.     }
  343.  
  344.     rNode.m_spNode = spElement;
  345.     rNode.m_spDocument = m_spDocument;
  346.     return m_spNode->replaceChild(spElement, InternalGetAt(Element, sNamespaceURI, sName, nIndex));
  347. }
  348.  
  349.  
  350. MSXML2::IXMLDOMNodePtr CNode::InternalGetAt(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, int nIndex)
  351. {
  352.     if (eNodeType == Element)
  353.     {
  354.         int nCount = 0;
  355.         for (MSXML2::IXMLDOMNodePtr spChild = m_spNode->firstChild; spChild; spChild = spChild->nextSibling)
  356.             if (CompareChildName(spChild, sNamespaceURI, sName))
  357.                 if (nCount++ == nIndex)
  358.                     return spChild;
  359.         if (nCount > 0)
  360.             throw CXmlException(CXmlException::eError1, _T("Index out of range"));
  361.         else
  362.             throw CXmlException(CXmlException::eError1, _T("Not found"));
  363.     }
  364.     else // eNodeType == Attribute
  365.     {
  366.         if (nIndex > 0)
  367.             throw CXmlException(CXmlException::eError1, _T("Index must be zero"));
  368.         MSXML2::IXMLDOMNodePtr spAttribute = m_spNode->attributes->getQualifiedItem(sName.c_str(), sNamespaceURI.c_str());
  369.         if (spAttribute)
  370.             return spAttribute;
  371.         else
  372.             throw CXmlException(CXmlException::eError1, _T("Index out of range"));
  373.     }
  374. }
  375.  
  376.  
  377. MSXML2::IXMLDOMNodePtr CNode::InternalSet(ENodeType eNodeType, const tstring& sNamespaceURI, const tstring& sName, const tstring& sValue)
  378. {
  379.     if (ChildCountInternal(eNodeType, sNamespaceURI, sName) == 0)
  380.         return InternalAppend(eNodeType, sNamespaceURI, sName, sValue);
  381.     else
  382.         return InternalReplaceAt(eNodeType, sNamespaceURI, sName, 0, sValue);
  383. }
  384.  
  385.  
  386. tstring CNode::LookupPrefix(MSXML2::IXMLDOMNodePtr spNode, const tstring& sURI)
  387. {
  388.     if (!(bool)spNode || sURI.empty())
  389.         return _T("");
  390.  
  391.     if (spNode->nodeTypeString == _bstr_t(L"element"))
  392.     {
  393.         MSXML2::IXMLDOMNamedNodeMapPtr spAttrs = spNode->attributes;
  394.         if (spAttrs)
  395.         {
  396.             long nLength = spAttrs->length;
  397.             for (int i = 0; i < nLength; i++)
  398.             {
  399.                 MSXML2::IXMLDOMAttributePtr spAttr = spAttrs->item\[i\];
  400.                 _bstr_t bsValue = spAttr->nodeValue;
  401.                 if ((LPCTSTR)bsValue == sURI)
  402.                 {
  403.                     tstring sName = (LPCTSTR)spAttr->nodeName;
  404.                     if (sName.length() >= 6 && sName.substr(0, 6) == _T("xmlns:"))
  405.                         return sName.substr(6);
  406.                     else
  407.                         return _T("");
  408.                 }
  409.             }
  410.         }
  411.         return LookupPrefix(spNode->parentNode, sURI);
  412.     }
  413.     else if (spNode->nodeTypeString == _bstr_t(L"attribute"))
  414.     {
  415.         return LookupPrefix(spNode->parentNode, sURI);
  416.     }
  417.     else
  418.     {
  419.         return _T("");
  420.     }
  421. }
  422.  
  423.  
  424. } // namespace altova
  425.