home *** CD-ROM | disk | FTP | other *** search
- ////////////////////////////////////////////////////////////////////////
- //
- // Doc.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 {
-
-
- MSXML2::IXMLDOMDocument2Ptr CDoc::m_spDocument = NULL;
- MSXML2::IXMLDOMDocumentFragmentPtr CDoc::m_spFragment = NULL;
- int CDoc::m_nTmpNameCounter = 0;
-
-
- MSXML2::IXMLDOMDocument2Ptr CDoc::GetDocument()
- {
- if (m_spDocument == NULL)
- {
- m_spDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
- m_spDocument->async = VARIANT_FALSE;
- m_spDocument->setProperty(L"NewParser", true);
- }
- return m_spDocument;
- }
-
-
- MSXML2::IXMLDOMNodePtr CDoc::CreateFragment()
- {
- //return GetDocument()->createDocumentFragment();
-
- TCHAR szNumber\[32\] = _T("tmp");
- _itot(m_nTmpNameCounter++, szNumber + 3, 10);
-
- if (m_spFragment == NULL)
- {
- m_spFragment = GetDocument()->createDocumentFragment();
- }
- MSXML2::IXMLDOMNodePtr spNode = GetDocument()->createElement(szNumber);
- m_spFragment->appendChild( spNode );
- return spNode;
- }
-
-
- void CDoc::CheckDocumentCounter()
- {
- if (m_spDocument)
- {
- m_spDocument->AddRef();
- if (m_spDocument->Release() == 1)
- {
- m_spFragment.Release();
- m_spDocument.Release();
- }
- }
- }
-
-
- MSXML2::IXMLDOMDocument2Ptr CDoc::Load(const tstring& sFilename)
- {
- MSXML2::IXMLDOMDocument2Ptr spDocument;
-
- spDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
- spDocument->async = VARIANT_FALSE;
- spDocument->validateOnParse = VARIANT_FALSE;
- spDocument->setProperty(L"NewParser", true);
-
- if (!spDocument->load(_variant_t(sFilename.c_str())))
- {
- MSXML2::IXMLDOMParseErrorPtr spError = spDocument->parseError;
- throw CXmlException(CXmlException::eError1, (LPCTSTR)spError->reason);
- }
- return spDocument;
- }
-
-
- void CDoc::SetRootElementName(tstring sNamespaceURI, tstring sName)
- {
- m_sName = sName;
- m_sNamespaceURI = sNamespaceURI;
- }
-
-
- void CDoc::SetSchemaLocation(const tstring& sSchemaLocation)
- {
- m_sSchemaLocation = sSchemaLocation;
- }
-
-
- void CDoc::Save(const tstring& sFilename, CNode& rNode)
- {
- if (rNode.m_spNode->parentNode->nodeTypeString == _bstr_t(L"documentfragment"))
- {
- FinalizeRootElement(rNode);
- }
- InternalSetSchemaLocation(rNode);
-
- rNode.m_spDocument->save(_variant_t(sFilename.c_str()));
- }
-
-
- void CDoc::Validate(CNode& rNode)
- {
-
- if (rNode.m_spNode->parentNode->nodeTypeString == _bstr_t(L"documentfragment"))
- {
- FinalizeRootElement(rNode);
- }
- InternalSetSchemaLocation(rNode);
-
- MSXML2::IXMLDOMParseErrorPtr spError = rNode.m_spDocument->validate();
- if (spError->errorCode)
- {
- throw CXmlException(CXmlException::eError1, (LPCTSTR)spError->reason);
- }
- }
-
-
- void CDoc::FinalizeRootElement(CNode& rNode)
- {
- if (m_sName.empty())
- {
- throw CXmlException(CXmlException::eError1, _T("Call SetRootElementName first"));
- }
-
- rNode.m_spDocument.CreateInstance(__uuidof(MSXML2::DOMDocument40));
- rNode.m_spDocument->async = VARIANT_FALSE;
- rNode.m_spDocument->setProperty(L"NewParser", true);
-
- rNode.m_spDocument->appendChild(rNode.m_spDocument->createProcessingInstruction(L"xml", L"version='1.0' encoding='UTF-8'"));
-
- rNode.m_spNode = rNode.InternalAppendNode(m_sNamespaceURI, m_sName, rNode, rNode.m_spDocument);
- MSXML2::IXMLDOMElementPtr spRootElement = rNode.m_spNode;
-
- spRootElement->setAttribute(L"xmlns:xsi", L"http://www.w3.org/2001/XMLSchema-instance");
-
- if (m_sNamespaceURI.empty())
- {
- if (!m_sSchemaLocation.empty())
- spRootElement->setAttribute(L"xsi:noNamespaceSchemaLocation", m_sSchemaLocation.c_str());
- }
- else
- {
- if (!m_sSchemaLocation.empty())
- spRootElement->setAttribute(L"xsi:schemaLocation", (m_sNamespaceURI + _T(" ") + m_sSchemaLocation).c_str());
- }
-
- DeclareNamespaces(rNode);
- }
-
-
- void CDoc::InternalSetSchemaLocation(CNode& rNode)
- {
- MSXML2::IXMLDOMElementPtr spRootElement = rNode.m_spNode;
-
- if (m_sNamespaceURI.empty())
- {
- if (!m_sSchemaLocation.empty() && spRootElement->getAttributeNode(L"xsi:noNamespaceSchemaLocation") == NULL)
- spRootElement->setAttribute(L"xsi:noNamespaceSchemaLocation", m_sSchemaLocation.c_str());
- }
- else
- {
- if (!m_sSchemaLocation.empty() && spRootElement->getAttributeNode(L"xsi:schemaLocation") == NULL)
- spRootElement->setAttribute(L"xsi:schemaLocation", (m_sNamespaceURI + _T(" ") + m_sSchemaLocation).c_str());
- }
- }
-
-
- void CDoc::DeclareNamespace(CNode& rNode, const tstring& sPrefix, const tstring& sURI)
- {
- rNode.DeclareNamespace(sPrefix, sURI);
- }
-
-
- } // namespace altova
-