Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

xercesDomUtil.h

Go to the documentation of this file.
00001 #ifndef XERCESDOMUTIL_H 00002 #define XERCESDOMUTIL_H 00003 00004 #include <exception> 00005 #include <string> 00006 #include <vector> 00007 #include <xercesc/dom/DOM.hpp> 00008 #include <xercesc/util/XMLString.hpp> 00009 #include <sys/time.h> 00010 00011 #include "Exceptions.h" 00012 00013 BOREALIS_NAMESPACE_BEGIN; 00014 00015 #ifdef XERCES_CPP_NAMESPACE 00016 using namespace xercesc; 00017 #endif 00018 00019 //=============================================================================== 00020 00021 // Creates an XML file with the specified name, whose contents is the specified 00022 // subtree. 00023 void writeDomDocToFile(const DOMDocument & doc, string filename) 00024 throw (exception); 00025 00026 // Creates a DOMDocument from the specified XML file using the provided 00027 // implementation object. 00028 DOMDocument * readDomDocFromFile(DOMImplementationLS & impl, string filename) 00029 throw (exception); 00030 00031 // Uses the DOMImplementationRegistry. Will throw an exception rather than 00032 // returning NULL. 00033 DOMImplementationLS * getDomImplementationLs() 00034 throw (exception); 00035 00036 // Uses the DOMImplementationRegistry 00037 DOMImplementation * getDomImplementation() 00038 throw (exception); 00039 00040 DOMElement * createDomElement(DOMDocument & docOwner, string elementName) 00041 throw (exception); 00042 00043 // Returns a pointer to the one and only child element with the the specified 00044 // tag. Throws an exception if there wasn't exactly one such child. 00045 DOMElement * getDomChildByKeyTag(const DOMElement & parent, string childTagName) 00046 throw (exception); 00047 00048 //=============================================================================== 00049 00050 // Tries to parse the specified string as a bool ('T' or 'F'). Throws an 00051 // exception if it can't. 00052 bool parseXmlStringAsBool(const XMLCh * pXml) 00053 throw (exception); 00054 00055 // Parse the XML string as the specified kind of number. Throw an exception if it 00056 // can't be done... 00057 00058 int parseXmlStringAsInt(const XMLCh * pXml) 00059 throw (exception); 00060 00061 unsigned int parseXmlStringAsUInt(const XMLCh * pXml) 00062 throw (exception); 00063 00064 unsigned long parseXmlStringAsLong(const XMLCh * pXml) 00065 throw (exception); 00066 00067 unsigned long parseXmlStringAsULong(const XMLCh * pXml) 00068 throw (exception); 00069 00070 // Note: This won't necessarily discover overflow, because I had to implement 00071 // this on my own and didn't want to take the time to make a perfect bad-parse 00072 // detector. -cjc 00073 unsigned long long parseXmlStringAsULongLong(const XMLCh * pXml) 00074 throw (exception); 00075 00076 double parseXmlStringAsDouble(const XMLCh * pXml) 00077 throw (exception); 00078 00079 //=============================================================================== 00080 00081 // Creates an XML subtree, rooted at 'elem', representing the content of the 00082 // vector. 00083 void saveIntVectorToXml(const vector<int> & v, DOMDocument & doc, DOMElement & elem) 00084 throw (exception); 00085 00086 // Given an element populated by saveIntVectorToXml, this will append 'v' with 00087 // the saved vector's elements (in proper order). 00088 void loadIntVectorFromXml(vector<int> & v, DOMElement & elem) 00089 throw (exception); 00090 00091 //=============================================================================== 00092 00093 // Creates an XML subtree, rooted at 'elem', representing the content of the 00094 // timeval. 00095 void saveTimevalToXml(const timeval & tv, DOMDocument & doc, DOMElement & elem) 00096 throw (exception); 00097 00098 // Given an element populated by saveTimevalToXml, this will populate 'tv' with 00099 // the saved timeval's fields. 00100 void loadTimevalFromXml(timeval & tv, DOMElement & elem) 00101 throw (exception); 00102 00103 //=============================================================================== 00104 00105 // Numeric type promotion + normal overloading keeps us out of trouble here, so 00106 // we can avoid user-specified templates. Thus, the proper version of this 00107 // function will always be called for parameters like size_t and ssize_t. 00108 00109 void setDomAttribute(DOMElement & elem, string attributeName, bool newValue) 00110 throw (exception); 00111 00112 void setDomAttribute(DOMElement & elem, string attributeName, int newValue) 00113 throw (exception); 00114 00115 void setDomAttribute(DOMElement & elem, string attributeName, unsigned int newValue) 00116 throw (exception); 00117 00118 void setDomAttribute(DOMElement & elem, string attributeName, long newValue) 00119 throw (exception); 00120 00121 void setDomAttribute(DOMElement & elem, string attributeName, unsigned long newValue) 00122 throw (exception); 00123 00124 void setDomAttribute(DOMElement & elem, string attributeName, unsigned long long newValue) 00125 throw (exception); 00126 00127 // NOTE! This might produce a messed up XML file if 'newValue' has characters 00128 // that are XML control characters. If/when we need to handle this, we can must 00129 // an encoding scheme in this function and the corresponding 'getDomAttribute' 00130 // function. 00131 void setDomAttribute(DOMElement & elem, string attributeName, string newValue) 00132 throw (exception); 00133 00134 void setDomAttribute(DOMElement & elem, string attributeName, double newValue) 00135 throw (exception); 00136 00137 00138 00139 void getDomAttribute(const DOMElement & elem, string attributeName, bool & value) 00140 throw (exception); 00141 00142 void getDomAttribute(const DOMElement & elem, string attributeName, int & value) 00143 throw (exception); 00144 00145 void getDomAttribute(const DOMElement & elem, string attributeName, unsigned int & value) 00146 throw (exception); 00147 00148 void getDomAttribute(const DOMElement & elem, string attributeName, long & value) 00149 throw (exception); 00150 00151 void getDomAttribute(const DOMElement & elem, string attributeName, unsigned long & value) 00152 throw (exception); 00153 00154 void getDomAttribute(const DOMElement & elem, string attributeName, unsigned long long & value) 00155 throw (exception); 00156 00157 void getDomAttribute(const DOMElement & elem, string attributeName, string & newValue) 00158 throw (exception); 00159 00160 void getDomAttribute(const DOMElement & elem, string attributeName, double & newValue) 00161 throw (exception); 00162 00163 00164 // Returns true if the attribute with the specified name is defined in the 00165 // element, otherwise returns false. 00166 bool isDomAttributePresent(const DOMElement & elem, string attributeName) 00167 throw (exception); 00168 00169 00170 00171 00172 //******************************************************************************* 00173 // Alternative approach to Xerces stuff that Jon Salz from MIT wrote. Should 00174 // peacefully coexist with the stuff above, and have only partial overlap of 00175 // functionality... 00176 //******************************************************************************* 00177 00178 // Utility functions for XML parsing. (There's some overlap with xercesDomUtil.H.) 00179 00180 enum XmlAttrReq { 00181 ATTR_NOT_REQUIRED = 0, 00182 ATTR_REQUIRED = 1, 00183 ATTR_NON_EMPTY = 2 00184 }; 00185 00186 // Parse a whole XML file. (Differs from readDomDocFromFile in that 00187 // you don't need to create your own DOMImplementationLS.) 00188 ptr<DOMDocument> parseXmlFile(string filename) throw (AuroraBadXmlException); 00189 00190 // Parse a string as an XML document. 00191 ptr<DOMDocument> parseXmlString(string data) throw (AuroraBadXmlException); 00192 00193 // Parse data as an XML document. 00194 ptr<DOMDocument> parseXmlString(const void *data, unsigned int length) throw (AuroraBadXmlException); 00195 00196 // Return an attribute of an elemnt. If req is ATTR_REQUIRED and the 00197 // attribute is not specified, or if req is ATTR_NON_EMPTY and the the 00198 // attribute is not specified or empty (""), throws an exception. 00199 string xmlAttribute(const DOMElement *element, string name, 00200 XmlAttrReq req = ATTR_NOT_REQUIRED) throw(AuroraBadEntityException); 00201 00202 // Return an attribute of an element, or def if the attribute is not 00203 // present. 00204 string xmlAttribute(const DOMElement *element, string name, string def) throw (AuroraBadEntityException); 00205 00206 // Throw an exception unless the given tag has a particular name. 00207 // (Basically an assert.) 00208 void xmlExpectTag(const DOMElement *element, string name) throw (AuroraBadEntityException); 00209 00210 // Reads an attribute value into the "value" argument. Throws an 00211 // exception if (a) a value is specified but has an invalid format, or 00212 // (b) req is ATTR_REQUIRED but the attribute value is not specified. 00213 // 00214 // You can specify an optional value by setting value before calling 00215 // this method. 00216 template <class T> 00217 inline bool xmlTypedAttribute(const DOMElement *element, string name, T& value, 00218 XmlAttrReq req = ATTR_NOT_REQUIRED) throw (AuroraBadEntityException) 00219 { 00220 XMLCh xname[64]; 00221 XMLString::transcode(name.c_str(), xname, sizeof xname - 1); 00222 00223 const DOMAttr *attr = element->getAttributeNode(xname); 00224 if (!attr) { 00225 if (req != ATTR_NOT_REQUIRED) { 00226 Throw(AuroraBadEntityException, 00227 "<" + to_string(element->getTagName()) + "> lacks " 00228 "required attribute \"" + name + "\""); 00229 } 00230 return false; 00231 } 00232 00233 // It's provided; coerce it 00234 string val = to_string(attr->getValue()); 00235 00236 istringstream i(val); 00237 if (i >> value) 00238 return true; 00239 00240 Throw(AuroraBadEntityException, "Invalid format for <" + 00241 to_string(element->getTagName()) + " " + name + "=\"" + val + "\">"); 00242 } 00243 00244 // Escapes the &<>"' characters in a string. 00245 string xmlEscape(string in); 00246 00247 // Returns the only tag within parent (or zero if none). Throws an exception 00248 // if there more than one tag, or if there are no tags and required is true. 00249 DOMElement* xmlOnlyTag(const DOMElement* parent, bool required = true) 00250 throw (AuroraException); 00251 00252 // Returns the only tag with a particular name within parent (or zero 00253 // if none). Throws an exception if there more than one tag, or if 00254 // there are no tags and required is true. 00255 DOMElement* xmlOnlyTag(const DOMElement* parent, string tag, bool required = true) 00256 throw (AuroraException); 00257 00258 // Returns immediate subnodes with a given tag. Clears children first. 00259 void xmlChildElements(vector<DOMElement*>& children, const DOMElement* parent, string tag = string()); 00260 00261 // Serialize a DOM node as text. 00262 string xmlSerialize(const DOMNode *node); 00263 00264 BOREALIS_NAMESPACE_END; 00265 00266 #endif

Generated on Fri Nov 12 15:15:22 2004 for Borealis by doxygen 1.3.8