1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#ifndef XFA_FDE_XML_FDE_XML_IMP_H_
#define XFA_FDE_XML_FDE_XML_IMP_H_
#include "core/fxcrt/include/fx_system.h"
#include "xfa/fde/xml/fde_xml.h"
#include "xfa/fgas/crt/fgas_memory.h"
#include "xfa/fgas/crt/fgas_stream.h"
class CFDE_BlockBuffer;
class CFDE_XMLInstruction;
class CFDE_XMLElement;
class CFDE_XMLText;
class CFDE_XMLDoc;
class CFDE_XMLDOMParser;
class CFDE_XMLSAXParser;
class CFDE_XMLSyntaxParser;
class IFDE_XMLParser;
class CFDE_XMLNode : public CFX_Target {
public:
enum NodeItem {
Root = 0,
Parent,
FirstSibling,
PriorSibling,
NextSibling,
LastSibling,
FirstNeighbor,
PriorNeighbor,
NextNeighbor,
LastNeighbor,
FirstChild,
LastChild
};
CFDE_XMLNode();
~CFDE_XMLNode() override;
virtual FDE_XMLNODETYPE GetType() const { return FDE_XMLNODE_Unknown; }
virtual int32_t CountChildNodes() const;
virtual CFDE_XMLNode* GetChildNode(int32_t index) const;
virtual int32_t GetChildNodeIndex(CFDE_XMLNode* pNode) const;
virtual CFDE_XMLNode* GetPath(const FX_WCHAR* pPath,
int32_t iLength = -1,
FX_BOOL bQualifiedName = TRUE) const;
virtual int32_t InsertChildNode(CFDE_XMLNode* pNode, int32_t index = -1);
virtual void RemoveChildNode(CFDE_XMLNode* pNode);
virtual void DeleteChildren();
virtual CFDE_XMLNode* GetNodeItem(CFDE_XMLNode::NodeItem eItem) const;
virtual int32_t GetNodeLevel() const;
virtual FX_BOOL InsertNodeItem(CFDE_XMLNode::NodeItem eItem,
CFDE_XMLNode* pNode);
virtual CFDE_XMLNode* RemoveNodeItem(CFDE_XMLNode::NodeItem eItem);
virtual CFDE_XMLNode* Clone(FX_BOOL bRecursive);
virtual void SaveXMLNode(IFX_Stream* pXMLStream);
void CloneChildren(CFDE_XMLNode* pClone);
CFDE_XMLNode* m_pParent;
CFDE_XMLNode* m_pChild;
CFDE_XMLNode* m_pPrior;
CFDE_XMLNode* m_pNext;
};
class CFDE_XMLInstruction : public CFDE_XMLNode {
public:
CFDE_XMLInstruction(const CFX_WideString& wsTarget);
~CFDE_XMLInstruction() override;
FDE_XMLNODETYPE GetType() const override { return FDE_XMLNODE_Instruction; }
CFDE_XMLNode* Clone(FX_BOOL bRecursive) override;
virtual void GetTargetName(CFX_WideString& wsTarget) const {
wsTarget = m_wsTarget;
}
virtual int32_t CountAttributes() const;
virtual FX_BOOL GetAttribute(int32_t index,
CFX_WideString& wsAttriName,
CFX_WideString& wsAttriValue) const;
virtual FX_BOOL HasAttribute(const FX_WCHAR* pwsAttriName) const;
virtual void GetString(const FX_WCHAR* pwsAttriName,
CFX_WideString& wsAttriValue,
const FX_WCHAR* pwsDefValue = NULL) const;
virtual void SetString(const CFX_WideString& wsAttriName,
const CFX_WideString& wsAttriValue);
virtual int32_t GetInteger(const FX_WCHAR* pwsAttriName,
int32_t iDefValue = 0) const;
virtual void SetInteger(const FX_WCHAR* pwsAttriName, int32_t iAttriValue);
virtual FX_FLOAT GetFloat(const FX_WCHAR* pwsAttriName,
FX_FLOAT fDefValue = 0) const;
virtual void SetFloat(const FX_WCHAR* pwsAttriName, FX_FLOAT fAttriValue);
virtual void RemoveAttribute(const FX_WCHAR* pwsAttriName);
virtual int32_t CountData() const;
virtual FX_BOOL GetData(int32_t index, CFX_WideString& wsData) const;
virtual void AppendData(const CFX_WideString& wsData);
virtual void RemoveData(int32_t index);
CFX_WideString m_wsTarget;
CFX_WideStringArray m_Attributes;
CFX_WideStringArray m_TargetData;
};
class CFDE_XMLElement : public CFDE_XMLNode {
public:
CFDE_XMLElement(const CFX_WideString& wsTag);
~CFDE_XMLElement() override;
FDE_XMLNODETYPE GetType() const override { return FDE_XMLNODE_Element; }
CFDE_XMLNode* Clone(FX_BOOL bRecursive) override;
virtual void GetTagName(CFX_WideString& wsTag) const;
virtual void GetLocalTagName(CFX_WideString& wsTag) const;
virtual void GetNamespacePrefix(CFX_WideString& wsPrefix) const;
virtual void GetNamespaceURI(CFX_WideString& wsNamespace) const;
virtual int32_t CountAttributes() const;
virtual FX_BOOL GetAttribute(int32_t index,
CFX_WideString& wsAttriName,
CFX_WideString& wsAttriValue) const;
virtual FX_BOOL HasAttribute(const FX_WCHAR* pwsAttriName) const;
virtual void GetString(const FX_WCHAR* pwsAttriName,
CFX_WideString& wsAttriValue,
const FX_WCHAR* pwsDefValue = NULL) const;
virtual void SetString(const CFX_WideString& wsAttriName,
const CFX_WideString& wsAttriValue);
virtual int32_t GetInteger(const FX_WCHAR* pwsAttriName,
int32_t iDefValue = 0) const;
virtual void SetInteger(const FX_WCHAR* pwsAttriName, int32_t iAttriValue);
virtual FX_FLOAT GetFloat(const FX_WCHAR* pwsAttriName,
FX_FLOAT fDefValue = 0) const;
virtual void SetFloat(const FX_WCHAR* pwsAttriName, FX_FLOAT fAttriValue);
virtual void RemoveAttribute(const FX_WCHAR* pwsAttriName);
virtual void GetTextData(CFX_WideString& wsText) const;
virtual void SetTextData(const CFX_WideString& wsText);
CFX_WideString m_wsTag;
CFX_WideStringArray m_Attributes;
};
class CFDE_XMLText : public CFDE_XMLNode {
public:
CFDE_XMLText(const CFX_WideString& wsText);
~CFDE_XMLText() override;
FDE_XMLNODETYPE GetType() const override { return FDE_XMLNODE_Text; }
CFDE_XMLNode* Clone(FX_BOOL bRecursive) override;
virtual void GetText(CFX_WideString& wsText) const { wsText = m_wsText; }
virtual void SetText(const CFX_WideString& wsText) { m_wsText = wsText; }
CFX_WideString m_wsText;
};
class CFDE_XMLDeclaration : public CFDE_XMLNode {
public:
CFDE_XMLDeclaration() : CFDE_XMLNode() {}
};
class CFDE_XMLCharData : public CFDE_XMLDeclaration {
public:
CFDE_XMLCharData(const CFX_WideString& wsCData);
~CFDE_XMLCharData() override;
FDE_XMLNODETYPE GetType() const override { return FDE_XMLNODE_CharData; }
CFDE_XMLNode* Clone(FX_BOOL bRecursive) override;
virtual void GetCharData(CFX_WideString& wsCharData) const {
wsCharData = m_wsCharData;
}
virtual void SetCharData(const CFX_WideString& wsCData) {
m_wsCharData = wsCData;
}
CFX_WideString m_wsCharData;
};
class CFDE_XMLDoc : public CFX_Target {
public:
CFDE_XMLDoc();
~CFDE_XMLDoc() override;
virtual FX_BOOL LoadXML(IFX_Stream* pXMLStream,
int32_t iXMLPlaneSize = 8192,
int32_t iTextDataSize = 256,
FDE_XMLREADERHANDLER* pHandler = NULL);
virtual FX_BOOL LoadXML(IFDE_XMLParser* pXMLParser);
virtual int32_t DoLoad(IFX_Pause* pPause = NULL);
virtual void CloseXML();
virtual CFDE_XMLNode* GetRoot() const { return m_pRoot; }
virtual void SaveXML(IFX_Stream* pXMLStream = NULL, FX_BOOL bSaveBOM = TRUE);
virtual void SaveXMLNode(IFX_Stream* pXMLStream, CFDE_XMLNode* pNode);
protected:
IFX_Stream* m_pStream;
int32_t m_iStatus;
CFDE_XMLNode* m_pRoot;
CFDE_XMLSyntaxParser* m_pSyntaxParser;
IFDE_XMLParser* m_pXMLParser;
void Reset(FX_BOOL bInitRoot);
void ReleaseParser();
};
typedef CFX_StackTemplate<CFDE_XMLNode*> CFDE_XMLDOMNodeStack;
class IFDE_XMLParser {
public:
virtual ~IFDE_XMLParser() {}
virtual int32_t DoParser(IFX_Pause* pPause) = 0;
};
class CFDE_XMLDOMParser : public IFDE_XMLParser, public CFX_Target {
public:
CFDE_XMLDOMParser(CFDE_XMLNode* pRoot, CFDE_XMLSyntaxParser* pParser);
~CFDE_XMLDOMParser() override;
int32_t DoParser(IFX_Pause* pPause) override;
private:
CFDE_XMLSyntaxParser* m_pParser;
CFDE_XMLNode* m_pParent;
CFDE_XMLNode* m_pChild;
CFDE_XMLDOMNodeStack m_NodeStack;
CFX_WideString m_ws1;
CFX_WideString m_ws2;
};
class CFDE_XMLTAG : public CFX_Target {
public:
CFDE_XMLTAG() : eType(FDE_XMLNODE_Unknown) {}
CFDE_XMLTAG(const CFDE_XMLTAG& src)
: wsTagName(src.wsTagName), eType(src.eType) {}
CFX_WideString wsTagName;
FDE_XMLNODETYPE eType;
};
typedef CFX_ObjectStackTemplate<CFDE_XMLTAG> CFDE_XMLTagStack;
class CFDE_XMLSAXParser : public IFDE_XMLParser, public CFX_Target {
public:
CFDE_XMLSAXParser(FDE_XMLREADERHANDLER* pHandler,
CFDE_XMLSyntaxParser* pParser);
~CFDE_XMLSAXParser() override;
int32_t DoParser(IFX_Pause* pPause) override;
private:
void Push(const CFDE_XMLTAG& xmlTag);
void Pop();
FDE_XMLREADERHANDLER* m_pHandler;
CFDE_XMLSyntaxParser* m_pParser;
CFDE_XMLTagStack m_TagStack;
CFDE_XMLTAG* m_pTagTop;
CFX_WideString m_ws1;
CFX_WideString m_ws2;
};
class CFDE_BlockBuffer : public CFX_Target {
public:
CFDE_BlockBuffer(int32_t iAllocStep = 1024 * 1024);
~CFDE_BlockBuffer() override;
FX_BOOL InitBuffer(int32_t iBufferSize = 1024 * 1024);
FX_BOOL IsInitialized() { return m_iBufferSize / m_iAllocStep >= 1; }
FX_WCHAR* GetAvailableBlock(int32_t& iIndexInBlock);
inline int32_t GetAllocStep() const { return m_iAllocStep; }
inline int32_t& GetDataLengthRef() { return m_iDataLength; }
inline void Reset(FX_BOOL bReserveData = TRUE) {
if (!bReserveData) {
m_iStartPosition = 0;
}
m_iDataLength = 0;
}
void SetTextChar(int32_t iIndex, FX_WCHAR ch);
int32_t DeleteTextChars(int32_t iCount, FX_BOOL bDirection = TRUE);
void GetTextData(CFX_WideString& wsTextData,
int32_t iStart = 0,
int32_t iLength = -1) const;
protected:
inline void TextDataIndex2BufIndex(const int32_t iIndex,
int32_t& iBlockIndex,
int32_t& iInnerIndex) const;
void ClearBuffer();
CFX_ArrayTemplate<FX_WCHAR*> m_BlockArray;
int32_t m_iDataLength;
int32_t m_iBufferSize;
int32_t m_iAllocStep;
int32_t m_iStartPosition;
};
class CFDE_XMLSyntaxParser : public CFX_Target {
public:
CFDE_XMLSyntaxParser();
~CFDE_XMLSyntaxParser() override;
void Init(IFX_Stream* pStream,
int32_t iXMLPlaneSize,
int32_t iTextDataSize = 256);
FDE_XmlSyntaxResult DoSyntaxParse();
int32_t GetStatus() const;
int32_t GetCurrentPos() const {
return m_iParsedChars + (m_pStart - m_pBuffer);
}
FX_FILESIZE GetCurrentBinaryPos() const;
int32_t GetCurrentNodeNumber() const { return m_iCurrentNodeNum; }
int32_t GetLastNodeNumber() const { return m_iLastNodeNum; }
void GetTargetName(CFX_WideString& wsTarget) const {
m_BlockBuffer.GetTextData(wsTarget, 0, m_iTextDataLength);
}
void GetTagName(CFX_WideString& wsTag) const {
m_BlockBuffer.GetTextData(wsTag, 0, m_iTextDataLength);
}
void GetAttributeName(CFX_WideString& wsAttriName) const {
m_BlockBuffer.GetTextData(wsAttriName, 0, m_iTextDataLength);
}
void GetAttributeValue(CFX_WideString& wsAttriValue) const {
m_BlockBuffer.GetTextData(wsAttriValue, 0, m_iTextDataLength);
}
void GetTextData(CFX_WideString& wsText) const {
m_BlockBuffer.GetTextData(wsText, 0, m_iTextDataLength);
}
void GetTargetData(CFX_WideString& wsData) const {
m_BlockBuffer.GetTextData(wsData, 0, m_iTextDataLength);
}
protected:
enum class FDE_XmlSyntaxState {
Text,
Node,
Target,
Tag,
AttriName,
AttriEqualSign,
AttriQuotation,
AttriValue,
Entity,
EntityDecimal,
EntityHex,
CloseInstruction,
BreakElement,
CloseElement,
SkipDeclNode,
DeclCharData,
SkipComment,
SkipCommentOrDecl,
SkipCData,
TargetData
};
IFX_Stream* m_pStream;
int32_t m_iXMLPlaneSize;
int32_t m_iCurrentPos;
int32_t m_iCurrentNodeNum;
int32_t m_iLastNodeNum;
int32_t m_iParsedChars;
int32_t m_iParsedBytes;
FX_WCHAR* m_pBuffer;
int32_t m_iBufferChars;
FX_BOOL m_bEOS;
FX_WCHAR* m_pStart;
FX_WCHAR* m_pEnd;
FDE_XMLNODE m_CurNode;
CFDE_XMLNodeStack m_XMLNodeStack;
CFDE_BlockBuffer m_BlockBuffer;
int32_t m_iAllocStep;
int32_t& m_iDataLength;
FX_WCHAR* m_pCurrentBlock;
int32_t m_iIndexInBlock;
int32_t m_iTextDataLength;
FDE_XmlSyntaxResult m_syntaxParserResult;
FDE_XmlSyntaxState m_syntaxParserState;
FX_WCHAR m_wQuotationMark;
int32_t m_iEntityStart;
CFX_DWordStack m_SkipStack;
FX_WCHAR m_SkipChar;
inline void ParseTextChar(FX_WCHAR ch);
};
#endif // XFA_FDE_XML_FDE_XML_IMP_H_
|