fx_xml.h revision ee451cb395940862dad63c85adfe8f2fd55e864c
123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell// Copyright 2014 PDFium Authors. All rights reserved.
223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell// Use of this source code is governed by a BSD-style license that can be
3c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul// found in the LICENSE file.
422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes
5c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes
723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#ifndef _FX_XML_H_
823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#define _FX_XML_H_
923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#ifndef _FX_BASIC_H_
1023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#include "fx_basic.h"
1123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell#endif
1223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellclass CXML_AttrItem : public CFX_Object
1322144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes{
1423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellpublic:
1523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    CFX_ByteStringL	m_QSpaceName;
1622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes    CFX_ByteStringL	m_AttrName;
1723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    CFX_WideStringL	m_Value;
1823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell
1923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    void	Empty(IFX_Allocator* pAllocator = NULL)
2023caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    {
2123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell        m_QSpaceName.Empty(pAllocator);
2223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell        m_AttrName.Empty(pAllocator);
2323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell        m_Value.Empty(pAllocator);
2423caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    }
2523caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell};
2623caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellclass CXML_AttrMap : public CFX_Object
2723caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell{
2823caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwellpublic:
2919f7db9c9cfa75f05bb92114bbca15cb3c668dc8Vinson Lee    CXML_AttrMap()
3019f7db9c9cfa75f05bb92114bbca15cb3c668dc8Vinson Lee    {
31c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian        m_pMap = NULL;
324d53fb525db56d0695eaa5b91bd8f0cefbc25866Brian Paul    }
3323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    ~CXML_AttrMap()
34c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    {
35c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul        RemoveAll(NULL);
36c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    }
37c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    const CFX_WideStringL*	Lookup(FX_BSTR space, FX_BSTR name) const;
38c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    void					SetAt(FX_BSTR space, FX_BSTR name, FX_WSTR value, IFX_Allocator* pAllocator = NULL);
39c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    void					RemoveAt(FX_BSTR space, FX_BSTR name, IFX_Allocator* pAllocator = NULL);
40c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    void					RemoveAll(IFX_Allocator* pAllocator = NULL);
41c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    int						GetSize() const;
42c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    CXML_AttrItem&			GetAt(int index) const;
43c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    CFX_ObjectArray<CXML_AttrItem>*	m_pMap;
44c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul};
45c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paulclass CXML_Content : public CFX_Object
46c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul{
47c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paulpublic:
48c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    CXML_Content() : m_bCDATA(FALSE), m_Content() {}
4923caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    ~CXML_Content()
50cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    {
51cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        Empty(NULL);
52cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    }
53cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    void	Empty(IFX_Allocator* pAllocator = NULL)
54cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    {
55cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        m_Content.Empty(pAllocator);
56cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    }
57cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    void	Set(FX_BOOL bCDATA, FX_WSTR content, IFX_Allocator* pAllocator = NULL)
58cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    {
59cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        m_bCDATA = bCDATA;
60cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        m_Content.Set(content, pAllocator);
61cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    }
62cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    FX_BOOL			m_bCDATA;
63cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    CFX_WideStringL	m_Content;
64cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell};
65cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwellclass CXML_Element : public CFX_Object
66cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell{
67cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwellpublic:
68cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
69cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    static CXML_Element*	Parse(const void* pBuffer, size_t size, FX_BOOL bSaveSpaceChars = FALSE, FX_FILESIZE* pParsedSize = NULL, IFX_Allocator* pAllocator = NULL);
70cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
71cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    static CXML_Element*	Parse(IFX_FileRead *pFile, FX_BOOL bSaveSpaceChars = FALSE, FX_FILESIZE* pParsedSize = NULL, IFX_Allocator* pAllocator = NULL);
72cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
73cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    static CXML_Element*	Parse(IFX_BufferRead *pBuffer, FX_BOOL bSaveSpaceChars = FALSE, FX_FILESIZE* pParsedSize = NULL, IFX_Allocator* pAllocator = NULL);
74cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
75cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    CXML_Element(FX_BSTR qSpace, FX_BSTR tagName, IFX_Allocator* pAllocator = NULL);
76cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
77cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    CXML_Element(FX_BSTR qTagName, IFX_Allocator* pAllocator = NULL);
78cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
794eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell    CXML_Element(IFX_Allocator* pAllocator = NULL);
804eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell
814eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell    ~CXML_Element();
824eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell
834eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell    void	Empty();
844eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell
854eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell
864eebc90a174722422daea6352d4e980bc81b4bb2Keith Whitwell
8774b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul    CFX_ByteString			GetTagName(FX_BOOL bQualified = FALSE) const;
8874b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul    void					GetTagName(CFX_ByteStringL &tagName, FX_BOOL bQualified = FALSE) const;
8974b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul
9074b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul    CFX_ByteString			GetNamespace(FX_BOOL bQualified = FALSE) const;
9174b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul    void					GetNamespace(CFX_ByteStringL &nameSpace, FX_BOOL bQualified = FALSE) const;
9274b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul
9374b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul    CFX_ByteString			GetNamespaceURI(FX_BSTR qName) const;
9474b493a5e61237de081a438e774e5d8139d4c6b7Brian Paul    void					GetNamespaceURI(FX_BSTR qName, CFX_ByteStringL &uri) const;
95c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul
96cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    CXML_Element*			GetParent() const
97cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    {
98cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        return m_pParent;
99cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    }
100cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
101cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    FX_DWORD				CountAttrs() const
102cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    {
103cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        return m_AttrMap.GetSize();
104c87809c9ed3579cdbd145479a3e55e4058489bf7Brian Paul    }
1057dfdf3a5b74d3498a98f71d18eb2e706f2fedd04Brian Paul
10612c037dbff3d5a812e31624645d577413cd54122Keith Whitwell    void					GetAttrByIndex(int index, CFX_ByteString &space, CFX_ByteString &name, CFX_WideString &value) const;
10712c037dbff3d5a812e31624645d577413cd54122Keith Whitwell    void					GetAttrByIndex(int index, CFX_ByteStringL &space, CFX_ByteStringL &name, CFX_WideStringL &value) const;
10812c037dbff3d5a812e31624645d577413cd54122Keith Whitwell
10912c037dbff3d5a812e31624645d577413cd54122Keith Whitwell    FX_BOOL					HasAttr(FX_BSTR qName) const;
11012c037dbff3d5a812e31624645d577413cd54122Keith Whitwell
11112c037dbff3d5a812e31624645d577413cd54122Keith Whitwell    FX_BOOL					GetAttrValue(FX_BSTR name, CFX_WideString& attribute) const;
11212c037dbff3d5a812e31624645d577413cd54122Keith Whitwell    CFX_WideString			GetAttrValue(FX_BSTR name) const
1137dfdf3a5b74d3498a98f71d18eb2e706f2fedd04Brian Paul    {
114cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        CFX_WideString attr;
115cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        GetAttrValue(name, attr);
116cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell        return attr;
117cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    }
118cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    const CFX_WideStringL*	GetAttrValuePtr(FX_BSTR name) const;
119cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell
120cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290Keith Whitwell    FX_BOOL					GetAttrValue(FX_BSTR space, FX_BSTR name, CFX_WideString& attribute) const;
12123caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    CFX_WideString			GetAttrValue(FX_BSTR space, FX_BSTR name) const
12223caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell    {
12323caf20169ac38436ee9c13914f1d6aa7cf6bb5eKeith Whitwell        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