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/cxfa_dataexporter.h"
8 
9 #include "core/fxcrt/fx_codepage.h"
10 #include "core/fxcrt/xml/cfx_xmlelement.h"
11 #include "core/fxcrt/xml/cfx_xmlnode.h"
12 #include "xfa/fxfa/parser/cxfa_document.h"
13 #include "xfa/fxfa/parser/cxfa_node.h"
14 #include "xfa/fxfa/parser/xfa_utils.h"
15 
16 CXFA_DataExporter::CXFA_DataExporter() = default;
17 
18 CXFA_DataExporter::~CXFA_DataExporter() = default;
19 
Export(const RetainPtr<IFX_SeekableStream> & pStream,CXFA_Node * pNode)20 bool CXFA_DataExporter::Export(const RetainPtr<IFX_SeekableStream>& pStream,
21                                CXFA_Node* pNode) {
22   ASSERT(pStream);
23 
24   if (!pStream)
25     return false;
26 
27   if (pNode->IsModelNode()) {
28     switch (pNode->GetPacketType()) {
29       case XFA_PacketType::Xdp: {
30         pStream->WriteString(
31             "<xdp:xdp xmlns:xdp=\"http://ns.adobe.com/xdp/\">");
32         for (CXFA_Node* pChild = pNode->GetFirstChild(); pChild;
33              pChild = pChild->GetNextSibling()) {
34           Export(pStream, pChild);
35         }
36         pStream->WriteString("</xdp:xdp\n>");
37         break;
38       }
39       case XFA_PacketType::Datasets: {
40         CFX_XMLElement* pElement = ToXMLElement(pNode->GetXMLMappingNode());
41         if (!pElement)
42           return false;
43 
44         CXFA_Node* pDataNode = pNode->GetFirstChild();
45         ASSERT(pDataNode);
46         XFA_DataExporter_DealWithDataGroupNode(pDataNode);
47         pElement->Save(pStream);
48         break;
49       }
50       case XFA_PacketType::Form:
51         XFA_DataExporter_RegenerateFormFile(pNode, pStream, false);
52         break;
53       case XFA_PacketType::Template:
54       default: {
55         CFX_XMLElement* pElement = ToXMLElement(pNode->GetXMLMappingNode());
56         if (!pElement)
57           return false;
58 
59         pElement->Save(pStream);
60         break;
61       }
62     }
63     return true;
64   }
65 
66   CXFA_Node* pDataNode = pNode->GetParent();
67   CXFA_Node* pExportNode = pNode;
68   for (CXFA_Node* pChildNode = pDataNode->GetFirstChild(); pChildNode;
69        pChildNode = pChildNode->GetNextSibling()) {
70     if (pChildNode != pNode) {
71       pExportNode = pDataNode;
72       break;
73     }
74   }
75   CFX_XMLElement* pElement = ToXMLElement(pExportNode->GetXMLMappingNode());
76   if (!pElement)
77     return false;
78 
79   XFA_DataExporter_DealWithDataGroupNode(pExportNode);
80   pElement->SetAttribute(L"xmlns:xfa",
81                          L"http://www.xfa.org/schema/xfa-data/1.0/");
82   pElement->Save(pStream);
83   pElement->RemoveAttribute(L"xmlns:xfa");
84   return true;
85 }
86