Re: [osg-users] osgDB::XmlParser question
Hi JS, Your reading of the code is correct... the code will fail every time, something I didn't catch as I haven't directly used this convenience function in the p3d plugin or elsewhere. Re-ordering the code so the input.readAllDataIntoBuffer() is done before the if (!input) looks it would be appropriate. I've made this change and checked it in, could you test it? Robert. On Wed, Jan 6, 2010 at 9:38 PM, Jean-Sébastien Guay jean-sebastien.g...@cm-labs.com wrote: Hi Robert, I need to read an XML file and thought of using osgDB::XmlParser to do it. Calling this: osgDB::readXmlFile(filename); results in Could not open XML file: somefilename.xml. But the file is there and some simple code using an ifstream to read the file (using the same filename variable) works. So I started looking into the code. One thing strikes me as weird in osgDB::readXmlFile() : // ... XmlNode::Input input; input.open(foundFile); if (!input) { // error Could not open XML file: ... } input.readAllDataIntoBuffer(); // ... XmlNode::Input::Input() initializes _currentPos to 0. Then input.open(foundFile) calls ifstream::open(foundFile.c_str()) so up to there, all's well. But then XmlNode::Input::operator bool() is this: operator bool () const { return _currentPos_buffer.size(); } Now, nothing has touched _buffer yet (readAllDataIntoBuffer() below the error is what fills it) so its size is 0, and _currentPos was set to 0 in the ctor, so this check ( _currentPos_buffer.size(); ) will obviously return false. I don't think that's what is intended... is it? I don't see how this code could work at all, won't it error out every time? I searched for readXmlFile in the OSG sources and didn't get any results, is this used at all? A change that makes it work would be, for example: operator bool () const { return (_fin.good() _currentPos == 0) || _currentPos_buffer.size(); } What's funny is that the XmlParser code is used in the present3D application (and I assume the p3d plugin too) but it doesn't use readXmlFile at all and just bypasses the check: osgDB::XmlNode::Input input; input.open(fileName); input.readAllDataIntoBuffer(); Is this intended? Why would OSG not use its own helper function? -- Once I do this in my code (awaiting a good fix to readXmlFile() ), I get errors end tag is not matched correctly for items like: item someattribute=somevalue / This is valid XML syntax, but is it not supported by the XmlParser? I could add support for it, but it seems weird that it's not supported... Thanks, J-S -- __ Jean-Sebastien Guay jean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] osgDB::XmlParser question
Hi Robert, Your reading of the code is correct... the code will fail every time, something I didn't catch as I haven't directly used this convenience function in the p3d plugin or elsewhere. Re-ordering the code so the input.readAllDataIntoBuffer() is done before the if (!input) looks it would be appropriate. I've made this change and checked it in, could you test it? Seems appropriate yes, as long as readAllDataIntoBuffer() handles the case where the file was not opened correctly (which it does). This seems to work fine. And what about my other question: Once I do this in my code (awaiting a good fix to readXmlFile() ), I get errors end tag is not matched correctly for items like: item someattribute=somevalue / This is valid XML syntax, but is it not supported by the XmlParser? I could add support for it, but it seems weird that it's not supported... I'm looking into this, will probably post a change soon, but it really seems weird to me that this kind of tag is not supported... It's basic XML syntax. J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] osgDB::XmlParser question
Hi J-S, On Thu, Jan 7, 2010 at 2:33 PM, Jean-Sébastien Guay jean-sebastien.g...@cm-labs.com wrote: Re-ordering the code so the input.readAllDataIntoBuffer() is done before the if (!input) looks it would be appropriate. I've made this change and checked it in, could you test it? Seems appropriate yes, as long as readAllDataIntoBuffer() handles the case where the file was not opened correctly (which it does). This seems to work fine. Good to hear, thanks for the testing. And what about my other question: Once I do this in my code (awaiting a good fix to readXmlFile() ), I get errors end tag is not matched correctly for items like: item someattribute=somevalue / This is valid XML syntax, but is it not supported by the XmlParser? I could add support for it, but it seems weird that it's not supported... I'm looking into this, will probably post a change soon, but it really seems weird to me that this kind of tag is not supported... It's basic XML syntax. Oo, too much speed reading on my behalf... I missed this part of your email. As for the syntax, I hadn't coded for this as I wasn't familiar with it. I guess that what you get when you get a non XML expert implementing a XML parser :-) I'm busy tackling the submissions so if you can tackle this it'd be great. Robert. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] osgDB::XmlParser question
Hi Robert, Oo, too much speed reading on my behalf... I missed this part of your email. Hehe, no problem, I had hidden it in plain sight at the end of the message :-) As for the syntax, I hadn't coded for this as I wasn't familiar with it. I guess that what you get when you get a non XML expert implementing a XML parser :-) I'm busy tackling the submissions so if you can tackle this it'd be great. No problem, I didn't mean to assume you would do it :-) Here are the changes. Essentially: 1. When reading, a tag can end with either or /. If it ends with /, then we don't read any children. 2. When writing, if a node has no children then it is written as blah ... /. With these changes, I can read a relatively complex XML file saved from an application (which is what I want to read from the start), then re-write it to another file and the application loads that new file correctly. Note that the read code assumes that / can only occur if it's part of the / (except if it's part of a value in a property). I think that's OK. But it highlights the main problem of writing OSG's own parser... We're not XML experts and the parser is surely not standards-compliant (which was also the cause for this very submission). Using the parser from some 3rd party would have been better in this regard, but I can appreciate the will to keep the number of dependencies down. It's a fine line to walk. I just wanted to highlight the fact that I hope my assumption is OK, but I'm not sure. Last thing, concerning the writing code, before my change the cases for GROUP and NODE were identical. But on read, we set the type to GROUP if the node has any children, so does the NODE case need to write its children at all? If the type is NODE it shouldn't have any children. Or is it written that way in case user code added children to a NODE without changing the type to GROUP? And in that case, do we really need a distinction between NODE and GROUP? Perhaps they should be treated exactly the same with regard to writing the file? Thanks, J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include osgDB/XmlParser #include osgDB/FileUtils #include osg/Notify using namespace osgDB; XmlNode* osgDB::readXmlFile(const std::string filename,const Options* options) { std::string foundFile = osgDB::findDataFile(filename, options); if (!foundFile.empty()) { XmlNode::Input input; input.open(foundFile); input.readAllDataIntoBuffer(); if (!input) { osg::notify(osg::NOTICE)Could not open XML file: filenamestd::endl; return 0; } osg::ref_ptrXmlNode root = new XmlNode; root-read(input); return root.release(); } else { osg::notify(osg::NOTICE)Could not find XML file: filenamestd::endl; return 0; } } std::string osgDB::trimEnclosingSpaces(const std::string str) { if (str.empty()) return str; std::string::size_type start = str.find_first_not_of(' '); if (start==std::string::npos) return std::string(); std::string::size_type end = str.find_last_not_of(' '); if (end==std::string::npos) return std::string(); return std::string(str, start, (end-start)+1); } XmlNode* osgDB::readXmlStream(std::istream fin) { XmlNode::Input input; input.attach(fin); input.readAllDataIntoBuffer(); if (!input) { osg::notify(osg::NOTICE)Could not attach to XML stream.std::endl; return 0; } osg::ref_ptrXmlNode root = new XmlNode; root-read(input); return root.release(); } XmlNode::Input::Input(): _currentPos(0) { setUpControlMappings(); } XmlNode::Input::Input(const Input): _currentPos(0) { setUpControlMappings(); } XmlNode::Input::~Input() { } void XmlNode::Input::setUpControlMappings() { addControlToCharacter(amp;,''); addControlToCharacter(lt;,''); addControlToCharacter(gt;,''); addControlToCharacter(quot;,''); addControlToCharacter(apos;,'\''); } void XmlNode::Input::addControlToCharacter(const std::string control, int c) { _controlToCharacterMap[control] = c;
[osg-users] osgDB::XmlParser question
Hi Robert, I need to read an XML file and thought of using osgDB::XmlParser to do it. Calling this: osgDB::readXmlFile(filename); results in Could not open XML file: somefilename.xml. But the file is there and some simple code using an ifstream to read the file (using the same filename variable) works. So I started looking into the code. One thing strikes me as weird in osgDB::readXmlFile() : // ... XmlNode::Input input; input.open(foundFile); if (!input) { // error Could not open XML file: ... } input.readAllDataIntoBuffer(); // ... XmlNode::Input::Input() initializes _currentPos to 0. Then input.open(foundFile) calls ifstream::open(foundFile.c_str()) so up to there, all's well. But then XmlNode::Input::operator bool() is this: operator bool () const { return _currentPos_buffer.size(); } Now, nothing has touched _buffer yet (readAllDataIntoBuffer() below the error is what fills it) so its size is 0, and _currentPos was set to 0 in the ctor, so this check ( _currentPos_buffer.size(); ) will obviously return false. I don't think that's what is intended... is it? I don't see how this code could work at all, won't it error out every time? I searched for readXmlFile in the OSG sources and didn't get any results, is this used at all? A change that makes it work would be, for example: operator bool () const { return (_fin.good() _currentPos == 0) || _currentPos_buffer.size(); } What's funny is that the XmlParser code is used in the present3D application (and I assume the p3d plugin too) but it doesn't use readXmlFile at all and just bypasses the check: osgDB::XmlNode::Input input; input.open(fileName); input.readAllDataIntoBuffer(); Is this intended? Why would OSG not use its own helper function? -- Once I do this in my code (awaiting a good fix to readXmlFile() ), I get errors end tag is not matched correctly for items like: item someattribute=somevalue / This is valid XML syntax, but is it not supported by the XmlParser? I could add support for it, but it seems weird that it's not supported... Thanks, J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org