aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-03-14 10:45:12 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-03-14 10:45:12 +0200
commitce298ad2e8dbc3601d180c178bee9d606b8edab2 (patch)
tree983545115dcb2d73cf56c3fc2e2f6be5d297eb43
parent17ad2a3fd8933aa49bff4c8ea49963d15cc0db92 (diff)
Add convenience constructors to XML parsing and serialization exceptions
-rw-r--r--cutl/xml/parser.cxx36
-rw-r--r--cutl/xml/parser.hxx27
-rw-r--r--cutl/xml/serializer.cxx19
-rw-r--r--cutl/xml/serializer.hxx8
4 files changed, 72 insertions, 18 deletions
diff --git a/cutl/xml/parser.cxx b/cutl/xml/parser.cxx
index 5724794..ca20edd 100644
--- a/cutl/xml/parser.cxx
+++ b/cutl/xml/parser.cxx
@@ -29,10 +29,26 @@ namespace cutl
const string& d)
: name_ (n), line_ (l), column_ (c), description_ (d)
{
+ init ();
+ }
+
+ parsing::
+ parsing (const parser& p, const std::string& d)
+ : name_ (p.input_name ()),
+ line_ (p.line ()),
+ column_ (p.column ()),
+ description_ (d)
+ {
+ init ();
+ }
+
+ void parsing::
+ init ()
+ {
ostringstream os;
- if (!n.empty ())
- os << n << ':';
- os << l << ':' << c << ": error: " << d;
+ if (!name_.empty ())
+ os << name_ << ':';
+ os << line_ << ':' << column_ << ": error: " << description_;
what_ = os.str ();
}
@@ -52,8 +68,8 @@ namespace cutl
}
parser::
- parser (istream& is, const string& name, feature_type f)
- : is_ (is), name_ (name), feature_ (f),
+ parser (istream& is, const string& iname, feature_type f)
+ : is_ (is), iname_ (iname), feature_ (f),
depth_ (0), state_ (state_next), event_ (eof), queue_ (eof),
pqname_ (&qname_), pvalue_ (&value_),
attr_i_ (0), start_ns_i_ (0), end_ns_i_ (0)
@@ -101,15 +117,15 @@ namespace cutl
switch (content ())
{
case empty:
- throw parsing (name_, line_, column_, "character in empty content");
+ throw parsing (iname_, line_, column_, "character in empty content");
case complex:
- throw parsing (name_, line_, column_, "character in complex content");
+ throw parsing (iname_, line_, column_, "character in complex content");
default:
assert (false);
}
}
else
- throw parsing (name_,
+ throw parsing (iname_,
XML_GetCurrentLineNumber (p_),
XML_GetCurrentColumnNumber (p_),
XML_ErrorString (e));
@@ -174,9 +190,9 @@ namespace cutl
switch (content ())
{
case empty:
- throw parsing (name_, line_, column_, "element in empty content");
+ throw parsing (iname_, line_, column_, "element in empty content");
case simple:
- throw parsing (name_, line_, column_, "element in simple content");
+ throw parsing (iname_, line_, column_, "element in simple content");
default:
break;
}
diff --git a/cutl/xml/parser.hxx b/cutl/xml/parser.hxx
index 5dc2a83..d7011af 100644
--- a/cutl/xml/parser.hxx
+++ b/cutl/xml/parser.hxx
@@ -33,6 +33,8 @@ namespace cutl
{
namespace xml
{
+ class parser;
+
struct LIBCUTL_EXPORT parsing: exception
{
virtual
@@ -43,6 +45,8 @@ namespace cutl
unsigned long long column,
const std::string& description);
+ parsing (const parser&, const std::string& description);
+
const std::string&
name () const {return name_;}
@@ -59,6 +63,10 @@ namespace cutl
what () const throw ();
private:
+ void
+ init ();
+
+ private:
std::string name_;
unsigned long long line_;
unsigned long long column_;
@@ -83,14 +91,20 @@ namespace cutl
receive_characters |
receive_attributes;
- // Parse std::istream. Name is used in diagnostics to identify the
- // document being parsed. std::ios_base::failure exception is used
- // to report io errors (badbit and failbit).
+ // Parse std::istream. Input name is used in diagnostics to identify
+ // the document being parsed. std::ios_base::failure exception is
+ // used to report io errors (badbit and failbit).
//
parser (std::istream&,
- const std::string& name,
+ const std::string& input_name,
feature_type = receive_default);
+ const std::string&
+ input_name () const {return iname_;}
+
+ // Parsing events.
+ //
+ public:
enum event_type
{
// If adding new events, also update the stream insertion operator.
@@ -135,6 +149,9 @@ namespace cutl
event_type
event () {return event_;}
+ // Event data.
+ //
+ public:
const qname_type& qname () const {return *pqname_;}
const std::string& namespace_ () const {return pqname_->namespace_ ();}
@@ -202,7 +219,7 @@ namespace cutl
private:
std::istream& is_;
- const std::string name_;
+ const std::string iname_;
feature_type feature_;
XML_Parser p_;
diff --git a/cutl/xml/serializer.cxx b/cutl/xml/serializer.cxx
index 3919635..8da3df5 100644
--- a/cutl/xml/serializer.cxx
+++ b/cutl/xml/serializer.cxx
@@ -22,14 +22,27 @@ namespace cutl
serialization (const string& n, const string& d)
: name_ (n), description_ (d)
{
- if (!n.empty ())
+ init ();
+ }
+
+ serialization::
+ serialization (const serializer& s, const std::string& d)
+ : name_ (s.output_name ()), description_ (d)
+ {
+ init ();
+ }
+
+ void serialization::
+ init ()
+ {
+ if (!name_.empty ())
{
- what_ += n;
+ what_ += name_;
what_ += ": ";
}
what_ += "error: ";
- what_ += d;
+ what_ += description_;
}
char const* serialization::
diff --git a/cutl/xml/serializer.hxx b/cutl/xml/serializer.hxx
index 591a20a..88164e2 100644
--- a/cutl/xml/serializer.hxx
+++ b/cutl/xml/serializer.hxx
@@ -20,6 +20,8 @@ namespace cutl
{
namespace xml
{
+ class serializer;
+
struct LIBCUTL_EXPORT serialization: exception
{
virtual
@@ -28,6 +30,8 @@ namespace cutl
serialization (const std::string& name,
const std::string& description);
+ serialization (const serializer&, const std::string& description);
+
const std::string&
name () const {return name_;}
@@ -38,6 +42,10 @@ namespace cutl
what () const throw ();
private:
+ void
+ init ();
+
+ private:
std::string name_;
std::string description_;
std::string what_;