From 5c3263289b3e10c49badd416e12075af0f2f294e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 6 Jun 2006 07:52:12 +0000 Subject: Added comment handling to the XML system. It just discards them completely, but later it will retain them so that even after modifying the nodes the comments could be kept in place so they aren't destroyed if something changes. Also added necesarry functions to the XmlDocument that lets the tests run again and fixes some issues with multiple ownernership when transfering the contents to a new document. --- src/test/xmlreadtest.cpp | 6 +++--- src/xmldocument.cpp | 7 +++++++ src/xmldocument.h | 8 ++++++++ src/xmlfilereader.cpp | 6 +++--- src/xmlfilereader.h | 2 +- src/xmlreader.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- src/xmlreader.h | 2 +- src/xmlstringreader.cpp | 6 +++--- src/xmlstringreader.h | 2 +- tests/comments.xml | 12 ++++++++++++ 10 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 tests/comments.xml diff --git a/src/test/xmlreadtest.cpp b/src/test/xmlreadtest.cpp index 5fbd021..f6d8d8b 100644 --- a/src/test/xmlreadtest.cpp +++ b/src/test/xmlreadtest.cpp @@ -14,14 +14,14 @@ int main( int argc, char *argv[] ) if( argv[1][0] == 'f' ) { XmlFileReader r( argv[2], true ); - XmlFileWriter w( argv[3], "\t", r.getRoot() ); + XmlFileWriter w( argv[3], "\t", r.detatchRoot() ); w.write(); - //XmlWriter::write( argv[3], r.getRoot(), "\t" ); } else if( argv[1][0] == 's' ) { XmlStringReader r( argv[2], true ); - //XmlWriter::write( argv[3], r.getRoot(), "\t" ); + XmlWriter w( argv[3], "\t", r.detatchRoot() ); + w.write(); } return 0; diff --git a/src/xmldocument.cpp b/src/xmldocument.cpp index 234ff81..32f1409 100644 --- a/src/xmldocument.cpp +++ b/src/xmldocument.cpp @@ -50,6 +50,13 @@ XmlNode *XmlDocument::getRoot() return pRoot; } +XmlNode *XmlDocument::detatchRoot() +{ + XmlNode *pTemp = pRoot; + pRoot = NULL; + return pTemp; +} + XmlNode *XmlDocument::getCurrent() { return pCurrent; diff --git a/src/xmldocument.h b/src/xmldocument.h index f9a8606..1a8eb52 100644 --- a/src/xmldocument.h +++ b/src/xmldocument.h @@ -147,6 +147,14 @@ public: */ XmlNode *getRoot(); + /** + * Get a pointer to the root object of this XmlDocument, and remove the + * ownership from this object. + *@returns A pointer to an internally owned XmlNode. Do not delete this + * XmlNode. + */ + XmlNode *detatchRoot(); + /** * Get the current context node, which could be the same as the root node. *@returns A pointer to an internally owned XmlNode. Do not delete this diff --git a/src/xmlfilereader.cpp b/src/xmlfilereader.cpp index dd4bc82..9c0b7c1 100644 --- a/src/xmlfilereader.cpp +++ b/src/xmlfilereader.cpp @@ -55,10 +55,10 @@ char XmlFileReader::getChar( int nIndex ) } } -void XmlFileReader::usedChar() +void XmlFileReader::usedChar( int nAmnt ) { - if( fbDataIn.getLength() > 0 ) + if( fbDataIn.getLength()-nAmnt >= 0 ) { - fbDataIn.usedData( 1 ); + fbDataIn.usedData( nAmnt ); } } diff --git a/src/xmlfilereader.h b/src/xmlfilereader.h index 3e996e6..24a6e28 100644 --- a/src/xmlfilereader.h +++ b/src/xmlfilereader.h @@ -39,7 +39,7 @@ public: private: char getChar( int nIndex = 0 ); - void usedChar(); + void usedChar( int nAmnt = 1 ); FILE *fh; /**< The file handle. */ FlexBuf fbDataIn; /**< The input buffer. */ }; diff --git a/src/xmlreader.cpp b/src/xmlreader.cpp index 76c6258..70fd1d7 100644 --- a/src/xmlreader.cpp +++ b/src/xmlreader.cpp @@ -332,8 +332,7 @@ bool XmlReader::content() } setContent( fbContent.getData() ); } - usedChar(); - usedChar(); + usedChar( 2 ); gcall( ws() ); FlexBuf fbName; while( true ) @@ -368,6 +367,46 @@ bool XmlReader::content() throw XmlException("Malformed close tag."); } } + else if( getChar(1) == '!' ) + { + // We know it's a comment, let's see if it's proper + if( getChar(2) != '-' || + getChar(3) != '-' ) + { + // Not a valid XML comment + throw XmlException("Malformed comment start tag found."); + } + + usedChar( 4 ); + + // Now burn text until we find the close tag + for(;;) + { + if( getChar() == '-' ) + { + if( getChar( 1 ) == '-' ) + { + // The next one has to be a '>' now + if( getChar( 2 ) != '>' ) + { + throw XmlException("Malformed comment close tag found. You cannot have a '--' that isn't followed by a '>' in a comment."); + } + usedChar( 3 ); + break; + } + else + { + // Found a dash followed by a non dash, that's ok... + usedChar( 2 ); + } + } + else + { + // Burn comment chars + usedChar(); + } + } + } else { if( fbContent.getLength() > 0 ) diff --git a/src/xmlreader.h b/src/xmlreader.h index 19e485a..4117dfd 100644 --- a/src/xmlreader.h +++ b/src/xmlreader.h @@ -57,7 +57,7 @@ private: /** * Called to increment the current stream position by a single character. */ - virtual void usedChar() = 0; + virtual void usedChar( int nAmnt = 1) = 0; /** * Automoton function: is whitespace. diff --git a/src/xmlstringreader.cpp b/src/xmlstringreader.cpp index 82caacd..211df78 100644 --- a/src/xmlstringreader.cpp +++ b/src/xmlstringreader.cpp @@ -29,10 +29,10 @@ char XmlStringReader::getChar( int nAdd ) } } -void XmlStringReader::usedChar() +void XmlStringReader::usedChar( int nAmnt ) { - if( nLength >= nIndex+1 ) + if( nLength >= nIndex+nAmnt ) { - nIndex++; + nIndex += nAmnt; } } diff --git a/src/xmlstringreader.h b/src/xmlstringreader.h index 07da83c..19df427 100644 --- a/src/xmlstringreader.h +++ b/src/xmlstringreader.h @@ -40,7 +40,7 @@ public: private: char getChar( int nIndex = 0 ); - void usedChar(); + void usedChar( int nAmnt = 1 ); const char *sString; /**< Internal pointer to the input string. */ int nIndex; /**< Our index into the string */ int nLength; /**< The computed length of the string */ diff --git a/tests/comments.xml b/tests/comments.xml new file mode 100644 index 0000000..df05b3b --- /dev/null +++ b/tests/comments.xml @@ -0,0 +1,12 @@ + + + + Aaaugh! + + -- cgit v1.2.3