Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLParser_ordered.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 
45 #include "Teuchos_XMLParser.hpp"
47 
48 using std::string;
49 using std::endl;
50 
51 namespace Teuchos {
52 
53 
54 /* Simple test of Teuchos XMLParser class */
55 TEUCHOS_UNIT_TEST( XMLParser, orderedWriteRead )
56 {
57  out << endl;
58 
59  /* create a ParameterList object */
60  string xmlstring1;
61  ParameterList wrotepl1("Parent");
62  {
63  ParameterList c1("Child1");
64  ParameterList c2("Child2");
65  c1.set("cp1", "first1");
66  c1.set("cp2", "second1");
67  c2.set("cp3", "first2");
68  c2.set("cp4", "second2");
69  wrotepl1.set("FirstSublist",c1);
70  wrotepl1.set("SecondSublist",c2);
71  /* create an XML object from the ParameterList and write it to a string */
73  XMLObject xmlprob = xml2pl.toXML(wrotepl1);
74  std::ostringstream ss;
75  ss << xmlprob;
76  xmlstring1 = ss.str();
77  out << "*** String 1" << endl;
78  out << xmlstring1 << endl;
79  }
80 
81  string xmlstring2;
82  ParameterList wrotepl2("Parent");
83  {
84  ParameterList c1("Child1");
85  ParameterList c2("Child2");
86  // swap the ordering
87  c1.set("cp2", "second1");
88  c1.set("cp1", "first1");
89  c2.set("cp4", "second2");
90  c2.set("cp3", "first2");
91  wrotepl2.set("SecondSublist",c2);
92  wrotepl2.set("FirstSublist",c1);
93  /* create an XML object from the ParameterList and write it to a string */
95  XMLObject xmlprob = xml2pl.toXML(wrotepl2);
96  std::ostringstream ss;
97  ss << xmlprob;
98  xmlstring2 = ss.str();
99  out << "*** String 2" << endl;
100  out << xmlstring2 << endl;
101  }
102 
103  // the different PL orderings should be reflected in the ParameterLists and their XML string representations
104  TEST_INEQUALITY(wrotepl1, wrotepl2);
105  TEST_INEQUALITY(xmlstring1, xmlstring2);
106 
107  /* create a input source, parser to read the string */
108  ParameterList readpl1, readpl2;
109  {
110  StringInputSource src(xmlstring1);
111  XMLParser parser(src.stream());
112  XMLObject xmlprob = parser.parse();
113  XMLParameterListReader pl2xml;
114  readpl1 = pl2xml.toParameterList(xmlprob);
115  }
116  {
117  StringInputSource src(xmlstring2);
118  XMLParser parser(src.stream());
119  XMLObject xmlprob = parser.parse();
120  XMLParameterListReader pl2xml;
121  readpl2 = pl2xml.toParameterList(xmlprob);
122  }
123 
124  /* check that the parameter lists do not match */
125  TEST_INEQUALITY(readpl1, readpl2);
126 
127 }
128 
129 
130 TEUCHOS_UNIT_TEST( XMLParser, simpleOrderedRead )
131 {
132 
133  /* create a ParameterList object */
134  string xmlstring1;
135  ParameterList plGold("ParentList");
136  {
137  ParameterList c1("Z");
138  ParameterList c2("A");
139  c1.set("A", "first1");
140  c1.set("Z", "second1");
141  c2.set("Z", "first2");
142  c2.set("A", "second2");
143  plGold.set("9FirstSublist",c1);
144  plGold.set("1SecondSublist",c2);
145  }
146 
147  string xmlsrc(
148  "<ParameterList name=\"ParentList\">\n"
149  " <ParameterList name=\"9FirstSublist\">\n"
150  " <Parameter name=\"A\" type=\"string\" value=\"first1\"/>\n"
151  " <Parameter name=\"Z\" type=\"string\" value=\"second1\"/>\n"
152  " </ParameterList>\n"
153  " <ParameterList name=\"1SecondSublist\">\n"
154  " <Parameter name=\"Z\" type=\"string\" value=\"first2\"/>\n"
155  " <Parameter name=\"A\" type=\"string\" value=\"second2\"/>\n"
156  " </ParameterList>\n"
157  "</ParameterList>\n");
158  ParameterList plTest;
159  {
160  StringInputSource src(xmlsrc);
161  XMLParser parser(src.stream());
162  XMLObject xmlprob = parser.parse();
163  XMLParameterListReader pl2xml;
164  plTest = pl2xml.toParameterList(xmlprob);
165  }
166 
167  TEST_EQUALITY(plTest, plGold);
168 }
169 
170 
171 } // namespace Teuchos
Writes an XML object to a parameter list.
XMLObject toXML(const ParameterList &p, RCP< const DependencySheet > depSheet=null) const
#define TEST_INEQUALITY(v1, v2)
Assert the inequality of v1 and v2.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Set a parameter whose value has type T.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
XMLParser consumes characters from an XMLInputStream object, parsing the XML and using a TreeBuilding...
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
Writes a ParameterList to an XML object.
Unit testing support.
virtual RCP< XMLInputStream > stream() const
Create a StringInputStream.
RCP< ParameterList > toParameterList(const XMLObject &xml, RCP< DependencySheet > depSheet) const
A list of parameters of arbitrary type.
Definition of XMLInputSource derived class for reading XML from a std::string.
Writes an XML object to a parameter list.
Instantiation of XMLInputSource class for reading XML from a std::string.
A class providing a simple XML parser. Methods can be overloaded to exploit external XML parsing libr...
Writes a ParameterList to an XML object.