Home | History | Annotate | Download | only in parser
      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 "core/fxcrt/xml/cfx_xmlnode.h"
     13 #include "third_party/base/ptr_util.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_node.h"
     18 #include "xfa/fxfa/parser/cxfa_simple_parser.h"
     19 
     20 CXFA_DataImporter::CXFA_DataImporter(CXFA_Document* pDocument)
     21     : m_pDocument(pDocument) {
     22   ASSERT(m_pDocument);
     23 }
     24 
     25 CXFA_DataImporter::~CXFA_DataImporter() {}
     26 
     27 bool CXFA_DataImporter::ImportData(
     28     const RetainPtr<IFX_SeekableStream>& pDataDocument) {
     29   auto pDataDocumentParser =
     30       pdfium::MakeUnique<CXFA_SimpleParser>(m_pDocument.Get());
     31   if (pDataDocumentParser->StartParse(
     32           pDataDocument, XFA_PacketType::Datasets) != XFA_PARSESTATUS_Ready) {
     33     return false;
     34   }
     35   if (pDataDocumentParser->DoParse() < XFA_PARSESTATUS_Done)
     36     return false;
     37 
     38   CXFA_Node* pImportDataRoot = pDataDocumentParser->GetRootNode();
     39   if (!pImportDataRoot)
     40     return false;
     41 
     42   CXFA_Node* pDataModel =
     43       ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Datasets));
     44   if (!pDataModel)
     45     return false;
     46 
     47   CXFA_Node* pDataNode = ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Data));
     48   if (pDataNode)
     49     pDataModel->RemoveChild(pDataNode, true);
     50 
     51   if (pImportDataRoot->GetElementType() == XFA_Element::DataModel) {
     52     while (CXFA_Node* pChildNode = pImportDataRoot->GetFirstChild()) {
     53       pImportDataRoot->RemoveChild(pChildNode, true);
     54       pDataModel->InsertChild(pChildNode, nullptr);
     55     }
     56   } else {
     57     CFX_XMLNode* pXMLNode = pImportDataRoot->GetXMLMappingNode();
     58     CFX_XMLNode* pParentXMLNode = pXMLNode->GetNodeItem(CFX_XMLNode::Parent);
     59     if (pParentXMLNode)
     60       pParentXMLNode->RemoveChildNode(pXMLNode);
     61     pDataModel->InsertChild(pImportDataRoot, nullptr);
     62   }
     63   m_pDocument->DoDataRemerge(false);
     64   return true;
     65 }
     66