diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-11-27 18:28:06 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-11-27 18:28:06 +0000 |
commit | f351ba03ebf31103c0a6a0c00b1477d39c060139 (patch) | |
tree | df924970828054a2ec8c1fa00c7546a15de106c8 /xfa | |
parent | 9f0bba48c208722c8d05eaa5382254f3183c65b7 (diff) | |
download | pdfium-f351ba03ebf31103c0a6a0c00b1477d39c060139.tar.xz |
Add some helpers for attribute lookup
This CL adds helpers to CXFA_Node to convert from strings to attributes
and from attributes to their string names. A static_assert was added to
make sure the list of attributes is the same size as the attribute data
so the checks can be removed.
Change-Id: Idebc65021d71f604bcf498e4cf42252af00d802b
Reviewed-on: https://pdfium-review.googlesource.com/19270
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'xfa')
-rw-r--r-- | xfa/fxfa/fxfa_basic.h | 2 | ||||
-rw-r--r-- | xfa/fxfa/parser/cxfa_dataexporter.cpp | 16 | ||||
-rw-r--r-- | xfa/fxfa/parser/cxfa_node.cpp | 13 | ||||
-rw-r--r-- | xfa/fxfa/parser/cxfa_node.h | 2 | ||||
-rw-r--r-- | xfa/fxfa/parser/cxfa_node_unittest.cpp | 17 | ||||
-rw-r--r-- | xfa/fxfa/parser/cxfa_simple_parser.cpp | 14 | ||||
-rw-r--r-- | xfa/fxfa/parser/xfa_basic_data_attributes.cpp | 3 | ||||
-rw-r--r-- | xfa/fxfa/parser/xfa_utils.cpp | 5 |
8 files changed, 52 insertions, 20 deletions
diff --git a/xfa/fxfa/fxfa_basic.h b/xfa/fxfa/fxfa_basic.h index e2d4056095..cdd53a965b 100644 --- a/xfa/fxfa/fxfa_basic.h +++ b/xfa/fxfa/fxfa_basic.h @@ -618,7 +618,7 @@ enum class XFA_Attribute : uint8_t { Intact, XdpContent, DecipherOnly, - + LastAttributePlaceholder, Unknown = 255, }; diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp index f28649fffa..edb7e1e186 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.cpp +++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp @@ -87,7 +87,7 @@ WideString ExportEncodeContent(const WideString& str) { void SaveAttribute(CXFA_Node* pNode, XFA_Attribute eName, - const WideStringView& wsName, + const WideString& wsName, bool bProto, WideString& wsOutput) { if (!bProto && !pNode->JSNode()->HasAttribute(eName)) @@ -175,13 +175,13 @@ void RegenerateFormFile_Changed(CXFA_Node* pNode, if (attr == XFA_Attribute::Unknown) break; - const XFA_ATTRIBUTEINFO* pAttr = XFA_GetAttributeByID(attr); - if (pAttr->eName == XFA_Attribute::Name || - (AttributeSaveInDataModel(pNode, pAttr->eName) && !bSaveXML)) { + if (attr == XFA_Attribute::Name || + (AttributeSaveInDataModel(pNode, attr) && !bSaveXML)) { continue; } WideString wsAttr; - SaveAttribute(pNode, pAttr->eName, pAttr->pName, bSaveXML, wsAttr); + SaveAttribute(pNode, attr, CXFA_Node::AttributeToName(attr), bSaveXML, + wsAttr); wsAttrs += wsAttr; } @@ -360,13 +360,11 @@ void RegenerateFormFile_Container( XFA_Attribute attr = pNode->GetAttribute(i); if (attr == XFA_Attribute::Unknown) break; - - const XFA_ATTRIBUTEINFO* pAttr = XFA_GetAttributeByID(attr); - if (pAttr->eName == XFA_Attribute::Name) + if (attr == XFA_Attribute::Name) continue; WideString wsAttr; - SaveAttribute(pNode, pAttr->eName, pAttr->pName, false, wsAttr); + SaveAttribute(pNode, attr, CXFA_Node::AttributeToName(attr), false, wsAttr); wsOutput += wsAttr; } diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index ccbf31048d..4402208900 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -138,6 +138,19 @@ const XFA_ATTRIBUTEENUMINFO* GetAttributeEnumByID(XFA_ATTRIBUTEENUM eName) { return g_XFAEnumData + eName; } +// static +WideString CXFA_Node::AttributeToName(XFA_Attribute attr) { + return XFA_GetAttributeByID(attr)->pName; +} + +// static +XFA_Attribute CXFA_Node::NameToAttribute(const WideStringView& name) { + const XFA_ATTRIBUTEINFO* attr = XFA_GetAttributeByName(name); + if (!attr) + return XFA_Attribute::Unknown; + return attr->eName; +} + CXFA_Node::CXFA_Node(CXFA_Document* pDoc, uint16_t ePacket, uint32_t validPackets, diff --git a/xfa/fxfa/parser/cxfa_node.h b/xfa/fxfa/parser/cxfa_node.h index 7b7086ecb5..e5f47a1828 100644 --- a/xfa/fxfa/parser/cxfa_node.h +++ b/xfa/fxfa/parser/cxfa_node.h @@ -53,6 +53,8 @@ class CXFA_Node : public CXFA_Object { uint8_t flags; }; + static WideString AttributeToName(XFA_Attribute attr); + static XFA_Attribute NameToAttribute(const WideStringView& name); static XFA_Element NameToElement(const WideString& name); static std::unique_ptr<CXFA_Node> Create(CXFA_Document* doc, XFA_Element element, diff --git a/xfa/fxfa/parser/cxfa_node_unittest.cpp b/xfa/fxfa/parser/cxfa_node_unittest.cpp new file mode 100644 index 0000000000..93df084391 --- /dev/null +++ b/xfa/fxfa/parser/cxfa_node_unittest.cpp @@ -0,0 +1,17 @@ +// Copyright 2017 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. + +#include "xfa/fxfa/parser/cxfa_node.h" + +#include "testing/gtest/include/gtest/gtest.h" +#include "testing/test_support.h" + +TEST(CXFA_NodeTest, NameToAttribute) { + EXPECT_EQ(XFA_Attribute::Unknown, CXFA_Node::NameToAttribute(L"")); + EXPECT_EQ(XFA_Attribute::Unknown, CXFA_Node::NameToAttribute(L"nonesuch")); + EXPECT_EQ(XFA_Attribute::H, CXFA_Node::NameToAttribute(L"h")); + EXPECT_EQ(XFA_Attribute::Short, CXFA_Node::NameToAttribute(L"short")); + EXPECT_EQ(XFA_Attribute::DecipherOnly, + CXFA_Node::NameToAttribute(L"decipherOnly")); +} diff --git a/xfa/fxfa/parser/cxfa_simple_parser.cpp b/xfa/fxfa/parser/cxfa_simple_parser.cpp index 4ee9ca76b3..453ae8d059 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.cpp +++ b/xfa/fxfa/parser/cxfa_simple_parser.cpp @@ -898,17 +898,17 @@ CXFA_Node* CXFA_SimpleParser::NormalLoader(CXFA_Node* pXFANode, if (wsAttrName == L"nil" && it.second == L"true") IsNeedValue = false; - const XFA_ATTRIBUTEINFO* lpAttrInfo = - XFA_GetAttributeByName(wsAttrName.AsStringView()); - if (!lpAttrInfo) + XFA_Attribute attr = + CXFA_Node::NameToAttribute(wsAttrName.AsStringView()); + if (attr == XFA_Attribute::Unknown) continue; - if (!bUseAttribute && lpAttrInfo->eName != XFA_Attribute::Name && - lpAttrInfo->eName != XFA_Attribute::Save) { + if (!bUseAttribute && attr != XFA_Attribute::Name && + attr != XFA_Attribute::Save) { continue; } - pXFAChild->JSNode()->SetAttribute(lpAttrInfo->eName, - it.second.AsStringView(), false); + pXFAChild->JSNode()->SetAttribute(attr, it.second.AsStringView(), + false); } pXFANode->InsertChild(pXFAChild, nullptr); if (eType == XFA_Element::Validate || eType == XFA_Element::Locale) { diff --git a/xfa/fxfa/parser/xfa_basic_data_attributes.cpp b/xfa/fxfa/parser/xfa_basic_data_attributes.cpp index 66f5c893ba..7b371a0239 100644 --- a/xfa/fxfa/parser/xfa_basic_data_attributes.cpp +++ b/xfa/fxfa/parser/xfa_basic_data_attributes.cpp @@ -704,6 +704,9 @@ const XFA_ATTRIBUTEINFO g_XFAAttributeData[] = { }; const int32_t g_iXFAAttributeCount = sizeof(g_XFAAttributeData) / sizeof(XFA_ATTRIBUTEINFO); +static_assert(g_iXFAAttributeCount == + static_cast<int32_t>(XFA_Attribute::LastAttributePlaceholder), + "Attribute count mismatch"); const XFA_NOTSUREATTRIBUTE g_XFANotsureAttributes[] = { {XFA_Element::SubformSet, XFA_Attribute::Relation, XFA_AttributeType::Enum, diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp index b17a7535eb..03b8943a1f 100644 --- a/xfa/fxfa/parser/xfa_utils.cpp +++ b/xfa/fxfa/parser/xfa_utils.cpp @@ -284,9 +284,8 @@ const XFA_ATTRIBUTEINFO* XFA_GetAttributeByName(const WideStringView& wsName) { } const XFA_ATTRIBUTEINFO* XFA_GetAttributeByID(XFA_Attribute eName) { - return (static_cast<uint8_t>(eName) < g_iXFAAttributeCount) - ? (g_XFAAttributeData + static_cast<uint8_t>(eName)) - : nullptr; + ASSERT(static_cast<uint8_t>(eName) < g_iXFAAttributeCount); + return g_XFAAttributeData + static_cast<uint8_t>(eName); } const XFA_ATTRIBUTEENUMINFO* XFA_GetAttributeEnumByName( |