documentation
This commit is contained in:
@@ -46,7 +46,10 @@
|
|||||||
All tags are by default specified as 0..n (optional and any number
|
All tags are by default specified as 0..n (optional and any number
|
||||||
there of).
|
there of).
|
||||||
|
|
||||||
<code>
|
@code
|
||||||
|
#include <xml-cxx/xml.hxx>
|
||||||
|
#include <iostream>
|
||||||
|
[...]
|
||||||
xml::Factory test(xml::Node("persons") // root node
|
xml::Factory test(xml::Node("persons") // root node
|
||||||
<<(xml::Node("person") // child of persons
|
<<(xml::Node("person") // child of persons
|
||||||
.attr("id", xml::mandatory)
|
.attr("id", xml::mandatory)
|
||||||
@@ -55,11 +58,13 @@
|
|||||||
<<(xml::Node("friends") // friends of person
|
<<(xml::Node("friends") // friends of person
|
||||||
<<(xml::Node("friend") // a friend
|
<<(xml::Node("friend") // a friend
|
||||||
.attr("id", xml::mandatory)))));
|
.attr("id", xml::mandatory)))));
|
||||||
|
[...]
|
||||||
try {
|
try {
|
||||||
std::auto_ptr<xml::Node> persons(test.read(std::ifstream("file.xml)));
|
std::auto_ptr<xml::Node> persons(test.read(std::ifstream("file.xml)));
|
||||||
// Here we can be sure, that our structure is valid,
|
// Here we can be sure, that our structure is valid,
|
||||||
// but we must check optional elements before access, otherwise
|
// but we must check optional elements before access, otherwise
|
||||||
// we get an exception.
|
// we get an exception.
|
||||||
|
[...]
|
||||||
for (xml::Node::size_type i(0); i<persons.children(); ++i) {
|
for (xml::Node::size_type i(0); i<persons.children(); ++i) {
|
||||||
std::cout<<"Person: "<<*persons[i]["name"]; // exception if no "name"
|
std::cout<<"Person: "<<*persons[i]["name"]; // exception if no "name"
|
||||||
if (persons[i]("friends")) // check if "friends" is set
|
if (persons[i]("friends")) // check if "friends" is set
|
||||||
@@ -68,17 +73,30 @@
|
|||||||
std::cout<<" has no friend list";
|
std::cout<<" has no friend list";
|
||||||
std::cout<<std::endl;
|
std::cout<<std::endl;
|
||||||
}
|
}
|
||||||
|
[...]
|
||||||
} catch (const std::exception& x) {
|
} catch (const std::exception& x) {
|
||||||
std::cerr<<"**** Error in file \"file.xml\":"<<std::endl
|
std::cerr<<"**** Error in file \"file.xml\":"<<std::endl
|
||||||
<<x.what()<<std::endl;
|
<<x.what()<<std::endl;
|
||||||
}
|
}
|
||||||
</code> */
|
@endcode */
|
||||||
namespace xml {
|
namespace xml {
|
||||||
|
|
||||||
//============================================================================
|
//============================================================================
|
||||||
enum NodeType {START, END, EMPTY, SPECIAL};
|
//! Type of an xml node.
|
||||||
const bool mandatory = true;
|
/*! Only start nodes and empty nodes may have attributes. */
|
||||||
const bool optional = false;
|
enum NodeType {
|
||||||
|
START, //!< start node, such as <code><node></code>
|
||||||
|
END, //!< end node, such as <code></node></code>
|
||||||
|
EMPTY, //!< empty node, such as <code><node/></code>
|
||||||
|
SPECIAL //!< special node, such as
|
||||||
|
//! a comment <code><!-- ... --></code>,
|
||||||
|
//! a xml start indication <code><?xml?></code>
|
||||||
|
//! or a document type declaration <code><!DOCTYPE ...></code>
|
||||||
|
};
|
||||||
|
//! Declares an attribute to be mandatory.
|
||||||
|
const bool mandatory(true);
|
||||||
|
//! Declares an attribute to be optional.
|
||||||
|
const bool optional(false);
|
||||||
|
|
||||||
//================================================================= EXCEPTIONS
|
//================================================================= EXCEPTIONS
|
||||||
struct Tag;
|
struct Tag;
|
||||||
@@ -243,26 +261,19 @@ namespace xml {
|
|||||||
//! Attribute values ar mainly a std::pair.
|
//! Attribute values ar mainly a std::pair.
|
||||||
/*! In addition to a normal std::pair, attributes offer an
|
/*! In addition to a normal std::pair, attributes offer an
|
||||||
assignment operator to set the value, and can be constructed
|
assignment operator to set the value, and can be constructed
|
||||||
as empty attribute, given only a key. */
|
as empty attribute, given only a key.
|
||||||
|
@note Simply use xml::Attr instead of xml::Attributes::Value. */
|
||||||
class Value: public value_type {
|
class Value: public value_type {
|
||||||
public:
|
public:
|
||||||
//! Construct an empty attribute.
|
Value(const value_type& o) throw();
|
||||||
Value(const std::string& name) throw();
|
Value(const std::string& name) throw();
|
||||||
//! Construct an attribute with name an value.
|
|
||||||
Value(const std::string& name, const std::string& namevalue) throw();
|
Value(const std::string& name, const std::string& namevalue) throw();
|
||||||
//! Assign a value.
|
Value& operator=(const std::string& value) throw();
|
||||||
value_type& operator=(const std::string& value) throw();
|
|
||||||
//! Get the attribute name.
|
|
||||||
const std::string& name() const throw();
|
const std::string& name() const throw();
|
||||||
//! Get the attribute value.
|
|
||||||
const std::string& value() const throw();
|
const std::string& value() const throw();
|
||||||
//! Get the attribute value.
|
|
||||||
std::string& value() throw();
|
std::string& value() throw();
|
||||||
//! Convert the attribute to a boolean.
|
|
||||||
operator bool() const throw();
|
operator bool() const throw();
|
||||||
//! Convert the attribute to a number.
|
|
||||||
operator unsigned long() const throw();
|
operator unsigned long() const throw();
|
||||||
//! Convert the attribute to a space separated list.
|
|
||||||
operator List() const throw();
|
operator List() const throw();
|
||||||
List toList(const std::string& separators=" \t\n\r") const throw();
|
List toList(const std::string& separators=" \t\n\r") const throw();
|
||||||
private:
|
private:
|
||||||
@@ -270,16 +281,16 @@ namespace xml {
|
|||||||
};
|
};
|
||||||
Attributes() throw();
|
Attributes() throw();
|
||||||
Attributes(const std::string& empty) throw();
|
Attributes(const std::string& empty) throw();
|
||||||
Attributes& operator<<(const value_type& v) throw();
|
Attributes(const std::string& key, const std::string& value) throw();
|
||||||
|
Attributes& operator<<(const Value& v) throw();
|
||||||
Attributes& operator<<(const std::string& key) throw();
|
Attributes& operator<<(const std::string& key) throw();
|
||||||
Attributes& operator=(const std::string& value) throw();
|
|
||||||
private:
|
|
||||||
iterator _active;
|
|
||||||
};
|
};
|
||||||
|
//! Simplification: Use xml::Attr instead of xml::Attributes::Value.
|
||||||
typedef Attributes::Value Attr;
|
typedef Attributes::Value Attr;
|
||||||
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
//! @internal structure for parsing tags
|
||||||
struct Tag {
|
struct Tag {
|
||||||
std::string name;
|
std::string name;
|
||||||
NodeType type;
|
NodeType type;
|
||||||
@@ -289,6 +300,9 @@ namespace xml {
|
|||||||
};
|
};
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
//! An xml Node.
|
||||||
|
/*! XML Nodes may contain either text or other nodes, but not both
|
||||||
|
at the same time. */
|
||||||
class Node {
|
class Node {
|
||||||
private:
|
private:
|
||||||
typedef std::vector<Node*> Contents;
|
typedef std::vector<Node*> Contents;
|
||||||
|
53
src/xml.cxx
53
src/xml.cxx
@@ -126,36 +126,55 @@ namespace xml {
|
|||||||
//============================================================================
|
//============================================================================
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
//! Copy an attribute.
|
||||||
|
Attributes::Value::Value(const value_type& o) throw():
|
||||||
|
Attributes::value_type(o) {
|
||||||
|
}
|
||||||
|
//! Construct an empty attribute.
|
||||||
Attributes::Value::Value(const std::string& name) throw():
|
Attributes::Value::Value(const std::string& name) throw():
|
||||||
Attributes::value_type(name, std::string()) {
|
Attributes::value_type(name, std::string()) {
|
||||||
}
|
}
|
||||||
|
//! Construct an attribute with name an value.
|
||||||
Attributes::Value::Value(const std::string& name,
|
Attributes::Value::Value(const std::string& name,
|
||||||
const std::string& value) throw():
|
const std::string& value) throw():
|
||||||
Attributes::value_type(name, value) {
|
Attributes::value_type(name, value) {
|
||||||
}
|
}
|
||||||
|
//! Assign a value.
|
||||||
|
Attributes::Value& Attributes::Value::operator=(const std::string& value)
|
||||||
|
throw() {
|
||||||
|
second = value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
//! Get the attribute name.
|
||||||
const std::string& Attributes::Value::name() const throw() {
|
const std::string& Attributes::Value::name() const throw() {
|
||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
//! Get the attribute value.
|
||||||
const std::string& Attributes::Value::value() const throw() {
|
const std::string& Attributes::Value::value() const throw() {
|
||||||
return second;
|
return second;
|
||||||
}
|
}
|
||||||
|
//! Get the attribute value.
|
||||||
std::string& Attributes::Value::value() throw() {
|
std::string& Attributes::Value::value() throw() {
|
||||||
return second;
|
return second;
|
||||||
}
|
}
|
||||||
|
//! Convert the attribute to a boolean.
|
||||||
Attributes::Value::operator bool() const throw() {
|
Attributes::Value::operator bool() const throw() {
|
||||||
/*! @return @c true if the value is set and not equal to one of:
|
/*! @return @c true if the value is set and not equal to one of:
|
||||||
@c false @c no @c 0. */
|
@c false @c no @c 0. */
|
||||||
return !(second.size()||second=="false"||second=="no"||second=="0");
|
return !(second.size()||second=="false"||second=="no"||second=="0");
|
||||||
}
|
}
|
||||||
|
//! Convert the attribute to a number.
|
||||||
Attributes::Value::operator unsigned long() const throw() {
|
Attributes::Value::operator unsigned long() const throw() {
|
||||||
std::stringstream ss(second);
|
std::stringstream ss(second);
|
||||||
int i(0);
|
int i(0);
|
||||||
ss>>i;
|
ss>>i;
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
//! Convert the attribute to a space separated list.
|
||||||
Attributes::Value::operator List() const throw() {
|
Attributes::Value::operator List() const throw() {
|
||||||
return toList();
|
return toList();
|
||||||
}
|
}
|
||||||
|
//! Convert the attribute to a space separated list.
|
||||||
Attributes::List Attributes::Value::toList(const std::string& separators)
|
Attributes::List Attributes::Value::toList(const std::string& separators)
|
||||||
const throw() {
|
const throw() {
|
||||||
List l;
|
List l;
|
||||||
@@ -166,30 +185,31 @@ namespace xml {
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Attributes::value_type& Attributes::Value::operator=(const std::string& value)
|
//! Empty attribute list
|
||||||
throw() {
|
Attributes::Attributes() throw() {}
|
||||||
second = value;
|
//! Attribute list with first one empty attribute given.
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Attributes::Attributes() throw(): _active(end()) {}
|
|
||||||
Attributes::Attributes(const std::string& empty) throw() {
|
Attributes::Attributes(const std::string& empty) throw() {
|
||||||
_active = insert(Value(empty)).first;
|
insert(Value(empty));
|
||||||
}
|
}
|
||||||
Attributes& Attributes::operator<<(const value_type& v) throw() {
|
//! Attribute list with first attribute given.
|
||||||
_active = insert(v).first;
|
Attributes::Attributes(const std::string& key,
|
||||||
|
const std::string& value) throw() {
|
||||||
|
insert(Value(key, value));
|
||||||
|
}
|
||||||
|
//! Add a new key-value pair to the attribute list.
|
||||||
|
Attributes& Attributes::operator<<(const Attributes::Value& v) throw() {
|
||||||
|
insert(v);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
//! Add a new empty key to the attribute list.
|
||||||
Attributes& Attributes::operator<<(const std::string& key) throw() {
|
Attributes& Attributes::operator<<(const std::string& key) throw() {
|
||||||
_active = insert(Value(key)).first;
|
insert(Value(key));
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Attributes& Attributes::operator=(const std::string& value) throw() {
|
|
||||||
if (_active==end()) return *this;
|
|
||||||
_active->second = value;
|
|
||||||
_active = end();
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
//! Nodes must be given a name.
|
||||||
|
/*! Unnamed nodes are not allowed, therefore there's no default
|
||||||
|
constructor. */
|
||||||
Node::Node(std::string name) throw():
|
Node::Node(std::string name) throw():
|
||||||
_name(name), _parent(0) {
|
_name(name), _parent(0) {
|
||||||
}
|
}
|
||||||
@@ -210,6 +230,7 @@ namespace xml {
|
|||||||
Node::~Node() throw() {
|
Node::~Node() throw() {
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
//! Clones But clears the parent.
|
||||||
std::auto_ptr<Node> Node::clone() const throw() {
|
std::auto_ptr<Node> Node::clone() const throw() {
|
||||||
std::auto_ptr<Node> res(new Node(*this));
|
std::auto_ptr<Node> res(new Node(*this));
|
||||||
res->_parent = 0;
|
res->_parent = 0;
|
||||||
|
Reference in New Issue
Block a user