xref: /aosp_15_r20/external/pdfium/core/fxcrt/xml/cfx_xmlelement.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef CORE_FXCRT_XML_CFX_XMLELEMENT_H_
8 #define CORE_FXCRT_XML_CFX_XMLELEMENT_H_
9 
10 #include <map>
11 
12 #include "core/fxcrt/widestring.h"
13 #include "core/fxcrt/xml/cfx_xmlnode.h"
14 
15 class CFX_XMLDocument;
16 
17 class CFX_XMLElement final : public CFX_XMLNode {
18  public:
19   explicit CFX_XMLElement(const WideString& wsTag);
20   ~CFX_XMLElement() override;
21 
22   // CFX_XMLNode
23   Type GetType() const override;
24   CFX_XMLNode* Clone(CFX_XMLDocument* doc) override;
25   void Save(const RetainPtr<IFX_RetainableWriteStream>& pXMLStream) override;
26 
GetName()27   const WideString& GetName() const { return name_; }
28 
GetAttributes()29   const std::map<WideString, WideString>& GetAttributes() const {
30     return attrs_;
31   }
32   bool HasAttribute(const WideString& name) const;
33   void SetAttribute(const WideString& name, const WideString& value);
34   WideString GetAttribute(const WideString& name) const;
35   void RemoveAttribute(const WideString& name);
36 
37   CFX_XMLElement* GetFirstChildNamed(WideStringView name) const;
38   CFX_XMLElement* GetNthChildNamed(WideStringView name, size_t idx) const;
39 
40   WideString GetLocalTagName() const;
41   WideString GetNamespacePrefix() const;
42   WideString GetNamespaceURI() const;
43 
44   WideString GetTextData() const;
45 
46  private:
47   WideString AttributeToString(const WideString& name, const WideString& value);
48 
49   const WideString name_;
50   std::map<WideString, WideString> attrs_;
51 };
52 
ToXMLElement(CFX_XMLNode * pNode)53 inline CFX_XMLElement* ToXMLElement(CFX_XMLNode* pNode) {
54   return pNode && pNode->GetType() == CFX_XMLNode::Type::kElement
55              ? static_cast<CFX_XMLElement*>(pNode)
56              : nullptr;
57 }
58 
ToXMLElement(const CFX_XMLNode * pNode)59 inline const CFX_XMLElement* ToXMLElement(const CFX_XMLNode* pNode) {
60   return pNode && pNode->GetType() == CFX_XMLNode::Type::kElement
61              ? static_cast<const CFX_XMLElement*>(pNode)
62              : nullptr;
63 }
64 
65 #endif  // CORE_FXCRT_XML_CFX_XMLELEMENT_H_
66