Re: [libxml++] xmlKeepBlanksDefault



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Le Mardi 20 Mai 2003 11:02, Murray Cumming Comneon com a écrit :
> Looks good. Minor points:
> - If we are going to have the constant then we should use it everywhere,
> instead of something using true explicitly.
> - In fact, this constant could go in the KeepBlanks class, and could be the
> default.

good idea. I was wondering where to put it...

> - I like each class to be in a file with the same name, so this would be
> keepblanks.[h|cc].

yes

>
> I'm pleased that it seems to work without libxml changes.

me too.


See attached modified patch.

I completely removed the keep_blanks context field setting in the 
initialise_context method. I assume that KeepBlank is used before calling it 
(I added KeepBlanks were needed).
I use KeepBlanks this way :

KeepBlanks k( KeepBlanks::Default );

I think it's much more clear what it does this way. Moreover I still have the 
idea, once it works correctly, to give the possibility to change the default 
value on a per-parser and per-document basis. We have to discuss this before 
anyway (I suspect a long thread about this :-).


Cheers,

Christophe

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+yfqHB+sU3TyOQjARAnoXAKCa5+2yz2a+IBOIH4LzQbAY+42qTQCfRTpw
Kqd5U+7Cdbv/Do16Ca/FXs0=
=HGsW
-----END PGP SIGNATURE-----
/* settings.cc
 * libxml++ and this file are
 * copyright (C) 2003 by The libxml++ Development Team, and
 * are covered by the GNU Lesser General Public License, which should be
 * included with libxml++ as the file COPYING.
 */

#include <libxml++/keepblanks.h>

#include <libxml/globals.h>

namespace xmlpp
{
  KeepBlanks::KeepBlanks(bool value)
  {
    oldIndentTreeOutput = xmlIndentTreeOutput;
    oldKeepBlanksDefault = xmlKeepBlanksDefault( value?1:0 );
  }

  KeepBlanks::~KeepBlanks()
  {
    xmlKeepBlanksDefault(oldKeepBlanksDefault);
    xmlIndentTreeOutput = oldIndentTreeOutput;
  }
};

/* settings.h
 * libxml++ and this file are
 * copyright (C) 2003 by The libxml++ Development Team, and
 * are covered by the GNU Lesser General Public License, which should be
 * included with libxml++ as the file COPYING.
 */

#ifndef __LIBXMLPP_KEEPBLANKS_H
#define __LIBXMLPP_KEEPBLANKS_H

namespace xmlpp
{

  class KeepBlanks {
    public:
      static const bool Default = true;

    public:
      KeepBlanks(bool value);
      ~KeepBlanks();

    private:
      int oldKeepBlanksDefault;
      int oldIndentTreeOutput;
  };

};

