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