22 #define TIXML_USE_TICPP 30 using namespace ticpp;
44 if (0 != firstAttribute) {
86 SetTiXmlPointer(
new TiXmlAttribute(name, value));
122 return m_tiXmlPointer->Name();
129 if (0 == attribute) {
130 if (throwIfNoAttribute) {
138 attribute->m_spawnedWrappers.push_back(temp);
147 if (0 == attribute) {
148 if (throwIfNoAttribute) {
156 attribute->m_spawnedWrappers.push_back(temp);
176 SetImpRC(newPointer);
183 bool rememberSpawnedWrapper)
const {
184 if (0 == tiXmlNode) {
195 case TiXmlNode::DOCUMENT:
196 temp =
new Document(tiXmlNode->ToDocument());
199 case TiXmlNode::ELEMENT:
200 temp =
new Element(tiXmlNode->ToElement());
203 case TiXmlNode::COMMENT:
204 temp =
new Comment(tiXmlNode->ToComment());
207 case TiXmlNode::TEXT:
208 temp =
new Text(tiXmlNode->ToText());
211 case TiXmlNode::DECLARATION:
212 temp =
new Declaration(tiXmlNode->ToDeclaration());
215 case TiXmlNode::STYLESHEETREFERENCE:
216 temp =
new StylesheetReference(tiXmlNode->ToStylesheetReference());
223 if (rememberSpawnedWrapper) {
224 tiXmlNode->m_spawnedWrappers.push_back(temp);
237 if ((0 == parent) && throwIfNoParent) {
249 return FirstChild(value.c_str(), throwIfNoChildren);
255 if (0 == strlen(value)) {
261 if ((0 == childNode) && throwIfNoChildren) {
262 TICPPTHROW(
"Child with the value of \"" << value <<
"\" not found");
273 return LastChild(value.c_str(), throwIfNoChildren);
279 if (0 == strlen(value)) {
285 if ((0 == childNode) && throwIfNoChildren) {
286 TICPPTHROW(
"Child with the value of \"" << value <<
"\" not found");
308 pointer = GetTiXmlPointer()->IterateChildren(value, 0);
311 GetTiXmlPointer()->IterateChildren(value, previous->GetTiXmlPointer());
319 TICPPTHROW(
"Node is a Document and can't be inserted");
334 TICPPTHROW(
"Node is a Document and can't be linked");
349 TICPPTHROW(
"Node is a Document and can't be inserted");
367 TICPPTHROW(
"Node is a Document and can't be inserted");
385 TICPPTHROW(
"Node is a Document and can't be inserted");
403 TICPPTHROW(
"Node to remove (" << removeThis->Value()
404 <<
") is not a child of this Node (" 415 bool throwIfNoSiblings)
const {
416 return PreviousSibling(value.c_str(), throwIfNoSiblings);
422 if (0 == strlen(value)) {
428 if ((0 == sibling) && throwIfNoSiblings) {
429 TICPPTHROW(
"No Siblings found with value, '" << value
430 <<
"', Prior to this Node (" 443 return NextSibling(value.c_str(), throwIfNoSiblings);
449 if (0 == strlen(value)) {
455 if ((0 == sibling) && throwIfNoSiblings) {
456 TICPPTHROW(
"No Siblings found with value, '" << value <<
"', After this Node (" 469 bool throwIfNoSiblings)
const {
470 return NextSiblingElement(value.c_str(), throwIfNoSiblings);
474 bool throwIfNoSiblings)
const {
477 if (0 == strlen(value)) {
484 if (throwIfNoSiblings) {
485 TICPPTHROW(
"No Element Siblings found with value, '" 487 <<
"', After this Node (" 496 sibling->m_spawnedWrappers.push_back(temp);
506 bool throwIfNoChildren)
const {
507 return FirstChildElement(value.c_str(), throwIfNoChildren);
513 if (0 == strlen(value)) {
520 if (throwIfNoChildren) {
522 <<
") does NOT contain a child with the value of '" 531 element->m_spawnedWrappers.push_back(temp);
542 if (throwIfNoDocument) {
543 TICPPTHROW(
"This node (" << Value() <<
") is not linked under a document")
550 doc->m_spawnedWrappers.push_back(temp);
561 TICPPTHROW(
"This node (" << Value() <<
") is not a Document")
565 doc->m_spawnedWrappers.push_back(temp);
574 TICPPTHROW(
"This node (" << Value() <<
") is not a Element")
578 doc->m_spawnedWrappers.push_back(temp);
587 TICPPTHROW(
"This node (" << Value() <<
") is not a Comment")
591 doc->m_spawnedWrappers.push_back(temp);
600 TICPPTHROW(
"This node (" << Value() <<
") is not a Text")
604 doc->m_spawnedWrappers.push_back(temp);
613 TICPPTHROW(
"This node (" << Value() <<
") is not a Declaration")
617 doc->m_spawnedWrappers.push_back(temp);
626 TICPPTHROW(
"This node (" << Value() <<
") is not a StylesheetReference")
630 doc->m_spawnedWrappers.push_back(temp);
642 std::unique_ptr< Node > temp(NodeFactory(node,
false,
false));
645 temp->m_impRC->InitRef();
667 m_tiXmlPointer->SetValue(comment);
706 if (!m_tiXmlPointer->LoadFile(encoding)) {
707 TICPPTHROW(
"Couldn't load " << m_tiXmlPointer->Value());
712 if (!m_tiXmlPointer->SaveFile()) {
713 TICPPTHROW(
"Couldn't save " << m_tiXmlPointer->Value());
718 if (!m_tiXmlPointer->LoadFile(filename.c_str(), encoding)) {
724 if (!m_tiXmlPointer->LoadFile(filename, encoding)) {
730 if (!m_tiXmlPointer->SaveFile(filename.c_str())) {
736 bool throwIfParseError,
738 m_tiXmlPointer->Parse(xml.c_str(), 0, encoding);
740 if (throwIfParseError && m_tiXmlPointer->Error()) {
749 "DefaultValueCausedByCreatingAnElementWithNoParameters")) {
770 if ((0 == attribute) && throwIfNoAttributes) {
771 TICPPTHROW(
"This Element (" << Value() <<
") has no attributes")
774 if (0 == attribute) {
775 if (throwIfNoAttributes) {
776 TICPPTHROW(
"Element (" << Value() <<
") has no attributes")
783 attribute->m_spawnedWrappers.push_back(temp);
792 if ((0 == attribute) && throwIfNoAttributes) {
793 TICPPTHROW(
"This Element (" << Value() <<
") has no attributes")
796 if (0 == attribute) {
797 if (throwIfNoAttributes) {
798 TICPPTHROW(
"Element (" << Value() <<
") has no attributes")
805 attribute->m_spawnedWrappers.push_back(temp);
814 if (!GetAttributeImp(name, &value)) {
822 return GetAttributeOrDefault(name, std::string());
827 return (0 != m_tiXmlPointer->Attribute(name.c_str()));
832 m_tiXmlPointer->RemoveAttribute(name.c_str());
839 const char* retVal = m_tiXmlPointer->Attribute(name.c_str());
854 const char* retVal = m_tiXmlPointer->GetText();
876 const std::string& encoding,
877 const std::string& standalone)
888 return m_tiXmlPointer->Standalone();
903 const std::string& href)
926 m_tiRC =
new TiCppRCImp(
this);
929 void TiCppRC::DeleteSpawnedWrappers() {
930 std::vector< Base* >::reverse_iterator wrapper;
932 for (wrapper = m_spawnedWrappers.rbegin(); wrapper != m_spawnedWrappers.rend();
937 m_spawnedWrappers.clear();
940 TiCppRC::~TiCppRC() {
941 DeleteSpawnedWrappers();
944 this->m_tiRC->Nullify();
947 this->m_tiRC->DecRef();
952 TiCppRCImp::TiCppRCImp(TiCppRC* tiCppRC)
954 , m_tiCppRC(tiCppRC) {}
956 void TiCppRCImp::IncRef() { m_count++; }
958 void TiCppRCImp::DecRef() {
967 void TiCppRCImp::InitRef() { m_count = 1; }
969 void TiCppRCImp::Nullify() { m_tiCppRC = 0; }
971 TiCppRC* TiCppRCImp::Get() {
return m_tiCppRC; }
973 bool TiCppRCImp::IsNull() {
return 0 == m_tiCppRC; }
Wrapper around TiXmlNode.
std::unique_ptr< Node > Clone() const
Create an exact duplicate of this node and return it.
void RemoveChild(Node *removeThis)
Delete a child of this node.
TiXmlNode * FirstChild(const char *_value)
TiXmlNode * LastChild(const char *_value)
The last child of this node matching 'value'. Will be null if there are no children.
Document(TiXmlDocument *document)
Constructor.
An attribute is a name-value pair.
void operator=(const Attribute ©)
Node * FirstChild(const char *value, bool throwIfNoChildren=true) const
If you call the Accept() method, it requires being passed a TiXmlVisitor class to handle callbacks...
Node * PreviousSibling(const char *value, bool throwIfNoSiblings=true) const
Node * PreviousSibling(bool throwIfNoSiblings=true) const
Navigate to a sibling node.
void Clear()
Clear all Nodes below this.
TiXmlElement * NextSiblingElement()
Wrapper around TiXmlElement.
void IteratePrevious(const std::string &, Attribute **previous) const
TiXmlNode * PreviousSibling(const char *_prev)
TiXmlNode * LastChild()
The last child of this node. Will be null if there are no children.
virtual TiXmlElement * ToElement()
Cast to a more defined type. Will return null if not of the requested type.
virtual bool Visit(const TiXmlUnknown &)
Visit an unknow node.
Comment * ToComment() const
Pointer conversion - replaces TiXmlNode::ToComment.
Document * ToDocument() const
Pointer conversion - replaces TiXmlNode::ToDocument.
Text(const std::string &value)
Constructor.
StylesheetReference(const std::string &type, const std::string &href)
Constructor.
virtual bool Visit(const TiXmlDeclaration &declaration)
Visit a declaration.
#define TICPPTHROW(message)
This allows you to stream your exceptions in.
virtual bool Visit(const TiXmlText &text)
Visit a text node.
virtual bool Visit(const Text &)
Visit a text node.
StylesheetReference()
Default Constructor.
bool Accept(TiXmlVisitor *visitor) const
Accept a hierchical visit the nodes in the TinyXML DOM.
Declaration(const std::string &version, const std::string &encoding, const std::string &standalone)
Constructor.
Node * NextSibling(const std::string &value, bool throwIfNoSiblings=true) const
Navigate to a sibling node with the given value.
bool NoChildren() const
Check if this node has no children.
Attribute * Previous(bool throwIfNoAttribute=true) const
Get the previous sibling attribute in the DOM.
virtual bool Visit(const Declaration &)
Visit a declaration.
void ValidatePointer() const
TiXmlElement * FirstChildElement(const char *_value)
void SetTiXmlPointer(TiXmlAttribute *newPointer)
TiXmlNode * ReplaceChild(TiXmlNode *replaceThis, const TiXmlNode &withThis)
Replace a child of this node.
const std::string & ValueStr() const
Return Value() as a std::string.
TiXmlNode * NextSibling()
void SaveFile(const std::string &filename) const
Save a file using the given filename.
TiXmlAttribute()
Construct an empty attribute.
Declaration()
Default Constructor.
Document()
Default Constructor.
TiXmlElement * NextSiblingElement(const char *_next)
bool GetTextImp(std::string *value) const
TiXmlElement * FirstChildElement()
Node * LastChild(bool throwIfNoChildren=true) const
The last child of this node.
std::string GetAttribute(const std::string &name) const
Gets an attribute of name from an element.
virtual void Print(FILE *cfile, int depth) const
All TinyXml classes can print themselves to a filestream or the string class (TiXmlString in non-STL ...
Attribute(const std::string &name, const std::string &value)
Construct an attribute with name and value.
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
Add a new node related to this.
TiXmlNode * InsertBeforeChild(TiXmlNode *beforeThis, const TiXmlNode &addThis)
Add a new node related to this.
void LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
Wrapper around TiXmlText.
const std::string & ValueStr() const
Return the value of this attribute.
StylesheetReference * ToStylesheetReference() const
Pointer conversion - replaces TiXmlNode::ToStylesheetReference.
TiXmlNode * PreviousSibling()
Element(const std::string &value)
Default Constructor.
std::string Standalone() const
StandAlone.
Node * FirstChild(const std::string &value, bool throwIfNoChildren=true) const
The first child of this node with the matching value.
virtual TiXmlStylesheetReference * ToStylesheetReference()
Cast to a more defined type. Will return null if not of the requested type.
void LoadFile(const std::string &filename, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the given filename.
TiXmlNode * LinkEndChild(TiXmlNode *addThis)
Add a new node related to this.
void SaveFile() const
Save a file using the current document value.
Document(const std::string &documentName)
Constructor.
Text(TiXmlText *text)
Constructor.
virtual TiXmlNode * GetTiXmlPointer() const =0
A stylesheet reference looks like this:
Element * FirstChildElement(const std::string &value, bool throwIfNoChildren=true) const
The first child element of this node with the matching value.
virtual bool Accept(TiXmlVisitor *visitor) const =0
Accept a hierchical visit the nodes in the TinyXML DOM.
In correct XML the declaration is the first entry in the file.
Element * ToElement() const
Pointer conversion - replaces TiXmlNode::ToElement.
std::string Value() const
Get the value of this node.
std::string Href() const
Href.
void Parse(const std::string &xml, bool throwIfParseError=true, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Parse the given xml data.
Element()
Default Constructor.
Any tag that tinyXml doesn't recognize is saved as an unknown.
void IterateNext(const std::string &, Attribute **next) const
TiXmlNode * NextSibling(const char *_next)
virtual bool VisitEnter(const Element &, const Attribute *)
Visit an element.
int Type() const
Query the type (as TiXmlNode::NodeType ) of this node.
virtual bool VisitExit(const Element &)
Visit an element.
Node * ReplaceChild(Node *replaceThis, Node &withThis)
Replace a child of this node.
bool GetAttributeImp(const std::string &name, std::string *value) const
const char * what() const
Override std::exception::what() to return m_details.
void RemoveAttribute(const std::string &name)
Removes attribute from element.
void IncRef()
Increment Reference Count.
Node * IterateChildren(Node *previous) const
An alternate way to walk the children of a node.
This is a ticpp exception class.
Attribute * FirstAttribute(bool throwIfNoAttributes=true) const
Access the first attribute in this element.
Wrapper around TiXmlDeclaration.
friend class TiXmlElement
Wrapper around TiXmlBase.
Always the top level node.
Node * IterateChildren(const std::string &value, Node *previous) const
This flavor of IterateChildren searches for children with a particular value.
virtual bool Visit(const Comment &)
Visit a comment node.
std::string Type() const
Type.
TiXmlNode * InsertAfterChild(TiXmlNode *afterThis, const TiXmlNode &addThis)
Add a new node related to this.
Node * NextSibling(const char *value, bool throwIfNoSiblings=true) const
virtual TiXmlDeclaration * ToDeclaration()
Cast to a more defined type. Will return null if not of the requested type.
Node * LastChild(const std::string &value, bool throwIfNoChildren=true) const
The last child of this node with the matching value.
std::string GetAttributeOrDefault(const std::string &name, const std::string &defaultValue) const
Gets an attribute of name from an element, if it doesn't exist it will return the defaultValue...
virtual TiXmlComment * ToComment()
Cast to a more defined type. Will return null if not of the requested type.
std::string Name() const
Get the value of this attribute.
StylesheetReference(TiXmlStylesheetReference *stylesheetReference)
Constructor.
Node * Parent(bool throwIfNoParent=true) const
The Parent of this Node.
virtual bool VisitExit(const TiXmlDocument &doc)
Visit a document.
std::string Encoding() const
Encoding.
TiXmlNode * Parent()
One step up the DOM.
int Type() const
Query the type (as an enumerated value, above) of this node.
Element(TiXmlElement *element)
Constructor.
TiXmlAttribute * m_tiXmlPointer
Element * NextSiblingElement(bool throwIfNoSiblings=true) const
Navigate to a sibling element.
Wrapper around TiXmlVisitor.
Element * FirstChildElement(const char *value, bool throwIfNoChildren=true) const
TiXmlNode * IterateChildren(const TiXmlNode *previous)
TiXmlAttribute * Previous()
The parent class for everything in the Document Object Model.
Node * PreviousSibling(const std::string &value, bool throwIfNoSiblings=true) const
Navigate to a sibling node with the given value.
bool NoChildren() const
Returns true if this node has no children.
std::string Version() const
Version.
void InitRef()
Set Reference Count to 1 - dangerous! - Use only if you are sure of the consequences.
virtual bool Visit(const TiXmlComment &comment)
Visit a comment node.
Node * NextSibling(bool throwIfNoSiblings=true) const
Navigate to a sibling node.
Node * InsertEndChild(Node &addThis)
Adds a child past the LastChild.
void LoadFile(const char *filename, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the given filename.
virtual bool Visit(const TiXmlStylesheetReference &stylesheet)
Visit a stylesheet reference.
virtual void Print(FILE *file, int depth) const
All TinyXml classes can print themselves to a filestream.
Text * ToText() const
Pointer conversion - replaces TiXmlNode::ToText.
Node * InsertAfterChild(Node *afterThis, Node &addThis)
Adds a child after the specified child.
Document(const char *documentName)
Constructor.
Wrapper around TiXmlDocument.
std::string Value() const
Get the value of this attribute.
Attribute * Next(bool throwIfNoAttribute=true) const
Get the next sibling attribute in the DOM.
TiCppRCImp * m_impRC
Holds status of internal TiXmlPointer - use this to determine if object has been deleted already...
Node * LastChild(const char *value, bool throwIfNoChildren=true) const
bool RemoveChild(TiXmlNode *removeThis)
Delete a child of this node.
virtual bool VisitExit(const TiXmlElement &element)
Visit an element.
Attribute()
Construct an empty attribute.
Node * LinkEndChild(Node *childNode)
Adds a child past the LastChild.
virtual bool VisitEnter(const TiXmlDocument &doc)
Visit a document.
Element * FirstChildElement(bool throwIfNoChildren=true) const
The first child element of this node.
void Clear()
Delete all the children of this node. Does not affect 'this'.
void DecRef()
Decrement Reference Count.
Node * FirstChild(bool throwIfNoChildren=true) const
The first child of this node.
Wrapper around TiXmlStylesheetReference.
Attribute(const Attribute ©)
Element(const char *value)
Default Constructor.
Node * InsertBeforeChild(Node *beforeThis, Node &addThis)
Adds a child before the specified child.
Attribute * LastAttribute(bool throwIfNoAttributes=true) const
Access the last attribute in this element.
Exception(const std::string &details)
Construct an exception with a message.
virtual bool Visit(const TiXmlUnknown &)
Visit an unknow node.
virtual bool VisitEnter(const Document &)
Visit a document.
virtual TiXmlNode * Clone() const =0
Create an exact duplicate of this node and return it.
Attribute(TiXmlAttribute *attribute)
Element * NextSiblingElement(const std::string &value, bool throwIfNoSiblings=true) const
Navigate to a sibling element with the given value.
Declaration(TiXmlDeclaration *declaration)
Constructor.
Node * NodeFactory(TiXmlNode *tiXmlNode, bool throwIfNull=true, bool rememberSpawnedWrapper=true) const
Element * NextSiblingElement(const char *value, bool throwIfNoSiblings=true) const
Declaration * ToDeclaration() const
Pointer conversion - replaces TiXmlNode::ToDeclaration.
virtual TiXmlText * ToText()
Cast to a more defined type. Will return null if not of the requested type.
Wrapper around TiXmlAttribute.
virtual bool VisitEnter(const TiXmlElement &element, const TiXmlAttribute *firstAttribute)
Visit an element.
Document * GetDocument(bool throwIfNoDocument=true) const
Return a pointer to the Document this node lives in.
The element is a container class.
bool HasAttribute(const std::string &name) const
Returns true, if attribute exists.
virtual bool Visit(const StylesheetReference &)
Visit a stylesheet reference.