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_INCLUDE_FXCRT_FX_XML_H_
8 #define CORE_INCLUDE_FXCRT_FX_XML_H_
9 
10 #include "fx_basic.h"
11 
12 class CXML_AttrItem {
13  public:
14   CFX_ByteString m_QSpaceName;
15   CFX_ByteString m_AttrName;
16   CFX_WideString m_Value;
17 };
18 class CXML_AttrMap {
19  public:
CXML_AttrMap()20   CXML_AttrMap() { m_pMap = NULL; }
~CXML_AttrMap()21   ~CXML_AttrMap() { RemoveAll(); }
22   const CFX_WideString* Lookup(const CFX_ByteStringC& space,
23                                const CFX_ByteStringC& name) const;
24   void SetAt(const CFX_ByteStringC& space,
25              const CFX_ByteStringC& name,
26              const CFX_WideStringC& value);
27   void RemoveAt(const CFX_ByteStringC& space, const CFX_ByteStringC& name);
28   void RemoveAll();
29   int GetSize() const;
30   CXML_AttrItem& GetAt(int index) const;
31   CFX_ObjectArray<CXML_AttrItem>* m_pMap;
32 };
33 class CXML_Content {
34  public:
CXML_Content()35   CXML_Content() : m_bCDATA(FALSE), m_Content() {}
Set(FX_BOOL bCDATA,const CFX_WideStringC & content)36   void Set(FX_BOOL bCDATA, const CFX_WideStringC& content) {
37     m_bCDATA = bCDATA;
38     m_Content = content;
39   }
40   FX_BOOL m_bCDATA;
41   CFX_WideString m_Content;
42 };
43 class CXML_Element {
44  public:
45   static CXML_Element* Parse(const void* pBuffer,
46                              size_t size,
47                              FX_BOOL bSaveSpaceChars = FALSE,
48                              FX_FILESIZE* pParsedSize = NULL);
49   static CXML_Element* Parse(IFX_FileRead* pFile,
50                              FX_BOOL bSaveSpaceChars = FALSE,
51                              FX_FILESIZE* pParsedSize = NULL);
52   static CXML_Element* Parse(IFX_BufferRead* pBuffer,
53                              FX_BOOL bSaveSpaceChars = FALSE,
54                              FX_FILESIZE* pParsedSize = NULL);
55   CXML_Element(const CFX_ByteStringC& qSpace, const CFX_ByteStringC& tagName);
56   CXML_Element(const CFX_ByteStringC& qTagName);
57   CXML_Element();
58 
59   ~CXML_Element();
60 
61   void Empty();
62 
63   CFX_ByteString GetTagName(FX_BOOL bQualified = FALSE) const;
64 
65   CFX_ByteString GetNamespace(FX_BOOL bQualified = FALSE) const;
66 
67   CFX_ByteString GetNamespaceURI(const CFX_ByteStringC& qName) const;
68 
GetParent()69   CXML_Element* GetParent() const { return m_pParent; }
70 
CountAttrs()71   FX_DWORD CountAttrs() const { return m_AttrMap.GetSize(); }
72 
73   void GetAttrByIndex(int index,
74                       CFX_ByteString& space,
75                       CFX_ByteString& name,
76                       CFX_WideString& value) const;
77 
78   FX_BOOL HasAttr(const CFX_ByteStringC& qName) const;
79 
80   FX_BOOL GetAttrValue(const CFX_ByteStringC& name,
81                        CFX_WideString& attribute) const;
GetAttrValue(const CFX_ByteStringC & name)82   CFX_WideString GetAttrValue(const CFX_ByteStringC& name) const {
83     CFX_WideString attr;
84     GetAttrValue(name, attr);
85     return attr;
86   }
87 
88   FX_BOOL GetAttrValue(const CFX_ByteStringC& space,
89                        const CFX_ByteStringC& name,
90                        CFX_WideString& attribute) const;
GetAttrValue(const CFX_ByteStringC & space,const CFX_ByteStringC & name)91   CFX_WideString GetAttrValue(const CFX_ByteStringC& space,
92                               const CFX_ByteStringC& name) const {
93     CFX_WideString attr;
94     GetAttrValue(space, name, attr);
95     return attr;
96   }
97 
98   FX_BOOL GetAttrInteger(const CFX_ByteStringC& name, int& attribute) const;
GetAttrInteger(const CFX_ByteStringC & name)99   int GetAttrInteger(const CFX_ByteStringC& name) const {
100     int attr = 0;
101     GetAttrInteger(name, attr);
102     return attr;
103   }
104 
105   FX_BOOL GetAttrInteger(const CFX_ByteStringC& space,
106                          const CFX_ByteStringC& name,
107                          int& attribute) const;
GetAttrInteger(const CFX_ByteStringC & space,const CFX_ByteStringC & name)108   int GetAttrInteger(const CFX_ByteStringC& space,
109                      const CFX_ByteStringC& name) const {
110     int attr = 0;
111     GetAttrInteger(space, name, attr);
112     return attr;
113   }
114 
115   FX_BOOL GetAttrFloat(const CFX_ByteStringC& name, FX_FLOAT& attribute) const;
GetAttrFloat(const CFX_ByteStringC & name)116   FX_FLOAT GetAttrFloat(const CFX_ByteStringC& name) const {
117     FX_FLOAT attr = 0;
118     GetAttrFloat(name, attr);
119     return attr;
120   }
121 
122   FX_BOOL GetAttrFloat(const CFX_ByteStringC& space,
123                        const CFX_ByteStringC& name,
124                        FX_FLOAT& attribute) const;
GetAttrFloat(const CFX_ByteStringC & space,const CFX_ByteStringC & name)125   FX_FLOAT GetAttrFloat(const CFX_ByteStringC& space,
126                         const CFX_ByteStringC& name) const {
127     FX_FLOAT attr = 0;
128     GetAttrFloat(space, name, attr);
129     return attr;
130   }
131 
132   FX_DWORD CountChildren() const;
133 
134   enum ChildType { Invalid, Element, Content };
135 
136   ChildType GetChildType(FX_DWORD index) const;
137 
138   CFX_WideString GetContent(FX_DWORD index) const;
139 
140   CXML_Element* GetElement(FX_DWORD index) const;
141 
GetElement(const CFX_ByteStringC & space,const CFX_ByteStringC & tag)142   CXML_Element* GetElement(const CFX_ByteStringC& space,
143                            const CFX_ByteStringC& tag) const {
144     return GetElement(space, tag, 0);
145   }
146 
147   FX_DWORD CountElements(const CFX_ByteStringC& space,
148                          const CFX_ByteStringC& tag) const;
149 
150   CXML_Element* GetElement(const CFX_ByteStringC& space,
151                            const CFX_ByteStringC& tag,
152                            int index) const;
153 
154   FX_DWORD FindElement(CXML_Element* pChild) const;
155 
156   void SetTag(const CFX_ByteStringC& qSpace, const CFX_ByteStringC& tagname);
157 
158   void SetTag(const CFX_ByteStringC& qTagName);
159 
160   void RemoveChildren();
161 
162   void RemoveChild(FX_DWORD index);
163 
164  protected:
165   CXML_Element* m_pParent;
166   CFX_ByteString m_QSpaceName;
167   CFX_ByteString m_TagName;
168 
169   CXML_AttrMap m_AttrMap;
170 
171   CFX_PtrArray m_Children;
172   friend class CXML_Parser;
173   friend class CXML_Composer;
174 };
175 
176 #endif  // CORE_INCLUDE_FXCRT_FX_XML_H_
177