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/cxfa_textlayout.cpp | 18 ++++---- xfa/fxfa/cxfa_textparser.cpp | 70 ++++++++++++++++---------------- xfa/fxfa/cxfa_textprovider.cpp | 9 ++-- 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 +--- 9 files changed, 96 insertions(+), 133 deletions(-) (limited to 'xfa/fxfa') diff --git a/xfa/fxfa/cxfa_textlayout.cpp b/xfa/fxfa/cxfa_textlayout.cpp index 32a1b393a6..8bf955bd32 100644 --- a/xfa/fxfa/cxfa_textlayout.cpp +++ b/xfa/fxfa/cxfa_textlayout.cpp @@ -77,19 +77,17 @@ CFX_XMLNode* CXFA_TextLayout::GetXMLContainerNode() { if (!pXMLRoot) return nullptr; - CFX_XMLNode* pXMLContainer = nullptr; for (CFX_XMLNode* pXMLChild = pXMLRoot->GetFirstChild(); pXMLChild; pXMLChild = pXMLChild->GetNextSibling()) { - if (pXMLChild->GetType() == FX_XMLNODE_Element) { - CFX_XMLElement* pXMLElement = static_cast(pXMLChild); - WideString wsTag = pXMLElement->GetLocalTagName(); - if (wsTag == L"body" || wsTag == L"html") { - pXMLContainer = pXMLChild; - break; - } - } + CFX_XMLElement* pXMLElement = ToXMLElement(pXMLChild); + if (!pXMLElement) + continue; + WideString wsTag = pXMLElement->GetLocalTagName(); + if (wsTag == L"body" || wsTag == L"html") + return pXMLChild; } - return pXMLContainer; + + return nullptr; } std::unique_ptr CXFA_TextLayout::CreateBreak(bool bDefault) { diff --git a/xfa/fxfa/cxfa_textparser.cpp b/xfa/fxfa/cxfa_textparser.cpp index f0a6dd2af1..c60b774051 100644 --- a/xfa/fxfa/cxfa_textparser.cpp +++ b/xfa/fxfa/cxfa_textparser.cpp @@ -276,18 +276,18 @@ bool CXFA_TextParser::TagValidate(const WideString& wsName) const { std::unique_ptr CXFA_TextParser::ParseTagInfo( CFX_XMLNode* pXMLNode) { auto tagProvider = pdfium::MakeUnique(); - - WideString wsName; - if (pXMLNode->GetType() == FX_XMLNODE_Element) { - CFX_XMLElement* pXMLElement = static_cast(pXMLNode); - wsName = pXMLElement->GetLocalTagName(); + CFX_XMLElement* pXMLElement = ToXMLElement(pXMLNode); + if (pXMLElement) { + WideString wsName = pXMLElement->GetLocalTagName(); tagProvider->SetTagName(wsName); tagProvider->m_bTagAvailable = TagValidate(wsName); - WideString wsValue = pXMLElement->GetAttribute(L"style"); if (!wsValue.IsEmpty()) tagProvider->SetAttribute(L"style", wsValue); - } else if (pXMLNode->GetType() == FX_XMLNODE_Text) { + + return tagProvider; + } + if (pXMLNode->GetType() == FX_XMLNODE_Text) { tagProvider->m_bTagAvailable = true; tagProvider->m_bContent = true; } @@ -502,38 +502,38 @@ bool CXFA_TextParser::GetEmbbedObj(CXFA_TextProvider* pTextProvider, if (!pXMLNode) return false; - bool bRet = false; - if (pXMLNode->GetType() == FX_XMLNODE_Element) { - CFX_XMLElement* pElement = static_cast(pXMLNode); - WideString wsAttr = pElement->GetAttribute(L"xfa:embed"); - if (wsAttr.IsEmpty()) - return false; - if (wsAttr[0] == L'#') - wsAttr.Delete(0); - - WideString ws = pElement->GetAttribute(L"xfa:embedType"); - if (ws.IsEmpty()) - ws = L"som"; - else - ws.MakeLower(); + CFX_XMLElement* pElement = ToXMLElement(pXMLNode); + if (!pElement) + return false; + + WideString wsAttr = pElement->GetAttribute(L"xfa:embed"); + if (wsAttr.IsEmpty()) + return false; - bool bURI = (ws == L"uri"); - if (!bURI && ws != L"som") - return false; + if (wsAttr[0] == L'#') + wsAttr.Delete(0); - ws = pElement->GetAttribute(L"xfa:embedMode"); - if (ws.IsEmpty()) - ws = L"formatted"; - else - ws.MakeLower(); + WideString ws = pElement->GetAttribute(L"xfa:embedType"); + if (ws.IsEmpty()) + ws = L"som"; + else + ws.MakeLower(); - bool bRaw = (ws == L"raw"); - if (!bRaw && ws != L"formatted") - return false; + bool bURI = (ws == L"uri"); + if (!bURI && ws != L"som") + return false; - bRet = pTextProvider->GetEmbbedObj(bURI, bRaw, wsAttr, wsValue); - } - return bRet; + ws = pElement->GetAttribute(L"xfa:embedMode"); + if (ws.IsEmpty()) + ws = L"formatted"; + else + ws.MakeLower(); + + bool bRaw = (ws == L"raw"); + if (!bRaw && ws != L"formatted") + return false; + + return pTextProvider->GetEmbbedObj(bURI, bRaw, wsAttr, wsValue); } CXFA_TextParseContext* CXFA_TextParser::GetParseContextFromMap( diff --git a/xfa/fxfa/cxfa_textprovider.cpp b/xfa/fxfa/cxfa_textprovider.cpp index 60c52942ed..ba48cd8490 100644 --- a/xfa/fxfa/cxfa_textprovider.cpp +++ b/xfa/fxfa/cxfa_textprovider.cpp @@ -61,13 +61,12 @@ CXFA_Node* CXFA_TextProvider::GetTextNode(bool& bRichText) { if (m_eType == XFA_TEXTPROVIDERTYPE_Datasets) { CXFA_Node* pBind = m_pNode->GetBindData(); CFX_XMLNode* pXMLNode = pBind->GetXMLMappingNode(); - ASSERT(pXMLNode); for (CFX_XMLNode* pXMLChild = pXMLNode->GetFirstChild(); pXMLChild; pXMLChild = pXMLChild->GetNextSibling()) { - if (pXMLChild->GetType() == FX_XMLNODE_Element) { - CFX_XMLElement* pElement = static_cast(pXMLChild); - if (XFA_RecognizeRichText(pElement)) - bRichText = true; + CFX_XMLElement* pElement = ToXMLElement(pXMLChild); + if (pElement && XFA_RecognizeRichText(pElement)) { + bRichText = true; + break; } } return pBind; 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