[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [4782] Add some capabilities that will soon become useful
From: |
Greg Chicares |
Subject: |
[lmi-commits] [4782] Add some capabilities that will soon become useful |
Date: |
Tue, 16 Mar 2010 01:56:26 +0000 |
Revision: 4782
http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=4782
Author: chicares
Date: 2010-03-16 01:56:25 +0000 (Tue, 16 Mar 2010)
Log Message:
-----------
Add some capabilities that will soon become useful
Modified Paths:
--------------
lmi/trunk/xml_lmi.cpp
lmi/trunk/xml_lmi.hpp
Modified: lmi/trunk/xml_lmi.cpp
===================================================================
--- lmi/trunk/xml_lmi.cpp 2010-02-26 02:32:22 UTC (rev 4781)
+++ lmi/trunk/xml_lmi.cpp 2010-03-16 01:56:25 UTC (rev 4782)
@@ -82,6 +82,40 @@
/// Parse an xml stream.
///
+/// Precondition: arguments describe an xml string.
+///
+/// Postconditions: member parser_ is a non-null pointer; the object
+/// it points to is valid in that its operator!() returns false.
+///
+/// Throws: std::runtime_error, via fatal_error(), if a precondition
+/// is violated, or if xml-library calls throw an exception derived
+/// from std::exception.
+
+xml_lmi::dom_parser::dom_parser(char const* data, std::size_t length)
+{
+ try
+ {
+ error_context_ = "Unable to parse xml data: ";
+ parser_.reset(new DomParser(data, length));
+ if(0 == parser_.get())
+ {
+ throw std::runtime_error("Parser not initialized.");
+ }
+ if(true == parser_->operator!())
+ {
+ throw std::runtime_error
+ ("Parser failed: " + parser_->get_error_message()
+ );
+ }
+ }
+ catch(std::exception const& e)
+ {
+ fatal_error() << error_context_ << e.what() << LMI_FLUSH;
+ }
+}
+
+/// Parse an xml stream.
+///
/// XMLWRAPP !! xmlwrapp has no such ctor as
/// xml::tree_parser(std::istream&)
/// Therefore, read the std::istream into a std::string with
@@ -207,6 +241,20 @@
return document_->get_root_node();
}
+void xml_lmi::xml_document::save(std::string const& filename)
+{
+ bool okay = document_->save_to_file(filename.c_str());
+ if(!okay)
+ {
+ fatal_error()
+ << "Unable to save file '"
+ << filename
+ << "'."
+ << LMI_FLUSH
+ ;
+ }
+}
+
std::string xml_lmi::xml_document::str()
{
std::string s;
@@ -223,6 +271,24 @@
element.push_back(xml::element(name.c_str(), content.c_str()));
}
+xml::node::const_iterator retrieve_element
+ (xml::element const& parent
+ ,std::string const& name
+ )
+{
+ xml::node::const_iterator i = parent.find(name.c_str());
+ if(parent.end() == i)
+ {
+ fatal_error()
+ << "Required element '"
+ << name
+ << "' not found."
+ << LMI_FLUSH
+ ;
+ }
+ return i;
+}
+
std::string get_content(xml::element const& element)
{
try
Modified: lmi/trunk/xml_lmi.hpp
===================================================================
--- lmi/trunk/xml_lmi.hpp 2010-02-26 02:32:22 UTC (rev 4781)
+++ lmi/trunk/xml_lmi.hpp 2010-03-16 01:56:25 UTC (rev 4782)
@@ -31,11 +31,11 @@
#include <boost/scoped_ptr.hpp>
#include <boost/utility.hpp>
-#include <xmlwrapp/node.h> // xml::node::const_iterator
+#include <xmlwrapp/node.h> // (for xml::element)
+#include <cstddef> // std::size_t
#include <iosfwd>
#include <string>
-#include <vector>
/// Interface to xmlwrapp.
@@ -48,6 +48,7 @@
public:
dom_parser(std::string const& filename);
+ dom_parser(char const* data, std::size_t length);
dom_parser(std::istream const&);
~dom_parser();
@@ -68,6 +69,8 @@
Document const& document() const {return *document_;}
xml::element& root_node();
+
+ void save(std::string const& filename);
std::string str();
private:
@@ -81,6 +84,13 @@
,std::string const& content
);
+ /// Find an element subnode by name, throwing if it is not found.
+
+ xml::node::const_iterator retrieve_element
+ (xml::element const& parent
+ ,std::string const& name
+ );
+
/// Retrieve an xml element's full text-node contents.
///
/// The contents of all text-node children are concatenated.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [lmi-commits] [4782] Add some capabilities that will soon become useful,
Greg Chicares <=