1 #ifndef _ifmsxmldocument_
\r
2 #define _ifmsxmldocument_
\r
4 #include <Poco/DOM/Document.h>
\r
5 #include <Poco/DOM/Element.h>
\r
6 #include <Poco/DOM/Text.h>
\r
7 #include <Poco/DOM/CDATASection.h>
\r
8 #include <Poco/DOM/DOMParser.h>
\r
9 #include <Poco/DOM/DOMWriter.h>
\r
10 #include <Poco/XML/XMLWriter.h>
\r
11 #include <Poco/AutoPtr.h>
\r
13 #include "stringfunctions.h"
\r
22 \brief Interface for objects that represent an XML document
\r
24 class IFMSXMLDocument
\r
29 \brief Returns xml document represented by this object
\r
31 \return xml document
\r
33 virtual std::string GetXML()=0;
\r
36 \brief Parses an xml document into this object
\r
38 \return true if the document was parsed successfully, false if it was not
\r
40 virtual const bool ParseXML(const std::string &xml)=0;
\r
44 Poco doesn't like CDATA with whitespace outside the tags
\r
45 This will remove the whitespace from around CDATA tags
\r
47 virtual const std::string FixCDATA(const std::string &xmlstr)
\r
49 std::string rstring=xmlstr;
\r
50 std::string::size_type beg1=std::string::npos;
\r
51 std::string::size_type end1=rstring.find("<![CDATA[");
\r
52 std::string::size_type beg2=std::string::npos;
\r
53 std::string::size_type end2=std::string::npos;
\r
55 while(end1!=std::string::npos)
\r
57 beg1=rstring.rfind(">",end1);
\r
60 rstring.erase(beg1+1,end1-(beg1+1));
\r
63 beg2=rstring.find("]]>",end1);
\r
64 if(beg2!=std::string::npos)
\r
66 end2=rstring.find("<",beg2);
\r
67 if(end2!=std::string::npos)
\r
69 rstring.erase(beg2+3,end2-(beg2+3));
\r
73 end1=rstring.find("<![CDATA[",end1+1);
\r
78 \brief Creates and returns an element with a boolean value
\r
80 virtual Poco::AutoPtr<Poco::XML::Element> XMLCreateBooleanElement(Poco::AutoPtr<Poco::XML::Document> doc, const std::string &name, const bool value)
\r
84 Poco::AutoPtr<Poco::XML::Text> txt=doc->createTextNode(value ? "true" : "false");
\r
85 Poco::AutoPtr<Poco::XML::Element> el=doc->createElement(name);
\r
86 el->appendChild(txt);
\r
96 \brief Creates and returns an element with a CDATA value
\r
98 virtual Poco::AutoPtr<Poco::XML::Element> XMLCreateCDATAElement(Poco::AutoPtr<Poco::XML::Document> doc, const std::string &name, const std::string &data)
\r
102 // Poco XML won't break up CDATA sections correctly when assigned a string with the
\r
103 // end tag is present. However, it will parse it correctly, so we will manually break the
\r
104 // CDATA into separate parts
\r
105 Poco::AutoPtr<Poco::XML::CDATASection> sec=doc->createCDATASection(StringFunctions::Replace(data,"]]>","]]]><![CDATA[]>"));
\r
106 Poco::AutoPtr<Poco::XML::Element> el=doc->createElement(name);
\r
107 el->appendChild(sec);
\r
117 \brief Creates and returns a text element
\r
119 virtual Poco::AutoPtr<Poco::XML::Element> XMLCreateTextElement(Poco::AutoPtr<Poco::XML::Document> doc, const std::string &name, const std::string &data)
\r
123 Poco::AutoPtr<Poco::XML::Text> txt=doc->createTextNode(data);
\r
124 Poco::AutoPtr<Poco::XML::Element> el=doc->createElement(name);
\r
125 el->appendChild(txt);
\r
134 virtual Poco::AutoPtr<Poco::XML::Element> XMLCreateTextElement(Poco::AutoPtr<Poco::XML::Document> doc, const std::string &name, const long data)
\r
138 std::string datastr;
\r
139 StringFunctions::Convert(data,datastr);
\r
140 return XMLCreateTextElement(doc,name,datastr);
\r
148 virtual const bool XMLGetBooleanElement(Poco::XML::Element *parent, const std::string &name)
\r
150 Poco::XML::Element *el=XMLGetFirstChild(parent,name);
\r
151 if(el && el->firstChild())
\r
153 if(el->firstChild()->getNodeValue()=="true")
\r
165 virtual Poco::XML::Element *XMLGetFirstChild(Poco::XML::Node *parent, const std::string &name)
\r
169 Poco::XML::Node *child=parent->firstChild();
\r
170 while(child && child->nodeName()!=name)
\r
172 child=child->nextSibling();
\r
174 //return child.cast<Poco::XML::Element>();
\r
175 return static_cast<Poco::XML::Element *>(child);
\r
183 virtual Poco::XML::Element *XMLGetNextSibling(Poco::XML::Node *node, const std::string &name)
\r
187 Poco::XML::Node *next=node->nextSibling();
\r
188 while(next && next->nodeName()!=name)
\r
190 next=next->nextSibling();
\r
192 //return next.cast<Poco::XML::Element>();
\r
193 return static_cast<Poco::XML::Element *>(next);
\r
201 const std::string SanitizeSingleString(const std::string &text)
\r
203 std::string returntext=text;
\r
204 // remove bogus chars from text string
\r
205 for(char i=1; i<32; i++)
\r
207 returntext=StringFunctions::Replace(returntext,std::string(1,i),"");
\r
212 const std::string GenerateXML(Poco::AutoPtr<Poco::XML::Document> doc)
\r
214 std::ostringstream str;
\r
217 Poco::XML::DOMWriter dr;
\r
218 dr.setOptions(Poco::XML::XMLWriter::WRITE_XML_DECLARATION | Poco::XML::XMLWriter::PRETTY_PRINT);
\r
219 dr.setNewLine(Poco::XML::XMLWriter::NEWLINE_CRLF);
\r
220 dr.writeNode(str,doc);
\r
227 #endif // _ifmsxmldocument_
\r