QMCPACK
Libxml2Doc.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Raymond Clay III, j.k.rofling@gmail.com, Lawrence Livermore National Laboratory
10 // Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef LIBXML2_DOCUMENT_H
17 #define LIBXML2_DOCUMENT_H
18 
19 #include <libxml/xpath.h>
20 #include <string>
21 #include <string_view>
22 #include "libxmldefs.h"
23 
24 /** class to handle xmlXPathObject
25  */
27 {
28  //default constructor
30 
31  /** constructor
32  * @param expression xpath expression
33  * @param context xmlXPathContext with which expression is evaluated
34  */
35  OhmmsXPathObject(const char* expression, xmlXPathContextPtr context);
36 
37  /** constructor
38  * @param expression xpath expression
39  * @param cur xmlNodePtr
40  *
41  * Create m_context
42  */
43  OhmmsXPathObject(const char* expression, xmlNodePtr cur);
44 
46 
47  /** evaluate the expression and create the object
48  * @param expression xpath expression
49  * @param context xmlXPathContext with which expression is evaluated
50  */
51  void put(const char* expression, xmlXPathContextPtr context);
52 
53  inline bool empty() { return NumObjects == 0; }
54 
55  inline int size() { return NumObjects; }
56 
57  inline xmlNodePtr operator[](int i)
58  {
59  if (result != NULL && i < NumObjects)
60  {
61  return result->nodesetval->nodeTab[i];
62  }
63  else
64  {
65  return NULL;
66  }
67  }
68 
70  xmlXPathObjectPtr result;
71  xmlXPathContextPtr m_context;
72 };
73 
74 /** class that handles xmlDoc
75  */
77 {
79  Libxml2Document(const std::string& fname);
81 
82  void newDoc(const std::string& rootName);
83 
84  bool parse(const std::string& fname);
85  bool parseFromString(const std::string_view data);
86 
87  inline xmlDocPtr getDocument() { return m_doc; }
88  inline xmlNodePtr getRoot() { return m_root; }
89  xmlXPathContextPtr getXPathContext();
90 
91  void dump(const std::string& newxml);
92  void addChild(xmlNodePtr newnode);
93 
94  void addChild(const std::string& expression, xmlNodePtr newnode);
95 
96 
97  xmlNodePtr addChild(xmlNodePtr parent, const std::string& nodeName);
98 
99  xmlNodePtr addChild(xmlNodePtr parent, const std::string& nodeName, const bool& value)
100  {
101  std::string s = value ? "true" : "false";
102  xmlNodePtr node = xmlNewChild(parent, NULL, BAD_CAST nodeName.c_str(), BAD_CAST s.c_str());
103  return node;
104  }
105 
106  template<typename T>
107  xmlNodePtr addChild(xmlNodePtr parent, const std::string& nodeName, const T& value)
108  {
109  std::stringstream s;
110  s << value;
111  xmlNodePtr node = xmlNewChild(parent, NULL, BAD_CAST nodeName.c_str(), BAD_CAST s.str().c_str());
112  return node;
113  }
114 
115  xmlDocPtr m_doc;
116  xmlNodePtr m_root;
117  xmlXPathContextPtr m_context;
118  std::string InFileRoot;
119 };
120 
121 #endif
class that handles xmlDoc
Definition: Libxml2Doc.h:76
xmlDocPtr m_doc
Definition: Libxml2Doc.h:115
if(!okay) throw std xmlNodePtr node
xmlXPathContextPtr getXPathContext()
Definition: Libxml2Doc.cpp:100
void newDoc(const std::string &rootName)
Definition: Libxml2Doc.cpp:93
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
A collection of put/get functions to read from or write to a xmlNode defined in libxml2.
class to handle xmlXPathObject
Definition: Libxml2Doc.h:26
void put(const char *expression, xmlXPathContextPtr context)
evaluate the expression and create the object
Definition: Libxml2Doc.cpp:64
xmlXPathContextPtr m_context
Definition: Libxml2Doc.h:71
xmlNodePtr addChild(xmlNodePtr parent, const std::string &nodeName, const bool &value)
Definition: Libxml2Doc.h:99
xmlNodePtr m_root
Definition: Libxml2Doc.h:116
xmlXPathContextPtr m_context
Definition: Libxml2Doc.h:117
xmlNodePtr operator[](int i)
Definition: Libxml2Doc.h:57
xmlDocPtr getDocument()
Definition: Libxml2Doc.h:87
std::string InFileRoot
Definition: Libxml2Doc.h:118
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
void addChild(xmlNodePtr newnode)
Definition: Libxml2Doc.cpp:111
bool parse(const std::string &fname)
Definition: Libxml2Doc.cpp:180
xmlNodePtr addChild(xmlNodePtr parent, const std::string &nodeName, const T &value)
Definition: Libxml2Doc.h:107
void dump(const std::string &newxml)
Definition: Libxml2Doc.cpp:109
xmlXPathObjectPtr result
Definition: Libxml2Doc.h:70