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 "Node.h"
- #include "Doc.h"
-
-
- namespace altova {
-
-
- xercesc::DOMImplementation* CDoc::sm_ImplementationCore = NULL;
- xercesc::DOMImplementationLS* CDoc::sm_ImplementationLS = NULL;
- xercesc::DOMBuilder* CDoc::sm_Builder = NULL;
- xercesc::DOMWriter* CDoc::sm_Writer = NULL;
- xercesc::DOMDocument* CDoc::sm_Document = NULL;
- xercesc::DOMDocumentFragment* CDoc::sm_Fragment = NULL;
- int CDoc::sm_nTmpNameCounter = 0;
- int CDoc::sm_nFragmentCounter = 0;
- int CDoc::sm_nBuilderCounter = 0;
- int CDoc::sm_nWriterCounter = 0;
-
-
- class MyErrorHandler : public xercesc::ErrorHandler
- {
- public:
- virtual void warning(const xercesc::SAXParseException& e)
- {
- }
-
- virtual void error(const xercesc::SAXParseException& e)
- {
- throw CXmlException(CXmlException::eError1, XC2TS(e.getMessage()));
- }
-
- virtual void fatalError(const xercesc::SAXParseException& e)
- {
- throw CXmlException(CXmlException::eError2, XC2TS(e.getMessage()));
- }
-
- virtual void resetErrors()
- {
- }
- };
-
-
- xercesc::DOMImplementation* CDoc::GetImplementationCore()
- {
- if (sm_ImplementationCore == NULL)
- {
- sm_ImplementationCore =
- xercesc::DOMImplementationRegistry::getDOMImplementation(X("Core"));
-
- if (sm_ImplementationCore == NULL)
- throw exception("Could not create xercesc::DOMImplementation");
- }
-
- return sm_ImplementationCore;
- }
-
-
- xercesc::DOMImplementationLS* CDoc::GetImplementationLS()
- {
- if (sm_ImplementationLS == NULL)
- {
- sm_ImplementationLS = static_cast<xercesc::DOMImplementationLS*>(
- xercesc::DOMImplementationRegistry::getDOMImplementation(X("LS"))
- );
-
- if (sm_ImplementationLS == NULL)
- throw exception("Could not create xercesc::DOMImplementationLS");
- }
-
- return sm_ImplementationLS;
- }
-
-
- xercesc::DOMBuilder* CDoc::GetBuilder()
- {
- if (sm_Builder == NULL)
- {
- sm_Builder = GetImplementationLS()->createDOMBuilder(
- xercesc::DOMImplementationLS::MODE_SYNCHRONOUS,
- 0
- );
-
- if (sm_Builder == NULL)
- throw exception("Could not create xercesc::DOMBuilder");
-
- sm_Builder->setFeature(xercesc::XMLUni::fgDOMNamespaces, true);
- }
-
- return sm_Builder;
- }
-
-
- xercesc::DOMWriter* CDoc::GetWriter()
- {
- if (sm_Writer == NULL)
- {
- sm_Writer = GetImplementationLS()->createDOMWriter();
-
- if (sm_Writer == NULL)
- throw exception("Could not create xercesc::DOMWriter");
- }
-
- return sm_Writer;
- }
-
-
- xercesc::DOMDocument* CDoc::GetDocument()
- {
- if (sm_Document == NULL)
- {
- sm_Document = GetImplementationCore()->createDocument();
- }
- return sm_Document;
- }
-
-
- xercesc::DOMNode* CDoc::CreateFragment()
- {
- char szNumber\[32\] = "tmp";
- itoa(sm_nTmpNameCounter++, szNumber + 3, 10);
- sm_nFragmentCounter++;
-
- if (sm_Fragment == NULL)
- {
- sm_Fragment = GetDocument()->createDocumentFragment();
- if (sm_Fragment == NULL)
- throw exception("Could not create fragment");
- }
- xercesc::DOMNode* pNode = GetDocument()->createElement(X(szNumber));
- sm_Fragment->appendChild(pNode);
- return pNode;
- }
-
-
- void CDoc::ReleaseFragment(xercesc::DOMNode* pDOMNode)
- {
- if (pDOMNode != NULL && pDOMNode->getParentNode() != NULL && pDOMNode->getParentNode()->getNodeType() == xercesc::DOMNode::DOCUMENT_FRAGMENT_NODE)
- {
- sm_nFragmentCounter--;
- }
- }
-
-
- CDoc::CDoc() : m_eSchemaType(None), m_bBuilder(false), m_bWriter(false)
- {
- }
-
-
- CDoc::~CDoc()
- {
- if (sm_nFragmentCounter == 0)
- {
- sm_Fragment = NULL;
- delete sm_Document;
- sm_Document = NULL;
- }
- if (m_bBuilder)
- {
- if (--sm_nBuilderCounter <= 0)
- {
- delete sm_Builder;
- sm_Builder = NULL;
- }
- }
- if (m_bWriter)
- {
- if (--sm_nWriterCounter <= 0)
- {
- delete sm_Writer;
- sm_Writer = NULL;
- }
- }
- }
-
-
- xercesc::DOMDocument* CDoc::Load(const tstring& sURI)
- {
- xercesc::DOMDocument* pDoc = GetBuilder()->parseURI(X(sURI));
- m_bBuilder = true;
- if (pDoc == NULL)
- throw exception("Could not load URI");
- return pDoc;
- }
-
-
- void CDoc::Save(const tstring& sURI, CNode& rNode, bool bQualified)
- {
- if (rNode.m_pDOMNode->getParentNode()->getNodeType() == xercesc::DOMNode::DOCUMENT_FRAGMENT_NODE)
- {
- FinalizeRootElement(rNode);
- }
-
- CNode::InternalAdjustPrefix(rNode.m_pDOMNode, bQualified);
- rNode.AdjustPrefix();
-
- xercesc::DOMWriter* pWriter = GetWriter();
- m_bWriter = true;
- xercesc::LocalFileFormatTarget myFormatTarget(sURI.c_str());
-
- if (pWriter->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
- pWriter->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
-
- pWriter->writeNode(&myFormatTarget, *(rNode.m_pDOMNode->getOwnerDocument()));
- }
-
-
- void CDoc::SetRootElementName(const tstring& sRootElementNamespaceURI, const tstring& sRootElementName)
- {
- m_sRootElementNamespaceURI = sRootElementNamespaceURI;
- m_sRootElementName = sRootElementName;
- }
-
-
- void CDoc::SetSchemaLocation(const tstring& sSchemaLocation)
- {
- m_eSchemaType = Schema;
- m_sSchemaLocation = sSchemaLocation;
- }
-
-
- void CDoc::SetDTDLocation(const tstring& sDTDLocation)
- {
- m_eSchemaType = DTD;
- m_sSchemaLocation = sDTDLocation;
- }
-
-
- void CDoc::RemoveSchemaLocation()
- {
- m_eSchemaType = None;
- m_sSchemaLocation = _T("");
- }
-
-
- void CDoc::FinalizeRootElement(CNode& rNode)
- {
- if (m_sRootElementName.empty())
- {
- throw std::exception("Call SetRootElementName first");
- }
-
- xercesc::DOMDocument* pDoc = GetImplementationCore()->createDocument();
- xercesc::DOMElement* pRootElement = pDoc->createElementNS(X(m_sRootElementNamespaceURI), X(m_sRootElementName));
- pDoc->appendChild(pRootElement);
-
- pRootElement->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance"));
- pRootElement->setAttribute(X("tmp"), X(""));
-
- if (m_sRootElementNamespaceURI.empty())
- {
- if (!m_sSchemaLocation.empty())
- pRootElement->setAttribute(X("xsi:noNamespaceSchemaLocation"), X(m_sSchemaLocation));
- }
- else
- {
- if (!m_sSchemaLocation.empty())
- pRootElement->setAttribute(X("xsi:schemaLocation"), X(m_sRootElementNamespaceURI + _T(" ") + m_sSchemaLocation));
- }
-
- while (xercesc::DOMNode* pNode = rNode.m_pDOMNode->getFirstChild())
- {
- pRootElement->appendChild(pDoc->importNode(pNode, true));
- rNode.m_pDOMNode->removeChild(pNode);
- }
-
- xercesc::DOMNamedNodeMap* pSrcAttributes = rNode.m_pDOMNode->getAttributes();
- xercesc::DOMNamedNodeMap* pDstAttributes = pRootElement->getAttributes();
- while (pSrcAttributes->getLength() > 0)
- {
- xercesc::DOMNode* pNode = pSrcAttributes->item(0);
- pDstAttributes->setNamedItemNS(pDoc->importNode(pNode, true));
- pSrcAttributes->removeNamedItemNS(pNode->getNamespaceURI(), pNode->getLocalName());
- }
-
- DeclareNamespaces(pRootElement);
-
- ReleaseFragment(rNode.m_pDOMNode);
- rNode.m_pDOMNode = pRootElement;
- }
-
-
- void CDoc::DeclareNamespace(xercesc::DOMElement* pElement, const tstring& sPrefix, const tstring& sURI)
- {
- CNode::DeclareNamespace(pElement, sPrefix, sURI);
- }
-
-
- } // namespace altova
-