// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "xfa/fxfa/parser/xfa_document_serialize.h"
#include "xfa/fde/xml/fde_xml_imp.h"
#include "xfa/fgas/crt/fgas_codepage.h"
#include "xfa/fxfa/fm2js/xfa_fm2jsapi.h"
#include "xfa/fxfa/parser/xfa_docdata.h"
#include "xfa/fxfa/parser/xfa_doclayout.h"
#include "xfa/fxfa/parser/xfa_document.h"
#include "xfa/fxfa/parser/xfa_localemgr.h"
#include "xfa/fxfa/parser/xfa_object.h"
#include "xfa/fxfa/parser/xfa_parser.h"
#include "xfa/fxfa/parser/xfa_parser_imp.h"
#include "xfa/fxfa/parser/xfa_script.h"
#include "xfa/fxfa/parser/xfa_utils.h"
CXFA_DataImporter::CXFA_DataImporter(CXFA_Document* pDocument)
: m_pDocument(pDocument) {
ASSERT(m_pDocument);
}
FX_BOOL CXFA_DataImporter::ImportData(IFX_FileRead* pDataDocument) {
IXFA_Parser* pDataDocumentParser = IXFA_Parser::Create(m_pDocument);
if (!pDataDocumentParser) {
return FALSE;
}
if (pDataDocumentParser->StartParse(pDataDocument, XFA_XDPPACKET_Datasets) !=
XFA_PARSESTATUS_Ready) {
pDataDocumentParser->Release();
return FALSE;
}
if (pDataDocumentParser->DoParse(NULL) < XFA_PARSESTATUS_Done) {
pDataDocumentParser->Release();
return FALSE;
}
CXFA_Node* pImportDataRoot = pDataDocumentParser->GetRootNode();
if (!pImportDataRoot) {
pDataDocumentParser->Release();
return FALSE;
}
CXFA_Node* pDataModel =
ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Datasets));
if (!pDataModel) {
pDataDocumentParser->Release();
return FALSE;
}
CXFA_Node* pDataNode = ToNode(m_pDocument->GetXFAObject(XFA_HASHCODE_Data));
if (pDataNode) {
pDataModel->RemoveChild(pDataNode);
}
if (pImportDataRoot->GetClassID() == XFA_ELEMENT_DataModel) {
while (CXFA_Node* pChildNode =
pImportDataRoot->GetNodeItem(XFA_NODEITEM_FirstChild)) {
pImportDataRoot->RemoveChild(pChildNode);
pDataModel->InsertChild(pChildNode);
}
} else {
CFDE_XMLNode* pXMLNode = pImportDataRoot->GetXMLMappingNode();
CFDE_XMLNode* pParentXMLNode = pXMLNode->GetNodeItem(CFDE_XMLNode::Parent);
if (pParentXMLNode) {
pParentXMLNode->RemoveChildNode(pXMLNode);
}
pDataModel->InsertChild(pImportDataRoot);
}
m_pDocument->DoDataRemerge(FALSE);
pDataDocumentParser->Release();
return TRUE;
}
CFX_WideString XFA_ExportEncodeAttribute(const CFX_WideString& str) {
CFX_WideTextBuf textBuf;
int32_t iLen = str.GetLength();
for (int32_t i = 0; i < iLen; i++) {
switch (str[i]) {
case '&':
textBuf << FX_WSTRC(L"&");
break;
case '<':
textBuf << FX_WSTRC(L"<");
break;
case '>':
textBuf << FX_WSTRC(L">");
break;
case '\'':
textBuf << FX_WSTRC(L"'");
break;
case '\"':
textBuf << FX_WSTRC(L""");
break;
default:
textBuf.AppendChar(str[i]);
}
}
return textBuf.GetWideString();
}
CFX_WideString XFA_ExportEncodeContent(const CFX_WideStringC& str) {
CFX_WideTextBuf textBuf;
int32_t iLen = str.GetLength();
for (int32_t i = 0; i < iLen; i++) {
FX_WCHAR ch = str.GetAt(i);
if (!FDE_IsXMLValidChar(ch)) {
continue;
}
if (ch == '&') {
textBuf << FX_WSTRC(L"&");
} else if (ch == '<') {
textBuf << FX_WSTRC(L"<");
} else if (ch == '>') {
textBuf << FX_WSTRC(L">");
} else if (ch == '\'') {
textBuf << FX_WSTRC(L"'");
} else if (ch == '\"') {
textBuf << FX_WSTRC(L""");
} else if (ch == ' ') {
if (i && str.GetAt(i - 1) != ' ') {
textBuf.AppendChar(' ');
} else {
textBuf << FX_WSTRC(L" ");
}
} else {
textBuf.AppendChar(str.GetAt(i));
}
}
return textBuf.GetWideString();
}
static void XFA_SaveAttribute(CXFA_Node* pNode,
XFA_ATTRIBUTE eName,
const CFX_WideStringC& wsName,
FX_BOOL bProto,
CFX_WideString& wsOutput) {
CFX_WideString wsValue;
if ((!bProto && !pNode->HasAttribute((XFA_ATTRIBUTE)eName, bProto)) ||
!pNode->GetAttribute((XFA_ATTRIBUTE)eName, wsValue, FALSE)) {
return;
}
wsValue = XFA_ExportEncodeAttribute(wsValue);
wsOutput += FX_WSTRC(L" ");
wsOutput += wsName;
wsOutput += FX_WSTRC(L"=\"");
wsOutput += wsValue;
wsOutput += FX_WSTRC(L"\"");
}
static FX_BOOL XFA_DataExporter_AttributeSaveInDataModel(
CXFA_Node* pNode,
XFA_ATTRIBUTE eAttribute) {
FX_BOOL bSaveInDataModel = FALSE;
if (pNode->GetClassID() != XFA_ELEMENT_Image) {
return bSaveInDataModel;
}
CXFA_Node* pValueNode = pNode->GetNodeItem(XFA_NODEITEM_Parent);
if (!pValueNode || pValueNode->GetClassID() != XFA_ELEMENT_Value) {
return bSaveInDataModel;
}
CXFA_Node* pFieldNode = pValueNode->GetNodeItem(XFA_NODEITEM_Parent);
if (pFieldNode && pFieldNode->GetBindData() &&
eAttribute == XFA_ATTRIBUTE_Href) {
bSaveInDataModel = TRUE;
}
return bSaveInDataModel;
}
FX_BOOL XFA_DataExporter_ContentNodeNeedtoExport(CXFA_Node* pContentNode) {
CFX_WideString wsContent;
if (!pContentNode->TryContent(wsContent, FALSE, FALSE)) {
return FALSE;
}
FXSYS_assert(pContentNode->GetObjectType() == XFA_OBJECTTYPE_ContentNode);
CXFA_Node* pParentNode = pContentNode->GetNodeItem(XFA_NODEITEM_Parent);
if (!pParentNode || pParentNode->GetClassID() != XFA_ELEMENT_Value) {
return TRUE;
}
CXFA_Node* pGrandParentNode = pParentNode->GetNodeItem(XFA_NODEITEM_Parent);
if (!pGrandParentNode ||
pGrandParentNode->GetObjectType() != XFA_OBJECTTYPE_ContainerNode) {
return TRUE;
}
if (pGrandParentNode->GetBindData()) {
return FALSE;
}
CXFA_WidgetData* pWidgetData = pGrandParentNode->GetWidgetData();
XFA_ELEMENT eUIType = pWidgetData->GetUIType();
if (eUIType == XFA_ELEMENT_PasswordEdit) {
return FALSE;
}
return TRUE;
}
static void XFA_DataExporter_RecognizeXFAVersionNumber(
CXFA_Node* pTemplateRoot,
CFX_WideString& wsVersionNumber) {
wsVersionNumber.Empty();
if (!pTemplateRoot) {
return;
}
CFX_WideString wsTemplateNS;
if (!pTemplateRoot->TryNamespace(wsTemplateNS)) {
return;
}
XFA_VERSION eVersion =
pTemplateRoot->GetDocument()->RecognizeXFAVersionNumber(wsTemplateNS);
if (eVersion == XFA_VERSION_UNKNOWN) {
eVersion = XFA_VERSION_DEFAULT;
}
wsVersionNumber.Format(L"%i.%i", eVersion / 100, eVersion % 100);
}
static void XFA_DataExporter_RegenerateFormFile_Changed(
CXFA_Node* pNode,
CFX_WideTextBuf& buf,
FX_BOOL bSaveXML = FALSE) {
CFX_WideString wsAttrs;
int32_t iAttrs = 0;
const uint8_t* pAttrs = XFA_GetElementAttributes(pNode->GetClassID(), iAttrs);
while (iAttrs--) {
const XFA_ATTRIBUTEINFO* pAttr =
XFA_GetAttributeByID((XFA_ATTRIBUTE)pAttrs[iAttrs]);
if (pAttr->eName == XFA_ATTRIBUTE_Name ||
(XFA_DataExporter_AttributeSaveInDataModel(pNode, pAttr->eName) &&
!bSaveXML)) {
continue;
}
CFX_WideString wsAttr;
XFA_SaveAttribute(pNode, pAttr->eName, pAttr->pName, bSaveXML, wsAttr);
wsAttrs += wsAttr;
}
CFX_WideString wsChildren;
switch (pNode->GetObjectType()) {
case XFA_OBJECTTYPE_ContentNode: {
if (!bSaveXML && !XFA_DataExporter_ContentNodeNeedtoExport(pNode)) {
break;
}
CXFA_Node* pRawValueNode = pNode->GetNodeItem(XFA_NODEITEM_FirstChild);
while (pRawValueNode &&
pRawValueNode->GetClassID() != XFA_ELEMENT_SharpxHTML &&
pRawValueNode->GetClassID() != XFA_ELEMENT_Sharptext &&
pRawValueNode->GetClassID() != XFA_ELEMENT_Sharpxml) {
pRawValueNode = pRawValueNode->GetNodeItem(XFA_NODEITEM_NextSibling);
}
if (!pRawValueNode) {
break;
}
CFX_WideString wsContentType;
pNode->GetAttribute(XFA_ATTRIBUTE_ContentType, wsContentType, FALSE);
if (pRawValueNode->GetClassID() == XFA_ELEMENT_SharpxHTML &&
wsContentType == FX_WSTRC(L"text/html")) {
CFDE_XMLNode* pExDataXML = pNode->GetXMLMappingNode();
if (!pExDataXML) {
break;
}
CFDE_XMLNode* pRichTextXML =
pExDataXML->GetNodeItem(CFDE_XMLNode::FirstChild);
if (!pRichTextXML) {
break;
}
IFX_MemoryStream* pMemStream = FX_CreateMemoryStream(TRUE);
IFX_Stream* pTempStream = IFX_Stream::CreateStream(
(IFX_FileWrite*)pMemStream, FX_STREAMACCESS_Text |
FX_STREAMACCESS_Write |
FX_STREAMACCESS_Append);
pTempStream->SetCodePage(FX_CODEPAGE_UTF8);
pRichTextXML->SaveXMLNode(pTempStream);
wsChildren += CFX_WideString::FromUTF8(
(const FX_CHAR*)pMemStream->GetBuffer(), pMemStream->GetSize());
pTempStream->Release();
pMemStream->Release();
} else if (pRawValueNode->GetClassID() == XFA_ELEMENT_Sharpxml &&
wsContentType == FX_WSTRC(L"text/xml")) {
CFX_WideString wsRawValue;
pRawValueNode->GetAttribute(XFA_ATTRIBUTE_Value, wsRawValue, FALSE);
if (wsRawValue.IsEmpty()) {
break;
}
CFX_WideStringArray wsSelTextArray;
int32_t iStart = 0;
int32_t iEnd = wsRawValue.Find(L'\n', iStart);
iEnd = (iEnd == -1) ? wsRawValue.GetLength() : iEnd;
while (iEnd >= iStart) {
wsSelTextArray.Add(wsRawValue.Mid(iStart, iEnd - iStart));
iStart = iEnd + 1;
if (iStart >= wsRawValue.GetLength()) {
break;
}
iEnd = wsRawValue.Find(L'\n', iStart);
}
CXFA_Node* pParentNode = pNode->GetNodeItem(XFA_NODEITEM_Parent);
FXSYS_assert(pParentNode);
CXFA_Node* pGrandparentNode =
pParentNode->GetNodeItem(XFA_NODEITEM_Parent);
FXSYS_assert(pGrandparentNode);
CFX_WideString bodyTagName;
bodyTagName = pGrandparentNode->GetCData(XFA_ATTRIBUTE_Name);
if (bodyTagName.IsEmpty()) {
bodyTagName = FX_WSTRC(L"ListBox1");
}
buf << FX_WSTRC(L"<");
buf << bodyTagName;
buf << FX_WSTRC(L" xmlns=\"\"\n>");
for (int32_t i = 0; i < wsSelTextArray.GetSize(); i++) {
buf << FX_WSTRC(L"