ADTF
builds/digitalwerk/solutions/adtf_content/adtf_base/adtf_core/src/libraries/a_utils/include/a_utils/std/dom.h
Go to the documentation of this file.
1 
7 #ifndef _DOM_CLASS_HEADER_
8 #define _DOM_CLASS_HEADER_
9 
10 namespace A_UTILS_NS
11 {
12 
13 class cDOM;
14 class cDOMElement;
15 
19 typedef std::list<cDOMElement> cDOMElementList;
21 typedef std::list<cDOMElement*> cDOMElementRefList;
22 
24 typedef cDOMElementList::iterator cDOMElementItor;
26 typedef cDOMElementList::const_iterator cDOMElementItorConst;
28 typedef cDOMElementRefList::iterator cDOMElementRefItor;
30 typedef cDOMElementRefList::const_iterator cDOMElementRefItorConst;
31 
34 {
43  bool operator()(const cString& str1, const cString& str2) const
44  {
45  return ((str1.Compare(str2)) < 0);
46  }
47 };
49 typedef std::map<cString, cString, xml_string_compare_func> cDOMAttributes;
50 
55 {
57 
58  protected:
66 
69 
70  public:
76 
82 
86  virtual ~cDOMElement();
87 
93  cDOMElement& operator=(const cDOMElement& oElement);
94 
105  const cString& GetName() const;
106 
112  tVoid SetName(const cString& strName);
113 
120 
126  const cDOMElement& GetParent() const;
127 
134 
140  const cDOM& GetDocument() const;
141 
148 
154  const cDOMElementList& GetChildren() const;
155 
161  const cString& GetData() const;
162 
168  tVoid SetData(const cString& strData);
169 
176 
183 
189  tBool Exists(const cString& strName) const;
190 
197  cString GetAttribute(const cString& strName, const cString& strDefault = cString()) const;
198 
199 
206  tInt GetAttributeInt(const cString& strName, tInt nDefault = 0) const;
207 
214  tUInt32 GetAttributeUInt32(const cString& strName, tUInt32 nDefault = 0) const;
215 
222  tBool GetAttributeBool(const cString& strName, tBool bDefault = tFalse) const;
223 
230  tFloat64 GetAttributeFloat64(const cString& strName, tFloat64 fDefault = 0.0) const;
231 
238  tVoid SetAttribute(const cString& strName, const cString& strValue);
239 
245  tVoid EraseAttribute(const cString& strName);
246 
252  const cString& GetPath() const;
253 
289  tResult FindNode(const tChar* strQuery, cDOMElement*& poElement);
290 
301  tResult FindNodes(const tChar* strQuery, cDOMElementRefList& lstElements, tInt32 ui32Flags = 0);
302 
308  tResult FromString(const tChar* strXML);
309 
314  cString ToString() const;
315 
322  tResult CreateChild(const cString& strName, cDOMElement*& poChild);
323 
330 
337  tResult InsertBefore(const cDOMElement& oChild, cDOMElement* pBefore);
338 
344  cDOMElement* GetChild(const cString& strName);
345 
351  const cDOMElement* GetChild(const cString& strName) const;
352 
358 
359  protected:
368  tResult Create(const tChar*& ptr, cDOMElement* pParent, cDOM* pDocument, const tChar* pStart = nullptr);
369 
376  tVoid CreateChild(const std::list<cString>& lstComment, const tInt& nFlag = 0);
377 
384  tResult ReadAttributes(const tChar*& ptr, const tChar* pStart = nullptr);
385 
392  tResult ReadData(const tChar*& ptr, const tChar* pStart = nullptr);
393 
400  tBool MatchQuery(const tChar *ptr, tUInt32 &o_nOffset);
401 
407  tBool MatchAttributes(const tChar *pString);
408 
417  tResult FindNodesImpl(const tChar* strQuery, cDOMElementRefList& lstElements,
418  tBool& bFinished, tUInt32 ui32Flags);
419 
420  protected:
427  cString ToStringImpl(tUInt32 ui32Flags, tInt nIndentLevel=0) const;
428 
429  private:
430  tResult MergeIncludeNode(cDOMElement& oIncludeNode);
431  tVoid Copy(const cDOMElement& oFromElement, cDOMElement* pParent, cDOM* pDocument, const cString& strPath);
432 
433 
434  friend class cDOM;
435 };
436 
458 class cDOM
459 {
460  A_UTILS_D(cDOM)
461 
462  friend class cDOMElement;
463 
464  protected:
471 
473  static const tSize m_nTABSIZE = 1;
474 
475 
476  public:
482 
486  virtual ~cDOM();
487 
492  A_UTILS_DEPRECATED(cDOM)(const cDOM& oDOM);
493 
499  cDOM& operator=(const cDOM& oDOM);
500 
511  tResult Load(const tChar* strFilename,
512  const tBool& bIgnoreIncludes=tFalse,
514 
521  tResult Save(const tChar* strFilename, const tBool& bKeepIncludes=tFalse) const;
522 
529  tResult FromString(const tChar* ptrXML, const tBool& bIgnoreIncludes=tFalse);
530 
535  cString ToString() const;
536 
542 
549 
555  const cDOMElement& GetRoot() const;
556 
557 
563  tVoid SetRoot(const cDOMElement& oNewRoot);
564 
569 
574 
579 
584 
593  tResult FindNode(const tChar* strQuery, cDOMElement*& poElement);
594 
603  tResult FindNodes(const tChar* strQuery, cDOMElementRefList& lstElements);
604 
622  tVoid AddIncludedFile(const cString& strIncludedFile);
623 
624  public:
635  static tResult SkipComment(cDOM* pDom, const tChar*& ptr, std::list<cString>& lstComment, const tChar* pStart = nullptr);
636 
637 
644 
645 
646  private:
647  tResult DropIncludes(cDOMElement* pElement = nullptr);
648  static tVoid DOMError(cDOM* pDom,
649  const tChar * pStart,
650  const tChar * pCur,
651  const tChar * pMsg);
652  tVoid SetDOMError(cString& strError);
653  tBool IsIgnoreIncludesEnabled();
654 };
655 
656 } // namespace
657 
658 #endif // _DOM_CLASS_HEADER_
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
int32_t tInt32
type definition for signed integer values (32bit) (platform and compiler independent type).
void tVoid
The tVoid is always the definition for the void (non-type).
int tInt
type definition for signed integer value (platform and compiler dependent type).
double tFloat64
type definition for Float64 (64bit double values) (platform and compiler independent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
uint32_t tUInt32
type definition for unsigned integer values (32bit) (platform and compiler independent type).
size_t tSize
type definition for a array size values, map size values etc.
tVoid SetData(const cString &strData)
Sets the data of the element.
tResult ReadData(const tChar *&ptr, const tChar *pStart=nullptr)
Parses the data from the input string.
cString ToStringImpl(tUInt32 ui32Flags, tInt nIndentLevel=0) const
Converts the DOM element into a textual XML like representation.
cString GetAttribute(const cString &strName, const cString &strDefault=cString()) const
Returns the value of an attribute.
tVoid CreateChild(const std::list< cString > &lstComment, const tInt &nFlag=0)
Parses comments from and creates pseudo DOM childs.
cString ToString() const
Creates an xml representation of the element.
tUInt32 GetAttributeUInt32(const cString &strName, tUInt32 nDefault=0) const
Returns the value of an attribute interpreted as an integer.
cDOMElementList & GetChildren()
Returns a list of all child elements of the element.
tFloat64 GetAttributeFloat64(const cString &strName, tFloat64 fDefault=0.0) const
Returns the value of an attribute interpreted as a float.
tResult FindNodesImpl(const tChar *strQuery, cDOMElementRefList &lstElements, tBool &bFinished, tUInt32 ui32Flags)
Find nodes who matches the query string.
tBool Exists(const cString &strName) const
Checks if an attribute exists.
tInt GetAttributeInt(const cString &strName, tInt nDefault=0) const
Returns the value of an attribute interpreted as an integer.
const cDOM & GetDocument() const
Returns the document that the element belongs to.
const cString & GetPath() const
returns the current path
cDOMElement & GetParent()
Returns the elements parent element.
tResult Free()
Frees all allocated resources.
tBool GetAttributeBool(const cString &strName, tBool bDefault=tFalse) const
Returns the value of an attribute interpreted as a boolean.
tBool MatchQuery(const tChar *ptr, tUInt32 &o_nOffset)
Matches the query with the child attributes.
cDOM & GetDocument()
Returns the document that the element belongs to.
cDOMElement * GetChild(const cString &strName)
Find the first child of that name of this element.
const cDOMAttributes & GetAttributes() const
Returns a map of alle attributes of the element.
tVoid SetName(const cString &strName)
Sets the name of the element.
tResult Delete()
Removes the element from its parent element.
tResult FindNode(const tChar *strQuery, cDOMElement *&poElement)
Finds a node based on a query.
const cDOMElementList & GetChildren() const
Returns a list of all child elements of the element.
const cString & GetName() const
Returns the name of the element.
tVoid EraseAttribute(const cString &strName)
Removes an attribute.
const cString & GetData() const
Returns the data of the element.
tResult Create(const tChar *&ptr, cDOMElement *pParent, cDOM *pDocument, const tChar *pStart=nullptr)
Parses the DOM element from the input string.
tResult FromString(const tChar *strXML)
Creates the element from an xml string.
virtual ~cDOMElement()
Destructor.
const cDOMElement * GetChild(const cString &strName) const
Find the first child of that name of this element.
cDOMElement & operator=(const cDOMElement &oElement)
Copy assignment operator.
tBool MatchAttributes(const tChar *pString)
Matches the attributes of the DOM child.
cDOMAttributes & GetAttributes()
Returns a map of alle attributes of the element.
const cDOMElement & GetParent() const
Returns the elements parent element.
tResult CreateChild(const cString &strName, cDOMElement *&poChild)
Creates a new child element.
tVoid SetAttribute(const cString &strName, const cString &strValue)
Sets an attribute.
tResult InsertBefore(const cDOMElement &oChild, cDOMElement *pBefore)
Inserts an existing child element at a specified position.
tResult ReadAttributes(const tChar *&ptr, const tChar *pStart=nullptr)
Parses the attributes from the input string.
tResult FindNodes(const tChar *strQuery, cDOMElementRefList &lstElements, tInt32 ui32Flags=0)
Finds nodes based on a query.
tResult InsertChild(const cDOMElement &oChild)
Adds an existing child element.
cDOM implements a XML parser which can be used to read in XML files and perform simple (!...
const cDOMElementList & GetPreRootSibling() const
For internal use only.
tResult Save(const tChar *strFilename, const tBool &bKeepIncludes=tFalse) const
Save dom to an xml file.
cString ToString() const
Creates an xml string representation of the dom.
cDOMElement & GetRoot()
Returns the root element of the dom.
static tResult SkipComment(cDOM *pDom, const tChar *&ptr, std::list< cString > &lstComment, const tChar *pStart=nullptr)
Skips xml comment sections in a string.
tResult FromString(const tChar *ptrXML, const tBool &bIgnoreIncludes=tFalse)
Create the dom from an input string.
cDOM & operator=(const cDOM &oDOM)
Copy assignment operator.
tResult Free()
Frees all allocated resources.
tVoid AddPreRootSibling(const cDOMElement &oChild)
For internal use only.
const cDOMElement & GetRoot() const
Returns the root element of the dom.
const cDOMElementList & GetPostRootSibling() const
For internal use only.
tResult FindNode(const tChar *strQuery, cDOMElement *&poElement)
Finds a node based on a query, see cDOMElement::FindNode.
cString GetLastDOMError()
Returns the last error description occurred.
cString GetFilePath()
Returns the name of the file that the dom was created from.
cString m_strLastErrorDescription
if error occurs this will hold the parser error description
cStringList GetIncludedFiles()
Returns a list of files that are included in the loaded file.
tVoid AddPostRootSibling(const cDOMElement &oChild)
For internal use only.
tResult Load(const tChar *strFilename, const tBool &bIgnoreIncludes=tFalse, const tUInt32 &ui32OpenMode=cFile::OM_Read|cFile::OM_SharedRead|cFile::OM_SequentialAccess)
Loads an xml file and creates the dom.
tVoid AddIncludedFile(const cString &strIncludedFile)
Adds an include file statement to the dom.
tVoid SetRoot(const cDOMElement &oNewRoot)
Set the root element of the dom.
tResult FindNodes(const tChar *strQuery, cDOMElementRefList &lstElements)
Finds nodes based on a query, see cDOMElement::FindNode.
virtual ~cDOM()
Destructor.
@ OM_Read
Open existing file.
Definition: file.h:36
@ OM_SharedRead
Allows shared read access, use in addition to flags above.
Definition: file.h:52
@ OM_SequentialAccess
Optimized for sequential access, use in addition to flags above.
Definition: file.h:60
File name class.
Definition: filename.h:59
tInt Compare(const _myType &strString, tSize nPos=0, tSize nLength=InvalidPos) const
This function compares a cStringA object with another string using the generic-text function _tcscmp.
Definition: string.h:695
#define tFalse
Value for tBool.
Definition: constants.h:60
#define A_UTILS_DEPRECATED(_func_)
Mark a function or variable as deprecated.
Definition: constants.h:267
#define A_UTILS_D(__pclassname_)
Helper macro for d-pattern definitions.
Definition: d_ptr.h:270
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2784
cDOMElementRefList::iterator cDOMElementRefItor
iterator type for cDOMElementRefList
cDOMElementRefList::const_iterator cDOMElementRefItorConst
constant iterator type for cDOMElementRefList
cDOMElementList::const_iterator cDOMElementItorConst
constant iterator type for cDOMElementList
std::list< cDOMElement * > cDOMElementRefList
own data type for cDOMElement pointers lists
std::map< cString, cString, xml_string_compare_func > cDOMAttributes
own data type for cDOM Attributes