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