Commit 0c7dc959 authored by peastman's avatar peastman
Browse files

XmlSerializer generates XML directly rather than going through TinyXml

parent fa11f993
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2010 Stanford University and the Authors. * * Portions copyright (c) 2010-2014 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -79,7 +79,7 @@ public: ...@@ -79,7 +79,7 @@ public:
private: private:
static void serialize(const SerializationNode& node, std::ostream& stream); static void serialize(const SerializationNode& node, std::ostream& stream);
static void* deserializeStream(std::istream& stream); static void* deserializeStream(std::istream& stream);
static TiXmlElement* encodeNode(const SerializationNode& node); static void encodeNode(const SerializationNode& node, std::ostream& stream, int depth);
static void decodeNode(SerializationNode& node, const TiXmlElement& element); static void decodeNode(SerializationNode& node, const TiXmlElement& element);
}; };
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Biological Structures at Stanford, funded under the NIH Roadmap for * * Biological Structures at Stanford, funded under the NIH Roadmap for *
* Medical Research, grant U54 GM072970. See https://simtk.org. * * Medical Research, grant U54 GM072970. See https://simtk.org. *
* * * *
* Portions copyright (c) 2010 Stanford University and the Authors. * * Portions copyright (c) 2010-2014 Stanford University and the Authors. *
* Authors: Peter Eastman * * Authors: Peter Eastman *
* Contributors: * * Contributors: *
* * * *
...@@ -36,25 +36,32 @@ using namespace OpenMM; ...@@ -36,25 +36,32 @@ using namespace OpenMM;
using namespace std; using namespace std;
void XmlSerializer::serialize(const SerializationNode& node, std::ostream& stream) { void XmlSerializer::serialize(const SerializationNode& node, std::ostream& stream) {
TiXmlDocument doc; stream << "<?xml version=\"1.0\" ?>\n";
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); encodeNode(node, stream, 0);
doc.LinkEndChild(decl);
doc.LinkEndChild(encodeNode(node));
TiXmlPrinter printer;
printer.SetIndent("\t");
doc.Accept(&printer);
stream << printer.Str();
} }
TiXmlElement* XmlSerializer::encodeNode(const SerializationNode& node) { void XmlSerializer::encodeNode(const SerializationNode& node, std::ostream& stream, int depth) {
TiXmlElement* element = new TiXmlElement(node.getName()); for (int i = 0; i < depth; i++)
stream << '\t';
stream << '<' << node.getName();
const map<string, string>& properties = node.getProperties(); const map<string, string>& properties = node.getProperties();
for (map<string, string>::const_iterator iter = properties.begin(); iter != properties.end(); ++iter) for (map<string, string>::const_iterator iter = properties.begin(); iter != properties.end(); ++iter) {
element->SetAttribute(iter->first.c_str(), iter->second.c_str()); string name, value;
TiXmlBase::EncodeString(iter->first, &name);
TiXmlBase::EncodeString(iter->second, &value);
stream << ' ' << name << "=\"" << value << '\"';
}
const vector<SerializationNode>& children = node.getChildren(); const vector<SerializationNode>& children = node.getChildren();
if (children.size() == 0)
stream << "/>\n";
else {
stream << ">\n";
for (int i = 0; i < (int) children.size(); i++) for (int i = 0; i < (int) children.size(); i++)
element->LinkEndChild(encodeNode(children[i])); encodeNode(children[i], stream, depth+1);
return element; for (int i = 0; i < depth; i++)
stream << '\t';
stream << "</" << node.getName() << ">\n";
}
} }
void* XmlSerializer::deserializeStream(std::istream& stream) { void* XmlSerializer::deserializeStream(std::istream& stream) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment