Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parameterlist/test/XML/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Teuchos_ConfigDefs.hpp"
43 #include "Teuchos_XMLObject.hpp"
47 #include "Teuchos_Version.hpp"
48 #include <fstream>
49 
50 using std::string;
51 using Teuchos::XMLObject;
54 
55 /* Test of Teuchos XML handling classes */
56 
57 int main(int argc, char** argv)
58 {
59  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
60 
61  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
62 
63  try
64  {
65  /* create an XML object */
66  XMLObject problem("Problem");
67  XMLObject solver("Solver");
68  XMLObject prec("Preconditioner");
69 
70  solver.addAttribute("type", "gmres");
71  solver.addInt("maxiters", 1000);
72  solver.addInt("restarts", 100);
73  solver.addDouble("tol", 1.0e-10);
74 
75  solver.addChild(prec);
76 
77  prec.addAttribute("type", "ILUk");
78  prec.addInt("k", 2);
79 
80  problem.addChild(solver);
81 
82  int foundIndex = problem.findFirstChild("Solver");
83  if(foundIndex == -1)
84  {
85  std::cerr << "Find child didn't find the child!"
86  <<std::endl << std::endl;
87  return -1;
88  }
89 
90  const XMLObject foundChild = problem.getChild(foundIndex);
91  if(foundChild.getTag() != solver.getTag())
92  {
93  std::cerr << "Find child found the wrong tag!" << std::endl <<
94  "Found index was: " << foundIndex << std::endl <<
95  std::endl << std::endl;
96  return -1;
97  }
98 
99  if(problem.findFirstChild("NON EXSISTENT CHILD") != -1){
100  std::cerr << "First first child didn't return -1 when it was "
101  "suppose to!" <<std::endl << std::endl;
102  return -1;
103  }
104 
105  std::string str = problem.toString();
106  std::cerr << str << std::endl;
107 
108  /* parse XML in a std::string */
109  StringInputSource src(str);
110  XMLObject reread = src.getObject();
111 
112  std::cerr << reread << std::endl;
113 
114  /* write to a file, and then read and parse the file */
115  std::ofstream of("tmp.xml");
116  of << reread << std::endl;
117 
118  FileInputSource fileSrc("tmp.xml");
119  XMLObject fileXML = fileSrc.getObject();
120 
121  std::cerr << fileXML << std::endl;
122 
123  return 0;
124  }
125  catch(std::exception& e)
126  {
127  std::cerr << e.what() << std::endl;
128  }
129 }
const std::string & getTag() const
Return the tag of the current node.
void addChild(const XMLObject &child)
Add a child node to the node.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Initialize, finalize, and query the global MPI session.
std::string toString() const
Represent this node and its children as a std::string.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
XMLObject getObject() const
Get an object by invoking the TreeBuildingXMLHandler on the input data.
Instantiation of XMLInputSource class for reading XML from a file.
void addAttribute(const std::string &name, T value)
Lookup whether or not Doubles are allowed.
std::string Teuchos_Version()
int main(int argc, char *argv[])
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Definition of XMLInputSource derived class for reading XML from a std::string.
void addInt(const std::string &name, int val)
Add an int as an attribute.
Definition of XMLInputSource derived class for reading XML from a file.
int findFirstChild(std::string tagName) const
Returns the index of the first child found with the given tag name. Returns -1 if no child is found...
const XMLObject & getChild(int i) const
Return the i-th child node.
Instantiation of XMLInputSource class for reading XML from a std::string.
void addDouble(const std::string &name, double val)
Add a double as an attribute.
An object representation of a subset of XML data.