diff --git a/applications/present3D/SpellChecker.cpp b/applications/present3D/SpellChecker.cpp index 97694af8a..ae2da32ad 100644 --- a/applications/present3D/SpellChecker.cpp +++ b/applications/present3D/SpellChecker.cpp @@ -12,6 +12,7 @@ #include #include +#include #include @@ -89,3 +90,167 @@ SpellChecker::WordList SpellChecker::suggest(const std::string& word) const { return WordList(); } + +////////////////////////////////////////////////////////////////////////////////////////// +XmlPatcher::XmlPatcher() +{ +} + +void XmlPatcher::stripP3dXml(const std::string& filename, std::ostream& fout) const +{ + std::string foundFileName = osgDB::findDataFile( filename ); + if (foundFileName.empty()) return; + + std::ifstream fin(foundFileName.c_str()); + + osgDB::XmlNode::Input input; + input.attach(fin); + input.readAllDataIntoBuffer(); + + osg::ref_ptr doc = new osgDB::XmlNode; + doc->read(input); + + if (!doc) return; + + stripXml(doc.get(), fout); +} + +void XmlPatcher::stripXml(osgDB::XmlNode* node, std::ostream& fout) const +{ + if (node->name=="presentation" || + node->name=="slide" || + node->name=="layer" || + node->name=="page" || + node->name=="paragraph" || + node->name=="bullet") + { + if (!(node->children.empty())) + { + fout<<"<"<name<<">"<children.begin(); + itr != node->children.end(); + ++itr) + { + stripXml(itr->get(), fout); + } + fout<<"name<<">"<name<<">"<contents<<"name<<">"<children.begin(); + itr != node->children.end(); + ++itr) + { + stripXml(itr->get(), fout); + } + } +} + +osgDB::XmlNode* XmlPatcher::simplifyP3dXml(const std::string& filename) const +{ + std::string foundFileName = osgDB::findDataFile( filename ); + if (foundFileName.empty()) return 0; + + std::ifstream fin(foundFileName.c_str()); + + osgDB::XmlNode::Input input; + input.attach(fin); + input.readAllDataIntoBuffer(); + + osg::ref_ptr doc = new osgDB::XmlNode; + doc->read(input); + + if (!doc) return 0; + + return simplifyXml(doc.get()); +} + +osgDB::XmlNode* XmlPatcher::simplifyXml(osgDB::XmlNode* node) const +{ + if (node->name.empty() || + node->name=="presentation" || + node->name=="slide" || + node->name=="layer" || + node->name=="page" || + node->name=="paragraph" || + node->name=="bullet") + { + osgDB::XmlNode* newNode = new osgDB::XmlNode; + newNode->type = node->type; + newNode->name = node->name; + newNode->contents = node->contents; + for(osgDB::XmlNode::Children::iterator itr = node->children.begin(); + itr != node->children.end(); + ++itr) + { + osgDB::XmlNode* child = simplifyXml(itr->get()); + if (child) newNode->children.push_back(child); + } + return newNode; + } + else + { + return 0; + } +} +osgDB::XmlNode* XmlPatcher::mergeP3dXml(const std::string& lhs_filename, const std::string& rhs_filename) const +{ + std::string lhs_foundFileName = osgDB::findDataFile( lhs_filename ); + if (lhs_foundFileName.empty()) return 0; + + std::string rhs_foundFileName = osgDB::findDataFile( rhs_filename ); + if (rhs_foundFileName.empty()) return 0; + + osg::ref_ptr lhs_doc = new osgDB::XmlNode; + osg::ref_ptr rhs_doc = new osgDB::XmlNode; + + { + std::ifstream fin(lhs_foundFileName.c_str()); + + osgDB::XmlNode::Input input; + input.attach(fin); + input.readAllDataIntoBuffer(); + + lhs_doc->read(input); + if (!lhs_doc) return 0; + } + + { + std::ifstream fin(rhs_foundFileName.c_str()); + + osgDB::XmlNode::Input input; + input.attach(fin); + input.readAllDataIntoBuffer(); + + rhs_doc->read(input); + if (!rhs_doc) return 0; + } + + lhs_doc = mergeXml(lhs_doc.get(), rhs_doc.get()); + return lhs_doc.release(); +} + +osgDB::XmlNode* XmlPatcher::mergeXml(osgDB::XmlNode* lhs_node, osgDB::XmlNode* rhs_node) const +{ + if (lhs_node->name == rhs_node->name) + { + lhs_node->contents = rhs_node->contents; + osgDB::XmlNode::Children::iterator rhs_itr = rhs_node->children.begin(); + for(osgDB::XmlNode::Children::iterator lhs_itr = lhs_node->children.begin(); + lhs_itr != lhs_node->children.end() && rhs_itr != rhs_node->children.end(); + ++lhs_itr) + { + if ((*lhs_itr)->name == (*rhs_itr)->name) + { + mergeXml(lhs_itr->get(), rhs_itr->get()); + ++rhs_itr; + } + } + } + return lhs_node; +} diff --git a/applications/present3D/SpellChecker.h b/applications/present3D/SpellChecker.h index 370d5c0b8..12781208f 100644 --- a/applications/present3D/SpellChecker.h +++ b/applications/present3D/SpellChecker.h @@ -34,6 +34,23 @@ class SpellChecker WordList suggest(const std::string& word) const; }; + +class XmlPatcher +{ + public: + XmlPatcher(); + + void stripP3dXml(const std::string& filename, std::ostream& fout) const; + void stripXml(osgDB::XmlNode* xmlNode, std::ostream& fout) const; + + osgDB::XmlNode* simplifyP3dXml(const std::string& filename) const; + osgDB::XmlNode* simplifyXml(osgDB::XmlNode* xmlNode) const; + + osgDB::XmlNode* mergeP3dXml(const std::string& lhs_filename, const std::string& rhs_filename) const; + osgDB::XmlNode* mergeXml(osgDB::XmlNode* lhs_node, osgDB::XmlNode* rhs_node) const; + +}; + } #endif diff --git a/applications/present3D/present3D.cpp b/applications/present3D/present3D.cpp index fa051d439..9ce6fc6c6 100644 --- a/applications/present3D/present3D.cpp +++ b/applications/present3D/present3D.cpp @@ -436,6 +436,31 @@ int main( int argc, char **argv ) return 1; } + if (arguments.read("--strip-text",filename)) + { + p3d::XmlPatcher patcher; + // patcher.stripP3dXml(filename, osg::notify(osg::NOTICE)); + + osg::ref_ptr newNode = patcher.simplifyP3dXml(filename); + if (newNode.valid()) + { + newNode->write(std::cout); + } + return 1; + } + + std::string lhs_filename, rhs_filename; + if (arguments.read("--merge",lhs_filename, rhs_filename)) + { + p3d::XmlPatcher patcher; + osg::ref_ptr newNode = patcher.mergeP3dXml(lhs_filename, rhs_filename); + if (newNode.valid()) + { + newNode->write(std::cout); + } + return 1; + } + // construct the viewer. osgViewer::Viewer viewer(arguments);