#endif // __LIBXMLPP_KEEPBLANKS_H
Index: ChangeLog
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/ChangeLog,v
retrieving revision 1.104
diff -u -u -r1.104 ChangeLog
--- ChangeLog	16 May 2003 05:14:27 -0000	1.104
+++ ChangeLog	20 May 2003 09:50:14 -0000
@@ -1,3 +1,11 @@
+2003-05-20  Christophe de Vienne  <cdevienne netcourrier com>
+
+	* libxml++/keepblanks.[h|cc]: New KeepBlanks class which change settings
+	related to xmlKeepBlanksDefault and xmlIndentTreeOuput
+	* libxml++/[document.cc|parsers/*parsers.cc]: use KeepBlanks instead of
+	manually call xmlKeepBlanksDefault().
+	* example/dom_read_write/example_output.xml: removed.
+
 2003-05-16  Murray Cumming  <murrayc usa net>
 
 	* libxml++/noncopyable.[h|cc]: New xmlpp::NonCopyable base class, which
Index: libxml++/Makefile.am
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/Makefile.am,v
retrieving revision 1.13
diff -u -u -r1.13 Makefile.am
--- libxml++/Makefile.am	16 May 2003 05:14:28 -0000	1.13
+++ libxml++/Makefile.am	20 May 2003 09:50:14 -0000
@@ -2,9 +2,9 @@
 
 INCLUDES = -I$(top_srcdir) @LIBXML_CFLAGS@
 
-h_sources_public = libxml++.h attribute.h dtd.h document.h noncopyable.h
+h_sources_public = libxml++.h attribute.h dtd.h document.h noncopyable.h keepblanks.h
 h_sources_private = 
-cc_sources = attribute.cc dtd.cc document.cc noncopyable.cc
+cc_sources = attribute.cc dtd.cc document.cc noncopyable.cc keepblanks.cc
 cc_sources_private =
 
 lib_LTLIBRARIES = libxml++-0.1.la
Index: libxml++/document.cc
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/document.cc,v
retrieving revision 1.9
diff -u -u -r1.9 document.cc
--- libxml++/document.cc	20 Apr 2003 11:01:36 -0000	1.9
+++ libxml++/document.cc	20 May 2003 09:50:14 -0000
@@ -14,6 +14,7 @@
 #include <libxml++/nodes/textnode.h>
 #include <libxml++/nodes/commentnode.h>
 #include <libxml++/exceptions/internal_error.h>
+#include <libxml++/keepblanks.h>
 
 #include <assert.h>
 
@@ -24,8 +25,9 @@
 {
   // Allow discovery of line numbers from Node.
   xmlLineNumbersDefault(1);
-  // Don't use libxml1 compatability. This is the default anyway.
-  xmlKeepBlanksDefault(0);
+
+  // Don't use libxml1 compatibility. This is the default anyway.
+  KeepBlanks k(KeepBlanks::Default);
 
   xmlDoc* doc = xmlParseFile(filename.c_str());
 
@@ -36,7 +38,10 @@
 Document* parse_memory(const std::string& contents) throw(exception)
 {
   xmlLineNumbersDefault(1);
-  xmlKeepBlanksDefault(0);
+
+  // Don't use libxml1 compatibility. This is the default anyway.
+  KeepBlanks k(KeepBlanks::Default);
+
 
   xmlDoc* doc = xmlParseMemory(contents.c_str(), contents.size());
 
@@ -49,7 +54,10 @@
 Document* parse_stream(std::istream& is) throw(exception)
 {
   xmlLineNumbersDefault(1);
-  xmlKeepBlanksDefault(0);
+
+  // Don't use libxml1 compatibility. This is the default anyway.
+  KeepBlanks k(KeepBlanks::Default);
+
 
   xmlParserCtxtPtr context = xmlCreatePushParserCtxt(0, 0, 0, 0, "");
 
@@ -234,11 +242,12 @@
 
 void Document::write_to_file(const std::string& filename, const std::string& encoding) throw(exception)
 {
+  KeepBlanks k(KeepBlanks::Default);
   int result = 0;
   if(!encoding.empty())
-    result = xmlSaveFormatFileEnc(filename.c_str(), _impl, encoding.c_str(), 1);
+    result = xmlSaveFileEnc(filename.c_str(), _impl, encoding.c_str());
   else
-    result = xmlSaveFormatFile(filename.c_str(), _impl, 1);
+    result = xmlSaveFile(filename.c_str(), _impl);
 
   if(result == -1)
     throw exception("write_to_file() failed.");
@@ -247,6 +256,7 @@
 std::string Document::write_to_string(const std::string& encoding)
   throw(exception)
 {
+  KeepBlanks k(KeepBlanks::Default);
   xmlChar* buffer = 0;
   int length = 0;
   
Index: libxml++/parsers/domparser.cc
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/parsers/domparser.cc,v
retrieving revision 1.27
diff -u -u -r1.27 domparser.cc
--- libxml++/parsers/domparser.cc	15 Mar 2003 10:01:50 -0000	1.27
+++ libxml++/parsers/domparser.cc	20 May 2003 09:50:14 -0000
@@ -9,6 +9,7 @@
 #include "libxml++/nodes/element.h"
 #include "libxml++/nodes/textnode.h"
 #include "libxml++/nodes/commentnode.h"
+#include "libxml++/keepblanks.h"
 #include "libxml++/exceptions/internal_error.h"
 #include <libxml/parserInternals.h>//For xmlCreateFileParserCtxt().
 
@@ -38,11 +39,7 @@
 {
   release_underlying(); //Free any existing document.
 
-  //These default values seem to affect the execution of xmlCreateFileParserCtxt(),
-  //leading to extra blank text nodes from xmlParseDocument().
-  //We set the same stuff per-context in initialize_context(), before calling xmlParseDocument().
-  //but that seems to be too late.
-  int old_keepblanks = xmlKeepBlanksDefault(CONSTANT_KeepBlanksSetting); 
+  KeepBlanks k(KeepBlanks::Default);
 
   //The following is based on the implementation of xmlParseFile(), in xmlSAXParseFileWithData():
   _context = xmlCreateFileParserCtxt(filename.c_str());
@@ -52,10 +49,6 @@
     throw internal_error("Couldn't create parsing context");
   }
 
-  //Restore the old value and trust initialize_context() to deal with this from now on:
-  xmlKeepBlanksDefault(old_keepblanks); 
-
-
   if(_context->directory == NULL)
   {
     char* directory = xmlParserGetDirectory(filename.c_str());
@@ -69,9 +62,7 @@
 {
   release_underlying(); //Free any existing document.
 
-  //See parse_file() about this - I haven't tested that it's necessary with xmlCreateMemoryParserCtxt() too,
-  //but it probably is:
-  int old_keepblanks = xmlKeepBlanksDefault(CONSTANT_KeepBlanksSetting);
+  KeepBlanks k(KeepBlanks::Default);
 
   //The following is based on the implementation of xmlParseFile(), in xmlSAXParseFileWithData():
   _context = xmlCreateMemoryParserCtxt(contents.c_str(), contents.size());
@@ -81,13 +72,13 @@
     throw internal_error("Couldn't create parsing context");
   }
 
-  xmlKeepBlanksDefault(old_keepblanks);
-
   parse_context();
 }
 
 void DomParser::parse_context() throw(exception)
 {
+  KeepBlanks k(KeepBlanks::Default);
+
   //The following is based on the implementation of xmlParseFile(), in xmlSAXParseFileWithData():
   //and the implementation of xmlParseMemory(), in xmlSaxParseMemoryWithData().
   initialize_context();
@@ -123,6 +114,8 @@
 void DomParser::parse_stream(std::istream& in) throw(exception)
 {
   release_underlying(); //Free any existing document.
+
+  KeepBlanks k(KeepBlanks::Default);
 
   _context = xmlCreatePushParserCtxt(
       NULL, // setting thoses two parameters to NULL force the parser
Index: libxml++/parsers/parser.cc
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/parsers/parser.cc,v
retrieving revision 1.4
diff -u -u -r1.4 parser.cc
--- libxml++/parsers/parser.cc	8 Mar 2003 12:26:42 -0000	1.4
+++ libxml++/parsers/parser.cc	20 May 2003 09:50:14 -0000
@@ -37,7 +37,6 @@
   //then some other code which uses a global function, such as xmlKeepBlanksDefault(),
   // could cause this to use the wrong settings:
   _context->linenumbers = 1; // TRUE - This is the default anyway.
-  _context->keepBlanks = CONSTANT_KeepBlanksSetting; // FALSE - This is the default anyway.
 
   //Turn on/off validation:
   _context->validate = (_validate ? 1 : 0);
Index: libxml++/parsers/parser.h
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/parsers/parser.h,v
retrieving revision 1.16
diff -u -u -r1.16 parser.h
--- libxml++/parsers/parser.h	16 May 2003 05:14:28 -0000	1.16
+++ libxml++/parsers/parser.h	20 May 2003 09:50:14 -0000
@@ -53,11 +53,6 @@
   static void _callback_validity_error(void* ctx, const char* msg, ...);
   static void _callback_validity_warning(void* ctx, const char* msg, ...);
   
-  enum enumConstants
-  {
-    CONSTANT_KeepBlanksSetting = 0 // =FALSE - Don't use libxml1 compatability. This is the default anyway.
-  };
-  
   xmlParserCtxtPtr _context;
   exception* _exception;
   std::string _validate_error, _validate_warning; //Built gradually - used in an exception at the end of parsing.
Index: libxml++/parsers/saxparser.cc
===================================================================
RCS file: /cvsroot/libxmlplusplus/libxml++/libxml++/parsers/saxparser.cc,v
retrieving revision 1.29
diff -u -u -r1.29 saxparser.cc
--- libxml++/parsers/saxparser.cc	23 Apr 2003 23:12:49 -0000	1.29
+++ libxml++/parsers/saxparser.cc	20 May 2003 09:50:14 -0000
@@ -8,10 +8,9 @@
  */
 
 #include "libxml++/parsers/saxparser.h"
+#include "libxml++/keepblanks.h"
 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
-#include <fstream> //For std::ifstream.
 #include <cstdarg> //For va_list.
-#include <iostream> //For cerr
 
 namespace xmlpp {
 
@@ -132,6 +131,8 @@
   if(_context)
     throw parse_error("Attempt to start a second parse while a parse is in progress.");
 
+  KeepBlanks k(KeepBlanks::Default);
+
   _context = xmlCreateFileParserCtxt(filename.c_str());
   parse();
 }
@@ -141,6 +142,8 @@
   if(_context)
     throw parse_error("Attempt to start a second parse while a parse is in progress.");
 
+  KeepBlanks k(KeepBlanks::Default);
+
   _context = xmlCreateMemoryParserCtxt(contents.c_str(), contents.length());
   parse();
 }
@@ -150,6 +153,8 @@
   if(_context)
     throw parse_error("Attempt to start a second parse while a parse is in progress.");
 
+  KeepBlanks k(KeepBlanks::Default);
+
   _context = xmlCreatePushParserCtxt(
       &_sax_handler,
       this, // user_data
@@ -180,6 +185,8 @@
 
 void SaxParser::parse_chunk(const std::string& chunk) throw(exception)
 {
+  KeepBlanks k(KeepBlanks::Default);
+
   if(!_context)
   {
     _context = xmlCreatePushParserCtxt(


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