From c9171e16d9d4477501d326d8d456fdc03e0f832e Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 26 Jul 2018 19:34:26 +0000 Subject: Use moar ToXMLElement() in place of static_cast<>. Introduces checks in a few new places, but mainly just consolidates checking/casting logic. Change-Id: I634a03060d254db099972c6978249992367e146c Reviewed-on: https://pdfium-review.googlesource.com/38900 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- xfa/fxfa/parser/cxfa_dataexporter.cpp | 16 ++++----- xfa/fxfa/parser/cxfa_document.cpp | 7 ++-- xfa/fxfa/parser/cxfa_document_parser.cpp | 58 +++++++++++++------------------- xfa/fxfa/parser/cxfa_node.cpp | 14 ++++---- xfa/fxfa/parser/cxfa_xmllocale.cpp | 30 ++++++----------- xfa/fxfa/parser/xfa_utils.cpp | 7 ++-- 6 files changed, 49 insertions(+), 83 deletions(-) (limited to 'xfa/fxfa/parser') diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp index eb3e04afd8..820d3546f7 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.cpp +++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp @@ -38,9 +38,8 @@ bool CXFA_DataExporter::Export(const RetainPtr& pStream, break; } case XFA_PacketType::Datasets: { - CFX_XMLElement* pElement = - static_cast(pNode->GetXMLMappingNode()); - if (!pElement || pElement->GetType() != FX_XMLNODE_Element) + CFX_XMLElement* pElement = ToXMLElement(pNode->GetXMLMappingNode()); + if (!pElement) return false; CXFA_Node* pDataNode = pNode->GetFirstChild(); @@ -54,9 +53,8 @@ bool CXFA_DataExporter::Export(const RetainPtr& pStream, break; case XFA_PacketType::Template: default: { - CFX_XMLElement* pElement = - static_cast(pNode->GetXMLMappingNode()); - if (!pElement || pElement->GetType() != FX_XMLNODE_Element) + CFX_XMLElement* pElement = ToXMLElement(pNode->GetXMLMappingNode()); + if (!pElement) return false; pElement->Save(pStream); @@ -75,9 +73,8 @@ bool CXFA_DataExporter::Export(const RetainPtr& pStream, break; } } - CFX_XMLElement* pElement = - static_cast(pExportNode->GetXMLMappingNode()); - if (!pElement || pElement->GetType() != FX_XMLNODE_Element) + CFX_XMLElement* pElement = ToXMLElement(pExportNode->GetXMLMappingNode()); + if (!pElement) return false; XFA_DataExporter_DealWithDataGroupNode(pExportNode); @@ -85,6 +82,5 @@ bool CXFA_DataExporter::Export(const RetainPtr& pStream, L"http://www.xfa.org/schema/xfa-data/1.0/"); pElement->Save(pStream); pElement->RemoveAttribute(L"xmlns:xfa"); - return true; } diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp index 3e8046677a..3b086faf69 100644 --- a/xfa/fxfa/parser/cxfa_document.cpp +++ b/xfa/fxfa/parser/cxfa_document.cpp @@ -510,9 +510,8 @@ void CreateDataBinding(CXFA_Node* pFormNode, wsHref = image->GetHref(); } CFX_XMLElement* pXMLDataElement = - static_cast(pDataNode->GetXMLMappingNode()); + ToXMLElement(pDataNode->GetXMLMappingNode()); ASSERT(pXMLDataElement); - pDataNode->JSObject()->SetAttributeValue( wsValue, pFormNode->GetFormatDataValue(wsValue), false, false); pDataNode->JSObject()->SetCData(XFA_Attribute::ContentType, @@ -656,9 +655,7 @@ void CreateDataBinding(CXFA_Node* pFormNode, CXFA_Image* image = defValue ? defValue->GetImageIfExists() : nullptr; if (image) { CFX_XMLElement* pXMLDataElement = - static_cast(pDataNode->GetXMLMappingNode()); - ASSERT(pXMLDataElement); - + ToXMLElement(pDataNode->GetXMLMappingNode()); WideString wsContentType = pXMLDataElement->GetAttribute(L"xfa:contentType"); if (!wsContentType.IsEmpty()) { diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp index b126d8267f..34955829e9 100644 --- a/xfa/fxfa/parser/cxfa_document_parser.cpp +++ b/xfa/fxfa/parser/cxfa_document_parser.cpp @@ -115,10 +115,10 @@ bool MatchNodeName(CFX_XMLNode* pNode, const WideStringView& wsLocalTagName, const WideStringView& wsNamespaceURIPrefix, uint32_t eMatchFlags = XFA_XDPPACKET_FLAGS_NOMATCH) { - if (!pNode || pNode->GetType() != FX_XMLNODE_Element) + CFX_XMLElement* pElement = ToXMLElement(pNode); + if (!pElement) return false; - CFX_XMLElement* pElement = static_cast(pNode); WideString wsNodeStr = pElement->GetLocalTagName(); if (wsNodeStr != wsLocalTagName) return false; @@ -449,7 +449,7 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_XDP( m_pRootNode = pXFARootNode; pXFARootNode->JSObject()->SetCData(XFA_Attribute::Name, L"xfa", false, false); - CFX_XMLElement* pElement = static_cast(pXMLDocumentNode); + CFX_XMLElement* pElement = ToXMLElement(pXMLDocumentNode); for (auto it : pElement->GetAttributes()) { if (it.first == L"uuid") pXFARootNode->JSObject()->SetCData(XFA_Attribute::Uuid, it.second, false, @@ -483,12 +483,10 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_XDP( CFX_XMLNode* pXMLTemplateDOMRoot = nullptr; for (CFX_XMLNode* pChildItem = pXMLDocumentNode->GetFirstChild(); pChildItem; pChildItem = pChildItem->GetNextSibling()) { - if (!pChildItem || pChildItem->GetType() != FX_XMLNODE_Element) - continue; - if (pChildItem == pXMLConfigDOMRoot) + CFX_XMLElement* pElement = ToXMLElement(pChildItem); + if (!pElement || pElement == pXMLConfigDOMRoot) continue; - CFX_XMLElement* pElement = static_cast(pChildItem); WideString wsPacketName = pElement->GetLocalTagName(); const PacketInfo* pPacketInfo = GetPacketByName(wsPacketName.AsStringView()); @@ -591,8 +589,7 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_Template( pNode->JSObject()->SetCData(XFA_Attribute::Name, packet->name, false, false); - CFX_XMLElement* pXMLDocumentElement = - static_cast(pXMLDocumentNode); + CFX_XMLElement* pXMLDocumentElement = ToXMLElement(pXMLDocumentNode); WideString wsNamespaceURI = pXMLDocumentElement->GetNamespaceURI(); if (wsNamespaceURI.IsEmpty()) wsNamespaceURI = pXMLDocumentElement->GetAttribute(L"xmlns:xfa"); @@ -661,8 +658,7 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_Data( CFX_XMLNode* pDataXMLNode = nullptr; if (MatchNodeName(pXMLDocumentNode, L"data", packet->uri, packet->flags)) { - static_cast(pXMLDocumentNode) - ->RemoveAttribute(L"xmlns:xfa"); + ToXMLElement(pXMLDocumentNode)->RemoveAttribute(L"xmlns:xfa"); pDataXMLNode = pXMLDocumentNode; } else { auto* pDataElement = xml_doc_->CreateNode(L"xfa:data"); @@ -686,8 +682,7 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_Data( if (!pNode) return nullptr; - WideString wsLocalName = - static_cast(pDataXMLNode)->GetLocalTagName(); + WideString wsLocalName = ToXMLElement(pDataXMLNode)->GetLocalTagName(); pNode->JSObject()->SetCData(XFA_Attribute::Name, wsLocalName, false, false); if (!DataLoader(pNode, pDataXMLNode, true)) return nullptr; @@ -742,8 +737,7 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_User( if (!pNode) return nullptr; - WideString wsName = - static_cast(pXMLDocumentNode)->GetLocalTagName(); + WideString wsName = ToXMLElement(pXMLDocumentNode)->GetLocalTagName(); pNode->JSObject()->SetCData(XFA_Attribute::Name, wsName, false, false); if (!UserPacketLoader(pNode, pXMLDocumentNode)) return nullptr; @@ -875,20 +869,18 @@ void CXFA_DocumentParser::ParseContentNode(CXFA_Node* pXFANode, if (eNodeType == FX_XMLNODE_Instruction) continue; + CFX_XMLElement* pElement = ToXMLElement(pXMLChild); if (element == XFA_Element::SharpxHTML) { - if (eNodeType != FX_XMLNODE_Element) + if (!pElement) break; - - if (XFA_RecognizeRichText(static_cast(pXMLChild))) - wsValue += - GetPlainTextFromRichText(static_cast(pXMLChild)); + if (XFA_RecognizeRichText(pElement)) + wsValue += GetPlainTextFromRichText(pElement); } else if (element == XFA_Element::Sharpxml) { - if (eNodeType != FX_XMLNODE_Element) + if (!pElement) break; - - ConvertXMLToPlainText(static_cast(pXMLChild), wsValue); + ConvertXMLToPlainText(pElement, wsValue); } else { - if (eNodeType == FX_XMLNODE_Element) + if (pElement) break; if (eNodeType == FX_XMLNODE_Text || eNodeType == FX_XMLNODE_CharData) wsValue = static_cast(pXMLChild)->GetText(); @@ -951,12 +943,10 @@ void CXFA_DocumentParser::ParseDataGroup(CXFA_Node* pXFANode, if (eNodeType == XFA_Element::DataModel) { for (CFX_XMLNode* pXMLDataChild = pXMLElement->GetFirstChild(); pXMLDataChild; pXMLDataChild = pXMLDataChild->GetNextSibling()) { - if (pXMLDataChild->GetType() == FX_XMLNODE_Element) { - if (!XFA_RecognizeRichText( - static_cast(pXMLDataChild))) { - eNodeType = XFA_Element::DataGroup; - break; - } + CFX_XMLElement* pElement = ToXMLElement(pXMLDataChild); + if (pElement && !XFA_RecognizeRichText(pElement)) { + eNodeType = XFA_Element::DataGroup; + break; } } } @@ -1063,9 +1053,8 @@ void CXFA_DocumentParser::ParseDataValue(CXFA_Node* pXFANode, pXMLCurValueNode = pXMLChild; wsCurValueTextBuf << wsText; - } else if (XFA_RecognizeRichText(static_cast(pXMLChild))) { - WideString wsText = - GetPlainTextFromRichText(static_cast(pXMLChild)); + } else if (XFA_RecognizeRichText(ToXMLElement(pXMLChild))) { + WideString wsText = GetPlainTextFromRichText(ToXMLElement(pXMLChild)); if (!pXMLCurValueNode) pXMLCurValueNode = pXMLChild; @@ -1097,8 +1086,7 @@ void CXFA_DocumentParser::ParseDataValue(CXFA_Node* pXFANode, if (!pXFAChild) return; - WideString wsNodeStr = - static_cast(pXMLChild)->GetLocalTagName(); + WideString wsNodeStr = ToXMLElement(pXMLChild)->GetLocalTagName(); pXFAChild->JSObject()->SetCData(XFA_Attribute::Name, wsNodeStr, false, false); ParseDataValue(pXFAChild, pXMLChild, ePacketID); diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 363af54028..9fdea14cae 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -1259,11 +1259,9 @@ void CXFA_Node::RemoveChild(CXFA_Node* pNode, bool bNotify) { return; } - ASSERT(pNode->xml_node_.Get() == xml_node_.Get() && - xml_node_->GetType() == FX_XMLNODE_Element); - if (pNode->xml_node_->GetType() == FX_XMLNODE_Element) { - CFX_XMLElement* pXMLElement = - static_cast(pNode->xml_node_.Get()); + ASSERT(pNode->xml_node_.Get() == xml_node_.Get()); + CFX_XMLElement* pXMLElement = ToXMLElement(pNode->xml_node_.Get()); + if (pXMLElement) { WideString wsAttributeName = pNode->JSObject()->GetCData(XFA_Attribute::QualifiedName); pXMLElement->RemoveAttribute(wsAttributeName); @@ -4700,10 +4698,10 @@ bool CXFA_Node::PresenceRequiresSpace() const { } void CXFA_Node::SetToXML(const WideString& value) { - auto* elem = static_cast(GetXMLMappingNode()); - FX_XMLNODETYPE eXMLType = elem->GetType(); - switch (eXMLType) { + auto* pNode = GetXMLMappingNode(); + switch (pNode->GetType()) { case FX_XMLNODE_Element: { + auto* elem = static_cast(pNode); if (IsAttributeInXML()) { elem->SetAttribute(JSObject()->GetCData(XFA_Attribute::QualifiedName), value); diff --git a/xfa/fxfa/parser/cxfa_xmllocale.cpp b/xfa/fxfa/parser/cxfa_xmllocale.cpp index 1922b31d7e..e4858e907e 100644 --- a/xfa/fxfa/parser/cxfa_xmllocale.cpp +++ b/xfa/fxfa/parser/cxfa_xmllocale.cpp @@ -41,17 +41,15 @@ std::unique_ptr CXFA_XMLLocale::Create( CFX_XMLElement* locale = nullptr; for (auto* child = doc->GetRoot()->GetFirstChild(); child; child = child->GetNextSibling()) { - if (child->GetType() != FX_XMLNODE_Element) - continue; - - CFX_XMLElement* elem = static_cast(child); - if (elem->GetName() == L"locale") { + CFX_XMLElement* elem = ToXMLElement(child); + if (elem && elem->GetName() == L"locale") { locale = elem; break; } } if (!locale) return nullptr; + return pdfium::MakeUnique(std::move(doc), locale); } @@ -129,11 +127,8 @@ WideString CXFA_XMLLocale::GetCalendarSymbol(const WideStringView& symbol, CFX_XMLElement* name_child = nullptr; for (auto* name = child->GetFirstChild(); name; name = name->GetNextSibling()) { - if (name->GetType() != FX_XMLNODE_Element) - continue; - - auto* elem = static_cast(name); - if (elem->GetName() != pstrSymbolNames) + CFX_XMLElement* elem = ToXMLElement(name); + if (!elem || elem->GetName() != pstrSymbolNames) continue; WideString abbr = elem->GetAttribute(L"abbr"); @@ -213,16 +208,11 @@ WideString CXFA_XMLLocale::GetPattern(CFX_XMLElement* patterns, const WideStringView& wsName) const { for (auto* child = patterns->GetFirstChild(); child; child = child->GetNextSibling()) { - if (child->GetType() != FX_XMLNODE_Element) - continue; - - CFX_XMLElement* pattern = static_cast(child); - if (pattern->GetName() != bsTag) - continue; - if (pattern->GetAttribute(L"name") != wsName) - continue; - - return pattern->GetTextData(); + CFX_XMLElement* pattern = ToXMLElement(child); + if (pattern && pattern->GetName() == bsTag && + pattern->GetAttribute(L"name") == wsName) { + return pattern->GetTextData(); + } } return L""; } diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp index 54cca88409..1331a43756 100644 --- a/xfa/fxfa/parser/xfa_utils.cpp +++ b/xfa/fxfa/parser/xfa_utils.cpp @@ -470,11 +470,8 @@ bool XFA_FDEExtension_ResolveNamespaceQualifier(CFX_XMLElement* pNode, } for (CFX_XMLNode* pParent = pNode; pParent != pFakeRoot; pParent = pParent->GetParent()) { - if (pParent->GetType() != FX_XMLNODE_Element) - continue; - - auto* pElement = static_cast(pParent); - if (pElement->HasAttribute(wsNSAttribute)) { + CFX_XMLElement* pElement = ToXMLElement(pParent); + if (pElement && pElement->HasAttribute(wsNSAttribute)) { *wsNamespaceURI = pElement->GetAttribute(wsNSAttribute); return true; } -- cgit v1.2.3