From 70180648ffd01dd3716871758411d2031aaaebbe Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 2 May 2018 16:02:03 +0000 Subject: Add a CFX_XMLDocument class. This CL adds a CFX_XMLDocument to act as the XML node container. All nodes are now owned by the document and the document is returned by the CFX_XMLParser. Classes which parse XML files now store the document instead of the root node. BUG: chromium:835636 Change-Id: I1e07d6115cf14714911d6fd4c3fa920c94fd5faf Reviewed-on: https://pdfium-review.googlesource.com/31313 Reviewed-by: Henrique Nakashima Commit-Queue: dsinclair --- xfa/fxfa/parser/cxfa_document_parser.cpp | 76 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 39 deletions(-) (limited to 'xfa/fxfa/parser/cxfa_document_parser.cpp') diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp index fe246d2300..6f3e56ce4c 100644 --- a/xfa/fxfa/parser/cxfa_document_parser.cpp +++ b/xfa/fxfa/parser/cxfa_document_parser.cpp @@ -110,16 +110,6 @@ CFX_XMLNode* GetDocumentNode(CFX_XMLNode* pRootNode) { return nullptr; } -WideString GetElementTagNamespaceURI(CFX_XMLElement* pElement) { - WideString wsNodeStr = pElement->GetNamespacePrefix(); - WideString wsNamespaceURI; - if (!XFA_FDEExtension_ResolveNamespaceQualifier(pElement, wsNodeStr, - &wsNamespaceURI)) { - return WideString(); - } - return wsNamespaceURI; -} - bool MatchNodeName(CFX_XMLNode* pNode, const WideStringView& wsLocalTagName, const WideStringView& wsNamespaceURIPrefix, @@ -132,7 +122,7 @@ bool MatchNodeName(CFX_XMLNode* pNode, if (wsNodeStr != wsLocalTagName) return false; - wsNodeStr = GetElementTagNamespaceURI(pElement); + wsNodeStr = pElement->GetNamespaceURI(); if (eMatchFlags & XFA_XDPPACKET_FLAGS_NOMATCH) return true; if (eMatchFlags & XFA_XDPPACKET_FLAGS_PREFIXMATCH) { @@ -318,8 +308,8 @@ WideString GetPlainTextFromRichText(CFX_XMLNode* pXMLNode) { } // namespace bool XFA_RecognizeRichText(CFX_XMLElement* pRichTextXMLNode) { - return pRichTextXMLNode && GetElementTagNamespaceURI(pRichTextXMLNode) == - L"http://www.w3.org/1999/xhtml"; + return pRichTextXMLNode && + pRichTextXMLNode->GetNamespaceURI() == L"http://www.w3.org/1999/xhtml"; } CXFA_DocumentParser::CXFA_DocumentParser(CXFA_Document* pFactory) @@ -329,30 +319,38 @@ CXFA_DocumentParser::~CXFA_DocumentParser() = default; bool CXFA_DocumentParser::Parse(const RetainPtr& pStream, XFA_PacketType ePacketID) { - m_pNodeTree = LoadXML(pStream); - if (!m_pNodeTree) + xml_doc_ = LoadXML(pStream); + if (!xml_doc_) + return false; + + CFX_XMLNode* root = GetDocumentNode(xml_doc_->GetRoot()); + if (!root) return false; - m_pRootNode = ParseAsXDPPacket(GetDocumentNode(m_pNodeTree.get()), ePacketID); + m_pRootNode = ParseAsXDPPacket(root, ePacketID); return !!m_pRootNode; } CFX_XMLNode* CXFA_DocumentParser::ParseXMLData(const ByteString& wsXML) { auto pStream = pdfium::MakeRetain( const_cast(wsXML.raw_str()), wsXML.GetLength(), false); - m_pNodeTree = LoadXML(pStream); - return m_pNodeTree ? GetDocumentNode(m_pNodeTree.get()) : nullptr; + xml_doc_ = LoadXML(pStream); + if (!xml_doc_) + return nullptr; + return GetDocumentNode(xml_doc_->GetRoot()); } -std::unique_ptr CXFA_DocumentParser::LoadXML( +std::unique_ptr CXFA_DocumentParser::LoadXML( const RetainPtr& pStream) { ASSERT(pStream); - auto root = pdfium::MakeUnique(L"ROOT"); - root->AppendChild(pdfium::MakeUnique(L"xml")); - - CFX_XMLParser parser(root.get(), pStream); - return parser.Parse() ? std::move(root) : nullptr; + CFX_XMLParser parser(pStream); + std::unique_ptr doc = parser.Parse(); + if (doc) { + doc->GetRoot()->InsertChildNode(doc->CreateNode(L"xml"), + 0); + } + return doc; } void CXFA_DocumentParser::ConstructXFANode(CXFA_Node* pXFANode, @@ -461,14 +459,15 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_XDP( CFX_XMLNode* pXMLConfigDOMRoot = nullptr; CXFA_Node* pXFAConfigDOMRoot = nullptr; + const PacketInfo* config_packet_info = + GetPacketByIndex(XFA_PacketType::Config); for (CFX_XMLNode* pChildItem = pXMLDocumentNode->GetFirstChild(); pChildItem; pChildItem = pChildItem->GetNextSibling()) { - const PacketInfo* pPacketInfo = GetPacketByIndex(XFA_PacketType::Config); - if (!MatchNodeName(pChildItem, pPacketInfo->name, pPacketInfo->uri, - pPacketInfo->flags)) { + if (!MatchNodeName(pChildItem, config_packet_info->name, + config_packet_info->uri, config_packet_info->flags)) { continue; } - if (pXFARootNode->GetFirstChildByName(pPacketInfo->hash)) + if (pXFARootNode->GetFirstChildByName(config_packet_info->hash)) return nullptr; pXMLConfigDOMRoot = pChildItem; @@ -516,7 +515,7 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_XDP( if (pXMLTemplateDOMRoot) return nullptr; - CXFA_Node* pPacketNode = ParseAsXDPPacket(pElement, ePacket); + CXFA_Node* pPacketNode = ParseAsXDPPacket_Template(pElement); if (pPacketNode) { pXMLTemplateDOMRoot = pElement; pXFARootNode->InsertChild(pPacketNode, nullptr); @@ -658,13 +657,13 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_Data( return pNode; } - MaybeOwned pDataXMLNode; + CFX_XMLNode* pDataXMLNode = nullptr; if (MatchNodeName(pXMLDocumentNode, L"data", packet->uri, packet->flags)) { static_cast(pXMLDocumentNode) ->RemoveAttribute(L"xmlns:xfa"); - pDataXMLNode.Reset(pXMLDocumentNode); + pDataXMLNode = pXMLDocumentNode; } else { - auto pDataElement = pdfium::MakeUnique(L"xfa:data"); + auto* pDataElement = xml_doc_->CreateNode(L"xfa:data"); CFX_XMLNode* pParentXMLNode = pXMLDocumentNode->GetParent(); if (pParentXMLNode) pParentXMLNode->RemoveChildNode(pXMLDocumentNode); @@ -676,9 +675,8 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_Data( } // The node was either removed from the parent above, or already has no // parent so we can take ownership. - pDataElement->AppendChild( - pdfium::WrapUnique(pXMLDocumentNode)); - pDataXMLNode.Reset(std::move(pDataElement)); + pDataElement->AppendChild(pXMLDocumentNode); + pDataXMLNode = pDataElement; } if (!pDataXMLNode) return nullptr; @@ -689,12 +687,12 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_Data( return nullptr; WideString wsLocalName = - static_cast(pDataXMLNode.Get())->GetLocalTagName(); + static_cast(pDataXMLNode)->GetLocalTagName(); pNode->JSObject()->SetCData(XFA_Attribute::Name, wsLocalName, false, false); - if (!DataLoader(pNode, pDataXMLNode.Get(), true)) + if (!DataLoader(pNode, pDataXMLNode, true)) return nullptr; - pNode->SetXMLMappingNode(std::move(pDataXMLNode)); + pNode->SetXMLMappingNode(pDataXMLNode); return pNode; } @@ -915,7 +913,7 @@ void CXFA_DocumentParser::ParseDataGroup(CXFA_Node* pXFANode, case FX_XMLNODE_Element: { CFX_XMLElement* pXMLElement = static_cast(pXMLChild); { - WideString wsNamespaceURI = GetElementTagNamespaceURI(pXMLElement); + WideString wsNamespaceURI = pXMLElement->GetNamespaceURI(); if (wsNamespaceURI == L"http://www.xfa.com/schema/xfa-package/" || wsNamespaceURI == L"http://www.xfa.org/schema/xfa-package/" || wsNamespaceURI == L"http://www.w3.org/2001/XMLSchema-instance") { -- cgit v1.2.3