MSXML_DOMDocument.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #if (HAVE_LIBXML == 0 && _MSC_VER > 0)
00027 #include "MSXML_DOMDocument.h"
00028 #include <sstream>
00029 
00030 namespace FIX
00031 {
00032   MSXML_DOMAttributes::~MSXML_DOMAttributes()
00033   {
00034     if(m_pNodeMap) m_pNodeMap->Release();
00035   }
00036 
00037   bool MSXML_DOMAttributes::get( const std::string& name, std::string& value )
00038   {
00039     if(!m_pNodeMap) return false;
00040     MSXML2::IXMLDOMNode* pNode = NULL;
00041     m_pNodeMap->getNamedItem(_bstr_t(name.c_str()), &pNode);
00042     if( pNode == NULL ) return false;
00043 
00044     BSTR result;
00045     pNode->get_text(&result);
00046     value = (char*)_bstr_t(result);
00047     ::SysFreeString(result);
00048     pNode->Release();
00049     return true;
00050   }
00051 
00052   DOMAttributes::map MSXML_DOMAttributes::toMap()
00053   {
00054     return DOMAttributes::map();
00055   }
00056 
00057   MSXML_DOMNode::~MSXML_DOMNode()
00058   {
00059     m_pNode->Release();
00060   }
00061 
00062   DOMNodePtr MSXML_DOMNode::getFirstChildNode()
00063   {
00064     MSXML2::IXMLDOMNode* pNode = NULL;
00065     m_pNode->get_firstChild(&pNode);
00066     if( pNode == NULL ) return DOMNodePtr();
00067     return DOMNodePtr(new MSXML_DOMNode(pNode));
00068   }
00069 
00070   DOMNodePtr MSXML_DOMNode::getNextSiblingNode()
00071   {
00072     MSXML2::IXMLDOMNode* pNode = NULL;
00073     m_pNode->get_nextSibling(&pNode);
00074     if( pNode == NULL ) return DOMNodePtr();
00075     return DOMNodePtr(new MSXML_DOMNode(pNode));
00076   }
00077 
00078   DOMAttributesPtr MSXML_DOMNode::getAttributes()
00079   {
00080     return DOMAttributesPtr(new MSXML_DOMAttributes(m_pNode));
00081   }
00082 
00083   std::string MSXML_DOMNode::getName()
00084   {
00085     BSTR result;
00086     m_pNode->get_nodeName(&result);
00087     std::string name = (char*)_bstr_t(result);
00088     ::SysFreeString(result);
00089     return name;
00090   }
00091 
00092   std::string MSXML_DOMNode::getText()
00093   {
00094     BSTR result;
00095     m_pNode->get_text(&result);
00096     std::string text = (char*)_bstr_t(result);
00097     ::SysFreeString(result);
00098     return text;
00099   }
00100 
00101   MSXML_DOMDocument::MSXML_DOMDocument() throw( ConfigError )
00102   : m_pDoc(NULL)
00103   {
00104     if(FAILED(CoInitializeEx( 0, COINIT_MULTITHREADED )))
00105       if(FAILED(CoInitializeEx( 0, COINIT_APARTMENTTHREADED )))
00106         throw ConfigError("Could not initialize COM");
00107 
00108     HRESULT hr = CoCreateInstance(
00109       MSXML2::CLSID_DOMDocument, NULL, CLSCTX_ALL, __uuidof( MSXML2::IXMLDOMDocument2 ),
00110       ( void ** ) & m_pDoc );
00111 
00112     if ( hr != S_OK )
00113       throw( ConfigError( "MSXML DOM Document could not be created" ) );
00114   }
00115 
00116   MSXML_DOMDocument::~MSXML_DOMDocument()
00117   {
00118     if(m_pDoc != NULL)
00119       m_pDoc->Release();
00120     CoUninitialize();
00121   }
00122 
00123   bool MSXML_DOMDocument::load( std::istream& stream )
00124   {
00125     try
00126     {
00127       m_pDoc->put_async(VARIANT_FALSE);
00128       m_pDoc->put_resolveExternals(VARIANT_FALSE);
00129       m_pDoc->setProperty(_bstr_t("SelectionLanguage"), _variant_t("XPath"));
00130 
00131       std::stringstream sstream;
00132       sstream << stream.rdbuf();
00133 
00134       VARIANT_BOOL success = FALSE;
00135       m_pDoc->loadXML(_bstr_t(sstream.str().c_str()), &success);
00136       return success != FALSE;
00137     }
00138     catch( ... ) { return false; }
00139   }
00140 
00141   bool MSXML_DOMDocument::load( const std::string& url )
00142   {
00143     try
00144     {
00145       m_pDoc->put_async(VARIANT_FALSE);
00146       m_pDoc->put_resolveExternals(VARIANT_FALSE);
00147       m_pDoc->setProperty(_bstr_t("SelectionLanguage"), _variant_t("XPath"));
00148 
00149       VARIANT_BOOL success = FALSE;
00150       m_pDoc->load(_variant_t(url.c_str()), &success);
00151       return success != FALSE;
00152     }
00153     catch( ... ) { return false; }
00154   }
00155 
00156   bool MSXML_DOMDocument::xml( std::ostream& out )
00157   {
00158     try
00159     {
00160       BSTR result;
00161       HRESULT hr = m_pDoc->get_xml(&result);
00162       if( hr != S_OK ) return false;
00163       out << (char*)_bstr_t(result);
00164       ::SysFreeString(result);
00165       return true;
00166     }
00167     catch( ... ) { return false; }
00168   }
00169 
00170   DOMNodePtr MSXML_DOMDocument::getNode( const std::string& XPath )
00171   {
00172     HRESULT hr;
00173 
00174     MSXML2::IXMLDOMNode* pNode = NULL;
00175     hr = m_pDoc->selectSingleNode(_bstr_t(XPath.c_str()), &pNode);
00176     if( pNode == NULL ) return DOMNodePtr();
00177     return DOMNodePtr(new MSXML_DOMNode(pNode));
00178   }
00179 }
00180 
00181 #endif

Generated on Mon Jun 23 2014 23:49:38 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001