From 8b0cf76f00c6e89e8bb7bf4bcf2189b27baac31c Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 16 Apr 2018 21:24:57 +0000 Subject: Merge CFX_XMLElement and CFX_XMLAttributeNode CFX_XMLElement is the only subclass of CFX_XMLAttributeNode. This CL merges the two classes together. The {Set|Get}String method has been renamed to {Set|Get}Attribute to make it clearer what you're retrieving. Change-Id: I158c961d4d8c5f563d937a3e7a35321a33622562 Reviewed-on: https://pdfium-review.googlesource.com/30710 Commit-Queue: dsinclair Reviewed-by: Henrique Nakashima --- core/fxcrt/xml/cfx_xmlattributenode.cpp | 34 ------------- core/fxcrt/xml/cfx_xmlattributenode.h | 44 ----------------- core/fxcrt/xml/cfx_xmlelement.cpp | 85 ++++++++++++++++++++++----------- core/fxcrt/xml/cfx_xmlelement.h | 22 ++++++++- core/fxcrt/xml/cfx_xmlinstruction.h | 2 +- core/fxcrt/xml/cfx_xmlnode.cpp | 10 ---- core/fxcrt/xml/cfx_xmlnode.h | 1 - core/fxcrt/xml/cfx_xmlparser.cpp | 4 +- 8 files changed, 79 insertions(+), 123 deletions(-) delete mode 100644 core/fxcrt/xml/cfx_xmlattributenode.cpp delete mode 100644 core/fxcrt/xml/cfx_xmlattributenode.h (limited to 'core/fxcrt') diff --git a/core/fxcrt/xml/cfx_xmlattributenode.cpp b/core/fxcrt/xml/cfx_xmlattributenode.cpp deleted file mode 100644 index 6104747793..0000000000 --- a/core/fxcrt/xml/cfx_xmlattributenode.cpp +++ /dev/null @@ -1,34 +0,0 @@ -// 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. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fxcrt/xml/cfx_xmlattributenode.h" - -#include "core/fxcrt/fx_extension.h" - -CFX_XMLAttributeNode::CFX_XMLAttributeNode(const WideString& name) - : CFX_XMLNode(), name_(name) { - ASSERT(name_.GetLength() > 0); -} - -CFX_XMLAttributeNode::~CFX_XMLAttributeNode() {} - -bool CFX_XMLAttributeNode::HasAttribute(const WideString& name) const { - return attrs_.find(name) != attrs_.end(); -} - -WideString CFX_XMLAttributeNode::GetString(const WideString& name) const { - auto it = attrs_.find(name); - return it != attrs_.end() ? it->second : WideString(); -} - -void CFX_XMLAttributeNode::SetString(const WideString& name, - const WideString& value) { - attrs_[name] = value; -} - -void CFX_XMLAttributeNode::RemoveAttribute(const WideString& name) { - attrs_.erase(name); -} diff --git a/core/fxcrt/xml/cfx_xmlattributenode.h b/core/fxcrt/xml/cfx_xmlattributenode.h deleted file mode 100644 index 1ac9b84db2..0000000000 --- a/core/fxcrt/xml/cfx_xmlattributenode.h +++ /dev/null @@ -1,44 +0,0 @@ -// 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. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_XML_CFX_XMLATTRIBUTENODE_H_ -#define CORE_FXCRT_XML_CFX_XMLATTRIBUTENODE_H_ - -#include -#include - -#include "core/fxcrt/fx_string.h" -#include "core/fxcrt/xml/cfx_xmlnode.h" - -class CFX_XMLAttributeNode : public CFX_XMLNode { - public: - explicit CFX_XMLAttributeNode(const WideString& name); - ~CFX_XMLAttributeNode() override; - - // CFX_XMLNode - FX_XMLNODETYPE GetType() const override = 0; - std::unique_ptr Clone() override = 0; - - WideString GetName() const { return name_; } - const std::map& GetAttributes() const { - return attrs_; - } - void SetAttributes(const std::map& attrs) { - attrs_ = attrs; - } - bool HasAttribute(const WideString& name) const; - - void SetString(const WideString& name, const WideString& value); - WideString GetString(const WideString& name) const; - - void RemoveAttribute(const WideString& name); - - private: - WideString name_; - std::map attrs_; -}; - -#endif // CORE_FXCRT_XML_CFX_XMLATTRIBUTENODE_H_ diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp index 27d136305b..42588c6393 100644 --- a/core/fxcrt/xml/cfx_xmlelement.cpp +++ b/core/fxcrt/xml/cfx_xmlelement.cpp @@ -16,17 +16,19 @@ #include "third_party/base/stl_util.h" CFX_XMLElement::CFX_XMLElement(const WideString& wsTag) - : CFX_XMLAttributeNode(wsTag) {} + : CFX_XMLNode(), name_(wsTag) { + ASSERT(!name_.IsEmpty()); +} -CFX_XMLElement::~CFX_XMLElement() {} +CFX_XMLElement::~CFX_XMLElement() = default; FX_XMLNODETYPE CFX_XMLElement::GetType() const { return FX_XMLNODE_Element; } std::unique_ptr CFX_XMLElement::Clone() { - auto pClone = pdfium::MakeUnique(GetName()); - pClone->SetAttributes(GetAttributes()); + auto pClone = pdfium::MakeUnique(name_); + pClone->attrs_ = attrs_; WideString wsText; for (CFX_XMLNode* pChild = GetFirstChild(); pChild; @@ -39,15 +41,14 @@ std::unique_ptr CFX_XMLElement::Clone() { } WideString CFX_XMLElement::GetLocalTagName() const { - auto pos = GetName().Find(L':'); - return pos.has_value() - ? GetName().Right(GetName().GetLength() - pos.value() - 1) - : GetName(); + auto pos = name_.Find(L':'); + return pos.has_value() ? name_.Right(name_.GetLength() - pos.value() - 1) + : name_; } WideString CFX_XMLElement::GetNamespacePrefix() const { - auto pos = GetName().Find(L':'); - return pos.has_value() ? GetName().Left(pos.value()) : WideString(); + auto pos = name_.Find(L':'); + return pos.has_value() ? name_.Left(pos.value()) : WideString(); } WideString CFX_XMLElement::GetNamespaceURI() const { @@ -68,7 +69,7 @@ WideString CFX_XMLElement::GetNamespaceURI() const { pNode = pNode->GetParent(); continue; } - return pElement->GetString(wsAttri); + return pElement->GetAttribute(wsAttri); } return WideString(); } @@ -94,30 +95,28 @@ void CFX_XMLElement::SetTextData(const WideString& wsText) { void CFX_XMLElement::Save( const RetainPtr& pXMLStream) { - WideString ws(L"<"); - ws += GetName(); - pXMLStream->WriteString(ws.AsStringView()); + pXMLStream->WriteString(L"<"); + pXMLStream->WriteString(name_.AsStringView()); - for (auto it : GetAttributes()) { + for (auto it : attrs_) { pXMLStream->WriteString( AttributeToString(it.first, it.second).AsStringView()); } - if (GetFirstChild()) { - ws = L"\n>"; - pXMLStream->WriteString(ws.AsStringView()); + if (!GetFirstChild()) { + pXMLStream->WriteString(L" />"); + return; + } - for (CFX_XMLNode* pChild = GetFirstChild(); pChild; - pChild = pChild->GetNextSibling()) { - pChild->Save(pXMLStream); - } - ws = L""; - } else { - ws = L"\n/>"; + pXMLStream->WriteString(L">"); + + for (CFX_XMLNode* pChild = GetFirstChild(); pChild; + pChild = pChild->GetNextSibling()) { + pChild->Save(pXMLStream); } - pXMLStream->WriteString(ws.AsStringView()); + pXMLStream->WriteString(L"WriteString(name_.AsStringView()); + pXMLStream->WriteString(L"\n>"); } CFX_XMLElement* CFX_XMLElement::GetFirstChildNamed( @@ -132,7 +131,7 @@ CFX_XMLElement* CFX_XMLElement::GetNthChildNamed(const WideStringView& name, continue; CFX_XMLElement* elem = static_cast(child); - if (elem->GetName() != name) + if (elem->name_ != name) continue; if (idx == 0) return elem; @@ -141,3 +140,31 @@ CFX_XMLElement* CFX_XMLElement::GetNthChildNamed(const WideStringView& name, } return nullptr; } + +bool CFX_XMLElement::HasAttribute(const WideString& name) const { + return attrs_.find(name) != attrs_.end(); +} + +WideString CFX_XMLElement::GetAttribute(const WideString& name) const { + auto it = attrs_.find(name); + return it != attrs_.end() ? it->second : L""; +} + +void CFX_XMLElement::SetAttribute(const WideString& name, + const WideString& value) { + attrs_[name] = value; +} + +void CFX_XMLElement::RemoveAttribute(const WideString& name) { + attrs_.erase(name); +} + +WideString CFX_XMLElement::AttributeToString(const WideString& name, + const WideString& value) { + WideString ret = L" "; + ret += name; + ret += L"=\""; + ret += EncodeEntities(value); + ret += L"\""; + return ret; +} diff --git a/core/fxcrt/xml/cfx_xmlelement.h b/core/fxcrt/xml/cfx_xmlelement.h index f713114eb7..f8533a1445 100644 --- a/core/fxcrt/xml/cfx_xmlelement.h +++ b/core/fxcrt/xml/cfx_xmlelement.h @@ -7,13 +7,14 @@ #ifndef CORE_FXCRT_XML_CFX_XMLELEMENT_H_ #define CORE_FXCRT_XML_CFX_XMLELEMENT_H_ +#include #include #include #include "core/fxcrt/fx_string.h" -#include "core/fxcrt/xml/cfx_xmlattributenode.h" +#include "core/fxcrt/xml/cfx_xmlnode.h" -class CFX_XMLElement : public CFX_XMLAttributeNode { +class CFX_XMLElement : public CFX_XMLNode { public: explicit CFX_XMLElement(const WideString& wsTag); ~CFX_XMLElement() override; @@ -23,6 +24,17 @@ class CFX_XMLElement : public CFX_XMLAttributeNode { std::unique_ptr Clone() override; void Save(const RetainPtr& pXMLStream) override; + WideString GetName() const { return name_; } + + const std::map& GetAttributes() const { + return attrs_; + } + bool HasAttribute(const WideString& name) const; + void SetAttribute(const WideString& name, const WideString& value); + WideString GetAttribute(const WideString& name) const; + + void RemoveAttribute(const WideString& name); + CFX_XMLElement* GetFirstChildNamed(const WideStringView& name) const; CFX_XMLElement* GetNthChildNamed(const WideStringView& name, size_t idx) const; @@ -33,6 +45,12 @@ class CFX_XMLElement : public CFX_XMLAttributeNode { WideString GetTextData() const; void SetTextData(const WideString& wsText); + + private: + WideString AttributeToString(const WideString& name, const WideString& value); + + WideString name_; + std::map attrs_; }; #endif // CORE_FXCRT_XML_CFX_XMLELEMENT_H_ diff --git a/core/fxcrt/xml/cfx_xmlinstruction.h b/core/fxcrt/xml/cfx_xmlinstruction.h index cbdf9e2538..7cee1d4fc0 100644 --- a/core/fxcrt/xml/cfx_xmlinstruction.h +++ b/core/fxcrt/xml/cfx_xmlinstruction.h @@ -11,7 +11,7 @@ #include #include "core/fxcrt/fx_string.h" -#include "core/fxcrt/xml/cfx_xmlattributenode.h" +#include "core/fxcrt/xml/cfx_xmlnode.h" class CFX_XMLInstruction : public CFX_XMLNode { public: diff --git a/core/fxcrt/xml/cfx_xmlnode.cpp b/core/fxcrt/xml/cfx_xmlnode.cpp index 5e787ed2b9..540b20e721 100644 --- a/core/fxcrt/xml/cfx_xmlnode.cpp +++ b/core/fxcrt/xml/cfx_xmlnode.cpp @@ -119,13 +119,3 @@ WideString CFX_XMLNode::EncodeEntities(const WideString& value) { ret.Replace(L"\"", L"""); return ret; } - -WideString CFX_XMLNode::AttributeToString(const WideString& name, - const WideString& value) { - WideString ret = L" "; - ret += name; - ret += L"=\""; - ret += EncodeEntities(value); - ret += L"\""; - return ret; -} diff --git a/core/fxcrt/xml/cfx_xmlnode.h b/core/fxcrt/xml/cfx_xmlnode.h index 76625e3ca2..7278d2b4ca 100644 --- a/core/fxcrt/xml/cfx_xmlnode.h +++ b/core/fxcrt/xml/cfx_xmlnode.h @@ -45,7 +45,6 @@ class CFX_XMLNode { void DeleteChildren(); protected: - WideString AttributeToString(const WideString& name, const WideString& value); WideString EncodeEntities(const WideString& value); private: diff --git a/core/fxcrt/xml/cfx_xmlparser.cpp b/core/fxcrt/xml/cfx_xmlparser.cpp index da287cf9cc..8292b33206 100644 --- a/core/fxcrt/xml/cfx_xmlparser.cpp +++ b/core/fxcrt/xml/cfx_xmlparser.cpp @@ -189,8 +189,8 @@ bool CFX_XMLParser::Parse() { break; case FX_XmlSyntaxResult::AttriValue: if (m_pChild && m_pChild->GetType() == FX_XMLNODE_Element) { - static_cast(m_pChild)->SetString(m_ws1, - GetAttributeName()); + static_cast(m_pChild)->SetAttribute( + m_ws1, GetAttributeName()); } m_ws1.clear(); break; -- cgit v1.2.3