2010-05-14 01:05:28 +04:00
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef OBJECT_XML_H_
# define OBJECT_XML_H_
# include <string>
# include <vector>
# include <libxml/tree.h>
# include <libxml/parser.h>
# include <libxml/xpath.h>
# include <libxml/xpathInternals.h>
using namespace std ;
/**
* This class represents a generic Object supported by a xml document .
* The class provides basic methods to query attributes , and get xml nodes
*/
class ObjectXML
{
public :
// ---------------------- Constructors ------------------------------------
ObjectXML ( ) : xml ( 0 ) , ctx ( 0 ) { } ;
/**
* Constructs an object using a XML document
*/
ObjectXML ( const string & xml_doc ) ;
/**
* Constructs an object using a XML Node . The node is copied to the new
* object
*/
ObjectXML ( const xmlNodePtr node ) ;
virtual ~ ObjectXML ( ) ;
/**
* Access Object elements using Xpath
* @ param xpath_expr the Xpath of the element
* @ return a vector with the elements
*/
vector < string > operator [ ] ( const char * xpath_expr ) ;
2011-02-25 15:54:39 +03:00
/**
* Gets and sets a xpath attribute , if the attribute is not found a default
* is used
* @ param value to set
* @ param xpath_expr of the xml element
* @ param def default value if the element is not found
*/
void xpath ( string & value , const char * xpath_expr , const char * def ) ;
/**
* Gets and sets a xpath attribute , if the attribute is not found a default
* is used
* @ param value to set
* @ param xpath_expr of the xml element
* @ param def default value if the element is not found
*/
void xpath ( int & value , const char * xpath_expr , const int & def ) ;
2011-02-25 17:23:46 +03:00
/**
* Gets and sets a xpath attribute , if the attribute is not found a default
* is used
* @ param value to set
* @ param xpath_expr of the xml element
* @ param def default value if the element is not found
*/
void xpath ( time_t & value , const char * xpath_expr , const time_t & def ) ;
2011-02-25 16:17:41 +03:00
/**
* Gets the value of an element from an xml string
* @ param value the value of the element
* @ param xml the xml string
* @ param xpath the xpath of the target element
*
* @ return - 1 if the element was not found
*/
static int xpath_value ( string & value , const char * xml , const char * xpath ) ;
2010-05-14 01:05:28 +04:00
/**
* Get xml nodes by Xpath
* @ param xpath_expr the Xpath for the elements
* @ param content nodes for the given Xpath expression . The nodes are
* returned as pointers to the object nodes .
* @ return the number of nodes found
*/
int get_nodes ( const char * xpath_expr , vector < xmlNodePtr > & content ) ;
/**
* Updates the object representation with a new XML document . Previous
* XML resources are freed
* @ param xml_doc the new xml document
*/
2011-02-24 20:12:26 +03:00
int update_from_str ( const string & xml_doc ) ;
/**
* Updates the object representation with a new XML document . Previous
* XML resources are freed
* @ param xml_doc the new xml document
*/
int update_from_node ( const xmlNodePtr node ) ;
2010-05-14 01:05:28 +04:00
2010-05-14 05:32:42 +04:00
// ---------------------------------------------------------
// Lex & bison parser for requirements and rank expressions
// ---------------------------------------------------------
/**
* Evaluates a requirement expression on the given host .
* @ param requirements string
* @ param result true if the host matches the requirements
* @ param errmsg string describing the error , must be freed by the
* calling function
* @ return 0 on success
*/
int eval_bool ( const string & expr , bool & result , char * * errmsg ) ;
/**
* Evaluates a rank expression on the given host .
* @ param rank string
* @ param result of the rank evaluation
* @ param errmsg string describing the error , must be freed by the
* calling function
* @ return 0 on success
*/
int eval_arith ( const string & expr , int & result , char * * errmsg ) ;
2010-05-14 21:15:20 +04:00
/**
* Function to write the Object in an output stream
*/
friend ostream & operator < < ( ostream & os , ObjectXML & oxml )
{
xmlChar * mem ;
int size ;
xmlDocDumpMemory ( oxml . xml , & mem , & size ) ;
string str ( reinterpret_cast < char * > ( mem ) ) ;
os < < str ;
xmlFree ( mem ) ;
return os ;
} ;
2010-05-14 01:05:28 +04:00
private :
/**
* XML representation of the Object
*/
xmlDocPtr xml ;
/**
* XPath Context to access Object elements
*/
xmlXPathContextPtr ctx ;
/**
* Parse a XML documents and initializes XPath contexts
*/
void xml_parse ( const string & xml_doc ) ;
} ;
# endif /*OBJECT_XML_H_*/