home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 October / INTERNET108.ISO / pc / software / windows / building / xmlspy / xmlspyentcomplete5.exe / Data1.cab / _2406CD546501484AB49449B04769E060 < prev    next >
Encoding:
Text File  |  2003-05-21  |  7.3 KB  |  307 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 "Node.h"
  20. #include "Doc.h"
  21.  
  22.  
  23. namespace altova {
  24.  
  25.  
  26. xercesc::DOMImplementation*        CDoc::sm_ImplementationCore    = NULL;
  27. xercesc::DOMImplementationLS*    CDoc::sm_ImplementationLS    = NULL;
  28. xercesc::DOMBuilder*            CDoc::sm_Builder            = NULL;
  29. xercesc::DOMWriter*                CDoc::sm_Writer                = NULL;
  30. xercesc::DOMDocument*            CDoc::sm_Document            = NULL;
  31. xercesc::DOMDocumentFragment*    CDoc::sm_Fragment            = NULL;
  32. int                                CDoc::sm_nTmpNameCounter    = 0;
  33. int                                CDoc::sm_nFragmentCounter    = 0;
  34. int                                CDoc::sm_nBuilderCounter    = 0;
  35. int                                CDoc::sm_nWriterCounter        = 0;
  36.  
  37.  
  38. class MyErrorHandler : public xercesc::ErrorHandler
  39. {
  40. public:
  41.     virtual void warning(const xercesc::SAXParseException& e)
  42.     {
  43.     }
  44.  
  45.     virtual void error(const xercesc::SAXParseException& e)
  46.     {
  47.         throw CXmlException(CXmlException::eError1, XC2TS(e.getMessage()));
  48.     }
  49.  
  50.     virtual void fatalError(const xercesc::SAXParseException& e)
  51.     {
  52.         throw CXmlException(CXmlException::eError2, XC2TS(e.getMessage()));
  53.     }
  54.  
  55.     virtual void resetErrors()
  56.     {
  57.     }
  58. };
  59.  
  60.  
  61. xercesc::DOMImplementation* CDoc::GetImplementationCore()
  62. {
  63.     if (sm_ImplementationCore == NULL)
  64.     {
  65.         sm_ImplementationCore =
  66.                 xercesc::DOMImplementationRegistry::getDOMImplementation(X("Core"));
  67.  
  68.         if (sm_ImplementationCore == NULL)
  69.             throw exception("Could not create xercesc::DOMImplementation");
  70.     }
  71.  
  72.     return sm_ImplementationCore;
  73. }
  74.  
  75.  
  76. xercesc::DOMImplementationLS* CDoc::GetImplementationLS()
  77. {
  78.     if (sm_ImplementationLS == NULL)
  79.     {
  80.         sm_ImplementationLS = static_cast<xercesc::DOMImplementationLS*>(
  81.                 xercesc::DOMImplementationRegistry::getDOMImplementation(X("LS"))
  82.                 );
  83.  
  84.         if (sm_ImplementationLS == NULL)
  85.             throw exception("Could not create xercesc::DOMImplementationLS");
  86.     }
  87.  
  88.     return sm_ImplementationLS;
  89. }
  90.  
  91.  
  92. xercesc::DOMBuilder* CDoc::GetBuilder()
  93. {
  94.     if (sm_Builder == NULL)
  95.     {
  96.         sm_Builder = GetImplementationLS()->createDOMBuilder(
  97.                 xercesc::DOMImplementationLS::MODE_SYNCHRONOUS,
  98.                 0
  99.                 );
  100.  
  101.         if (sm_Builder == NULL)
  102.             throw exception("Could not create xercesc::DOMBuilder");
  103.  
  104.         sm_Builder->setFeature(xercesc::XMLUni::fgDOMNamespaces, true);
  105.     }
  106.  
  107.     return sm_Builder;
  108. }
  109.  
  110.  
  111. xercesc::DOMWriter* CDoc::GetWriter()
  112. {
  113.     if (sm_Writer == NULL)
  114.     {
  115.         sm_Writer = GetImplementationLS()->createDOMWriter();
  116.  
  117.         if (sm_Writer == NULL)
  118.             throw exception("Could not create xercesc::DOMWriter");
  119.     }
  120.  
  121.     return sm_Writer;
  122. }
  123.  
  124.  
  125. xercesc::DOMDocument* CDoc::GetDocument()
  126. {
  127.     if (sm_Document == NULL)
  128.     {
  129.         sm_Document = GetImplementationCore()->createDocument();
  130.     }
  131.     return sm_Document;
  132. }
  133.  
  134.  
  135. xercesc::DOMNode* CDoc::CreateFragment()
  136. {
  137.     char szNumber\[32\] = "tmp";
  138.     itoa(sm_nTmpNameCounter++, szNumber + 3, 10);
  139.     sm_nFragmentCounter++;
  140.  
  141.     if (sm_Fragment == NULL)
  142.     {
  143.         sm_Fragment = GetDocument()->createDocumentFragment();
  144.         if (sm_Fragment == NULL)
  145.             throw exception("Could not create fragment");
  146.     }
  147.     xercesc::DOMNode* pNode = GetDocument()->createElement(X(szNumber));
  148.     sm_Fragment->appendChild(pNode);
  149.     return pNode;
  150. }
  151.  
  152.  
  153. void CDoc::ReleaseFragment(xercesc::DOMNode* pDOMNode)
  154. {
  155.     if (pDOMNode != NULL && pDOMNode->getParentNode() != NULL && pDOMNode->getParentNode()->getNodeType() == xercesc::DOMNode::DOCUMENT_FRAGMENT_NODE)
  156.     {
  157.         sm_nFragmentCounter--;
  158.     }
  159. }
  160.  
  161.  
  162. CDoc::CDoc() : m_eSchemaType(None), m_bBuilder(false), m_bWriter(false)
  163. {
  164. }
  165.  
  166.  
  167. CDoc::~CDoc()
  168. {
  169.     if (sm_nFragmentCounter == 0)
  170.     {
  171.         sm_Fragment = NULL;
  172.         delete sm_Document;
  173.         sm_Document = NULL;
  174.     }
  175.     if (m_bBuilder)
  176.     {
  177.         if (--sm_nBuilderCounter <= 0)
  178.         {
  179.             delete sm_Builder;
  180.             sm_Builder = NULL;
  181.         }
  182.     }
  183.     if (m_bWriter)
  184.     {
  185.         if (--sm_nWriterCounter <= 0)
  186.         {
  187.             delete sm_Writer;
  188.             sm_Writer = NULL;
  189.         }
  190.     }
  191. }
  192.  
  193.  
  194. xercesc::DOMDocument* CDoc::Load(const tstring& sURI)
  195. {
  196.     xercesc::DOMDocument* pDoc = GetBuilder()->parseURI(X(sURI));
  197.     m_bBuilder = true;
  198.     if (pDoc == NULL)
  199.         throw exception("Could not load URI");
  200.     return pDoc;
  201. }
  202.  
  203.  
  204. void CDoc::Save(const tstring& sURI, CNode& rNode, bool bQualified)
  205. {
  206.     if (rNode.m_pDOMNode->getParentNode()->getNodeType() == xercesc::DOMNode::DOCUMENT_FRAGMENT_NODE)
  207.     {
  208.         FinalizeRootElement(rNode);
  209.     }
  210.  
  211.     CNode::InternalAdjustPrefix(rNode.m_pDOMNode, bQualified);
  212.     rNode.AdjustPrefix();
  213.  
  214.     xercesc::DOMWriter* pWriter = GetWriter();
  215.     m_bWriter = true;
  216.     xercesc::LocalFileFormatTarget myFormatTarget(sURI.c_str());
  217.  
  218.     if (pWriter->canSetFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true))
  219.         pWriter->setFeature(xercesc::XMLUni::fgDOMWRTFormatPrettyPrint, true);
  220.  
  221.     pWriter->writeNode(&myFormatTarget, *(rNode.m_pDOMNode->getOwnerDocument()));
  222. }
  223.  
  224.  
  225. void CDoc::SetRootElementName(const tstring& sRootElementNamespaceURI, const tstring& sRootElementName)
  226. {
  227.     m_sRootElementNamespaceURI = sRootElementNamespaceURI;
  228.     m_sRootElementName = sRootElementName;
  229. }
  230.  
  231.  
  232. void CDoc::SetSchemaLocation(const tstring& sSchemaLocation)
  233. {
  234.     m_eSchemaType = Schema;
  235.     m_sSchemaLocation = sSchemaLocation;
  236. }
  237.  
  238.  
  239. void CDoc::SetDTDLocation(const tstring& sDTDLocation)
  240. {
  241.     m_eSchemaType = DTD;
  242.     m_sSchemaLocation = sDTDLocation;
  243. }
  244.  
  245.  
  246. void CDoc::RemoveSchemaLocation()
  247. {
  248.     m_eSchemaType = None;
  249.     m_sSchemaLocation = _T("");
  250. }
  251.  
  252.  
  253. void CDoc::FinalizeRootElement(CNode& rNode)
  254. {
  255.     if (m_sRootElementName.empty())
  256.     {
  257.         throw std::exception("Call SetRootElementName first");
  258.     }
  259.  
  260.     xercesc::DOMDocument* pDoc = GetImplementationCore()->createDocument();
  261.     xercesc::DOMElement* pRootElement = pDoc->createElementNS(X(m_sRootElementNamespaceURI), X(m_sRootElementName));
  262.     pDoc->appendChild(pRootElement);
  263.  
  264.     pRootElement->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance"));
  265.     pRootElement->setAttribute(X("tmp"), X(""));
  266.  
  267.     if (m_sRootElementNamespaceURI.empty())
  268.     {
  269.         if (!m_sSchemaLocation.empty())
  270.             pRootElement->setAttribute(X("xsi:noNamespaceSchemaLocation"), X(m_sSchemaLocation));
  271.     }
  272.     else
  273.     {
  274.         if (!m_sSchemaLocation.empty())
  275.             pRootElement->setAttribute(X("xsi:schemaLocation"), X(m_sRootElementNamespaceURI + _T(" ") + m_sSchemaLocation));
  276.     }
  277.  
  278.     while (xercesc::DOMNode* pNode = rNode.m_pDOMNode->getFirstChild())
  279.     {
  280.         pRootElement->appendChild(pDoc->importNode(pNode, true));
  281.         rNode.m_pDOMNode->removeChild(pNode);
  282.     }
  283.  
  284.     xercesc::DOMNamedNodeMap* pSrcAttributes = rNode.m_pDOMNode->getAttributes();
  285.     xercesc::DOMNamedNodeMap* pDstAttributes = pRootElement->getAttributes();
  286.     while (pSrcAttributes->getLength() > 0)
  287.     {
  288.         xercesc::DOMNode* pNode = pSrcAttributes->item(0);
  289.         pDstAttributes->setNamedItemNS(pDoc->importNode(pNode, true));
  290.         pSrcAttributes->removeNamedItemNS(pNode->getNamespaceURI(), pNode->getLocalName());
  291.     }
  292.  
  293.     DeclareNamespaces(pRootElement);
  294.  
  295.     ReleaseFragment(rNode.m_pDOMNode);
  296.     rNode.m_pDOMNode = pRootElement;
  297. }
  298.  
  299.  
  300. void CDoc::DeclareNamespace(xercesc::DOMElement* pElement, const tstring& sPrefix, const tstring& sURI)
  301. {
  302.     CNode::DeclareNamespace(pElement, sPrefix, sURI);
  303. }
  304.  
  305.  
  306. } // namespace altova
  307.