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#include "xfa/src/foxitlib.h"
8#include "xfa/src/fxfa/src/common/xfa_utils.h"
9#include "xfa/src/fxfa/src/common/xfa_object.h"
10#include "xfa/src/fxfa/src/common/xfa_document.h"
11#include "xfa/src/fxfa/src/common/xfa_parser.h"
12#include "xfa/src/fxfa/src/common/xfa_script.h"
13#include "xfa/src/fxfa/src/common/xfa_docdata.h"
14#include "xfa/src/fxfa/src/common/xfa_doclayout.h"
15#include "xfa/src/fxfa/src/common/xfa_localemgr.h"
16#include "xfa/src/fxfa/src/common/xfa_fm2jsapi.h"
17#include "xfa_basic_imp.h"
18#include "xfa_parser_imp.h"
19IXFA_Parser* IXFA_Parser::Create(IXFA_ObjFactory* pFactory,
20                                 FX_BOOL bDocumentParser) {
21  return new CXFA_SimpleParser(pFactory, bDocumentParser);
22}
23CXFA_SimpleParser::CXFA_SimpleParser(IXFA_ObjFactory* pFactory,
24                                     FX_BOOL bDocumentParser)
25    : m_pXMLParser(nullptr),
26      m_pXMLDoc(nullptr),
27      m_pStream(nullptr),
28      m_pFileRead(nullptr),
29      m_pFactory(pFactory),
30      m_pRootNode(nullptr),
31      m_ePacketID(XFA_XDPPACKET_UNKNOWN),
32      m_bDocumentParser(bDocumentParser) {
33}
34CXFA_SimpleParser::~CXFA_SimpleParser() {
35  CloseParser();
36}
37void CXFA_SimpleParser::SetFactory(IXFA_ObjFactory* pFactory) {
38  m_pFactory = pFactory;
39}
40static IFDE_XMLNode* XFA_FDEExtension_GetDocumentNode(
41    IFDE_XMLDoc* pXMLDoc,
42    FX_BOOL bVerifyWellFormness = FALSE) {
43  if (!pXMLDoc) {
44    return NULL;
45  }
46  IFDE_XMLNode* pXMLFakeRoot = pXMLDoc->GetRoot();
47  for (IFDE_XMLNode* pXMLNode =
48           pXMLFakeRoot->GetNodeItem(IFDE_XMLNode::FirstChild);
49       pXMLNode; pXMLNode = pXMLNode->GetNodeItem(IFDE_XMLNode::NextSibling)) {
50    if (pXMLNode->GetType() == FDE_XMLNODE_Element) {
51      if (bVerifyWellFormness) {
52        for (IFDE_XMLNode* pNextNode =
53                 pXMLNode->GetNodeItem(IFDE_XMLNode::NextSibling);
54             pNextNode;
55             pNextNode = pNextNode->GetNodeItem(IFDE_XMLNode::NextSibling)) {
56          if (pNextNode->GetType() == FDE_XMLNODE_Element) {
57            return FALSE;
58          }
59        }
60      }
61      return pXMLNode;
62    }
63  }
64  return NULL;
65}
66int32_t CXFA_SimpleParser::StartParse(IFX_FileRead* pStream,
67                                      XFA_XDPPACKET ePacketID) {
68  CloseParser();
69  m_pFileRead = pStream;
70  m_pStream = IFX_Stream::CreateStream(
71      pStream, FX_STREAMACCESS_Read | FX_STREAMACCESS_Text);
72  if (m_pStream == NULL) {
73    return XFA_PARSESTATUS_StreamErr;
74  }
75  FX_WORD wCodePage = m_pStream->GetCodePage();
76  if (wCodePage != FX_CODEPAGE_UTF16LE && wCodePage != FX_CODEPAGE_UTF16BE &&
77      wCodePage != FX_CODEPAGE_UTF8) {
78    m_pStream->SetCodePage(FX_CODEPAGE_UTF8);
79  }
80  m_pXMLDoc = IFDE_XMLDoc::Create();
81  if (m_pXMLDoc == NULL) {
82    return XFA_PARSESTATUS_StatusErr;
83  }
84  m_pXMLParser = new CXFA_XMLParser(m_pXMLDoc->GetRoot(), m_pStream);
85  if (m_pXMLParser == NULL) {
86    return XFA_PARSESTATUS_StatusErr;
87  }
88  if (!m_pXMLDoc->LoadXML(m_pXMLParser)) {
89    return XFA_PARSESTATUS_StatusErr;
90  }
91  m_ePacketID = ePacketID;
92  return XFA_PARSESTATUS_Ready;
93}
94int32_t CXFA_SimpleParser::DoParse(IFX_Pause* pPause) {
95  if (m_pXMLDoc == NULL || m_ePacketID == XFA_XDPPACKET_UNKNOWN) {
96    return XFA_PARSESTATUS_StatusErr;
97  }
98  int32_t iRet = m_pXMLDoc->DoLoad(pPause);
99  if (iRet < 0) {
100    return XFA_PARSESTATUS_SyntaxErr;
101  }
102  if (iRet < 100) {
103    return iRet / 2;
104  }
105  m_pRootNode = ParseAsXDPPacket(XFA_FDEExtension_GetDocumentNode(m_pXMLDoc),
106                                 m_ePacketID);
107  m_pXMLDoc->CloseXML();
108  if (m_pStream) {
109    m_pStream->Release();
110    m_pStream = NULL;
111  }
112  if (!m_pRootNode) {
113    return XFA_PARSESTATUS_StatusErr;
114  }
115  return XFA_PARSESTATUS_Done;
116}
117int32_t CXFA_SimpleParser::ParseXMLData(const CFX_WideString& wsXML,
118                                        IFDE_XMLNode*& pXMLNode,
119                                        IFX_Pause* pPause) {
120  CloseParser();
121  pXMLNode = NULL;
122  IFX_Stream* pStream = XFA_CreateWideTextRead(wsXML);
123  if (!pStream) {
124    return XFA_PARSESTATUS_StreamErr;
125  }
126  m_pStream = pStream;
127  m_pXMLDoc = IFDE_XMLDoc::Create();
128  if (m_pXMLDoc == NULL) {
129    return XFA_PARSESTATUS_StatusErr;
130  }
131  CXFA_XMLParser* pParser = new CXFA_XMLParser(m_pXMLDoc->GetRoot(), m_pStream);
132  if (pParser == NULL) {
133    return XFA_PARSESTATUS_StatusErr;
134  }
135#ifdef _XFA_VERIFY_Checksum_
136  pParser->m_dwCheckStatus = 0x03;
137#endif
138  if (!m_pXMLDoc->LoadXML(pParser)) {
139    return XFA_PARSESTATUS_StatusErr;
140  }
141  int32_t iRet = m_pXMLDoc->DoLoad(pPause);
142  if (iRet < 0 || iRet >= 100) {
143    m_pXMLDoc->CloseXML();
144  }
145  if (iRet < 0) {
146    return XFA_PARSESTATUS_SyntaxErr;
147  }
148  if (iRet < 100) {
149    return iRet / 2;
150  }
151  if (m_pStream) {
152    m_pStream->Release();
153    m_pStream = NULL;
154  }
155  pXMLNode = XFA_FDEExtension_GetDocumentNode(m_pXMLDoc);
156  return XFA_PARSESTATUS_Done;
157}
158void CXFA_SimpleParser::ConstructXFANode(CXFA_Node* pXFANode,
159                                         IFDE_XMLNode* pXMLNode) {
160  XFA_XDPPACKET ePacketID = (XFA_XDPPACKET)pXFANode->GetPacketID();
161  if (ePacketID == XFA_XDPPACKET_Datasets) {
162    if (pXFANode->GetClassID() == XFA_ELEMENT_DataValue) {
163      for (IFDE_XMLNode* pXMLChild =
164               pXMLNode->GetNodeItem(IFDE_XMLNode::FirstChild);
165           pXMLChild;
166           pXMLChild = pXMLChild->GetNodeItem(IFDE_XMLNode::NextSibling)) {
167        FDE_XMLNODETYPE eNodeType = pXMLChild->GetType();
168        if (eNodeType == FDE_XMLNODE_Instruction) {
169          continue;
170        }
171        if (eNodeType == FDE_XMLNODE_Element) {
172          CXFA_Node* pXFAChild = m_pFactory->CreateNode(XFA_XDPPACKET_Datasets,
173                                                        XFA_ELEMENT_DataValue);
174          if (pXFAChild == NULL) {
175            return;
176          }
177          CFX_WideString wsNodeStr;
178          ((IFDE_XMLElement*)pXMLChild)->GetLocalTagName(wsNodeStr);
179          pXFAChild->SetCData(XFA_ATTRIBUTE_Name, wsNodeStr);
180          CFX_WideString wsChildValue;
181#ifdef XFA_PARSE_HAS_LINEIDENTIFIER
182          XFA_GetPlainTextFromRichText((IFDE_XMLElement*)pXMLChild,
183                                       wsChildValue);
184#else
185          XFA_ConvertRichTextToPlainText((IFDE_XMLElement*)pXMLChild,
186                                         wsChildValue);
187#endif
188          if (!wsChildValue.IsEmpty()) {
189            pXFAChild->SetCData(XFA_ATTRIBUTE_Value, wsChildValue);
190          }
191          pXFANode->InsertChild(pXFAChild);
192          pXFAChild->SetXMLMappingNode(pXMLChild);
193          pXFAChild->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
194          break;
195        }
196      }
197      m_pRootNode = pXFANode;
198    } else {
199      m_pRootNode = DataLoader(pXFANode, pXMLNode, TRUE);
200    }
201  } else {
202    if (pXFANode->GetObjectType() == XFA_OBJECTTYPE_ContentNode) {
203      ParseContentNode(pXFANode, pXMLNode, ePacketID);
204      m_pRootNode = pXFANode;
205    } else {
206      m_pRootNode = NormalLoader(pXFANode, pXMLNode, ePacketID);
207    }
208  }
209}
210FX_BOOL XFA_FDEExtension_ResolveNamespaceQualifier(
211    IFDE_XMLElement* pNode,
212    const CFX_WideStringC& wsQualifier,
213    CFX_WideString& wsNamespaceURI) {
214  if (!pNode) {
215    return FALSE;
216  }
217  IFDE_XMLNode* pFakeRoot = pNode->GetNodeItem(IFDE_XMLNode::Root);
218  CFX_WideString wsNSAttribute;
219  FX_BOOL bRet = FALSE;
220  if (wsQualifier.IsEmpty()) {
221    wsNSAttribute = FX_WSTRC(L"xmlns");
222    bRet = TRUE;
223  } else {
224    wsNSAttribute = FX_WSTRC(L"xmlns:") + wsQualifier;
225  }
226  for (; pNode != pFakeRoot;
227       pNode = (IFDE_XMLElement*)pNode->GetNodeItem(IFDE_XMLNode::Parent)) {
228    if (pNode->GetType() != FDE_XMLNODE_Element) {
229      continue;
230    }
231    if (pNode->HasAttribute(wsNSAttribute)) {
232      pNode->GetString(wsNSAttribute, wsNamespaceURI);
233      return TRUE;
234    }
235  }
236  wsNamespaceURI.Empty();
237  return bRet;
238}
239static inline void XFA_FDEExtension_GetElementTagNamespaceURI(
240    IFDE_XMLElement* pElement,
241    CFX_WideString& wsNamespaceURI) {
242  CFX_WideString wsNodeStr;
243  pElement->GetNamespacePrefix(wsNodeStr);
244  if (!XFA_FDEExtension_ResolveNamespaceQualifier(pElement, wsNodeStr,
245                                                  wsNamespaceURI)) {
246    wsNamespaceURI.Empty();
247  }
248}
249static FX_BOOL XFA_FDEExtension_MatchNodeName(
250    IFDE_XMLNode* pNode,
251    const CFX_WideStringC& wsLocalTagName,
252    const CFX_WideStringC& wsNamespaceURIPrefix,
253    FX_DWORD eMatchFlags = XFA_XDPPACKET_FLAGS_NOMATCH) {
254  if (!pNode || pNode->GetType() != FDE_XMLNODE_Element) {
255    return FALSE;
256  }
257  IFDE_XMLElement* pElement = reinterpret_cast<IFDE_XMLElement*>(pNode);
258  CFX_WideString wsNodeStr;
259  pElement->GetLocalTagName(wsNodeStr);
260  if (wsNodeStr != wsLocalTagName) {
261    return FALSE;
262  }
263  XFA_FDEExtension_GetElementTagNamespaceURI(pElement, wsNodeStr);
264  if (eMatchFlags & XFA_XDPPACKET_FLAGS_NOMATCH) {
265    return TRUE;
266  }
267  if (eMatchFlags & XFA_XDPPACKET_FLAGS_PREFIXMATCH) {
268    return wsNodeStr.Left(wsNamespaceURIPrefix.GetLength()) ==
269           wsNamespaceURIPrefix;
270  }
271  return wsNodeStr == wsNamespaceURIPrefix;
272}
273static FX_BOOL XFA_FDEExtension_GetAttributeLocalName(
274    const CFX_WideStringC& wsAttributeName,
275    CFX_WideString& wsLocalAttrName) {
276  CFX_WideString wsAttrName(wsAttributeName);
277  FX_STRSIZE iFind = wsAttrName.Find(L':', 0);
278  if (iFind < 0) {
279    wsLocalAttrName = wsAttrName;
280    return FALSE;
281  } else {
282    wsLocalAttrName = wsAttrName.Right(wsAttrName.GetLength() - iFind - 1);
283    return TRUE;
284  }
285}
286static FX_BOOL XFA_FDEExtension_ResolveAttribute(
287    IFDE_XMLElement* pElement,
288    const CFX_WideStringC& wsAttributeName,
289    CFX_WideString& wsLocalAttrName,
290    CFX_WideString& wsNamespaceURI) {
291  CFX_WideString wsAttrName(wsAttributeName);
292  CFX_WideString wsNSPrefix;
293  if (XFA_FDEExtension_GetAttributeLocalName(wsAttributeName,
294                                             wsLocalAttrName)) {
295    wsNSPrefix = wsAttrName.Left(wsAttributeName.GetLength() -
296                                 wsLocalAttrName.GetLength() - 1);
297  }
298  if (wsLocalAttrName == FX_WSTRC(L"xmlns") ||
299      wsNSPrefix == FX_WSTRC(L"xmlns") || wsNSPrefix == FX_WSTRC(L"xml")) {
300    return FALSE;
301  }
302  if (!XFA_FDEExtension_ResolveNamespaceQualifier(pElement, wsNSPrefix,
303                                                  wsNamespaceURI)) {
304    wsNamespaceURI.Empty();
305    return FALSE;
306  }
307  return TRUE;
308}
309static FX_BOOL XFA_FDEExtension_FindAttributeWithNS(
310    IFDE_XMLElement* pElement,
311    const CFX_WideStringC& wsLocalAttributeName,
312    const CFX_WideStringC& wsNamespaceURIPrefix,
313    CFX_WideString& wsValue,
314    FX_BOOL bMatchNSAsPrefix = FALSE) {
315  if (!pElement) {
316    return FALSE;
317  }
318  CFX_WideString wsAttrName;
319  CFX_WideString wsAttrValue;
320  CFX_WideString wsAttrNS;
321  for (int32_t iAttrCount = pElement->CountAttributes(), i = 0; i < iAttrCount;
322       i++) {
323    pElement->GetAttribute(i, wsAttrName, wsAttrValue);
324    FX_STRSIZE iFind = wsAttrName.Find(L':', 0);
325    CFX_WideString wsNSPrefix;
326    if (iFind < 0) {
327      if (wsLocalAttributeName != wsAttrName) {
328        continue;
329      }
330    } else {
331      if (wsLocalAttributeName !=
332          wsAttrName.Right(wsAttrName.GetLength() - iFind - 1)) {
333        continue;
334      }
335      wsNSPrefix = wsAttrName.Left(iFind);
336    }
337    if (!XFA_FDEExtension_ResolveNamespaceQualifier(pElement, wsNSPrefix,
338                                                    wsAttrNS)) {
339      continue;
340    }
341    if (bMatchNSAsPrefix) {
342      if (wsAttrNS.Left(wsNamespaceURIPrefix.GetLength()) !=
343          wsNamespaceURIPrefix) {
344        continue;
345      }
346    } else {
347      if (wsAttrNS != wsNamespaceURIPrefix) {
348        continue;
349      }
350    }
351    wsValue = wsAttrValue;
352    return TRUE;
353  }
354  return FALSE;
355}
356CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket(IFDE_XMLNode* pXMLDocumentNode,
357                                               XFA_XDPPACKET ePacketID) {
358  switch (ePacketID) {
359    case XFA_XDPPACKET_UNKNOWN:
360      return NULL;
361    case XFA_XDPPACKET_XDP:
362      return ParseAsXDPPacket_XDP(pXMLDocumentNode, ePacketID);
363    case XFA_XDPPACKET_Config:
364      return ParseAsXDPPacket_Config(pXMLDocumentNode, ePacketID);
365    case XFA_XDPPACKET_Template:
366    case XFA_XDPPACKET_Form:
367      return ParseAsXDPPacket_TemplateForm(pXMLDocumentNode, ePacketID);
368    case XFA_XDPPACKET_Datasets:
369      return ParseAsXDPPacket_Data(pXMLDocumentNode, ePacketID);
370    case XFA_XDPPACKET_Xdc:
371      return ParseAsXDPPacket_Xdc(pXMLDocumentNode, ePacketID);
372    case XFA_XDPPACKET_LocaleSet:
373    case XFA_XDPPACKET_ConnectionSet:
374    case XFA_XDPPACKET_SourceSet:
375      return ParseAsXDPPacket_LocaleConnectionSourceSet(pXMLDocumentNode,
376                                                        ePacketID);
377    default:
378      return ParseAsXDPPacket_User(pXMLDocumentNode, ePacketID);
379  }
380  return NULL;
381}
382CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_XDP(
383    IFDE_XMLNode* pXMLDocumentNode,
384    XFA_XDPPACKET ePacketID) {
385  if (!XFA_FDEExtension_MatchNodeName(
386          pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_XDP)->pName,
387          XFA_GetPacketByIndex(XFA_PACKET_XDP)->pURI,
388          XFA_GetPacketByIndex(XFA_PACKET_XDP)->eFlags)) {
389    return nullptr;
390  }
391  CXFA_Node* pXFARootNode =
392      m_pFactory->CreateNode(XFA_XDPPACKET_XDP, XFA_ELEMENT_Xfa);
393  if (!pXFARootNode) {
394    return nullptr;
395  }
396  m_pRootNode = pXFARootNode;
397  pXFARootNode->SetCData(XFA_ATTRIBUTE_Name, FX_WSTRC(L"xfa"));
398  {
399    IFDE_XMLElement* pElement = (IFDE_XMLElement*)pXMLDocumentNode;
400    int32_t iAttributeCount = pElement->CountAttributes();
401    for (int32_t i = 0; i < iAttributeCount; i++) {
402      CFX_WideString wsAttriName, wsAttriValue;
403      pElement->GetAttribute(i, wsAttriName, wsAttriValue);
404      if (wsAttriName == FX_WSTRC(L"uuid")) {
405        pXFARootNode->SetCData(XFA_ATTRIBUTE_Uuid, wsAttriValue);
406      } else if (wsAttriName == FX_WSTRC(L"timeStamp")) {
407        pXFARootNode->SetCData(XFA_ATTRIBUTE_TimeStamp, wsAttriValue);
408      }
409    }
410  }
411  IFDE_XMLNode* pXMLConfigDOMRoot = nullptr;
412  CXFA_Node* pXFAConfigDOMRoot = nullptr;
413  {
414    for (IFDE_XMLNode* pChildItem =
415             pXMLDocumentNode->GetNodeItem(IFDE_XMLNode::FirstChild);
416         pChildItem;
417         pChildItem = pChildItem->GetNodeItem(IFDE_XMLNode::NextSibling)) {
418      XFA_LPCPACKETINFO pPacketInfo = XFA_GetPacketByIndex(XFA_PACKET_Config);
419      if (!XFA_FDEExtension_MatchNodeName(pChildItem, pPacketInfo->pName,
420                                          pPacketInfo->pURI,
421                                          pPacketInfo->eFlags)) {
422        continue;
423      }
424      if (pXFARootNode->GetFirstChildByName(pPacketInfo->uHash)) {
425        return nullptr;
426      }
427      pXMLConfigDOMRoot = pChildItem;
428      pXFAConfigDOMRoot =
429          ParseAsXDPPacket_Config(pXMLConfigDOMRoot, XFA_XDPPACKET_Config);
430      pXFARootNode->InsertChild(pXFAConfigDOMRoot, NULL);
431    }
432  }
433  IFDE_XMLNode* pXMLDatasetsDOMRoot = nullptr;
434  IFDE_XMLNode* pXMLFormDOMRoot = nullptr;
435  IFDE_XMLNode* pXMLTemplateDOMRoot = nullptr;
436  {
437    for (IFDE_XMLNode* pChildItem =
438             pXMLDocumentNode->GetNodeItem(IFDE_XMLNode::FirstChild);
439         pChildItem;
440         pChildItem = pChildItem->GetNodeItem(IFDE_XMLNode::NextSibling)) {
441      if (!pChildItem || pChildItem->GetType() != FDE_XMLNODE_Element) {
442        continue;
443      }
444      if (pChildItem == pXMLConfigDOMRoot) {
445        continue;
446      }
447      IFDE_XMLElement* pElement =
448          reinterpret_cast<IFDE_XMLElement*>(pChildItem);
449      CFX_WideString wsPacketName;
450      pElement->GetLocalTagName(wsPacketName);
451      XFA_LPCPACKETINFO pPacketInfo = XFA_GetPacketByName(wsPacketName);
452      if (pPacketInfo && pPacketInfo->pURI) {
453        if (!XFA_FDEExtension_MatchNodeName(pElement, pPacketInfo->pName,
454                                            pPacketInfo->pURI,
455                                            pPacketInfo->eFlags)) {
456          pPacketInfo = nullptr;
457        }
458      }
459      XFA_XDPPACKET ePacket =
460          pPacketInfo ? pPacketInfo->eName : XFA_XDPPACKET_USER;
461      if (ePacket == XFA_XDPPACKET_XDP) {
462        continue;
463      }
464      if (ePacket == XFA_XDPPACKET_Datasets) {
465        if (pXMLDatasetsDOMRoot) {
466          return nullptr;
467        }
468        pXMLDatasetsDOMRoot = pElement;
469      } else if (ePacket == XFA_XDPPACKET_Form) {
470        if (pXMLFormDOMRoot) {
471          return nullptr;
472        }
473        pXMLFormDOMRoot = pElement;
474      } else if (ePacket == XFA_XDPPACKET_Template) {
475        if (pXMLTemplateDOMRoot) {
476          // Found a duplicate template packet.
477          return nullptr;
478        }
479        CXFA_Node* pPacketNode = ParseAsXDPPacket(pElement, ePacket);
480        if (pPacketNode) {
481          pXMLTemplateDOMRoot = pElement;
482          pXFARootNode->InsertChild(pPacketNode);
483        }
484      } else {
485        CXFA_Node* pPacketNode = ParseAsXDPPacket(pElement, ePacket);
486        if (pPacketNode) {
487          if (pPacketInfo &&
488              (pPacketInfo->eFlags & XFA_XDPPACKET_FLAGS_SUPPORTONE) &&
489              pXFARootNode->GetFirstChildByName(pPacketInfo->uHash)) {
490            return nullptr;
491          }
492          pXFARootNode->InsertChild(pPacketNode);
493        }
494      }
495    }
496  }
497  if (!pXMLTemplateDOMRoot) {
498    // No template is found.
499    return nullptr;
500  }
501  if (pXMLDatasetsDOMRoot) {
502    CXFA_Node* pPacketNode =
503        ParseAsXDPPacket(pXMLDatasetsDOMRoot, XFA_XDPPACKET_Datasets);
504    if (pPacketNode) {
505      pXFARootNode->InsertChild(pPacketNode);
506    }
507  }
508  if (pXMLFormDOMRoot) {
509    CXFA_Node* pPacketNode =
510        ParseAsXDPPacket(pXMLFormDOMRoot, XFA_XDPPACKET_Form);
511    if (pPacketNode) {
512      pXFARootNode->InsertChild(pPacketNode);
513    }
514  }
515  pXFARootNode->SetXMLMappingNode(pXMLDocumentNode);
516  return pXFARootNode;
517}
518CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Config(
519    IFDE_XMLNode* pXMLDocumentNode,
520    XFA_XDPPACKET ePacketID) {
521  if (!XFA_FDEExtension_MatchNodeName(
522          pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_Config)->pName,
523          XFA_GetPacketByIndex(XFA_PACKET_Config)->pURI,
524          XFA_GetPacketByIndex(XFA_PACKET_Config)->eFlags)) {
525    return NULL;
526  }
527  CXFA_Node* pNode =
528      m_pFactory->CreateNode(XFA_XDPPACKET_Config, XFA_ELEMENT_Config);
529  if (!pNode) {
530    return NULL;
531  }
532  pNode->SetCData(XFA_ATTRIBUTE_Name,
533                  XFA_GetPacketByIndex(XFA_PACKET_Config)->pName);
534  if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID)) {
535    return NULL;
536  }
537  pNode->SetXMLMappingNode(pXMLDocumentNode);
538  return pNode;
539}
540CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_TemplateForm(
541    IFDE_XMLNode* pXMLDocumentNode,
542    XFA_XDPPACKET ePacketID) {
543  CXFA_Node* pNode = NULL;
544  if (ePacketID == XFA_XDPPACKET_Template) {
545    if (XFA_FDEExtension_MatchNodeName(
546            pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_Template)->pName,
547            XFA_GetPacketByIndex(XFA_PACKET_Template)->pURI,
548            XFA_GetPacketByIndex(XFA_PACKET_Template)->eFlags)) {
549      pNode =
550          m_pFactory->CreateNode(XFA_XDPPACKET_Template, XFA_ELEMENT_Template);
551      if (!pNode) {
552        return NULL;
553      }
554      pNode->SetCData(XFA_ATTRIBUTE_Name,
555                      XFA_GetPacketByIndex(XFA_PACKET_Template)->pName);
556      if (m_bDocumentParser) {
557        CFX_WideString wsNamespaceURI;
558        IFDE_XMLElement* pXMLDocumentElement =
559            (IFDE_XMLElement*)pXMLDocumentNode;
560        pXMLDocumentElement->GetNamespaceURI(wsNamespaceURI);
561        if (wsNamespaceURI.IsEmpty()) {
562          pXMLDocumentElement->GetString(L"xmlns:xfa", wsNamespaceURI);
563        }
564        pNode->GetDocument()->RecognizeXFAVersionNumber(wsNamespaceURI);
565      }
566      if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID)) {
567        return NULL;
568      }
569    }
570  } else if (ePacketID == XFA_XDPPACKET_Form) {
571    if (XFA_FDEExtension_MatchNodeName(
572            pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_Form)->pName,
573            XFA_GetPacketByIndex(XFA_PACKET_Form)->pURI,
574            XFA_GetPacketByIndex(XFA_PACKET_Form)->eFlags)) {
575      IFDE_XMLElement* pXMLDocumentElement = (IFDE_XMLElement*)pXMLDocumentNode;
576      CFX_WideString wsChecksum;
577      pXMLDocumentElement->GetString(L"checksum", wsChecksum);
578#ifdef _XFA_VERIFY_Checksum_
579      if (wsChecksum.GetLength() != 28 ||
580          m_pXMLParser->m_dwCheckStatus != 0x03) {
581        return NULL;
582      }
583      IXFA_ChecksumContext* pChecksum = XFA_Checksum_Create();
584      pChecksum->StartChecksum();
585      pChecksum->UpdateChecksum(m_pFileRead, m_pXMLParser->m_nStart[0],
586                                m_pXMLParser->m_nSize[0]);
587      pChecksum->UpdateChecksum(m_pFileRead, m_pXMLParser->m_nStart[1],
588                                m_pXMLParser->m_nSize[1]);
589      pChecksum->FinishChecksum();
590      CFX_ByteString bsCheck;
591      pChecksum->GetChecksum(bsCheck);
592      pChecksum->Release();
593      if (bsCheck != wsChecksum.UTF8Encode()) {
594        return NULL;
595      }
596#endif
597      pNode = m_pFactory->CreateNode(XFA_XDPPACKET_Form, XFA_ELEMENT_Form);
598      if (!pNode) {
599        return NULL;
600      }
601      pNode->SetCData(XFA_ATTRIBUTE_Name,
602                      XFA_GetPacketByIndex(XFA_PACKET_Form)->pName);
603      pNode->SetAttribute(XFA_ATTRIBUTE_Checksum, wsChecksum);
604      CXFA_Node* pTemplateRoot =
605          m_pRootNode->GetFirstChildByClass(XFA_ELEMENT_Template);
606      CXFA_Node* pTemplateChosen =
607          pTemplateRoot
608              ? pTemplateRoot->GetFirstChildByClass(XFA_ELEMENT_Subform)
609              : NULL;
610      FX_BOOL bUseAttribute = TRUE;
611      if (pTemplateChosen &&
612          pTemplateChosen->GetEnum(XFA_ATTRIBUTE_RestoreState) !=
613              XFA_ATTRIBUTEENUM_Auto) {
614        bUseAttribute = FALSE;
615      }
616      if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID, bUseAttribute)) {
617        return NULL;
618      }
619    }
620  }
621  if (pNode) {
622    pNode->SetXMLMappingNode(pXMLDocumentNode);
623  }
624  return pNode;
625}
626static IFDE_XMLNode* XFA_GetDataSetsFromXDP(IFDE_XMLNode* pXMLDocumentNode) {
627  if (XFA_FDEExtension_MatchNodeName(
628          pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_Datasets)->pName,
629          XFA_GetPacketByIndex(XFA_PACKET_Datasets)->pURI,
630          XFA_GetPacketByIndex(XFA_PACKET_Datasets)->eFlags)) {
631    return pXMLDocumentNode;
632  }
633  if (!XFA_FDEExtension_MatchNodeName(
634          pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_XDP)->pName,
635          XFA_GetPacketByIndex(XFA_PACKET_XDP)->pURI,
636          XFA_GetPacketByIndex(XFA_PACKET_XDP)->eFlags)) {
637    return NULL;
638  }
639  for (IFDE_XMLNode* pDatasetsNode =
640           pXMLDocumentNode->GetNodeItem(IFDE_XMLNode::FirstChild);
641       pDatasetsNode;
642       pDatasetsNode = pDatasetsNode->GetNodeItem(IFDE_XMLNode::NextSibling)) {
643    if (!XFA_FDEExtension_MatchNodeName(
644            pDatasetsNode, XFA_GetPacketByIndex(XFA_PACKET_Datasets)->pName,
645            XFA_GetPacketByIndex(XFA_PACKET_Datasets)->pURI,
646            XFA_GetPacketByIndex(XFA_PACKET_Datasets)->eFlags)) {
647      continue;
648    }
649    return pDatasetsNode;
650  }
651  return NULL;
652}
653CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Data(
654    IFDE_XMLNode* pXMLDocumentNode,
655    XFA_XDPPACKET ePacketID) {
656  IFDE_XMLNode* pDatasetsXMLNode = XFA_GetDataSetsFromXDP(pXMLDocumentNode);
657  if (pDatasetsXMLNode) {
658    CXFA_Node* pNode =
659        m_pFactory->CreateNode(XFA_XDPPACKET_Datasets, XFA_ELEMENT_DataModel);
660    if (!pNode) {
661      return NULL;
662    }
663    pNode->SetCData(XFA_ATTRIBUTE_Name,
664                    XFA_GetPacketByIndex(XFA_PACKET_Datasets)->pName);
665    if (!DataLoader(pNode, pDatasetsXMLNode, FALSE)) {
666      return NULL;
667    }
668    pNode->SetXMLMappingNode(pDatasetsXMLNode);
669    return pNode;
670  }
671  IFDE_XMLNode* pDataXMLNode = NULL;
672  if (XFA_FDEExtension_MatchNodeName(
673          pXMLDocumentNode, FX_WSTRC(L"data"),
674          XFA_GetPacketByIndex(XFA_PACKET_Datasets)->pURI,
675          XFA_GetPacketByIndex(XFA_PACKET_Datasets)->eFlags)) {
676    ((IFDE_XMLElement*)pXMLDocumentNode)->RemoveAttribute(L"xmlns:xfa");
677    pDataXMLNode = pXMLDocumentNode;
678  } else {
679    IFDE_XMLElement* pDataElement =
680        IFDE_XMLElement::Create(FX_WSTRC(L"xfa:data"));
681    IFDE_XMLNode* pParentXMLNode =
682        pXMLDocumentNode->GetNodeItem(IFDE_XMLNode::Parent);
683    if (pParentXMLNode) {
684      pParentXMLNode->RemoveChildNode(pXMLDocumentNode);
685    }
686    FXSYS_assert(pXMLDocumentNode->GetType() == FDE_XMLNODE_Element);
687    if (pXMLDocumentNode->GetType() == FDE_XMLNODE_Element) {
688      ((IFDE_XMLElement*)pXMLDocumentNode)->RemoveAttribute(L"xmlns:xfa");
689    }
690    pDataElement->InsertChildNode(pXMLDocumentNode);
691    pDataXMLNode = pDataElement;
692  }
693  if (pDataXMLNode) {
694    CXFA_Node* pNode =
695        m_pFactory->CreateNode(XFA_XDPPACKET_Datasets, XFA_ELEMENT_DataGroup);
696    if (!pNode) {
697      if (pDataXMLNode != pXMLDocumentNode) {
698        pDataXMLNode->Release();
699      }
700      return NULL;
701    }
702    CFX_WideString wsLocalName;
703    ((IFDE_XMLElement*)pDataXMLNode)->GetLocalTagName(wsLocalName);
704    pNode->SetCData(XFA_ATTRIBUTE_Name, wsLocalName);
705    if (!DataLoader(pNode, pDataXMLNode, TRUE)) {
706      return NULL;
707    }
708    pNode->SetXMLMappingNode(pDataXMLNode);
709    if (pDataXMLNode != pXMLDocumentNode) {
710      pNode->SetFlag(XFA_NODEFLAG_OwnXMLNode, TRUE, FALSE);
711    }
712    return pNode;
713  }
714  return NULL;
715}
716CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_LocaleConnectionSourceSet(
717    IFDE_XMLNode* pXMLDocumentNode,
718    XFA_XDPPACKET ePacketID) {
719  CXFA_Node* pNode = NULL;
720  if (ePacketID == XFA_XDPPACKET_LocaleSet) {
721    if (XFA_FDEExtension_MatchNodeName(
722            pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_LocaleSet)->pName,
723            XFA_GetPacketByIndex(XFA_PACKET_LocaleSet)->pURI,
724            XFA_GetPacketByIndex(XFA_PACKET_LocaleSet)->eFlags)) {
725      pNode = m_pFactory->CreateNode(XFA_XDPPACKET_LocaleSet,
726                                     XFA_ELEMENT_LocaleSet);
727      if (!pNode) {
728        return NULL;
729      }
730      pNode->SetCData(XFA_ATTRIBUTE_Name,
731                      XFA_GetPacketByIndex(XFA_PACKET_LocaleSet)->pName);
732      if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID)) {
733        return NULL;
734      }
735    }
736  } else if (ePacketID == XFA_XDPPACKET_ConnectionSet) {
737    if (XFA_FDEExtension_MatchNodeName(
738            pXMLDocumentNode,
739            XFA_GetPacketByIndex(XFA_PACKET_ConnectionSet)->pName,
740            XFA_GetPacketByIndex(XFA_PACKET_ConnectionSet)->pURI,
741            XFA_GetPacketByIndex(XFA_PACKET_ConnectionSet)->eFlags)) {
742      pNode = m_pFactory->CreateNode(XFA_XDPPACKET_ConnectionSet,
743                                     XFA_ELEMENT_ConnectionSet);
744      if (!pNode) {
745        return NULL;
746      }
747      pNode->SetCData(XFA_ATTRIBUTE_Name,
748                      XFA_GetPacketByIndex(XFA_PACKET_ConnectionSet)->pName);
749      if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID)) {
750        return NULL;
751      }
752    }
753  } else if (ePacketID == XFA_XDPPACKET_SourceSet) {
754    if (XFA_FDEExtension_MatchNodeName(
755            pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_SourceSet)->pName,
756            XFA_GetPacketByIndex(XFA_PACKET_SourceSet)->pURI,
757            XFA_GetPacketByIndex(XFA_PACKET_SourceSet)->eFlags)) {
758      pNode = m_pFactory->CreateNode(XFA_XDPPACKET_SourceSet,
759                                     XFA_ELEMENT_SourceSet);
760      if (!pNode) {
761        return NULL;
762      }
763      pNode->SetCData(XFA_ATTRIBUTE_Name,
764                      XFA_GetPacketByIndex(XFA_PACKET_SourceSet)->pName);
765      if (!NormalLoader(pNode, pXMLDocumentNode, ePacketID)) {
766        return NULL;
767      }
768    }
769  }
770  if (pNode) {
771    pNode->SetXMLMappingNode(pXMLDocumentNode);
772  }
773  return pNode;
774}
775CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Xdc(
776    IFDE_XMLNode* pXMLDocumentNode,
777    XFA_XDPPACKET ePacketID) {
778  if (XFA_FDEExtension_MatchNodeName(
779          pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_Xdc)->pName,
780          XFA_GetPacketByIndex(XFA_PACKET_Xdc)->pURI,
781          XFA_GetPacketByIndex(XFA_PACKET_Xdc)->eFlags)) {
782    CXFA_Node* pNode =
783        m_pFactory->CreateNode(XFA_XDPPACKET_Xdc, XFA_ELEMENT_Xdc);
784    if (!pNode) {
785      return NULL;
786    }
787    pNode->SetCData(XFA_ATTRIBUTE_Name,
788                    XFA_GetPacketByIndex(XFA_PACKET_Xdc)->pName);
789    pNode->SetXMLMappingNode(pXMLDocumentNode);
790    return pNode;
791  }
792  return NULL;
793}
794CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_User(
795    IFDE_XMLNode* pXMLDocumentNode,
796    XFA_XDPPACKET ePacketID) {
797  CXFA_Node* pNode =
798      m_pFactory->CreateNode(XFA_XDPPACKET_XDP, XFA_ELEMENT_Packet);
799  if (!pNode) {
800    return NULL;
801  }
802  CFX_WideString wsName;
803  ((IFDE_XMLElement*)pXMLDocumentNode)->GetLocalTagName(wsName);
804  pNode->SetCData(XFA_ATTRIBUTE_Name, wsName);
805  if (!UserPacketLoader(pNode, pXMLDocumentNode)) {
806    return NULL;
807  }
808  pNode->SetXMLMappingNode(pXMLDocumentNode);
809  return pNode;
810}
811CXFA_Node* CXFA_SimpleParser::UserPacketLoader(CXFA_Node* pXFANode,
812                                               IFDE_XMLNode* pXMLDoc) {
813  return pXFANode;
814}
815static FX_BOOL XFA_FDEExtension_IsStringAllWhitespace(CFX_WideString wsText) {
816  wsText.TrimRight(L"\x20\x9\xD\xA");
817  return wsText.IsEmpty();
818}
819CXFA_Node* CXFA_SimpleParser::DataLoader(CXFA_Node* pXFANode,
820                                         IFDE_XMLNode* pXMLDoc,
821                                         FX_BOOL bDoTransform) {
822  ParseDataGroup(pXFANode, pXMLDoc, XFA_XDPPACKET_Datasets);
823  return pXFANode;
824}
825CXFA_Node* CXFA_SimpleParser::NormalLoader(CXFA_Node* pXFANode,
826                                           IFDE_XMLNode* pXMLDoc,
827                                           XFA_XDPPACKET ePacketID,
828                                           FX_BOOL bUseAttribute) {
829  FX_BOOL bOneOfPropertyFound = FALSE;
830  for (IFDE_XMLNode* pXMLChild = pXMLDoc->GetNodeItem(IFDE_XMLNode::FirstChild);
831       pXMLChild;
832       pXMLChild = pXMLChild->GetNodeItem(IFDE_XMLNode::NextSibling)) {
833    switch (pXMLChild->GetType()) {
834      case FDE_XMLNODE_Element: {
835        IFDE_XMLElement* pXMLElement = (IFDE_XMLElement*)pXMLChild;
836        CFX_WideString wsTagName;
837        pXMLElement->GetLocalTagName(wsTagName);
838        XFA_LPCELEMENTINFO pElemInfo = XFA_GetElementByName(wsTagName);
839        if (!pElemInfo) {
840          continue;
841        }
842        XFA_LPCPROPERTY pPropertyInfo = XFA_GetPropertyOfElement(
843            pXFANode->GetClassID(), pElemInfo->eName, ePacketID);
844        if (pPropertyInfo &&
845            ((pPropertyInfo->uFlags &
846              (XFA_PROPERTYFLAG_OneOf | XFA_PROPERTYFLAG_DefaultOneOf)) != 0)) {
847          if (bOneOfPropertyFound) {
848            break;
849          }
850          bOneOfPropertyFound = TRUE;
851        }
852        CXFA_Node* pXFAChild =
853            m_pFactory->CreateNode(ePacketID, pElemInfo->eName);
854        if (pXFAChild == NULL) {
855          return NULL;
856        }
857        if (ePacketID == XFA_XDPPACKET_Config) {
858          pXFAChild->SetAttribute(XFA_ATTRIBUTE_Name, wsTagName);
859        }
860        FX_BOOL IsNeedValue = TRUE;
861        for (int32_t i = 0, count = pXMLElement->CountAttributes(); i < count;
862             i++) {
863          CFX_WideString wsAttrQualifiedName;
864          CFX_WideString wsAttrName;
865          CFX_WideString wsAttrValue;
866          pXMLElement->GetAttribute(i, wsAttrQualifiedName, wsAttrValue);
867          XFA_FDEExtension_GetAttributeLocalName(wsAttrQualifiedName,
868                                                 wsAttrName);
869          if (wsAttrName == FX_WSTRC(L"nil") &&
870              wsAttrValue == FX_WSTRC(L"true")) {
871            IsNeedValue = FALSE;
872          }
873          XFA_LPCATTRIBUTEINFO lpAttrInfo = XFA_GetAttributeByName(wsAttrName);
874          if (!lpAttrInfo) {
875            continue;
876          }
877          if (!bUseAttribute && lpAttrInfo->eName != XFA_ATTRIBUTE_Name &&
878              lpAttrInfo->eName != XFA_ATTRIBUTE_Save) {
879            continue;
880          }
881          pXFAChild->SetAttribute(lpAttrInfo->eName, wsAttrValue);
882        }
883        pXFANode->InsertChild(pXFAChild);
884        if (pElemInfo->eName == XFA_ELEMENT_Validate ||
885            pElemInfo->eName == XFA_ELEMENT_Locale) {
886          if (ePacketID == XFA_XDPPACKET_Config) {
887            ParseContentNode(pXFAChild, pXMLElement, ePacketID);
888          } else {
889            NormalLoader(pXFAChild, pXMLElement, ePacketID, bUseAttribute);
890          }
891          break;
892        }
893        switch (pXFAChild->GetObjectType()) {
894          case XFA_OBJECTTYPE_ContentNode:
895          case XFA_OBJECTTYPE_TextNode:
896          case XFA_OBJECTTYPE_NodeC:
897          case XFA_OBJECTTYPE_NodeV:
898            if (IsNeedValue) {
899              ParseContentNode(pXFAChild, pXMLElement, ePacketID);
900            }
901            break;
902          default:
903            NormalLoader(pXFAChild, pXMLElement, ePacketID, bUseAttribute);
904            break;
905        }
906      } break;
907      case FDE_XMLNODE_Instruction:
908        ParseInstruction(pXFANode, (IFDE_XMLInstruction*)pXMLChild, ePacketID);
909        break;
910      default:
911        break;
912    }
913  }
914  return pXFANode;
915}
916FX_BOOL XFA_RecognizeRichText(IFDE_XMLElement* pRichTextXMLNode) {
917  if (pRichTextXMLNode) {
918    CFX_WideString wsNamespaceURI;
919    XFA_FDEExtension_GetElementTagNamespaceURI(pRichTextXMLNode,
920                                               wsNamespaceURI);
921    if (wsNamespaceURI == FX_WSTRC(L"http://www.w3.org/1999/xhtml")) {
922      return TRUE;
923    }
924  }
925  return FALSE;
926}
927class RichTextNodeVisitor {
928 public:
929  static inline IFDE_XMLNode* GetFirstChild(IFDE_XMLNode* pNode) {
930    return pNode->GetNodeItem(IFDE_XMLNode::FirstChild);
931  }
932  static inline IFDE_XMLNode* GetNextSibling(IFDE_XMLNode* pNode) {
933    return pNode->GetNodeItem(IFDE_XMLNode::NextSibling);
934  }
935  static inline IFDE_XMLNode* GetParent(IFDE_XMLNode* pNode) {
936    return pNode->GetNodeItem(IFDE_XMLNode::Parent);
937  }
938};
939#ifndef XFA_PARSE_HAS_LINEIDENTIFIER
940void XFA_ConvertRichTextToPlainText(IFDE_XMLElement* pRichTextXMLNode,
941                                    CFX_WideString& wsOutput) {
942  CXFA_NodeIteratorTemplate<IFDE_XMLNode, RichTextNodeVisitor> sIterator(
943      pRichTextXMLNode);
944  CFX_WideTextBuf wsPlainTextBuf;
945  for (IFDE_XMLNode* pNode = sIterator.GetCurrent(); pNode;
946       pNode = sIterator.MoveToNext()) {
947    switch (pNode->GetType()) {
948      case FDE_XMLNODE_Text: {
949        CFX_WideString wsText;
950        ((IFDE_XMLText*)pNode)->GetText(wsText);
951        wsPlainTextBuf << wsText;
952      } break;
953      case FDE_XMLNODE_CharData: {
954        CFX_WideString wsText;
955        ((IFDE_XMLCharData*)pNode)->GetCharData(wsText);
956        wsPlainTextBuf << wsText;
957      } break;
958      default:
959        break;
960    }
961  }
962  wsOutput = wsPlainTextBuf.GetWideString();
963}
964#endif
965void XFA_ConvertXMLToPlainText(IFDE_XMLElement* pRootXMLNode,
966                               CFX_WideString& wsOutput) {
967  for (IFDE_XMLNode* pXMLChild =
968           pRootXMLNode->GetNodeItem(IFDE_XMLNode::FirstChild);
969       pXMLChild;
970       pXMLChild = pXMLChild->GetNodeItem(IFDE_XMLNode::NextSibling)) {
971#ifdef _DEBUG
972    FDE_XMLNODETYPE nodeType = pXMLChild->GetType();
973#endif
974    switch (pXMLChild->GetType()) {
975      case FDE_XMLNODE_Element: {
976        CFX_WideString wsTextData;
977        ((IFDE_XMLElement*)pXMLChild)->GetTextData(wsTextData);
978        wsTextData += FX_WSTRC(L"\n");
979        wsOutput += wsTextData;
980      } break;
981      case FDE_XMLNODE_Text: {
982        CFX_WideString wsText;
983        ((IFDE_XMLText*)pXMLChild)->GetText(wsText);
984        if (XFA_FDEExtension_IsStringAllWhitespace(wsText)) {
985          continue;
986        } else {
987          wsOutput = wsText;
988        }
989      } break;
990      case FDE_XMLNODE_CharData: {
991        CFX_WideString wsCharData;
992        ((IFDE_XMLCharData*)pXMLChild)->GetCharData(wsCharData);
993        if (XFA_FDEExtension_IsStringAllWhitespace(wsCharData)) {
994          continue;
995        } else {
996          wsOutput = wsCharData;
997        }
998      } break;
999      default:
1000        FXSYS_assert(FALSE);
1001        break;
1002    }
1003  }
1004}
1005void CXFA_SimpleParser::ParseContentNode(CXFA_Node* pXFANode,
1006                                         IFDE_XMLNode* pXMLNode,
1007                                         XFA_XDPPACKET ePacketID) {
1008  XFA_ELEMENT element = XFA_ELEMENT_Sharptext;
1009  if (pXFANode->GetClassID() == XFA_ELEMENT_ExData) {
1010    CFX_WideStringC wsContentType =
1011        pXFANode->GetCData(XFA_ATTRIBUTE_ContentType);
1012    if (wsContentType == FX_WSTRC(L"text/html")) {
1013      element = XFA_ELEMENT_SharpxHTML;
1014    } else if (wsContentType == FX_WSTRC(L"text/xml")) {
1015      element = XFA_ELEMENT_Sharpxml;
1016    }
1017  }
1018  if (element == XFA_ELEMENT_SharpxHTML) {
1019    pXFANode->SetXMLMappingNode(pXMLNode);
1020  }
1021  CFX_WideString wsValue;
1022  for (IFDE_XMLNode* pXMLChild =
1023           pXMLNode->GetNodeItem(IFDE_XMLNode::FirstChild);
1024       pXMLChild;
1025       pXMLChild = pXMLChild->GetNodeItem(IFDE_XMLNode::NextSibling)) {
1026    FDE_XMLNODETYPE eNodeType = pXMLChild->GetType();
1027    if (eNodeType == FDE_XMLNODE_Instruction) {
1028      continue;
1029    }
1030    if (element == XFA_ELEMENT_SharpxHTML) {
1031      if (eNodeType != FDE_XMLNODE_Element) {
1032        break;
1033      }
1034      if (XFA_RecognizeRichText((IFDE_XMLElement*)pXMLChild)) {
1035#ifdef XFA_PARSE_HAS_LINEIDENTIFIER
1036        XFA_GetPlainTextFromRichText((IFDE_XMLElement*)pXMLChild, wsValue);
1037#else
1038        XFA_ConvertRichTextToPlainText((IFDE_XMLElement*)pXMLChild, wsValue);
1039#endif
1040      }
1041    } else if (element == XFA_ELEMENT_Sharpxml) {
1042      if (eNodeType != FDE_XMLNODE_Element) {
1043        break;
1044      }
1045      XFA_ConvertXMLToPlainText((IFDE_XMLElement*)pXMLChild, wsValue);
1046    } else {
1047      if (eNodeType == FDE_XMLNODE_Element) {
1048        break;
1049      }
1050      if (eNodeType == FDE_XMLNODE_Text) {
1051        ((IFDE_XMLText*)pXMLChild)->GetText(wsValue);
1052      } else if (eNodeType == FDE_XMLNODE_CharData) {
1053        ((IFDE_XMLCharData*)pXMLChild)->GetCharData(wsValue);
1054      }
1055    }
1056    break;
1057  }
1058  if (!wsValue.IsEmpty()) {
1059    if (pXFANode->GetObjectType() == XFA_OBJECTTYPE_ContentNode) {
1060      CXFA_Node* pContentRawDataNode =
1061          m_pFactory->CreateNode(ePacketID, element);
1062      FXSYS_assert(pContentRawDataNode);
1063      pContentRawDataNode->SetCData(XFA_ATTRIBUTE_Value, wsValue);
1064      pXFANode->InsertChild(pContentRawDataNode);
1065    } else {
1066      pXFANode->SetCData(XFA_ATTRIBUTE_Value, wsValue);
1067    }
1068  }
1069}
1070void CXFA_SimpleParser::ParseDataGroup(CXFA_Node* pXFANode,
1071                                       IFDE_XMLNode* pXMLNode,
1072                                       XFA_XDPPACKET ePacketID) {
1073  for (IFDE_XMLNode* pXMLChild =
1074           pXMLNode->GetNodeItem(IFDE_XMLNode::FirstChild);
1075       pXMLChild;
1076       pXMLChild = pXMLChild->GetNodeItem(IFDE_XMLNode::NextSibling)) {
1077    switch (pXMLChild->GetType()) {
1078      case FDE_XMLNODE_Element: {
1079        IFDE_XMLElement* pXMLElement = (IFDE_XMLElement*)pXMLChild;
1080        {
1081          CFX_WideString wsNamespaceURI;
1082          XFA_FDEExtension_GetElementTagNamespaceURI(pXMLElement,
1083                                                     wsNamespaceURI);
1084          if (wsNamespaceURI ==
1085                  FX_WSTRC(L"http://www.xfa.com/schema/xfa-package/") ||
1086              wsNamespaceURI ==
1087                  FX_WSTRC(L"http://www.xfa.org/schema/xfa-package/") ||
1088              wsNamespaceURI ==
1089                  FX_WSTRC(L"http://www.w3.org/2001/XMLSchema-instance")) {
1090            continue;
1091          }
1092          if (0) {
1093            continue;
1094          }
1095        }
1096        XFA_ELEMENT eNodeType = XFA_ELEMENT_DataModel;
1097        if (eNodeType == XFA_ELEMENT_DataModel) {
1098          CFX_WideString wsDataNodeAttr;
1099          if (XFA_FDEExtension_FindAttributeWithNS(
1100                  pXMLElement, FX_WSTRC(L"dataNode"),
1101                  FX_WSTRC(L"http://www.xfa.org/schema/xfa-data/1.0/"),
1102                  wsDataNodeAttr)) {
1103            if (wsDataNodeAttr == FX_WSTRC(L"dataGroup")) {
1104              eNodeType = XFA_ELEMENT_DataGroup;
1105            } else if (wsDataNodeAttr == FX_WSTRC(L"dataValue")) {
1106              eNodeType = XFA_ELEMENT_DataValue;
1107            }
1108          }
1109        }
1110        CFX_WideString wsContentType;
1111        if (eNodeType == XFA_ELEMENT_DataModel) {
1112          if (XFA_FDEExtension_FindAttributeWithNS(
1113                  pXMLElement, FX_WSTRC(L"contentType"),
1114                  FX_WSTRC(L"http://www.xfa.org/schema/xfa-data/1.0/"),
1115                  wsContentType)) {
1116            if (!wsContentType.IsEmpty()) {
1117              eNodeType = XFA_ELEMENT_DataValue;
1118            }
1119          }
1120        }
1121        if (eNodeType == XFA_ELEMENT_DataModel) {
1122          for (IFDE_XMLNode* pXMLDataChild =
1123                   pXMLElement->GetNodeItem(IFDE_XMLNode::FirstChild);
1124               pXMLDataChild; pXMLDataChild = pXMLDataChild->GetNodeItem(
1125                                  IFDE_XMLNode::NextSibling)) {
1126            if (pXMLDataChild->GetType() == FDE_XMLNODE_Element) {
1127              if (!XFA_RecognizeRichText((IFDE_XMLElement*)pXMLDataChild)) {
1128                eNodeType = XFA_ELEMENT_DataGroup;
1129                break;
1130              }
1131            }
1132          }
1133        }
1134        if (eNodeType == XFA_ELEMENT_DataModel) {
1135          eNodeType = XFA_ELEMENT_DataValue;
1136        }
1137        CXFA_Node* pXFAChild =
1138            m_pFactory->CreateNode(XFA_XDPPACKET_Datasets, eNodeType);
1139        if (pXFAChild == NULL) {
1140          return;
1141        }
1142        CFX_WideString wsNodeName;
1143        pXMLElement->GetLocalTagName(wsNodeName);
1144        pXFAChild->SetCData(XFA_ATTRIBUTE_Name, wsNodeName);
1145        FX_BOOL bNeedValue = TRUE;
1146        if (1) {
1147          for (int32_t i = 0, count = pXMLElement->CountAttributes(); i < count;
1148               i++) {
1149            CFX_WideString wsAttrQualifiedName;
1150            CFX_WideString wsAttrValue;
1151            CFX_WideString wsAttrName;
1152            CFX_WideString wsAttrNamespaceURI;
1153            pXMLElement->GetAttribute(i, wsAttrQualifiedName, wsAttrValue);
1154            if (!XFA_FDEExtension_ResolveAttribute(
1155                    pXMLElement, wsAttrQualifiedName, wsAttrName,
1156                    wsAttrNamespaceURI)) {
1157              continue;
1158            }
1159            if (wsAttrName == FX_WSTRC(L"nil") &&
1160                wsAttrValue == FX_WSTRC(L"true")) {
1161              bNeedValue = FALSE;
1162              continue;
1163            }
1164            if (wsAttrNamespaceURI ==
1165                    FX_WSTRC(L"http://www.xfa.com/schema/xfa-package/") ||
1166                wsAttrNamespaceURI ==
1167                    FX_WSTRC(L"http://www.xfa.org/schema/xfa-package/") ||
1168                wsAttrNamespaceURI ==
1169                    FX_WSTRC(L"http://www.w3.org/2001/XMLSchema-instance") ||
1170                wsAttrNamespaceURI ==
1171                    FX_WSTRC(L"http://www.xfa.org/schema/xfa-data/1.0/")) {
1172              continue;
1173            }
1174            if (0) {
1175              continue;
1176            }
1177            CXFA_Node* pXFAMetaData = m_pFactory->CreateNode(
1178                XFA_XDPPACKET_Datasets, XFA_ELEMENT_DataValue);
1179            if (pXFAMetaData == NULL) {
1180              return;
1181            }
1182            pXFAMetaData->SetCData(XFA_ATTRIBUTE_Name, wsAttrName);
1183            pXFAMetaData->SetCData(XFA_ATTRIBUTE_QualifiedName,
1184                                   wsAttrQualifiedName);
1185            pXFAMetaData->SetCData(XFA_ATTRIBUTE_Value, wsAttrValue);
1186            pXFAMetaData->SetEnum(XFA_ATTRIBUTE_Contains,
1187                                  XFA_ATTRIBUTEENUM_MetaData);
1188            pXFAChild->InsertChild(pXFAMetaData);
1189            pXFAMetaData->SetXMLMappingNode(pXMLElement);
1190            pXFAMetaData->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
1191          }
1192          if (!bNeedValue) {
1193            CFX_WideString wsNilName = FX_WSTRC(L"xsi:nil");
1194            pXMLElement->RemoveAttribute(wsNilName);
1195          }
1196        }
1197        pXFANode->InsertChild(pXFAChild);
1198        if (eNodeType == XFA_ELEMENT_DataGroup) {
1199          ParseDataGroup(pXFAChild, pXMLElement, ePacketID);
1200        } else {
1201          if (bNeedValue) {
1202            ParseDataValue(pXFAChild, pXMLChild, XFA_XDPPACKET_Datasets);
1203          }
1204        }
1205        pXFAChild->SetXMLMappingNode(pXMLElement);
1206        pXFAChild->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
1207      }
1208        continue;
1209      case FDE_XMLNODE_CharData: {
1210        IFDE_XMLCharData* pXMLCharData = (IFDE_XMLCharData*)pXMLChild;
1211        CFX_WideString wsCharData;
1212        pXMLCharData->GetCharData(wsCharData);
1213        if (XFA_FDEExtension_IsStringAllWhitespace(wsCharData)) {
1214          continue;
1215        }
1216        CXFA_Node* pXFAChild = m_pFactory->CreateNode(XFA_XDPPACKET_Datasets,
1217                                                      XFA_ELEMENT_DataValue);
1218        if (pXFAChild == NULL) {
1219          return;
1220        }
1221        pXFAChild->SetCData(XFA_ATTRIBUTE_Value, wsCharData);
1222        pXFANode->InsertChild(pXFAChild);
1223        pXFAChild->SetXMLMappingNode(pXMLCharData);
1224        pXFAChild->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
1225      }
1226        continue;
1227      case FDE_XMLNODE_Text: {
1228        IFDE_XMLText* pXMLText = (IFDE_XMLText*)pXMLChild;
1229        CFX_WideString wsText;
1230        pXMLText->GetText(wsText);
1231        if (XFA_FDEExtension_IsStringAllWhitespace(wsText)) {
1232          continue;
1233        }
1234        CXFA_Node* pXFAChild = m_pFactory->CreateNode(XFA_XDPPACKET_Datasets,
1235                                                      XFA_ELEMENT_DataValue);
1236        if (pXFAChild == NULL) {
1237          return;
1238        }
1239        pXFAChild->SetCData(XFA_ATTRIBUTE_Value, wsText);
1240        pXFANode->InsertChild(pXFAChild);
1241        pXFAChild->SetXMLMappingNode(pXMLText);
1242        pXFAChild->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
1243      }
1244        continue;
1245      case FDE_XMLNODE_Instruction:
1246        continue;
1247      default:
1248        continue;
1249    }
1250  }
1251}
1252void CXFA_SimpleParser::ParseDataValue(CXFA_Node* pXFANode,
1253                                       IFDE_XMLNode* pXMLNode,
1254                                       XFA_XDPPACKET ePacketID) {
1255  CFX_WideTextBuf wsValueTextBuf;
1256  CFX_WideTextBuf wsCurValueTextBuf;
1257  FX_BOOL bMarkAsCompound = FALSE;
1258  IFDE_XMLNode* pXMLCurValueNode = NULL;
1259  for (IFDE_XMLNode* pXMLChild =
1260           pXMLNode->GetNodeItem(IFDE_XMLNode::FirstChild);
1261       pXMLChild;
1262       pXMLChild = pXMLChild->GetNodeItem(IFDE_XMLNode::NextSibling)) {
1263    FDE_XMLNODETYPE eNodeType = pXMLChild->GetType();
1264    if (eNodeType == FDE_XMLNODE_Instruction) {
1265      continue;
1266    }
1267    CFX_WideString wsText;
1268    if (eNodeType == FDE_XMLNODE_Text) {
1269      ((IFDE_XMLText*)pXMLChild)->GetText(wsText);
1270      if (!pXMLCurValueNode) {
1271        pXMLCurValueNode = pXMLChild;
1272      }
1273      wsCurValueTextBuf << wsText;
1274    } else if (eNodeType == FDE_XMLNODE_CharData) {
1275      ((IFDE_XMLCharData*)pXMLChild)->GetCharData(wsText);
1276      if (!pXMLCurValueNode) {
1277        pXMLCurValueNode = pXMLChild;
1278      }
1279      wsCurValueTextBuf << wsText;
1280    } else if (XFA_RecognizeRichText((IFDE_XMLElement*)pXMLChild)) {
1281#ifdef XFA_PARSE_HAS_LINEIDENTIFIER
1282      XFA_GetPlainTextFromRichText((IFDE_XMLElement*)pXMLChild, wsText);
1283#else
1284      XFA_ConvertRichTextToPlainText((IFDE_XMLElement*)pXMLChild, wsText);
1285#endif
1286      if (!pXMLCurValueNode) {
1287        pXMLCurValueNode = pXMLChild;
1288      }
1289      wsCurValueTextBuf << wsText;
1290    } else {
1291      bMarkAsCompound = TRUE;
1292      if (pXMLCurValueNode) {
1293        CFX_WideStringC wsCurValue = wsCurValueTextBuf.GetWideString();
1294        if (!wsCurValue.IsEmpty()) {
1295          CXFA_Node* pXFAChild =
1296              m_pFactory->CreateNode(ePacketID, XFA_ELEMENT_DataValue);
1297          if (pXFAChild == NULL) {
1298            return;
1299          }
1300          pXFAChild->SetCData(XFA_ATTRIBUTE_Name, FX_WSTRC(L""));
1301          pXFAChild->SetCData(XFA_ATTRIBUTE_Value, wsCurValue);
1302          pXFANode->InsertChild(pXFAChild);
1303          pXFAChild->SetXMLMappingNode(pXMLCurValueNode);
1304          pXFAChild->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
1305          wsValueTextBuf << wsCurValue;
1306          wsCurValueTextBuf.Clear();
1307        }
1308        pXMLCurValueNode = NULL;
1309      }
1310      CXFA_Node* pXFAChild =
1311          m_pFactory->CreateNode(ePacketID, XFA_ELEMENT_DataValue);
1312      if (pXFAChild == NULL) {
1313        return;
1314      }
1315      CFX_WideString wsNodeStr;
1316      ((IFDE_XMLElement*)pXMLChild)->GetLocalTagName(wsNodeStr);
1317      pXFAChild->SetCData(XFA_ATTRIBUTE_Name, wsNodeStr);
1318      ParseDataValue(pXFAChild, pXMLChild, ePacketID);
1319      pXFANode->InsertChild(pXFAChild);
1320      pXFAChild->SetXMLMappingNode(pXMLChild);
1321      pXFAChild->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
1322      CFX_WideStringC wsCurValue = pXFAChild->GetCData(XFA_ATTRIBUTE_Value);
1323      wsValueTextBuf << wsCurValue;
1324    }
1325  }
1326  if (pXMLCurValueNode) {
1327    CFX_WideStringC wsCurValue = wsCurValueTextBuf.GetWideString();
1328    if (!wsCurValue.IsEmpty()) {
1329      if (bMarkAsCompound) {
1330        CXFA_Node* pXFAChild =
1331            m_pFactory->CreateNode(ePacketID, XFA_ELEMENT_DataValue);
1332        if (pXFAChild == NULL) {
1333          return;
1334        }
1335        pXFAChild->SetCData(XFA_ATTRIBUTE_Name, FX_WSTRC(L""));
1336        pXFAChild->SetCData(XFA_ATTRIBUTE_Value, wsCurValue);
1337        pXFANode->InsertChild(pXFAChild);
1338        pXFAChild->SetXMLMappingNode(pXMLCurValueNode);
1339        pXFAChild->SetFlag(XFA_NODEFLAG_Initialized, TRUE, FALSE);
1340      }
1341      wsValueTextBuf << wsCurValue;
1342      wsCurValueTextBuf.Clear();
1343    }
1344    pXMLCurValueNode = NULL;
1345  }
1346  CFX_WideStringC wsNodeValue = wsValueTextBuf.GetWideString();
1347  pXFANode->SetCData(XFA_ATTRIBUTE_Value, wsNodeValue);
1348}
1349void CXFA_SimpleParser::ParseInstruction(CXFA_Node* pXFANode,
1350                                         IFDE_XMLInstruction* pXMLInstruction,
1351                                         XFA_XDPPACKET ePacketID) {
1352  if (!m_bDocumentParser) {
1353    return;
1354  }
1355  CFX_WideString wsTargetName;
1356  pXMLInstruction->GetTargetName(wsTargetName);
1357  if (wsTargetName == FX_WSTRC(L"originalXFAVersion")) {
1358    CFX_WideString wsData;
1359    if (pXMLInstruction->GetData(0, wsData) &&
1360        (pXFANode->GetDocument()->RecognizeXFAVersionNumber(wsData) !=
1361         XFA_VERSION_UNKNOWN)) {
1362      wsData.Empty();
1363      if (pXMLInstruction->GetData(1, wsData) &&
1364          wsData == FX_WSTRC(L"v2.7-scripting:1")) {
1365        pXFANode->GetDocument()->SetFlag(XFA_DOCFLAG_Scripting, TRUE);
1366      }
1367    }
1368  } else if (wsTargetName == FX_WSTRC(L"acrobat")) {
1369    CFX_WideString wsData;
1370    if (pXMLInstruction->GetData(0, wsData) &&
1371        wsData == FX_WSTRC(L"JavaScript")) {
1372      if (pXMLInstruction->GetData(1, wsData) &&
1373          wsData == FX_WSTRC(L"strictScoping")) {
1374        pXFANode->GetDocument()->SetFlag(XFA_DOCFLAG_StrictScoping, TRUE);
1375      }
1376    }
1377  }
1378}
1379void CXFA_SimpleParser::CloseParser() {
1380  if (m_pXMLDoc) {
1381    m_pXMLDoc->Release();
1382    m_pXMLDoc = NULL;
1383  }
1384  if (m_pStream) {
1385    m_pStream->Release();
1386    m_pStream = NULL;
1387  }
1388}
1389IXFA_DocParser* IXFA_DocParser::Create(IXFA_Notify* pNotify) {
1390  return new CXFA_DocumentParser(pNotify);
1391}
1392CXFA_DocumentParser::CXFA_DocumentParser(IXFA_Notify* pNotify)
1393    : m_nodeParser(NULL, TRUE), m_pNotify(pNotify), m_pDocument(NULL) {
1394}
1395CXFA_DocumentParser::~CXFA_DocumentParser() {
1396  CloseParser();
1397}
1398int32_t CXFA_DocumentParser::StartParse(IFX_FileRead* pStream,
1399                                        XFA_XDPPACKET ePacketID) {
1400  CloseParser();
1401  int32_t nRetStatus = m_nodeParser.StartParse(pStream, ePacketID);
1402  if (nRetStatus == XFA_PARSESTATUS_Ready) {
1403    m_pDocument = new CXFA_Document(this);
1404    m_nodeParser.SetFactory(m_pDocument);
1405  }
1406  return nRetStatus;
1407}
1408int32_t CXFA_DocumentParser::DoParse(IFX_Pause* pPause) {
1409  int32_t nRetStatus = m_nodeParser.DoParse(pPause);
1410  if (nRetStatus >= XFA_PARSESTATUS_Done) {
1411    FXSYS_assert(m_pDocument);
1412    m_pDocument->SetRoot(m_nodeParser.GetRootNode());
1413  }
1414  return nRetStatus;
1415}
1416int32_t CXFA_DocumentParser::ParseXMLData(const CFX_WideString& wsXML,
1417                                          IFDE_XMLNode*& pXMLNode,
1418                                          IFX_Pause* pPause) {
1419  CloseParser();
1420  int32_t nRetStatus = m_nodeParser.ParseXMLData(wsXML, pXMLNode, NULL);
1421  if (nRetStatus == XFA_PARSESTATUS_Done && pXMLNode) {
1422    m_pDocument = new CXFA_Document(this);
1423    m_nodeParser.SetFactory(m_pDocument);
1424  }
1425  return nRetStatus;
1426}
1427void CXFA_DocumentParser::ConstructXFANode(CXFA_Node* pXFANode,
1428                                           IFDE_XMLNode* pXMLNode) {
1429  if (!pXFANode || !pXMLNode) {
1430    return;
1431  }
1432  m_nodeParser.ConstructXFANode(pXFANode, pXMLNode);
1433  CXFA_Node* pRootNode = m_nodeParser.GetRootNode();
1434  if (m_pDocument && pRootNode) {
1435    m_pDocument->SetRoot(pRootNode);
1436  }
1437}
1438void CXFA_DocumentParser::CloseParser() {
1439  if (m_pDocument) {
1440    delete m_pDocument;
1441    m_pDocument = NULL;
1442  }
1443  m_nodeParser.CloseParser();
1444}
1445CXFA_XMLParser::CXFA_XMLParser(IFDE_XMLNode* pRoot, IFX_Stream* pStream)
1446    :
1447#ifdef _XFA_VERIFY_Checksum_
1448      m_nElementStart(0),
1449      m_dwCheckStatus(0),
1450      m_dwCurrentCheckStatus(0),
1451#endif
1452      m_pRoot(pRoot),
1453      m_pStream(pStream),
1454      m_pParser(nullptr),
1455      m_pParent(pRoot),
1456      m_pChild(nullptr),
1457      m_NodeStack(16),
1458      m_dwStatus(FDE_XMLSYNTAXSTATUS_None) {
1459  ASSERT(m_pParent && m_pStream);
1460  m_NodeStack.Push(m_pParent);
1461  m_pParser = IFDE_XMLSyntaxParser::Create();
1462  m_pParser->Init(m_pStream, 32 * 1024, 1024 * 1024);
1463}
1464CXFA_XMLParser::~CXFA_XMLParser() {
1465  if (m_pParser) {
1466    m_pParser->Release();
1467  }
1468  m_NodeStack.RemoveAll();
1469  m_ws1.Empty();
1470  m_ws2.Empty();
1471}
1472int32_t CXFA_XMLParser::DoParser(IFX_Pause* pPause) {
1473  if (m_dwStatus == FDE_XMLSYNTAXSTATUS_Error) {
1474    return -1;
1475  }
1476  if (m_dwStatus == FDE_XMLSYNTAXSTATUS_EOS) {
1477    return 100;
1478  }
1479  int32_t iCount = 0;
1480  while (TRUE) {
1481    m_dwStatus = m_pParser->DoSyntaxParse();
1482    switch (m_dwStatus) {
1483      case FDE_XMLSYNTAXSTATUS_InstructionOpen:
1484        break;
1485      case FDE_XMLSYNTAXSTATUS_InstructionClose:
1486        if (m_pChild) {
1487          if (m_pChild->GetType() != FDE_XMLNODE_Instruction) {
1488            m_dwStatus = FDE_XMLSYNTAXSTATUS_Error;
1489            break;
1490          }
1491        }
1492        m_pChild = m_pParent;
1493        break;
1494      case FDE_XMLSYNTAXSTATUS_ElementOpen:
1495#ifdef _XFA_VERIFY_Checksum_
1496        if (m_dwCheckStatus != 0x03 && m_NodeStack.GetSize() == 2) {
1497          m_nElementStart = m_pParser->GetCurrentPos() - 1;
1498        }
1499#endif
1500        break;
1501      case FDE_XMLSYNTAXSTATUS_ElementBreak:
1502        break;
1503      case FDE_XMLSYNTAXSTATUS_ElementClose:
1504        if (m_pChild->GetType() != FDE_XMLNODE_Element) {
1505          m_dwStatus = FDE_XMLSYNTAXSTATUS_Error;
1506          break;
1507        }
1508        m_pParser->GetTagName(m_ws1);
1509        ((IFDE_XMLElement*)m_pChild)->GetTagName(m_ws2);
1510        if (m_ws1.GetLength() > 0 && !m_ws1.Equal(m_ws2)) {
1511          m_dwStatus = FDE_XMLSYNTAXSTATUS_Error;
1512          break;
1513        }
1514        m_NodeStack.Pop();
1515        if (m_NodeStack.GetSize() < 1) {
1516          m_dwStatus = FDE_XMLSYNTAXSTATUS_Error;
1517          break;
1518        }
1519#ifdef _XFA_VERIFY_Checksum_
1520        else if (m_dwCurrentCheckStatus != 0 && m_NodeStack.GetSize() == 2) {
1521          m_nSize[m_dwCurrentCheckStatus - 1] =
1522              m_pParser->GetCurrentBinaryPos() -
1523              m_nStart[m_dwCurrentCheckStatus - 1];
1524          m_dwCurrentCheckStatus = 0;
1525        }
1526#endif
1527        m_pParent = (IFDE_XMLNode*)*m_NodeStack.GetTopElement();
1528        m_pChild = m_pParent;
1529        iCount++;
1530        break;
1531      case FDE_XMLSYNTAXSTATUS_TargetName:
1532        m_pParser->GetTargetName(m_ws1);
1533        if (m_ws1 == FX_WSTRC(L"originalXFAVersion") ||
1534            m_ws1 == FX_WSTRC(L"acrobat")) {
1535          m_pChild = IFDE_XMLInstruction::Create(m_ws1);
1536          m_pParent->InsertChildNode(m_pChild);
1537        } else {
1538          m_pChild = NULL;
1539        }
1540        m_ws1.Empty();
1541        break;
1542      case FDE_XMLSYNTAXSTATUS_TagName:
1543        m_pParser->GetTagName(m_ws1);
1544        m_pChild = IFDE_XMLElement::Create(m_ws1);
1545        m_pParent->InsertChildNode(m_pChild);
1546        m_NodeStack.Push(m_pChild);
1547        m_pParent = m_pChild;
1548#ifdef _XFA_VERIFY_Checksum_
1549        if (m_dwCheckStatus != 0x03 && m_NodeStack.GetSize() == 3) {
1550          CFX_WideString wsTag;
1551          ((IFDE_XMLElement*)m_pChild)->GetLocalTagName(wsTag);
1552          if (wsTag == FX_WSTRC(L"template")) {
1553            m_dwCheckStatus |= 0x01;
1554            m_dwCurrentCheckStatus = 0x01;
1555            m_nStart[0] = m_pParser->GetCurrentBinaryPos() -
1556                          (m_pParser->GetCurrentPos() - m_nElementStart);
1557          } else if (wsTag == FX_WSTRC(L"datasets")) {
1558            m_dwCheckStatus |= 0x02;
1559            m_dwCurrentCheckStatus = 0x02;
1560            m_nStart[1] = m_pParser->GetCurrentBinaryPos() -
1561                          (m_pParser->GetCurrentPos() - m_nElementStart);
1562          }
1563        }
1564#endif
1565        break;
1566      case FDE_XMLSYNTAXSTATUS_AttriName:
1567        m_pParser->GetAttributeName(m_ws1);
1568        break;
1569      case FDE_XMLSYNTAXSTATUS_AttriValue:
1570        if (m_pChild) {
1571          m_pParser->GetAttributeName(m_ws2);
1572          if (m_pChild->GetType() == FDE_XMLNODE_Element) {
1573            ((IFDE_XMLElement*)m_pChild)->SetString(m_ws1, m_ws2);
1574          }
1575        }
1576        m_ws1.Empty();
1577        break;
1578      case FDE_XMLSYNTAXSTATUS_Text:
1579        m_pParser->GetTextData(m_ws1);
1580        m_pChild = IFDE_XMLText::Create(m_ws1);
1581        m_pParent->InsertChildNode(m_pChild);
1582        m_pChild = m_pParent;
1583        break;
1584      case FDE_XMLSYNTAXSTATUS_CData:
1585        m_pParser->GetTextData(m_ws1);
1586        m_pChild = IFDE_XMLCharData::Create(m_ws1);
1587        m_pParent->InsertChildNode(m_pChild);
1588        m_pChild = m_pParent;
1589        break;
1590      case FDE_XMLSYNTAXSTATUS_TargetData:
1591        if (m_pChild) {
1592          if (m_pChild->GetType() != FDE_XMLNODE_Instruction) {
1593            m_dwStatus = FDE_XMLSYNTAXSTATUS_Error;
1594            break;
1595          }
1596          if (!m_ws1.IsEmpty()) {
1597            ((IFDE_XMLInstruction*)m_pChild)->AppendData(m_ws1);
1598          }
1599          m_pParser->GetTargetData(m_ws1);
1600          ((IFDE_XMLInstruction*)m_pChild)->AppendData(m_ws1);
1601        }
1602        m_ws1.Empty();
1603        break;
1604      default:
1605        break;
1606    }
1607    if (m_dwStatus == FDE_XMLSYNTAXSTATUS_Error ||
1608        m_dwStatus == FDE_XMLSYNTAXSTATUS_EOS) {
1609      break;
1610    }
1611    if (pPause != NULL && iCount > 500 && pPause->NeedToPauseNow()) {
1612      break;
1613    }
1614  }
1615  return (m_dwStatus == FDE_XMLSYNTAXSTATUS_Error || m_NodeStack.GetSize() != 1)
1616             ? -1
1617             : m_pParser->GetStatus();
1618}
1619