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