OLD | NEW |
| (Empty) |
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_INCLUDE_FX_XML_H_ | |
8 #define CORE_FXCRT_INCLUDE_FX_XML_H_ | |
9 | |
10 #include <memory> | |
11 #include <vector> | |
12 | |
13 #include "core/fxcrt/include/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: | |
43 CXML_Content() : m_bCDATA(FALSE), m_Content() {} | |
44 void Set(FX_BOOL bCDATA, const CFX_WideStringC& content) { | |
45 m_bCDATA = bCDATA; | |
46 m_Content = content; | |
47 } | |
48 | |
49 FX_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 CXML_Element* Parse(const void* pBuffer, | |
58 size_t size, | |
59 FX_BOOL bSaveSpaceChars = FALSE, | |
60 FX_FILESIZE* pParsedSize = nullptr); | |
61 static CXML_Element* Parse(IFX_FileRead* pFile, | |
62 FX_BOOL bSaveSpaceChars = FALSE, | |
63 FX_FILESIZE* pParsedSize = nullptr); | |
64 static CXML_Element* Parse(IFX_BufferRead* pBuffer, | |
65 FX_BOOL bSaveSpaceChars = FALSE, | |
66 FX_FILESIZE* pParsedSize = nullptr); | |
67 | |
68 CXML_Element(const CFX_ByteStringC& qSpace, const CFX_ByteStringC& tagName); | |
69 CXML_Element(const CFX_ByteStringC& qTagName); | |
70 CXML_Element(); | |
71 ~CXML_Element(); | |
72 | |
73 void Empty(); | |
74 CFX_ByteString GetTagName(FX_BOOL bQualified = FALSE) const; | |
75 CFX_ByteString GetNamespace(FX_BOOL bQualified = FALSE) const; | |
76 CFX_ByteString GetNamespaceURI(const CFX_ByteString& qName) const; | |
77 CXML_Element* GetParent() const { return m_pParent; } | |
78 uint32_t CountAttrs() const { return m_AttrMap.GetSize(); } | |
79 void GetAttrByIndex(int index, | |
80 CFX_ByteString& space, | |
81 CFX_ByteString& name, | |
82 CFX_WideString& value) const; | |
83 FX_BOOL HasAttr(const CFX_ByteStringC& qName) const; | |
84 FX_BOOL GetAttrValue(const CFX_ByteStringC& name, | |
85 CFX_WideString& attribute) const; | |
86 CFX_WideString GetAttrValue(const CFX_ByteStringC& name) const { | |
87 CFX_WideString attr; | |
88 GetAttrValue(name, attr); | |
89 return attr; | |
90 } | |
91 | |
92 FX_BOOL GetAttrValue(const CFX_ByteStringC& space, | |
93 const CFX_ByteStringC& name, | |
94 CFX_WideString& attribute) const; | |
95 CFX_WideString GetAttrValue(const CFX_ByteStringC& space, | |
96 const CFX_ByteStringC& name) const { | |
97 CFX_WideString attr; | |
98 GetAttrValue(space, name, attr); | |
99 return attr; | |
100 } | |
101 | |
102 FX_BOOL GetAttrInteger(const CFX_ByteStringC& name, int& attribute) const; | |
103 int GetAttrInteger(const CFX_ByteStringC& name) const { | |
104 int attr = 0; | |
105 GetAttrInteger(name, attr); | |
106 return attr; | |
107 } | |
108 | |
109 FX_BOOL GetAttrInteger(const CFX_ByteStringC& space, | |
110 const CFX_ByteStringC& name, | |
111 int& attribute) const; | |
112 int GetAttrInteger(const CFX_ByteStringC& space, | |
113 const CFX_ByteStringC& name) const { | |
114 int attr = 0; | |
115 GetAttrInteger(space, name, attr); | |
116 return attr; | |
117 } | |
118 | |
119 FX_BOOL GetAttrFloat(const CFX_ByteStringC& name, FX_FLOAT& attribute) const; | |
120 FX_FLOAT GetAttrFloat(const CFX_ByteStringC& name) const { | |
121 FX_FLOAT attr = 0; | |
122 GetAttrFloat(name, attr); | |
123 return attr; | |
124 } | |
125 | |
126 FX_BOOL GetAttrFloat(const CFX_ByteStringC& space, | |
127 const CFX_ByteStringC& name, | |
128 FX_FLOAT& attribute) const; | |
129 FX_FLOAT GetAttrFloat(const CFX_ByteStringC& space, | |
130 const CFX_ByteStringC& name) const { | |
131 FX_FLOAT attr = 0; | |
132 GetAttrFloat(space, name, attr); | |
133 return attr; | |
134 } | |
135 | |
136 uint32_t CountChildren() const { return m_Children.size(); } | |
137 ChildType GetChildType(uint32_t index) const; | |
138 CFX_WideString GetContent(uint32_t index) const; | |
139 CXML_Element* GetElement(uint32_t index) const; | |
140 CXML_Element* GetElement(const CFX_ByteStringC& space, | |
141 const CFX_ByteStringC& tag) const { | |
142 return GetElement(space, tag, 0); | |
143 } | |
144 | |
145 uint32_t CountElements(const CFX_ByteStringC& space, | |
146 const CFX_ByteStringC& tag) const; | |
147 CXML_Element* GetElement(const CFX_ByteStringC& space, | |
148 const CFX_ByteStringC& tag, | |
149 int index) const; | |
150 | |
151 uint32_t FindElement(CXML_Element* pChild) const; | |
152 void SetTag(const CFX_ByteStringC& qSpace, const CFX_ByteStringC& tagname); | |
153 void SetTag(const CFX_ByteStringC& qTagName); | |
154 void RemoveChildren(); | |
155 void RemoveChild(uint32_t index); | |
156 | |
157 protected: | |
158 struct ChildRecord { | |
159 ChildType type; | |
160 void* child; // CXML_Element and CXML_Content lack a common ancestor. | |
161 }; | |
162 | |
163 CXML_Element* m_pParent; | |
164 CFX_ByteString m_QSpaceName; | |
165 CFX_ByteString m_TagName; | |
166 CXML_AttrMap m_AttrMap; | |
167 std::vector<ChildRecord> m_Children; | |
168 | |
169 friend class CXML_Parser; | |
170 friend class CXML_Composer; | |
171 }; | |
172 | |
173 #endif // CORE_FXCRT_INCLUDE_FX_XML_H_ | |
OLD | NEW |