Re: [Libxmlplusplus-general] SAXParser: preventing expansion of predefined entities



On Wed, Dec 11, 2002 at 01:19:54PM +0100, Murray Cumming wrote:

> On Wed, 2002-12-11 at 13:09, Jonathan Wakely wrote:
> > Is there a good reason for this, or would you be willing to accept a
> > patch to (off the top of my head) make _callback_get_entity() call a
> > protected virtual function, so that derived classes can alter the
> > behaviour?
> 
> Yes, that callback should probably be like the others. It should call an
> on_get_entity() virtual function.

Hello again,
the first attached patch adds a protected virtual function to the
SaxParser class, as proposed yesterday, allowing derived classes to
override the default behaviour when an entity is parsed.

I've noticed that the other virtual functions only deal with std::strings
and standard types. This new function takes a const xmlChar* parameter
and returns an xmlEntityPtr, which goes against what I assume is an
intentional decision to not expose the libxml types. IMHO the function is
a bit lower level than the other events, so this might be unavoidable,
and as I'm still largely unfamiliar with the library I don't know of a
better way of doing it (but would be glad to hear one).
Is this OK?

Also, can I propose the second patch, for efficiency's sake. It replaces
two calls to std::string::c_str() with calls to std::string::data(). In
both cases the length of the string is used, so the NULL terminator that
c_str() appends is not required. As I understand it a conforming
std::string implementation is allowed to make an allocation and a copy
when c_str() is called, so data() is preferred when the NULL terminator
is not required (I don't think any implementations actually do it this
way, but they could). Personally I find data() makes it clearer that the
char array is not being treated as a NULL-terminated string, but as a
chunk of bytes.
On the other hand, if c_str() is used for a good reason, ignore me :-)

The patches are separate as they are unrelated changes, even though they
both affect libxml/parserssaxparser.cc. They are both against a clean
CVS HEAD and should apply OK in any order.

Any feedback welcome, if SaxParser::on_get_entity() can be done better
I'd like to know more,

jon

P.S. should I have submitted these patches using the sourceforge patch
tracker thingummie instead?


-- 
Let's remember that, fun though technology may be, it's not the important stuff.
	- James Dennet
Index: libxml++/parsers/saxparser.cc
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/parsers/saxparser.cc,v
retrieving revision 1.16
diff -u -r1.16 saxparser.cc
--- libxml++/parsers/saxparser.cc	9 Dec 2002 09:47:53 -0000	1.16
+++ libxml++/parsers/saxparser.cc	12 Dec 2002 12:02:19 -0000
@@ -53,6 +53,11 @@
     xmlFreeParserCtxt(_context);
 }
 
+xmlEntityPtr SaxParser::on_get_entity(const xmlChar* name)
+{
+    return xmlGetPredefinedEntity(name);
+}
+
 void SaxParser::on_start_document()
 {
 }
@@ -186,7 +191,8 @@
 }
 
 xmlEntityPtr SaxParser::_callback_get_entity(void *_parser, const xmlChar *name) {
-  return xmlGetPredefinedEntity(name);
+  SaxParser* parser = static_cast<SaxParser*>(_parser);
+  return parser->on_get_entity(name);
 }
 
 void SaxParser::_callback_start_document(void *_parser) {
Index: libxml++/parsers/saxparser.h
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/parsers/saxparser.h,v
retrieving revision 1.13
diff -u -r1.13 saxparser.h
--- libxml++/parsers/saxparser.h	9 Dec 2002 09:47:53 -0000	1.13
+++ libxml++/parsers/saxparser.h	12 Dec 2002 12:02:19 -0000
@@ -29,6 +29,7 @@
   virtual void parse_stream(std::istream& in) throw(exception);
 
 protected:
+  virtual xmlEntityPtr on_get_entity(const xmlChar* name);
   virtual void on_start_document();
   virtual void on_end_document();
   virtual void on_start_element(const std::string& name, const Node::AttributeMap& attributes);
Index: libxml++/parsers/saxparser.cc
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/parsers/saxparser.cc,v
retrieving revision 1.16
diff -u -r1.16 saxparser.cc
--- libxml++/parsers/saxparser.cc	9 Dec 2002 09:47:53 -0000	1.16
+++ libxml++/parsers/saxparser.cc	12 Dec 2002 12:04:33 -0000
@@ -141,7 +141,7 @@
   if(_context)
     throw parse_error("Attempt to start a second parse while a parse is in progress.");
 
-  _context = xmlCreateMemoryParserCtxt(contents.c_str(), contents.length());
+  _context = xmlCreateMemoryParserCtxt(contents.data(), contents.length());
   parse();
 }
 
@@ -168,7 +168,7 @@
     // about layout in certain cases.
     line += '\n';
 
-    xmlParseChunk(_context, line.c_str(), line.length(), 0);
+    xmlParseChunk(_context, line.data(), line.length(), 0);
   }
 
   if( ! _exception )


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]