9 #include "cantera/base/config.h"
43 m_msg(
"Error in XML file") {
45 m_msg +=
" at line " +
int2str(line+1);
81 m_msg +=
"<" + opentag +
"> paired with </" + closetag +
">.\n";
107 std::string child,
int line=0) :
109 m_msg +=
" The XML Node \"" + parent +
110 "\", does not contain a required\n" +
111 " XML child node named \""
114 ostringstream ss(ostringstream::out);
116 m_msg += ss.str() +
"\n";
143 m_msg +=
"Illegal units (" + units +
144 ") specified for node " + name +
".\n";
145 setError(
"XML_IllegalUnits", m_msg);
157 XML_Reader::XML_Reader(std::istream& input) :
182 int len =
static_cast<int>(aline.size());
184 for (i = len-1; i >= 0; i--) {
190 for (j = 0; j < i; j++) {
197 return aline.substr(j, i - j + 1);
207 int len =
static_cast<int>(aline.size());
209 for (i = len-1; i >= 0; i--)
210 if (aline[i] ==
'"') {
213 for (j = 0; j < i; j++)
214 if (aline[j] ==
'"') {
220 return aline.substr(j+1, i - j - 1);
232 std::string::size_type istart = 0)
234 string::size_type iloc, icurrent, len;
238 iloc = s.find(q, icurrent);
242 char cm1 = s[iloc-1];
244 if (iloc >= (len -1)) {
263 const char q1 =
'\'';
267 string::size_type iloc1, iloc2, ilocStart = 0;
275 if (iloc1 < ilocStart) {
293 rstring = s.substr(ilocStart + 1, iloc1 - 1);
297 return static_cast<int>(iloc1)+1;
305 std::map<std::string, std::string>& attribs)
const
307 string::size_type iloc;
312 name = s.substr(0, iloc);
313 s =
stripws(s.substr(iloc+1,s.size()));
314 if (s[s.size()-1] ==
'/') {
324 attr =
stripws(s.substr(0,iloc));
328 s =
stripws(s.substr(iloc+1,s.size()));
332 if (iloc < s.size()) {
333 s =
stripws(s.substr(iloc,s.size()));
346 string name, tag =
"";
347 bool incomment =
false;
350 if (
m_s.eof() || (
getchr(ch), ch ==
'<')) {
354 char ch1 =
' ', ch2 =
' ';
364 if (ch1 ==
'-' && ch2 ==
'!') {
368 }
else if (ch ==
'>') {
370 if (ch1 ==
'-' && ch2 ==
'-') {
404 }
else if (ch !=
' ') {
411 if (front && lastch ==
' ' && ch ==
' ') {
480 m_iscomment(right.m_iscomment),
481 m_linenum(right.m_linenum)
495 if (&right !=
this) {
497 for (
int i = 0; i < n; i++) {
516 "attempt to delete locked XML_Node "+
name());
518 for (
int i = 0; i < n; i++) {
531 for (
int i = 0; i < n; i++) {
558 addChild(
"comment", comment);
612 XML_Node& XML_Node::addChild(
const std::string& sname)
617 m_childindex.insert(pair<const std::string, XML_Node*>(sname, xxx));
625 return addChild(std::string(cstring));
638 XML_Node& XML_Node::addChild(
const std::string& name,
const std::string& value)
660 XML_Node& XML_Node::addChild(
const std::string& name,
const doublereal value,
661 const std::string fmt)
677 vector<XML_Node*>::iterator i;
689 return std::string(
"");
701 if (
m_name ==
"comment") {
755 return std::atoi(
m_value.c_str());
803 const doublereal value,
const std::string fmt)
843 std::map<std::string,std::string>::const_iterator i =
m_attribs.find(attr);
950 if (discardComments) {
984 string msg=
"XML_Node "+
name()+
" is required to have an attribute named " + a +
985 " with the value \"" + v +
"\", but instead the value is \"" +
attrib(a);
1012 const std::string& idTarget)
const
1016 std::string idattrib =
id();
1017 if (
name() == nameTarget) {
1018 if (idTarget ==
"" || idTarget == idattrib) {
1019 return const_cast<XML_Node*
>(
this);
1024 if (sc->
name() == nameTarget) {
1025 if (idTarget ==
"") {
1028 idattrib = sc->
id();
1029 if (idTarget == idattrib) {
1036 scResult = sc->
findNameID(nameTarget, idTarget);
1065 const std::string& idTarget,
const int index_i)
const
1069 std::string idattrib =
id();
1070 std::string ii =
attrib(
"index");
1071 std::string index_s =
int2str(index_i);
1072 int iMax = -1000000;
1073 if (
name() == nameTarget) {
1074 if (idTarget ==
"" || idTarget == idattrib) {
1075 if (index_s == ii) {
1076 return const_cast<XML_Node*
>(
this);
1082 if (sc->
name() == nameTarget) {
1083 ii = sc->
attrib(
"index");
1084 int indexR = atoi(ii.c_str());
1085 idattrib = sc->
id();
1086 if (idTarget == idattrib || idTarget ==
"") {
1087 if (index_s == ii) {
1091 if (indexR > iMax) {
1124 if (
attrib(
"id") ==
id) {
1125 return const_cast<XML_Node*
>(
this);
1130 for (
size_t i = 0; i <
nChildren(); i++) {
1156 const std::string& val,
int depth)
const
1159 if (
attrib(attr) == val) {
1160 return const_cast<XML_Node*
>(
this);
1166 for (
size_t i = 0; i < n; i++) {
1167 r =
m_children[i]->findByAttr(attr, val, depth - 1);
1194 for (
size_t i = 0; i <
nChildren(); i++) {
1218 return const_cast<XML_Node*
>(
this);
1222 for (
size_t i = 0; i <
nChildren(); i++) {
1238 s <<
"<?xml version=\"1.0\"?>" << endl;
1252 string nm, nm2, val;
1266 if (nm[nm.size() - 1] ==
'/') {
1267 nm2 = nm.substr(0,nm.size()-1);
1268 node = &node->addChild(nm2);
1273 }
else if (nm[0] !=
'/') {
1274 if (nm[0] !=
'!' && nm[0] !=
'-' && nm[0] !=
'?') {
1275 node = &node->addChild(nm);
1280 }
else if (nm.substr(0,2) ==
"--") {
1281 if (nm.substr(nm.size()-2,2) ==
"--") {
1286 if (node->
name() != nm.substr(1,nm.size()-1)) {
1311 map<string,string>::const_iterator b =
m_attribs.begin();
1317 const vector<XML_Node*> &vsc = node_dest->
children();
1323 for (
size_t idc = 0; idc < ndc; idc++) {
1351 (void) node_dest->addChild(sc->
name());
1376 map<string,string>::const_iterator b =
m_attribs.begin();
1380 const vector<XML_Node*> &vsc = node_dest->
children();
1386 (void) node_dest->addChild(sc->
name());
1419 std::vector<XML_Node*>& children)
const
1421 for (
size_t i = 0; i <
nChildren(); i++) {
1423 children.push_back(&
child(i));
1435 string::size_type iloc;
1438 std::multimap<std::string,XML_Node*>::const_iterator i;
1441 iloc = loc.find(
'/');
1443 cname = loc.substr(0,iloc);
1444 loc = loc.substr(iloc+1, loc.size());
1447 return i->second->child(loc);
1454 return *(i->second);
1474 string indent(level,
' ');
1481 s << endl << indent <<
"<!--";
1486 int ll =
static_cast<int>(m_value.size()) - 1;
1487 if (! isspace(m_value[ll])) {
1494 s << indent <<
"<" <<
m_name;
1495 map<string,string>::const_iterator b =
m_attribs.begin();
1497 s <<
" " << b->first <<
"=\"" << b->second <<
"\"";
1506 string::size_type ieol = vv.find(
'\n');
1509 ieol = vv.find(
'\n');
1512 s << endl << indent <<
" ";
1514 size_t jf = ieol - 1;
1515 for (
int j = 0; j < (int) ieol; j++) {
1516 if (! isspace(vv[j])) {
1521 s << endl << indent <<
" " << vv.substr(jf,ieol-jf);
1523 vv = vv.substr(ieol+1);
1525 int lll =
static_cast<int>(vv.size()) - 1;
1528 for (
int j = 0; j < lll; j++) {
1529 if (! isspace(vv[j])) {
1535 s << endl << indent <<
" " << vv.substr(jf);
1541 s << endl << indent;
1543 bool doSpace =
true;
1544 bool doNewLine =
false;
1545 int ll =
static_cast<int>(
m_value.size()) - 1;
1549 if (m_name ==
"floatArray") {
1557 s << endl << indent <<
" ";
1562 if (doSpace && (! isspace(
m_value[0]))) {
1570 if (doSpace && (! isspace(m_value[ll]))) {
1574 s << endl << indent;
1578 if (numRecursivesAllowed > 0) {
1581 m_children[i]->write_int(s,level + 2, numRecursivesAllowed - 1);
1585 s << endl << indent;
1587 s <<
"</" << m_name <<
">";
1605 m_children[i]->write_int(s,level, numRecursivesAllowed-1);
1609 write_int(s, level, numRecursivesAllowed);
1628 const std::string& idtarget)
1636 string rname = root->
name();
1637 if (rname ==
"phase") {
1638 if (idtarget ==
"") {
1641 idattrib = root->
id();
1642 if (idtarget == idattrib) {
1649 const vector<XML_Node*> &vsc = root->
children();
1650 for (
size_t n = 0; n < root->
nChildren(); n++) {
1652 if (sc->
name() ==
"phase") {
1653 if (idtarget ==
"") {
1656 idattrib = sc->
id();
1657 if (idtarget == idattrib) {
1662 for (
size_t n = 0; n < root->
nChildren(); n++) {
1664 if (sc->
name() !=
"phase") {