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