1 // Copyright 2016 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/cxfa_dataimporter.h" 8 9 #include <memory> 10 11 #include "core/fxcrt/fx_stream.h" 12 #include "third_party/base/ptr_util.h" 13 #include "xfa/fde/xml/fde_xml_imp.h" 14 #include "xfa/fxfa/fxfa.h" 15 #include "xfa/fxfa/fxfa_basic.h" 16 #include "xfa/fxfa/parser/cxfa_document.h" 17 #include "xfa/fxfa/parser/cxfa_simple_parser.h" 18 #include "xfa/fxfa/parser/xfa_object.h" 19 CXFA_DataImporter(CXFA_Document * pDocument)20CXFA_DataImporter::CXFA_DataImporter(CXFA_Document* pDocument) 21 : m_pDocument(pDocument) { 22 ASSERT(m_pDocument); 23 } 24 ImportData(const CFX_RetainPtr<IFX_SeekableReadStream> & pDataDocument)25bool CXFA_DataImporter::ImportData( 26 const CFX_RetainPtr<IFX_SeekableReadStream>& pDataDocument) { 27 auto pDataDocumentParser = 28 pdfium::MakeUnique<CXFA_SimpleParser>(m_pDocument, false); 29 if (pDataDocumentParser->StartParse(pDataDocument, XFA_XDPPACKET_Datasets) != 30 XFA_PARSESTATUS_Ready) { 31 return false; 32 } 33 if (pDataDocumentParser->DoParse(nullptr) < XFA_PARSESTATUS_Done) 34 return false; 35 36 CXFA_Node* pImportDataRoot = pDataDocumentParser->GetRootNode(); 37 if (!pImportDataRoot) 38 return false; 39 40 CXFA_Node* pDataModel = 41 ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Datasets)); 42 if (!pDataModel) 43 return false; 44 45 CXFA_Node* pDataNode = ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Data)); 46 if (pDataNode) 47 pDataModel->RemoveChild(pDataNode); 48 49 if (pImportDataRoot->GetElementType() == XFA_Element::DataModel) { 50 while (CXFA_Node* pChildNode = 51 pImportDataRoot->GetNodeItem(XFA_NODEITEM_FirstChild)) { 52 pImportDataRoot->RemoveChild(pChildNode); 53 pDataModel->InsertChild(pChildNode); 54 } 55 } else { 56 CFDE_XMLNode* pXMLNode = pImportDataRoot->GetXMLMappingNode(); 57 CFDE_XMLNode* pParentXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::Parent); 58 if (pParentXMLNode) 59 pParentXMLNode->RemoveChildNode(pXMLNode); 60 pDataModel->InsertChild(pImportDataRoot); 61 } 62 m_pDocument->DoDataRemerge(false); 63 return true; 64 } 65