MSXML_DOMDocument.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #include <atlbase.h>
00023 #include <atlconv.h>
00024 #else
00025 #include "config.h"
00026 #endif
00027 #include "CallStack.h"
00028
00029 #if (HAVE_LIBXML == 0 && _MSC_VER > 0)
00030 #include "MSXML_DOMDocument.h"
00031 #include <sstream>
00032
00033 namespace FIX
00034 {
00035 MSXML_DOMAttributes::~MSXML_DOMAttributes()
00036 { QF_STACK_IGNORE_BEGIN
00037 if(m_pNodeMap) m_pNodeMap->Release();
00038 QF_STACK_IGNORE_END
00039 }
00040
00041 bool MSXML_DOMAttributes::get( const std::string& name, std::string& value )
00042 { QF_STACK_PUSH(MSXML_DOMAttributes::get)
00043
00044 if(!m_pNodeMap) return false;
00045 MSXML2::IXMLDOMNode* pNode = NULL;
00046 m_pNodeMap->getNamedItem(_bstr_t(name.c_str()), &pNode);
00047 if( pNode == NULL ) return false;
00048
00049 CComBSTR result;
00050 pNode->get_text(&result);
00051 value = (char*)_bstr_t(result);
00052 pNode->Release();
00053 return true;
00054
00055 QF_STACK_POP
00056 }
00057
00058 DOMAttributes::map MSXML_DOMAttributes::toMap()
00059 {
00060 return DOMAttributes::map();
00061 }
00062
00063 MSXML_DOMNode::~MSXML_DOMNode()
00064 { QF_STACK_IGNORE_BEGIN
00065 m_pNode->Release();
00066 QF_STACK_IGNORE_END
00067 }
00068
00069 DOMNodePtr MSXML_DOMNode::getFirstChildNode()
00070 { QF_STACK_PUSH(MSXML_DOMNode::getFirstChildNode)
00071
00072 MSXML2::IXMLDOMNode* pNode = NULL;
00073 m_pNode->get_firstChild(&pNode);
00074 if( pNode == NULL ) return DOMNodePtr();
00075 return DOMNodePtr(new MSXML_DOMNode(pNode));
00076
00077 QF_STACK_POP
00078 }
00079
00080 DOMNodePtr MSXML_DOMNode::getNextSiblingNode()
00081 { QF_STACK_PUSH(MSXML_DOMNode::getNextSiblingNode)
00082
00083 MSXML2::IXMLDOMNode* pNode = NULL;
00084 m_pNode->get_nextSibling(&pNode);
00085 if( pNode == NULL ) return DOMNodePtr();
00086 return DOMNodePtr(new MSXML_DOMNode(pNode));
00087
00088 QF_STACK_POP
00089 }
00090
00091 DOMAttributesPtr MSXML_DOMNode::getAttributes()
00092 { QF_STACK_PUSH(MSXML_DOMNode::getAttributes)
00093 return DOMAttributesPtr(new MSXML_DOMAttributes(m_pNode));
00094 QF_STACK_POP
00095 }
00096
00097 std::string MSXML_DOMNode::getName()
00098 { QF_STACK_PUSH(MSXML_DOMNode::getName)
00099
00100 CComBSTR result;
00101 m_pNode->get_nodeName(&result);
00102 return (char*)_bstr_t(result);
00103
00104 QF_STACK_POP
00105 }
00106
00107 std::string MSXML_DOMNode::getText()
00108 { QF_STACK_PUSH(MSXML_DOMNode::getText)
00109
00110 CComBSTR result;
00111 m_pNode->get_text(&result);
00112 return (char*)_bstr_t(result);
00113
00114 QF_STACK_POP
00115 }
00116
00117 MSXML_DOMDocument::MSXML_DOMDocument() throw( ConfigError )
00118 : m_pDoc(NULL)
00119 {
00120 if(FAILED(CoInitializeEx( 0, COINIT_MULTITHREADED )))
00121 if(FAILED(CoInitializeEx( 0, COINIT_APARTMENTTHREADED )))
00122 throw ConfigError("Could not initialize COM");
00123
00124 HRESULT hr = CoCreateInstance(
00125 MSXML2::CLSID_DOMDocument, NULL, CLSCTX_ALL, __uuidof( MSXML2::IXMLDOMDocument2 ),
00126 ( void ** ) & m_pDoc );
00127
00128 if ( hr != S_OK )
00129 throw( ConfigError( "MSXML DOM Document could not be created" ) );
00130 }
00131
00132 MSXML_DOMDocument::~MSXML_DOMDocument()
00133 { QF_STACK_IGNORE_BEGIN
00134
00135 if(m_pDoc != NULL)
00136 m_pDoc->Release();
00137 CoUninitialize();
00138
00139 QF_STACK_IGNORE_END
00140 }
00141
00142 bool MSXML_DOMDocument::load( std::istream& stream )
00143 { QF_STACK_PUSH(MSXML_DOMDocument::load)
00144
00145 try
00146 {
00147 m_pDoc->put_async(VARIANT_FALSE);
00148 m_pDoc->put_resolveExternals(VARIANT_FALSE);
00149 m_pDoc->setProperty(_bstr_t("SelectionLanguage"), _variant_t("XPath"));
00150
00151 std::stringstream sstream;
00152 sstream << stream.rdbuf();
00153
00154 VARIANT_BOOL success = FALSE;
00155 m_pDoc->loadXML(_bstr_t(sstream.str().c_str()), &success);
00156 return success != FALSE;
00157 }
00158 catch( ... ) { return false; }
00159
00160 QF_STACK_POP
00161 }
00162
00163 bool MSXML_DOMDocument::load( const std::string& url )
00164 { QF_STACK_PUSH(MSXML_DOMDocument::load)
00165
00166 try
00167 {
00168 m_pDoc->put_async(VARIANT_FALSE);
00169 m_pDoc->put_resolveExternals(VARIANT_FALSE);
00170 m_pDoc->setProperty(_bstr_t("SelectionLanguage"), _variant_t("XPath"));
00171
00172 VARIANT_BOOL success = FALSE;
00173 m_pDoc->load(_variant_t(url.c_str()), &success);
00174 return success != FALSE;
00175 }
00176 catch( ... ) { return false; }
00177
00178 QF_STACK_POP
00179 }
00180
00181 bool MSXML_DOMDocument::xml( std::ostream& out )
00182 { QF_STACK_PUSH(MSXML_DOMDocument::xml)
00183
00184 try
00185 {
00186 CComBSTR result;
00187 HRESULT hr = m_pDoc->get_xml(&result);
00188 if( hr != S_OK ) return false;
00189 out << (char*)_bstr_t(result);
00190 return true;
00191 }
00192 catch( ... ) { return false; }
00193
00194 QF_STACK_POP
00195 }
00196
00197 DOMNodePtr MSXML_DOMDocument::getNode( const std::string& XPath )
00198 { QF_STACK_PUSH(MSXML_DOMDocument::getNode)
00199
00200 HRESULT hr;
00201
00202 MSXML2::IXMLDOMNode* pNode = NULL;
00203 hr = m_pDoc->selectSingleNode(_bstr_t(XPath.c_str()), &pNode);
00204 if( pNode == NULL ) return DOMNodePtr();
00205 return DOMNodePtr(new MSXML_DOMNode(pNode));
00206
00207 QF_STACK_POP
00208 }
00209 }
00210
00211 #endif