From 3f9549e7f00b649471c4d658bbfb6bf031b8f53e Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 16 Nov 2017 14:23:07 +0000 Subject: Convert TryNamespace to optional This CL changes CJX_Node::TryNamespace to return a pdfium::Optional instead of a bool with an out parameter. Change-Id: I50ccb3544179108d156d763c25e03abab4306c19 Reviewed-on: https://pdfium-review.googlesource.com/18551 Commit-Queue: dsinclair Reviewed-by: Tom Sepez --- fxjs/cjx_node.cpp | 39 ++++++++++++------------- fxjs/cjx_node.h | 2 +- xfa/fxfa/parser/cxfa_dataexporter.cpp | 7 +++-- xfa/fxfa/parser/cxfa_document.cpp | 12 ++++---- xfa/fxfa/parser/xfa_document_datamerger_imp.cpp | 32 ++++++++++---------- 5 files changed, 47 insertions(+), 45 deletions(-) diff --git a/fxjs/cjx_node.cpp b/fxjs/cjx_node.cpp index ac93397b77..17662dada6 100644 --- a/fxjs/cjx_node.cpp +++ b/fxjs/cjx_node.cpp @@ -930,10 +930,8 @@ void CJX_Node::Script_NodeClass_Ns(CFXJSE_Value* pValue, ThrowInvalidPropertyException(); return; } - - WideString wsNameSpace; - TryNamespace(wsNameSpace); - pValue->SetString(wsNameSpace.UTF8Encode().AsStringView()); + pValue->SetString( + TryNamespace().value_or(WideString()).UTF8Encode().AsStringView()); } void CJX_Node::Script_NodeClass_Model(CFXJSE_Value* pValue, @@ -1016,11 +1014,11 @@ void CJX_Node::Script_ModelClass_IsCompatibleNS(CFXJSE_Arguments* pArguments) { wsNameSpace = WideString::FromUTF8(bsNameSpace.AsStringView()); } - WideString wsNodeNameSpace; - TryNamespace(wsNodeNameSpace); CFXJSE_Value* pValue = pArguments->GetReturnValue(); - if (pValue) - pValue->SetBoolean(wsNodeNameSpace == wsNameSpace); + if (!pValue) + return; + + pValue->SetBoolean(TryNamespace().value_or(WideString()) == wsNameSpace); } void CJX_Node::Script_ModelClass_Context(CFXJSE_Value* pValue, @@ -3571,35 +3569,34 @@ pdfium::Optional CJX_Node::TryContent(bool bScriptModify, return {}; } -bool CJX_Node::TryNamespace(WideString& wsNamespace) { - wsNamespace.clear(); +pdfium::Optional CJX_Node::TryNamespace() { if (GetXFANode()->IsModelNode() || GetXFANode()->GetElementType() == XFA_Element::Packet) { CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode(); if (!pXMLNode || pXMLNode->GetType() != FX_XMLNODE_Element) - return false; + return {}; - wsNamespace = static_cast(pXMLNode)->GetNamespaceURI(); - return true; + return {static_cast(pXMLNode)->GetNamespaceURI()}; } if (GetXFANode()->GetPacketID() != XFA_XDPPACKET_Datasets) - return GetXFANode()->GetModelNode()->JSNode()->TryNamespace(wsNamespace); + return GetXFANode()->GetModelNode()->JSNode()->TryNamespace(); CFX_XMLNode* pXMLNode = GetXFANode()->GetXMLMappingNode(); - if (!pXMLNode) - return false; - if (pXMLNode->GetType() != FX_XMLNODE_Element) - return true; + if (!pXMLNode || pXMLNode->GetType() != FX_XMLNODE_Element) + return {}; if (GetXFANode()->GetElementType() == XFA_Element::DataValue && GetEnum(XFA_Attribute::Contains) == XFA_ATTRIBUTEENUM_MetaData) { - return XFA_FDEExtension_ResolveNamespaceQualifier( + WideString wsNamespace; + bool ret = XFA_FDEExtension_ResolveNamespaceQualifier( static_cast(pXMLNode), GetCData(XFA_Attribute::QualifiedName), &wsNamespace); + if (!ret) + return {}; + return {wsNamespace}; } - wsNamespace = static_cast(pXMLNode)->GetNamespaceURI(); - return true; + return {static_cast(pXMLNode)->GetNamespaceURI()}; } CXFA_Node* CJX_Node::GetProperty(int32_t index, diff --git a/fxjs/cjx_node.h b/fxjs/cjx_node.h index cb6b6867fd..686c54c164 100644 --- a/fxjs/cjx_node.h +++ b/fxjs/cjx_node.h @@ -108,7 +108,7 @@ class CJX_Node : public CJX_Object { XFA_MAPDATABLOCKCALLBACKINFO* pCallbackInfo); void* GetObject(XFA_Attribute eAttr); - bool TryNamespace(WideString& wsNamespace); + pdfium::Optional TryNamespace(); void MergeAllData(CXFA_Node* pDstModule); diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp index f7715dc374..c86a7f7d51 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.cpp +++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp @@ -152,12 +152,13 @@ void RecognizeXFAVersionNumber(CXFA_Node* pTemplateRoot, if (!pTemplateRoot) return; - WideString wsTemplateNS; - if (!pTemplateRoot->JSNode()->TryNamespace(wsTemplateNS)) + pdfium::Optional templateNS = + pTemplateRoot->JSNode()->TryNamespace(); + if (!templateNS) return; XFA_VERSION eVersion = - pTemplateRoot->GetDocument()->RecognizeXFAVersionNumber(wsTemplateNS); + pTemplateRoot->GetDocument()->RecognizeXFAVersionNumber(*templateNS); if (eVersion == XFA_VERSION_UNKNOWN) eVersion = XFA_VERSION_DEFAULT; diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp index 82a71dea28..44fc9089c1 100644 --- a/xfa/fxfa/parser/cxfa_document.cpp +++ b/xfa/fxfa/parser/cxfa_document.cpp @@ -151,14 +151,16 @@ CXFA_Object* CXFA_Document::GetXFAObject(XFA_HashCode dwNodeNameHash) { if (pDatasetsChild->GetNameHash() != XFA_HASHCODE_Data) continue; - WideString wsNamespaceURI; - if (!pDatasetsChild->JSNode()->TryNamespace(wsNamespaceURI)) + pdfium::Optional namespaceURI = + pDatasetsChild->JSNode()->TryNamespace(); + if (!namespaceURI) continue; - WideString wsDatasetsURI; - if (!pDatasetsNode->JSNode()->TryNamespace(wsDatasetsURI)) + pdfium::Optional datasetsURI = + pDatasetsNode->JSNode()->TryNamespace(); + if (!datasetsURI) continue; - if (wsNamespaceURI == wsDatasetsURI) + if (*namespaceURI == *datasetsURI) return pDatasetsChild; } return nullptr; diff --git a/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp b/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp index 4cf7601531..89a6500fa6 100644 --- a/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp +++ b/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp @@ -1052,12 +1052,11 @@ CXFA_Node* MaybeCreateDataNode(CXFA_Document* pDocument, if (pDDGroupNode->GetElementType() != XFA_Element::DataGroup) continue; - WideString wsNamespace; - if (!pDDGroupNode->JSNode()->TryNamespace(wsNamespace) || - wsNamespace != L"http://ns.adobe.com/data-description/") { + pdfium::Optional ns = pDDGroupNode->JSNode()->TryNamespace(); + if (!ns || *ns != L"http://ns.adobe.com/data-description/") continue; - } } + CXFA_Node* pDDNode = pDDGroupNode->GetFirstChildByName(wsName.AsStringView()); if (!pDDNode) @@ -1231,12 +1230,12 @@ void UpdateDataRelation(CXFA_Node* pDataNode, CXFA_Node* pDataDescriptionNode) { if (pDDGroupNode->GetElementType() != XFA_Element::DataGroup) continue; - WideString wsNamespace; - if (!pDDGroupNode->JSNode()->TryNamespace(wsNamespace) || - wsNamespace != L"http://ns.adobe.com/data-description/") { + pdfium::Optional ns = + pDDGroupNode->JSNode()->TryNamespace(); + if (!ns || *ns != L"http://ns.adobe.com/data-description/") continue; - } } + CXFA_Node* pDDNode = pDDGroupNode->GetFirstChildByName(dwNameHash); if (!pDDNode) continue; @@ -1398,8 +1397,8 @@ void CXFA_Document::DoDataMerge() { pDatasetsRoot->SetXMLMappingNode(pDatasetsXMLNode); } CXFA_Node *pDataRoot = nullptr, *pDDRoot = nullptr; - WideString wsDatasetsURI; - pDatasetsRoot->JSNode()->TryNamespace(wsDatasetsURI); + WideString wsDatasetsURI = + pDatasetsRoot->JSNode()->TryNamespace().value_or(WideString()); for (CXFA_Node* pChildNode = pDatasetsRoot->GetNodeItem(XFA_NODEITEM_FirstChild); pChildNode; @@ -1407,16 +1406,19 @@ void CXFA_Document::DoDataMerge() { if (pChildNode->GetElementType() != XFA_Element::DataGroup) continue; - WideString wsNamespaceURI; if (!pDDRoot && pChildNode->GetNameHash() == XFA_HASHCODE_DataDescription) { - if (!pChildNode->JSNode()->TryNamespace(wsNamespaceURI)) + pdfium::Optional namespaceURI = + pChildNode->JSNode()->TryNamespace(); + if (!namespaceURI) continue; - if (wsNamespaceURI == L"http://ns.adobe.com/data-description/") + if (*namespaceURI == L"http://ns.adobe.com/data-description/") pDDRoot = pChildNode; } else if (!pDataRoot && pChildNode->GetNameHash() == XFA_HASHCODE_Data) { - if (!pChildNode->JSNode()->TryNamespace(wsNamespaceURI)) + pdfium::Optional namespaceURI = + pChildNode->JSNode()->TryNamespace(); + if (!namespaceURI) continue; - if (wsNamespaceURI == wsDatasetsURI) + if (*namespaceURI == wsDatasetsURI) pDataRoot = pChildNode; } if (pDataRoot && pDDRoot) -- cgit v1.2.3