From: <vac...@us...> - 2008-11-24 02:37:52
|
Revision: 97 http://xmlwrapp.svn.sourceforge.net/xmlwrapp/?rev=97&view=rev Author: vaclavslavik Date: 2008-11-24 02:37:42 +0000 (Mon, 24 Nov 2008) Log Message: ----------- Fixed libxmlwrapp to not depend on libxslt if XSLT support is enabled (patch #1927398 by Vadim Zeitlin) Modified Paths: -------------- trunk/AUTHORS trunk/ChangeLog trunk/include/xmlwrapp/document.h trunk/src/libxml/document.cxx trunk/src/libxslt/stylesheet.cxx Added Paths: ----------- trunk/src/libxslt/result.h Modified: trunk/AUTHORS =================================================================== --- trunk/AUTHORS 2008-11-24 02:27:10 UTC (rev 96) +++ trunk/AUTHORS 2008-11-24 02:37:42 UTC (rev 97) @@ -7,6 +7,7 @@ Tom Browder <tbr...@us...> Other contributors: + Vadim Zeitlin <va...@tt...> Tiziano Mueller <ti...@us...> Greg Chicares <gch...@sb...> Daniel Evison Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2008-11-24 02:27:10 UTC (rev 96) +++ trunk/ChangeLog 2008-11-24 02:37:42 UTC (rev 97) @@ -1,3 +1,8 @@ +Version 0.6.0 + + Fixed libxmlwrapp to not depend on libxslt if XSLT support + is enabled (Vadim Zeitlin, #1927398). + Version 0.5.1 Various compilation fixes. Modified: trunk/include/xmlwrapp/document.h =================================================================== --- trunk/include/xmlwrapp/document.h 2008-11-24 02:27:10 UTC (rev 96) +++ trunk/include/xmlwrapp/document.h 2008-11-24 02:37:42 UTC (rev 97) @@ -46,7 +46,10 @@ #include <cstddef> // forward declaration -namespace xslt { class stylesheet; } +namespace xslt { + class result; + class stylesheet; +} // end xslt namespace namespace xml { @@ -532,7 +535,7 @@ private: doc_impl *pimpl_; void set_doc_data (void *data); - void set_doc_data_from_xslt (void *data, void *ss); + void set_doc_data_from_xslt (void *data, xslt::result *xr); void* get_doc_data (void); void* release_doc_data (void); Modified: trunk/src/libxml/document.cxx =================================================================== --- trunk/src/libxml/document.cxx 2008-11-24 02:27:10 UTC (rev 96) +++ trunk/src/libxml/document.cxx 2008-11-24 02:37:42 UTC (rev 97) @@ -58,13 +58,8 @@ #include <libxml/tree.h> #include <libxml/xinclude.h> -// bring in libxslt stuff if we need it. -#if defined(XMLWRAPP_WITH_XSLT) -# include <libxslt/xslt.h> -# include <libxslt/xsltInternals.h> -# include <libxslt/transform.h> -# include <libxslt/xsltutils.h> -#endif +// bring in private libxslt stuff (see bug #1927398) +#include "../libxslt/result.h" //#################################################################### namespace { @@ -73,19 +68,19 @@ //#################################################################### struct xml::doc_impl { //#################################################################### - doc_impl (void) : doc_(0), from_xslt_(0) { + doc_impl (void) : doc_(0), xslt_result_(0) { xmlDocPtr tmpdoc; if ( (tmpdoc = xmlNewDoc(0)) == 0) throw std::bad_alloc(); set_doc_data(tmpdoc, true); } //#################################################################### - doc_impl (const char *root_name) : doc_(0), from_xslt_(0), root_(root_name) { + doc_impl (const char *root_name) : doc_(0), xslt_result_(0), root_(root_name) { xmlDocPtr tmpdoc; if ( (tmpdoc = xmlNewDoc(0)) == 0) throw std::bad_alloc(); set_doc_data(tmpdoc, true); } //#################################################################### - doc_impl (const doc_impl &other) : doc_(0), from_xslt_(0) { + doc_impl (const doc_impl &other) : doc_(0), xslt_result_(0) { xmlDocPtr tmpdoc; if ( (tmpdoc = xmlCopyDoc(other.doc_, 1)) == 0) throw std::bad_alloc(); set_doc_data(tmpdoc, false); @@ -123,16 +118,17 @@ root_.set_node_data(new_root_node); if (old_root_node) xmlFreeNode(old_root_node); - from_xslt_ = 0; + xslt_result_ = 0; } //#################################################################### ~doc_impl (void) { if (doc_) xmlFreeDoc(doc_); + delete xslt_result_; } //#################################################################### xmlDocPtr doc_; - void *from_xslt_; + xslt::result *xslt_result_; node root_; std::string version_; mutable std::string encoding_; @@ -303,18 +299,11 @@ xmlChar *xml_string; int xml_string_length; -#if defined(XMLWRAPP_WITH_XSLT) - if (pimpl_->from_xslt_ != 0) { - if (xsltSaveResultToString(&xml_string, &xml_string_length, pimpl_->doc_, - static_cast<xsltStylesheetPtr>(pimpl_->from_xslt_)) >= 0) - { - xmlchar_helper helper(xml_string); - if (xml_string_length) s.assign(helper.get(), xml_string_length); - } + if (pimpl_->xslt_result_ != 0) { + pimpl_->xslt_result_->save_to_string(s); return; } -#endif const char *enc = pimpl_->encoding_.empty() ? 0 : pimpl_->encoding_.c_str(); xmlDocDumpFormatMemoryEnc(pimpl_->doc_, &xml_string, &xml_string_length, enc, 1); @@ -326,14 +315,12 @@ bool xml::document::save_to_file (const char *filename, int compression_level) const { std::swap(pimpl_->doc_->compression, compression_level); -#if defined(XMLWRAPP_WITH_XSLT) - if (pimpl_->from_xslt_ != 0) { - bool rc = xsltSaveResultToFilename(filename, pimpl_->doc_, static_cast<xsltStylesheetPtr>(pimpl_->from_xslt_), 0) >= 0; + if (pimpl_->xslt_result_ != 0) { + bool rc = pimpl_->xslt_result_->save_to_file(filename, compression_level); std::swap(pimpl_->doc_->compression, compression_level); return rc; } -#endif const char *enc = pimpl_->encoding_.empty() ? 0 : pimpl_->encoding_.c_str(); bool rc = xmlSaveFormatFileEnc(filename, pimpl_->doc_, enc, 1) > 0; @@ -345,13 +332,13 @@ void xml::document::set_doc_data (void *data) { // we own the doc now, don't free it! pimpl_->set_doc_data(static_cast<xmlDocPtr>(data), false); - pimpl_->from_xslt_ = 0; + pimpl_->xslt_result_ = 0; } //#################################################################### -void xml::document::set_doc_data_from_xslt (void *data, void *ss) { +void xml::document::set_doc_data_from_xslt (void *data, xslt::result *xr) { // this document came from a XSLT transformation pimpl_->set_doc_data(static_cast<xmlDocPtr>(data), false); - pimpl_->from_xslt_ = ss; + pimpl_->xslt_result_ = xr; } //#################################################################### void* xml::document::get_doc_data (void) { Added: trunk/src/libxslt/result.h =================================================================== --- trunk/src/libxslt/result.h (rev 0) +++ trunk/src/libxslt/result.h 2008-11-24 02:37:42 UTC (rev 97) @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2008 Vadim Zeitlin (va...@ze...) + * All Rights Reserved + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of the Author nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/** @file + * This file contains the declaration of the xslt::result class. +**/ + +#ifndef _xsltwrapp_result_h_ +#define _xsltwrapp_result_h_ + +// standard includes +#include <string> + +// forward declarations +typedef struct _xmlDoc *xmlDocPtr; + +namespace xslt { + +/** + * The xslt::result class is used as a callback by xml::document to allow + * special treatment of XML documents which were created by XSLT. + * + * This class is only meant to be used internally by the library and is + * necessary to avoid the dependency of xml::document, which is part of + * libxmlwrapp, on libxslt which should be only a dependency of libxsltwrapp + * as this precludes calling the XSLT functions which must be used with such + * "result" documents directly from xml::document code. + * + * @author Vadim Zeitlin + * @internal +**/ +class result { +public: + //#################################################################### + /** + * Save the contents of the given XML document in the provided string. + * + * @param s The string to place the XML text data. + **/ + //#################################################################### + virtual void save_to_string(std::string &s) const = 0; + + //#################################################################### + /** + * Save the contents of the given XML document in the provided filename. + * + * @param filename The name of the file to place the XML text data into. + * @param compression_level 0 is no compression, 1-9 allowed, where 1 is for better speed, and 9 is for smaller size + * @return True if the data was saved successfully. + * @return False otherwise. + **/ + //#################################################################### + virtual bool save_to_file (const char *filename, + int compression_level) const = 0; + + //#################################################################### + /** + * Trivial but virtual base class destructor. + **/ + //#################################################################### + virtual ~result (void) { } +}; + +} // end xslt namespace + +#endif Property changes on: trunk/src/libxslt/result.h ___________________________________________________________________ Added: svn:keywords + Id Added: svn:eol-style + native Modified: trunk/src/libxslt/stylesheet.cxx =================================================================== --- trunk/src/libxslt/stylesheet.cxx 2008-11-24 02:27:10 UTC (rev 96) +++ trunk/src/libxslt/stylesheet.cxx 2008-11-24 02:37:42 UTC (rev 97) @@ -39,6 +39,9 @@ #include "xmlwrapp/document.h" #include "xmlwrapp/tree_parser.h" +#include "result.h" +#include "../libxml/utility.h" + // libxslt includes #include <libxslt/xslt.h> #include <libxslt/xsltInternals.h> @@ -52,6 +55,42 @@ #include <vector> #include <map> +namespace { + +// implementation of xslt::result using xslt::stylesheet: we pass this object +// to xml::document for the documents obtained via XSLT so that some operations +// (currently only saving) could be done differently for them +class result_impl : public xslt::result { +public: + // We don't own the pointers given to us, their lifetime must be greater + // than the lifetime of this object. + result_impl(xmlDocPtr doc, xsltStylesheetPtr ss) : doc_(doc), ss_(ss) { } + + virtual void save_to_string(std::string &s) const + { + xmlChar *xml_string; + int xml_string_length; + + if (xsltSaveResultToString(&xml_string, &xml_string_length, doc_, ss_) >= 0) + { + xml::xmlchar_helper helper(xml_string); + if (xml_string_length) s.assign(helper.get(), xml_string_length); + } + } + + virtual bool + save_to_file (const char *filename, int /* compression_level */) const + { + return xsltSaveResultToFilename(filename, doc_, ss_, 0) >= 0; + } + +private: + xmlDocPtr doc_; + xsltStylesheetPtr ss_; +}; + +} // end of anonymous namespace + //#################################################################### namespace { void make_vector_param (std::vector<const char*> &v, const xslt::stylesheet::param_type &p); @@ -114,7 +153,7 @@ xmlDocPtr xmldoc = apply_stylesheet(pimpl_->ss_, input); if (xmldoc) { - result.set_doc_data_from_xslt(xmldoc, pimpl_->ss_); + result.set_doc_data_from_xslt(xmldoc, new result_impl(xmldoc, pimpl_->ss_)); return true; } @@ -126,7 +165,7 @@ xmlDocPtr xmldoc = apply_stylesheet(pimpl_->ss_, input, &with_params); if (xmldoc) { - result.set_doc_data_from_xslt(xmldoc, pimpl_->ss_); + result.set_doc_data_from_xslt(xmldoc, new result_impl(xmldoc, pimpl_->ss_)); return true; } @@ -142,7 +181,7 @@ throw std::runtime_error(pimpl_->error_); } - pimpl_->doc_.set_doc_data_from_xslt(xmldoc, pimpl_->ss_); + pimpl_->doc_.set_doc_data_from_xslt(xmldoc, new result_impl(xmldoc, pimpl_->ss_)); return pimpl_->doc_; } //#################################################################### @@ -155,7 +194,7 @@ throw std::runtime_error(pimpl_->error_); } - pimpl_->doc_.set_doc_data_from_xslt(xmldoc, pimpl_->ss_); + pimpl_->doc_.set_doc_data_from_xslt(xmldoc, new result_impl(xmldoc, pimpl_->ss_)); return pimpl_->doc_; } //#################################################################### This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |