00001
00002
00003
00004 #ifndef xmlBase_Dom_h
00005 #define xmlBase_Dom_h
00006 #include <xercesc/util/XercesDefs.hpp>
00007
00008 XERCES_CPP_NAMESPACE_BEGIN
00009 class DOMElement;
00010 class DOMNode;
00011 class XMLLCPTranscoder;
00012
00013 class DOMDocument;
00014 XERCES_CPP_NAMESPACE_END
00015 #include <string>
00016 #include <iostream>
00017 #include <vector>
00018 #include <map>
00019
00020
00021
00022
00023
00024
00025
00026
00027 namespace xmlBase {
00029 class DomException : std::exception {
00030 public:
00031 DomException(const std::string& extraInfo = "") : std::exception(),
00032 m_name("DomException"), m_extra(extraInfo) {}
00033 virtual ~DomException() throw() {}
00034 virtual std::string getMsg() {
00035 std::string msg = m_name + ": " + m_extra;
00036 return msg;}
00037 virtual const char* what() {
00038 return m_extra.c_str();
00039 }
00040 protected:
00041 std::string m_name;
00042 private:
00043 std::string m_extra;
00044 };
00045
00046 class NullNode : public DomException {
00047 public:
00048 NullNode(const std::string& extraInfo = "") : DomException(extraInfo)
00049 {m_name = "NullNode";}
00050 virtual ~NullNode() throw() {}
00051 };
00052
00053 class WrongAttributeType : public DomException {
00054 public:
00055 WrongAttributeType(const std::string& extraInfo = "") :
00056 DomException(extraInfo)
00057 {
00058 m_name = "WrongAttributeType";
00059 }
00060 virtual ~WrongAttributeType() throw() {}
00061 };
00062
00063 class WrongNodeType : public DomException {
00064 public:
00065 WrongNodeType(const std::string& extraInfo = "") :
00066 DomException(extraInfo)
00067 {
00068 m_name = "WrongNodeType";
00069 }
00070 virtual ~WrongNodeType() throw() {}
00071 };
00072
00073
00074 using XERCES_CPP_NAMESPACE_QUALIFIER DOMElement;
00075 using XERCES_CPP_NAMESPACE_QUALIFIER DOMNode;
00076 using XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument;
00080 class Dom {
00081
00082 public:
00090 static DOMElement* findFirstChildByName(const DOMElement* parent,
00091 const char* const name);
00092 static DOMElement* findFirstChildByName(const DOMElement* parent,
00093 const std::string name);
00094
00096
00099 static DOMElement* getSiblingElement(const DOMNode* child);
00100
00102 static DOMElement* getFirstChildElement(const DOMNode* parent);
00103
00104 static DOMElement* getElementById(const DOMDocument* doc,
00105 const std::string& id);
00106
00109 static std::string getNodeName(const DOMNode* elt);
00110
00113 static std::string getTagName(const DOMElement* node);
00114
00115
00118 static bool checkTagName(const DOMElement* element,
00119 const std::string& tagName);
00120
00124 static void getChildrenByTagName(const DOMElement* parent,
00125 const std::string& tagName,
00126 std::vector<DOMElement*>& children,
00127 bool clear = true);
00128
00132 static void getDescendantsByTagName(const DOMElement* parent,
00133 const std::string& tagName,
00134 std::vector<DOMElement*>& children,
00135 bool clear = true);
00136
00139 static void getAttributeNodeMap(const DOMNode* elt,
00140 std::map<std::string, DOMNode*>& atts,
00141 bool clear = true);
00142
00145 static bool hasAttribute(const DOMNode* elt, const char* attName);
00146
00150 static std::string getAttribute(const DOMElement* elt,
00151 const char* attName);
00155 static std::string getAttribute(const DOMElement* elt,
00156 std::string attName);
00157
00161 static std::string getAttribute(const DOMNode* elt,
00162 const char* attName);
00166 static std::string getAttribute(const DOMNode* elt,
00167 std::string attName);
00168
00171 static double getDoubleAttribute(const DOMNode* elt, std::string attName);
00172
00178 static unsigned getDoublesAttribute(const DOMNode* elt,
00179 std::string attName,
00180 std::vector<double>& values,
00181 bool clear=true);
00182
00188 static unsigned getFloatsAttribute(const DOMNode* elt,
00189 std::string attName,
00190 std::vector<float>& values,
00191 bool clear=true);
00192
00195 static int getIntAttribute(const DOMNode* elt, std::string attName);
00196
00202 static unsigned getIntsAttribute(const DOMNode* elt,
00203 std::string attName,
00204 std::vector<int>& values,
00205 bool clear=true);
00206
00210 static std::string getText(const DOMNode* textNode);
00211
00215 static std::string getTextContent(const DOMElement* elt);
00216
00218
00219
00220
00222 static void addAttribute(DOMElement* elt, std::string name,
00223 double value);
00224
00226 static void addAttribute(DOMElement* elt, std::string name,
00227 int value);
00228
00230 static void addAttribute(DOMElement* elt, std::string name,
00231 unsigned int value);
00232
00234 static void addAttribute(DOMElement* elt, std::string name,
00235 std::string value);
00236
00238 static void addAttribute(DOMElement* elt, std::string name,
00239 const char * const value);
00240
00241
00242
00243
00246
00252 static void printElement(DOMNode* elt, std::ostream& out);
00253
00256
00262 static void prettyPrintElement(DOMNode* elt, std::ostream& out,
00263 std::string prefix);
00264
00267 static void prune(DOMElement* elt);
00268
00269
00270
00271
00272
00273
00274 private:
00276
00280
00281 static char* transToChar(const XMLCh* const str);
00282 static char* transToChar(const XMLCh* const str, unsigned int len);
00283
00284
00286
00287 static XMLCh* transToXMLCh(const char* const src);
00288
00289
00290 static unsigned int transBufSize;
00291 static char* transBuf;
00292 static unsigned int xmlchBufSize;
00293 static XMLCh* xmlchBuf;
00294 static XERCES_CPP_NAMESPACE_QUALIFIER XMLLCPTranscoder* transcoder;
00295 static int initTrans();
00296
00297 static XMLCh* xmlchStar;
00298
00299
00300 };
00301 }
00302 #endif