home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 Autumn / INTERNET109.ISO / pc / software / windows / building / xmlspy / xmlspyentcomplete5.exe / Data1.cab / _68E0A7E63A5046198DC1B89840F3C5DA < prev    next >
Encoding:
Text File  |  2003-03-21  |  4.8 KB  |  190 lines

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Doc.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. MSXML2::IXMLDOMDocument2Ptr            CDoc::m_spDocument            = NULL;
  26. MSXML2::IXMLDOMDocumentFragmentPtr    CDoc::m_spFragment            = NULL;
  27. int                                    CDoc::m_nTmpNameCounter    = 0;
  28.  
  29.  
  30. MSXML2::IXMLDOMDocument2Ptr CDoc::GetDocument()
  31. {
  32.     if (m_spDocument == NULL)
  33.     {
  34.         m_spDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
  35.         m_spDocument->async = VARIANT_FALSE;
  36.         m_spDocument->setProperty(L"NewParser", true);
  37.     }
  38.     return m_spDocument;
  39. }
  40.  
  41.  
  42. MSXML2::IXMLDOMNodePtr CDoc::CreateFragment()
  43. {
  44.     //return GetDocument()->createDocumentFragment();
  45.  
  46.     TCHAR szNumber\[32\] = _T("tmp");
  47.     _itot(m_nTmpNameCounter++, szNumber + 3, 10);
  48.  
  49.     if (m_spFragment == NULL)
  50.     {
  51.         m_spFragment = GetDocument()->createDocumentFragment();
  52.     }
  53.     MSXML2::IXMLDOMNodePtr spNode = GetDocument()->createElement(szNumber);
  54.     m_spFragment->appendChild( spNode );
  55.     return spNode;
  56. }
  57.  
  58.  
  59. void CDoc::CheckDocumentCounter()
  60. {
  61.     if (m_spDocument)
  62.     {
  63.         m_spDocument->AddRef();
  64.         if (m_spDocument->Release() == 1)
  65.         {
  66.             m_spFragment.Release();
  67.             m_spDocument.Release();
  68.         }
  69.     }
  70. }
  71.  
  72.  
  73. MSXML2::IXMLDOMDocument2Ptr CDoc::Load(const tstring& sFilename)
  74. {
  75.     MSXML2::IXMLDOMDocument2Ptr spDocument;
  76.  
  77.     spDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
  78.     spDocument->async = VARIANT_FALSE;
  79.     spDocument->validateOnParse = VARIANT_FALSE;
  80.     spDocument->setProperty(L"NewParser", true);
  81.  
  82.     if (!spDocument->load(_variant_t(sFilename.c_str())))
  83.     {
  84.         MSXML2::IXMLDOMParseErrorPtr spError = spDocument->parseError;
  85.         throw CXmlException(CXmlException::eError1, (LPCTSTR)spError->reason);
  86.     }
  87.     return spDocument;
  88. }
  89.  
  90.  
  91. void CDoc::SetRootElementName(tstring sNamespaceURI, tstring sName)
  92. {
  93.     m_sName = sName;
  94.     m_sNamespaceURI = sNamespaceURI;
  95. }
  96.  
  97.  
  98. void CDoc::SetSchemaLocation(const tstring& sSchemaLocation)
  99. {
  100.     m_sSchemaLocation = sSchemaLocation;
  101. }
  102.  
  103.  
  104. void CDoc::Save(const tstring& sFilename, CNode& rNode)
  105. {
  106.     if (rNode.m_spNode->parentNode->nodeTypeString == _bstr_t(L"documentfragment"))
  107.     {
  108.         FinalizeRootElement(rNode);
  109.     }
  110.     InternalSetSchemaLocation(rNode);
  111.  
  112.     rNode.m_spDocument->save(_variant_t(sFilename.c_str()));
  113. }
  114.  
  115.  
  116. void CDoc::Validate(CNode& rNode)
  117. {
  118.  
  119.     if (rNode.m_spNode->parentNode->nodeTypeString == _bstr_t(L"documentfragment"))
  120.     {
  121.         FinalizeRootElement(rNode);
  122.     }
  123.     InternalSetSchemaLocation(rNode);
  124.  
  125.     MSXML2::IXMLDOMParseErrorPtr spError = rNode.m_spDocument->validate();
  126.     if (spError->errorCode)
  127.     {
  128.         throw CXmlException(CXmlException::eError1, (LPCTSTR)spError->reason);
  129.     }
  130. }
  131.  
  132.  
  133. void CDoc::FinalizeRootElement(CNode& rNode)
  134. {
  135.     if (m_sName.empty())
  136.     {
  137.         throw CXmlException(CXmlException::eError1, _T("Call SetRootElementName first"));
  138.     }
  139.  
  140.     rNode.m_spDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
  141.     rNode.m_spDocument->async = VARIANT_FALSE;
  142.     rNode.m_spDocument->setProperty(L"NewParser", true);
  143.  
  144.     rNode.m_spDocument->appendChild(rNode.m_spDocument->createProcessingInstruction(L"xml", L"version='1.0' encoding='UTF-8'"));
  145.  
  146.     rNode.m_spNode = rNode.InternalAppendNode(m_sNamespaceURI, m_sName, rNode, rNode.m_spDocument);
  147.     MSXML2::IXMLDOMElementPtr spRootElement = rNode.m_spNode;
  148.  
  149.     spRootElement->setAttribute(L"xmlns:xsi", L"http://www.w3.org/2001/XMLSchema-instance");
  150.  
  151.     if (m_sNamespaceURI.empty())
  152.     {
  153.         if (!m_sSchemaLocation.empty())
  154.             spRootElement->setAttribute(L"xsi:noNamespaceSchemaLocation", m_sSchemaLocation.c_str());
  155.     }
  156.     else
  157.     {
  158.         if (!m_sSchemaLocation.empty())
  159.             spRootElement->setAttribute(L"xsi:schemaLocation", (m_sNamespaceURI + _T(" ") + m_sSchemaLocation).c_str());
  160.     }
  161.  
  162.     DeclareNamespaces(rNode);
  163. }
  164.  
  165.  
  166. void CDoc::InternalSetSchemaLocation(CNode& rNode)
  167. {
  168.     MSXML2::IXMLDOMElementPtr spRootElement = rNode.m_spNode;
  169.  
  170.     if (m_sNamespaceURI.empty())
  171.     {
  172.         if (!m_sSchemaLocation.empty() && spRootElement->getAttributeNode(L"xsi:noNamespaceSchemaLocation") == NULL)
  173.             spRootElement->setAttribute(L"xsi:noNamespaceSchemaLocation", m_sSchemaLocation.c_str());
  174.     }
  175.     else
  176.     {
  177.         if (!m_sSchemaLocation.empty() && spRootElement->getAttributeNode(L"xsi:schemaLocation") == NULL)
  178.             spRootElement->setAttribute(L"xsi:schemaLocation", (m_sNamespaceURI + _T(" ") + m_sSchemaLocation).c_str());
  179.     }
  180. }
  181.  
  182.  
  183. void CDoc::DeclareNamespace(CNode& rNode, const tstring& sPrefix, const tstring& sURI)
  184. {
  185.     rNode.DeclareNamespace(sPrefix, sURI);
  186. }
  187.  
  188.  
  189. } // namespace altova
  190.