Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

XmlDoc.h

Go to the documentation of this file.
00001 #ifndef Impala_Util_XmlDoc_h
00002 #define Impala_Util_XmlDoc_h
00003 
00004 #include "Basis/ILog.h"
00005 
00006 #include <xercesc/util/XMLString.hpp>
00007 #include <xercesc/dom/DOM.hpp>
00008 
00009 namespace Impala
00010 {
00011 namespace Util
00012 {
00013 
00014 
00018 class XmlDoc
00019 {
00020 public:
00021 
00022     typedef XERCES_CPP_NAMESPACE_QUALIFIER XMLString XMLString;
00023     typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument DOMDocument;
00024     typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMNode DOMNode;
00025     typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMNodeList DOMNodeList;
00026     typedef XERCES_CPP_NAMESPACE_QUALIFIER DOMNamedNodeMap DOMNamedNodeMap;
00027 
00028     XmlDoc()
00029     {
00030     }
00031 
00032     virtual
00033     ~XmlDoc()
00034     {
00035     }
00036 
00037     static String
00038     XMLCh2String(const XMLCh* xs)
00039     {
00040         char* c = XMLString::transcode(xs);
00041         String res(c);
00042         XMLString::release(&c);
00043         return res;
00044     }
00045 
00046     // DOMDocument based
00047 
00048     // recursive search
00049     static DOMNodeList*
00050     GetElementsByName(const DOMDocument* const doc, const char* const tagName)
00051     {
00052         XMLCh *xTagName = XMLString::transcode(tagName);
00053         DOMNodeList* elements = doc->getElementsByTagName(xTagName);
00054         XMLString::release(&xTagName);
00055         return elements;
00056     }
00057 
00058     // DOMNode based
00059 
00060     static String
00061     GetNodeName(const DOMNode* const node)
00062     {
00063         return XMLCh2String(node->getNodeName());
00064     }
00065 
00066     static String
00067     GetNodeValue(const DOMNode* const node)
00068     {
00069         return XMLCh2String(node->getNodeValue());
00070     }
00071 
00072     // return val from node containing "<tag> val </tag>"
00073     static String
00074     GetElementValue(const DOMNode* const node)
00075     {
00076         DOMNode* child = node->getFirstChild();
00077         if (child == 0)
00078             return "";
00079         return XMLCh2String(child->getNodeValue());;
00080     }
00081 
00082     static DOMNode*
00083     GetChildNode(const DOMNode* const node, const char* const tagName,
00084                  bool required)
00085     {
00086         DOMNodeList* children = node->getChildNodes();
00087         XMLCh* xTagName = XMLString::transcode(tagName);
00088         DOMNode* result = 0;
00089         for (XMLSize_t i=0 ; i<children->getLength() ; i++)
00090         {
00091             DOMNode* child = children->item(i);
00092             const XMLCh *xChildName = child->getNodeName();
00093             if (XMLString::equals(child->getNodeName(), xTagName))
00094             {
00095                 result = child;
00096                 break;
00097             }
00098         }
00099         XMLString::release(&xTagName);
00100         if ((result == 0) && required)
00101             ILOG_INFO("No child node " + String(tagName));
00102         return result;
00103     }
00104 
00105     static DOMNode*
00106     GetChildNodeAfter(const DOMNode* const parent, const DOMNode* sibling,
00107                       const char* const tagName, bool required)
00108     {
00109         DOMNodeList* children = parent->getChildNodes();
00110         XMLCh* xTagName = XMLString::transcode(tagName);
00111         DOMNode* result = 0;
00112         bool foundSibling = false;
00113         for (XMLSize_t i=0 ; i<children->getLength() ; i++)
00114         {
00115             DOMNode* child = children->item(i);
00116             if (child == sibling)
00117             {
00118                 foundSibling = true;
00119                 continue;
00120             }
00121             const XMLCh *xChildName = child->getNodeName();
00122             if (XMLString::equals(child->getNodeName(), xTagName) && foundSibling)
00123             {
00124                 result = child;
00125                 break;
00126             }
00127         }
00128         XMLString::release(&xTagName);
00129         if ((result == 0) && required)
00130             ILOG_INFO("No child node " + String(tagName));
00131         return result;
00132     }
00133 
00134     static std::vector<DOMNode*>
00135     GetChildNodes(const DOMNode* const node, const char* const tagName)
00136     {
00137         DOMNodeList* children = node->getChildNodes();
00138         XMLCh* xTagName = XMLString::transcode(tagName);
00139         std::vector<DOMNode*> result;
00140         for (XMLSize_t i=0 ; i<children->getLength() ; i++)
00141         {
00142             DOMNode* child = children->item(i);
00143             const XMLCh *xChildName = child->getNodeName();
00144             if (XMLString::equals(child->getNodeName(), xTagName))
00145                 result.push_back(child);
00146         }
00147         XMLString::release(&xTagName);
00148         return result;
00149     }
00150 
00151     static void
00152     DumpChildNames(const DOMNode* const node)
00153     {
00154         std::cout << "Dumping children of " << GetNodeName(node) << std::endl;
00155         DOMNodeList* children = node->getChildNodes();
00156         for (XMLSize_t i=0 ; i<children->getLength() ; i++)
00157         {
00158             DOMNode* child = children->item(i);
00159             std::cout << "  " << i << " = " << GetNodeName(child) << std::endl;
00160         }
00161     }
00162 
00163     static String
00164     GetAttributeValue(const DOMNode* const node, const char* const attributeName)
00165     {
00166         if (!node->hasAttributes())
00167         {
00168             ILOG_INFO("No attribute " + String(attributeName));
00169             return "";
00170         }
00171         return GetAttributeValue(node->getAttributes(), attributeName);
00172     }
00173 
00174     static String
00175     GetAttributeValue(const DOMNode* const node, const char* const attributeName,
00176                       const char* const defaultV)
00177     {
00178         if (!node->hasAttributes())
00179             return defaultV;
00180         return GetAttributeValue(node->getAttributes(), attributeName, defaultV);
00181     }
00182 
00183     static String
00184     GetAttributeValue(const DOMNamedNodeMap* const attributes,
00185                       const char* const attributeName)
00186     {
00187         XMLCh* xAttribName = XMLString::transcode(attributeName);
00188         DOMNode* attrib = attributes->getNamedItem(xAttribName);
00189         XMLString::release(&xAttribName);
00190         if (attrib == 0)
00191         {
00192             ILOG_INFO("No attribute " + String(attributeName));
00193             return "";
00194         }
00195         return GetNodeValue(attrib);
00196     }
00197 
00198     static String
00199     GetAttributeValue(const DOMNamedNodeMap* const attributes,
00200                       const char* const attributeName,
00201                       const char* const defaultV)
00202     {
00203         XMLCh* xAttribName = XMLString::transcode(attributeName);
00204         DOMNode* attrib = attributes->getNamedItem(xAttribName);
00205         XMLString::release(&xAttribName);
00206         if (attrib == 0)
00207             return defaultV;
00208         return GetNodeValue(attrib);
00209     }
00210 
00211     ILOG_VAR_DEC;
00212 };
00213 
00214 ILOG_VAR_INIT(XmlDoc, Util);
00215 
00216 } //namespace Util
00217 } //namespace Impala
00218 
00219 #endif

Generated on Fri Mar 19 09:31:48 2010 for ImpalaSrc by  doxygen 1.5.1