Index Source Files Annotated Class List Alphabetical Class List Class Hierarchy Graphical Class Hierarchy

# LIBXML_DOMDocument.cpp

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 ** Copyright (c) quickfixengine.org  All rights reserved.
    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 #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 "LIBXML_DOMDocument.h"
    00031 #include <libxml/xpath.h>
    00032 #include <sstream>
    00033
    00034 namespace FIX
    00035 {
    00036   bool LIBXML_DOMAttributes::get( const std::string& name, std::string& value )
    00037   { QF_STACK_PUSH(LIBXML_DOMAttributes::get)
    00038
    00039     xmlChar* result = xmlGetProp(m_pNode, (const xmlChar*)name.c_str());
    00040     if(result == NULL) return false;
    00041     value = (char*)result;
    00042     xmlFree( result );
    00043     return true;
    00044
    00045     QF_STACK_POP
    00046   }
    00047
    00048   DOMAttributes::map LIBXML_DOMAttributes::toMap()
    00049   { QF_STACK_PUSH(LIBXML_DOMAttributes::toMap)
    00050
    00051     xmlAttr* attr = m_pNode->properties;
    00052     DOMAttributes::map map;
    00053     while( attr != 0 )
    00054     {
    00055       std::string value;
    00056       std::string name;
    00057       if( attr->name ) name = (char*)attr->name;
    00058       get(name, value);
    00059       map[name] = value;
    00060       attr = attr->next;
    00061     }
    00062     return map;
    00063
    00064     QF_STACK_POP
    00065   }
    00066
    00067   DOMNodePtr LIBXML_DOMNode::getFirstChildNode()
    00068   { QF_STACK_PUSH(LIBXML_DOMNode::getFirstChildNode)
    00069
    00070     if( !m_pNode->children ) return DOMNodePtr();
    00071     xmlNodePtr pNode = m_pNode->children;
    00072     if( pNode == NULL ) return DOMNodePtr();
    00073     return DOMNodePtr(new LIBXML_DOMNode(pNode));
    00074
    00075     QF_STACK_POP
    00076   }
    00077
    00078   DOMNodePtr LIBXML_DOMNode::getNextSiblingNode()
    00079   { QF_STACK_PUSH(LIBXML_DOMAttributes::getNextSiblingNode)
    00080
    00081     if( !m_pNode->next ) return DOMNodePtr();
    00082     xmlNodePtr pNode = m_pNode->next;
    00083     if( pNode == NULL ) return DOMNodePtr();
    00084     return DOMNodePtr(new LIBXML_DOMNode(pNode));
    00085
    00086     QF_STACK_POP
    00087   }
    00088
    00089   DOMAttributesPtr LIBXML_DOMNode::getAttributes()
    00090   { QF_STACK_PUSH(LIBXML_DOMAttributes::getAttributes)
    00091     return DOMAttributesPtr(new LIBXML_DOMAttributes(m_pNode));
    00092     QF_STACK_POP
    00093   }
    00094
    00095   std::string LIBXML_DOMNode::getName()
    00096   { QF_STACK_PUSH(LIBXML_DOMAttributes::getName)
    00097     return m_pNode->name ? (char*)m_pNode->name : "";
    00098     QF_STACK_POP
    00099   }
    00100
    00101   std::string LIBXML_DOMNode::getText()
    00102   { QF_STACK_PUSH(LIBXML_DOMAttributes::getText)
    00103     return m_pNode->content ? (char*)m_pNode->content : "";
    00104     QF_STACK_POP
    00105   }
    00106
    00107   LIBXML_DOMDocument::LIBXML_DOMDocument() throw( ConfigError )
    00108   : m_pDoc(NULL)
    00109   {
    00110   }
    00111
    00112   LIBXML_DOMDocument::~LIBXML_DOMDocument()
    00113   {
    00114     xmlFreeDoc(m_pDoc);
    00115   }
    00116
    00117   bool LIBXML_DOMDocument::load( std::istream& stream )
    00118   { QF_STACK_PUSH(LIBXML_DOMAttributes::load)
    00119
    00120     try
    00121     {
    00122       std::stringstream sstream;
    00123       sstream << stream.rdbuf();
    00124       m_pDoc = xmlParseDoc((xmlChar*)sstream.str().c_str());
    00125       return m_pDoc != NULL;
    00126     }
    00127     catch( ... ) { return false; }
    00128
    00129     QF_STACK_POP
    00130   }
    00131
    00132   bool LIBXML_DOMDocument::load( const std::string& url )
    00133   { QF_STACK_PUSH(LIBXML_DOMAttributes::lead)
    00134
    00135     try
    00136     {
    00137       m_pDoc = xmlParseFile(url.c_str());
    00138       return m_pDoc != NULL;
    00139     }
    00140     catch( ... ) { return false; }
    00141
    00142     QF_STACK_POP
    00143   }
    00144
    00145   bool LIBXML_DOMDocument::xml( std::ostream& out )
    00146   { QF_STACK_PUSH(LIBXML_DOMAttributes::xml)
    00147     return false;
    00148     QF_STACK_POP
    00149   }
    00150
    00151   DOMNodePtr LIBXML_DOMDocument::getNode( const std::string& XPath )
    00152   { QF_STACK_PUSH(LIBXML_DOMAttributes::getNode)
    00153
    00154     xmlXPathContextPtr context = xmlXPathNewContext(m_pDoc);
    00155     xmlXPathObjectPtr xpathObject = xmlXPathEval((xmlChar*)XPath.c_str(), context);
    00156
    00157     if( xpathObject == NULL
    00158         || xpathObject->nodesetval == NULL
    00159         || xpathObject->nodesetval->nodeNr != 1 )
    00160     {
    00161       xmlXPathFreeContext(context);
    00162       return DOMNodePtr();
    00163     }
    00164
    00165     DOMNodePtr result(new LIBXML_DOMNode(xpathObject->nodesetval->nodeTab[0]));
    00166     xmlXPathFreeContext(context);
    00167     xmlXPathFreeObject(xpathObject);
    00168     return result;
    00169
    00170     QF_STACK_POP
    00171   }
    00172 }
    00173
    00174 #endif