• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
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_FX_XML_H_
8 #define CORE_FXCRT_FX_XML_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/fx_basic.h"
14 
15 class CXML_AttrItem {
16  public:
17   bool Matches(const CFX_ByteString& space, const CFX_ByteString& name) const;
18 
19   CFX_ByteString m_QSpaceName;
20   CFX_ByteString m_AttrName;
21   CFX_WideString m_Value;
22 };
23 
24 class CXML_AttrMap {
25  public:
26   CXML_AttrMap();
27   ~CXML_AttrMap();
28 
29   const CFX_WideString* Lookup(const CFX_ByteString& space,
30                                const CFX_ByteString& name) const;
31   int GetSize() const;
32   CXML_AttrItem& GetAt(int index) const;
33 
34   void SetAt(const CFX_ByteString& space,
35              const CFX_ByteString& name,
36              const CFX_WideString& value);
37 
38   std::unique_ptr<std::vector<CXML_AttrItem>> m_pMap;
39 };
40 
41 class CXML_Content {
42  public:
CXML_Content()43   CXML_Content() : m_bCDATA(false), m_Content() {}
Set(bool bCDATA,const CFX_WideStringC & content)44   void Set(bool bCDATA, const CFX_WideStringC& content) {
45     m_bCDATA = bCDATA;
46     m_Content = content;
47   }
48 
49   bool m_bCDATA;
50   CFX_WideString m_Content;
51 };
52 
53 class CXML_Element {
54  public:
55   enum ChildType { Invalid, Element, Content };
56 
57   static std::unique_ptr<CXML_Element> Parse(const void* pBuffer, size_t size);
58 
59   CXML_Element(const CXML_Element* pParent,
60                const CFX_ByteStringC& qSpace,
61                const CFX_ByteStringC& tagname);
62   ~CXML_Element();
63 
64   void Empty();
65   CFX_ByteString GetTagName(bool bQualified = false) const;
66   CFX_ByteString GetNamespace(bool bQualified = false) const;
67   CFX_ByteString GetNamespaceURI(const CFX_ByteString& qName) const;
GetParent()68   const CXML_Element* GetParent() const { return m_pParent; }
CountAttrs()69   uint32_t CountAttrs() const { return m_AttrMap.GetSize(); }
70   void GetAttrByIndex(int index,
71                       CFX_ByteString& space,
72                       CFX_ByteString& name,
73                       CFX_WideString& value) const;
74   bool HasAttr(const CFX_ByteStringC& qName) const;
75   bool GetAttrValue(const CFX_ByteStringC& name,
76                     CFX_WideString& attribute) const;
GetAttrValue(const CFX_ByteStringC & name)77   CFX_WideString GetAttrValue(const CFX_ByteStringC& name) const {
78     CFX_WideString attr;
79     GetAttrValue(name, attr);
80     return attr;
81   }
82 
83   bool GetAttrValue(const CFX_ByteStringC& space,
84                     const CFX_ByteStringC& name,
85                     CFX_WideString& attribute) const;
GetAttrValue(const CFX_ByteStringC & space,const CFX_ByteStringC & name)86   CFX_WideString GetAttrValue(const CFX_ByteStringC& space,
87                               const CFX_ByteStringC& name) const {
88     CFX_WideString attr;
89     GetAttrValue(space, name, attr);
90     return attr;
91   }
92 
93   bool GetAttrInteger(const CFX_ByteStringC& name, int& attribute) const;
GetAttrInteger(const CFX_ByteStringC & name)94   int GetAttrInteger(const CFX_ByteStringC& name) const {
95     int attr = 0;
96     GetAttrInteger(name, attr);
97     return attr;
98   }
99 
100   bool GetAttrInteger(const CFX_ByteStringC& space,
101                       const CFX_ByteStringC& name,
102                       int& attribute) const;
GetAttrInteger(const CFX_ByteStringC & space,const CFX_ByteStringC & name)103   int GetAttrInteger(const CFX_ByteStringC& space,
104                      const CFX_ByteStringC& name) const {
105     int attr = 0;
106     GetAttrInteger(space, name, attr);
107     return attr;
108   }
109 
110   bool GetAttrFloat(const CFX_ByteStringC& name, FX_FLOAT& attribute) const;
GetAttrFloat(const CFX_ByteStringC & name)111   FX_FLOAT GetAttrFloat(const CFX_ByteStringC& name) const {
112     FX_FLOAT attr = 0;
113     GetAttrFloat(name, attr);
114     return attr;
115   }
116 
117   bool GetAttrFloat(const CFX_ByteStringC& space,
118                     const CFX_ByteStringC& name,
119                     FX_FLOAT& attribute) const;
GetAttrFloat(const CFX_ByteStringC & space,const CFX_ByteStringC & name)120   FX_FLOAT GetAttrFloat(const CFX_ByteStringC& space,
121                         const CFX_ByteStringC& name) const {
122     FX_FLOAT attr = 0;
123     GetAttrFloat(space, name, attr);
124     return attr;
125   }
126 
CountChildren()127   uint32_t CountChildren() const { return m_Children.size(); }
128   ChildType GetChildType(uint32_t index) const;
129   CFX_WideString GetContent(uint32_t index) const;
130   CXML_Element* GetElement(uint32_t index) const;
GetElement(const CFX_ByteStringC & space,const CFX_ByteStringC & tag)131   CXML_Element* GetElement(const CFX_ByteStringC& space,
132                            const CFX_ByteStringC& tag) const {
133     return GetElement(space, tag, 0);
134   }
135 
136   uint32_t CountElements(const CFX_ByteStringC& space,
137                          const CFX_ByteStringC& tag) const;
138   CXML_Element* GetElement(const CFX_ByteStringC& space,
139                            const CFX_ByteStringC& tag,
140                            int index) const;
141 
142   uint32_t FindElement(CXML_Element* pChild) const;
143   void SetTag(const CFX_ByteStringC& qTagName);
144   void RemoveChildren();
145   void RemoveChild(uint32_t index);
146 
147  protected:
148   struct ChildRecord {
149     ChildType type;
150     void* child;  // CXML_Element and CXML_Content lack a common ancestor.
151   };
152 
153   const CXML_Element* const m_pParent;
154   CFX_ByteString m_QSpaceName;
155   CFX_ByteString m_TagName;
156   CXML_AttrMap m_AttrMap;
157   std::vector<ChildRecord> m_Children;
158 
159   friend class CXML_Parser;
160   friend class CXML_Composer;
161 };
162 
163 #endif  // CORE_FXCRT_FX_XML_H_
164