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::XML::Element *XMLCreateBooleanElement(Poco::XML::Document *doc, const std::string &name, const bool value)
\r
84 Poco::XML::Text *txt=doc->createTextNode(value ? "true" : "false");
\r
85 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::XML::Element *XMLCreateCDATAElement(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::XML::CDATASection *sec=doc->createCDATASection(StringFunctions::Replace(data,"]]>","]]]><![CDATA[]>"));
\r
106 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::XML::Element *XMLCreateTextElement(Poco::XML::Document *doc, const std::string &name, const std::string &data)
\r
123 Poco::XML::Text *txt=doc->createTextNode(data);
\r
124 Poco::XML::Element *el=doc->createElement(name);
\r
125 el->appendChild(txt);
\r
134 virtual Poco::XML::Element *XMLCreateTextElement(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 static_cast<Poco::XML::Element *>(child);
\r
182 virtual Poco::XML::Element *XMLGetNextSibling(Poco::XML::Node *node, const std::string &name)
\r
186 Poco::XML::Node *next=node->nextSibling();
\r
187 while(next && next->nodeName()!=name)
\r
189 next=next->nextSibling();
\r
191 return static_cast<Poco::XML::Element *>(next);
\r
199 const std::string SanitizeSingleString(const std::string &text)
\r
201 std::string returntext=text;
\r
202 // remove bogus chars from text string
\r
203 for(char i=1; i<32; i++)
\r
205 returntext=StringFunctions::Replace(returntext,std::string(1,i),"");
\r
210 const std::string GenerateXML(Poco::XML::Document *doc)
\r
212 std::ostringstream str;
\r
215 Poco::XML::DOMWriter dr;
\r
216 dr.setOptions(Poco::XML::XMLWriter::WRITE_XML_DECLARATION | Poco::XML::XMLWriter::PRETTY_PRINT);
\r
217 dr.setNewLine(Poco::XML::XMLWriter::NEWLINE_CRLF);
\r
218 dr.writeNode(str,doc);
\r
225 #endif // _ifmsxmldocument_
\r