|
Boost-Commit : |
From: seefeld_at_[hidden]
Date: 2007-10-02 15:46:49
Author: stefan
Date: 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
New Revision: 39661
URL: http://svn.boost.org/trac/boost/changeset/39661
Log:
API refinements.
Added:
sandbox/xml/boost/xml/dom/io.hpp (contents, props changed)
Text files modified:
sandbox/xml/boost-build.jam | 2 +-
sandbox/xml/boost/xml/dom/cdata.hpp | 24 +++---------------------
sandbox/xml/boost/xml/dom/detail.hpp | 9 +++++++++
sandbox/xml/boost/xml/dom/document.hpp | 21 +--------------------
sandbox/xml/boost/xml/dom/element.hpp | 4 +++-
sandbox/xml/libs/xml/example/article.xml | 2 +-
sandbox/xml/libs/xml/example/config.cpp | 5 +++--
sandbox/xml/libs/xml/example/dom.cpp | 7 +++----
sandbox/xml/libs/xml/example/xinclude.cpp | 2 +-
9 files changed, 25 insertions(+), 51 deletions(-)
Modified: sandbox/xml/boost-build.jam
==============================================================================
--- sandbox/xml/boost-build.jam (original)
+++ sandbox/xml/boost-build.jam 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -7,5 +7,5 @@
# Set the BOOST_ROOT environment variable on your command-line or in the
# environment to point at the root of your regular Boost installation.
-JAMFILE = [Jj]amfile ;
+#JAMFILE = [Jj]amfile ;
boost-build $(BOOST_ROOT)/tools/build/v2 ;
Modified: sandbox/xml/boost/xml/dom/cdata.hpp
==============================================================================
--- sandbox/xml/boost/xml/dom/cdata.hpp (original)
+++ sandbox/xml/boost/xml/dom/cdata.hpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -13,32 +13,14 @@
template <typename S> class element;
template <typename S>
-class cdata : public node<S>
+class cdata : public text<S>
{
-friend class element<S>;
+ friend class element<S>;
public:
- S content() const;
- void set_content(S const &);
-
// private:
- cdata(xmlNode *a) : node<S>(a) {}
+ cdata(xmlNode *a) : text<S>(a) {}
};
-template <typename S>
-S cdata<S>::content() const
-{
- xmlChar *content = xmlNodeGetContent(this->impl());
- S retn = converter<S>::out(content);
- xmlFree(content);
- return retn;
-}
-
-template <typename S>
-void cdata<S>::set_content(S const &content)
-{
- xmlSetContent(this->impl(), converter<S>::in(content));
-}
-
} // namespace boost::xml::dom
} // namespace boost::xml
} // namespace boost
Modified: sandbox/xml/boost/xml/dom/detail.hpp
==============================================================================
--- sandbox/xml/boost/xml/dom/detail.hpp (original)
+++ sandbox/xml/boost/xml/dom/detail.hpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -50,6 +50,8 @@
struct pointee<node_ptr<N> > { typedef N type;};
template <typename T> class wrapper;
+template <typename T> T impl_cast(wrapper<T> *);
+template <typename T> T impl_cast(wrapper<T> const *);
//.
class accessor
@@ -68,6 +70,8 @@
class wrapper : public accessor
{
friend class accessor;
+ template <typename T1> friend T1 impl_cast(wrapper<T1> *);
+ template <typename T1> friend T1 impl_cast(wrapper<T1> const *);
public:
wrapper(T t) : impl_(t) {}
@@ -79,6 +83,11 @@
T impl_;
};
+template <typename T>
+T impl_cast(wrapper<T> *w) { return w->impl();}
+template <typename T>
+T impl_cast(wrapper<T> const *w) { return w->impl();}
+
template <typename N>
node_ptr<N> ptr_factory(xmlNode *n) { return N(n);}
Modified: sandbox/xml/boost/xml/dom/document.hpp
==============================================================================
--- sandbox/xml/boost/xml/dom/document.hpp (original)
+++ sandbox/xml/boost/xml/dom/document.hpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -3,6 +3,7 @@
#include <boost/xml/dom/element.hpp>
#include <boost/xml/dom/dtd.hpp>
+#include <boost/xml/dom/io.hpp>
#include <string>
#include <memory>
#include <stdexcept>
@@ -37,10 +38,6 @@
node_ptr<element<S> > create_root(std::string const &name,
std::string const &ns = std::string(),
std::string const &ns_prefix = std::string());
-
- void write_to_file(const std::string &filename,
- const std::string &encoding = std::string())
- throw(std::runtime_error);
private:
document(xmlDoc *doc) : detail::wrapper<xmlDoc*>(doc) {}
};
@@ -106,22 +103,6 @@
return root();
}
-template <typename S>
-inline void document<S>::write_to_file(const std::string &filename,
- const std::string &encoding)
- throw(std::runtime_error)
-{
- int result = 0;
- if(!encoding.empty())
- result = xmlSaveFormatFileEnc(filename.c_str(),
- this->impl(), encoding.c_str(), 1);
- else
- result = xmlSaveFormatFile(filename.c_str(), this->impl(), 1);
-
- if(result == -1)
- throw std::runtime_error("Failed to write document.");
-}
-
} // namespace boost::xml::dom
} // namespace boost::xml
} // namespace boost
Modified: sandbox/xml/boost/xml/dom/element.hpp
==============================================================================
--- sandbox/xml/boost/xml/dom/element.hpp (original)
+++ sandbox/xml/boost/xml/dom/element.hpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -84,7 +84,9 @@
node_ptr<node<S> > insert(child_iterator, node_ptr<node<S> > node);
void remove(child_iterator);
- bool has_content() const { return this->impl()->content;}
+ // FIXME: the implementation of has_content needs a full traversal
+ // of the subtree.
+ // bool has_content() const;
S content() const;
void set_content(S const &content);
void append_content(S const &content);
Added: sandbox/xml/boost/xml/dom/io.hpp
==============================================================================
--- (empty file)
+++ sandbox/xml/boost/xml/dom/io.hpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -0,0 +1,64 @@
+#ifndef boost_xml_dom_write_hpp_
+#define boost_xml_dom_write_hpp_
+
+#include <boost/xml/dom/document.hpp>
+#include <streambuf>
+#include <iostream>
+#include <libxml/xmlsave.h>
+
+namespace boost
+{
+namespace xml
+{
+namespace dom
+{
+namespace detail
+{
+int stream_write_callback(void *context, const char *buffer, int len)
+{
+ std::streambuf *sb = reinterpret_cast<std::streambuf *>(context);
+ int written = sb->sputn(buffer, len);
+ return written;
+}
+int stream_close_callback(void *context) { return 0;}
+}
+
+template <typename S>
+inline void write(document<S> const &document,
+ std::streambuf *sb,
+ std::string const &encoding = std::string())
+ throw(std::runtime_error)
+{
+ char const *enc = encoding.c_str();
+ if (enc[0] == '\0') enc = 0;
+ xmlSaveCtxt *ctx = xmlSaveToIO(detail::stream_write_callback,
+ detail::stream_close_callback,
+ sb, enc, XML_SAVE_FORMAT);
+ long bytes = xmlSaveDoc(ctx, detail::impl_cast(&document));
+ int status = xmlSaveClose(ctx);
+ if(bytes == -1 || status == -1)
+ throw std::runtime_error("Failed to stream document.");
+}
+
+template <typename S>
+inline void write_to_file(document<S> const &document,
+ std::string const &filename,
+ std::string const &encoding = std::string())
+ throw(std::runtime_error)
+{
+ int result = 0;
+ if(!encoding.empty())
+ result = xmlSaveFormatFileEnc(filename.c_str(),
+ detail::impl_cast(&document), encoding.c_str(), 1);
+ else
+ result = xmlSaveFormatFile(filename.c_str(), detail::impl_cast(&document), 1);
+
+ if(result == -1)
+ throw std::runtime_error("Failed to write document.");
+}
+
+} // namespace boost::xml::dom
+} // namespace boost::xml
+} // namespace boost
+
+#endif
Modified: sandbox/xml/libs/xml/example/article.xml
==============================================================================
--- sandbox/xml/libs/xml/example/article.xml (original)
+++ sandbox/xml/libs/xml/example/article.xml 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -15,4 +15,4 @@
<xref linkend="section2"/>, and <xref linkend="section4"/>.</para>
</section>
</section>
-</article>
\ No newline at end of file
+</article>
Modified: sandbox/xml/libs/xml/example/config.cpp
==============================================================================
--- sandbox/xml/libs/xml/example/config.cpp (original)
+++ sandbox/xml/libs/xml/example/config.cpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -5,7 +5,7 @@
#include <boost/xml/dom.hpp>
#include "string.hpp"
-#include <iostream>
+#include <fstream>
namespace dom = boost::xml::dom;
@@ -16,7 +16,8 @@
document_ptr document(new dom::document<std::string>());
element_ptr root = document->create_root("config");
dtd_ptr doc_type = document->create_internal_subset("foo", "bar", "baz");
- document->write_to_file("config.xml");
+ std::ofstream ofs("config.xml");
+ write(*document, ofs.rdbuf());
}
catch (std::exception const &e)
{
Modified: sandbox/xml/libs/xml/example/dom.cpp
==============================================================================
--- sandbox/xml/libs/xml/example/dom.cpp (original)
+++ sandbox/xml/libs/xml/example/dom.cpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -11,9 +11,9 @@
int main(int argc, char **argv)
{
- if (argc != 2)
+ if (argc != 3)
{
- std::cerr << "Usage: example <xml-file>" << std::endl;
+ std::cerr << "Usage: example <xml-input> <xml-output>" << std::endl;
return -1;
}
try
@@ -96,8 +96,7 @@
}
}
}
- std::cout << "writing modified document to 'article2.xml'..." << std::endl;
- document->write_to_file("article2.xml");
+ write_to_file(*document, argv[2]);
}
catch (std::exception const &e)
{
Modified: sandbox/xml/libs/xml/example/xinclude.cpp
==============================================================================
--- sandbox/xml/libs/xml/example/xinclude.cpp (original)
+++ sandbox/xml/libs/xml/example/xinclude.cpp 2007-10-02 15:46:48 EDT (Tue, 02 Oct 2007)
@@ -43,7 +43,7 @@
}
}
std::cout << "writing modified document to 'xi-book2.xml'..." << std::endl;
- document->write_to_file(argv[2]);
+ write_to_file(*document, argv[2]);
}
catch (const std::exception &e)
{
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk