summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2018-02-13 21:50:44 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-02-13 21:50:44 +0000
commit6515cf256ca6dc30b43b34eaf88908aaf4784fd3 (patch)
tree0f3ea937ce2809ed46bb9d6da49c334bed6147e1 /core
parentfa3765cce4da4c3923e525f0462afd794360d221 (diff)
downloadpdfium-6515cf256ca6dc30b43b34eaf88908aaf4784fd3.tar.xz
Split CFX_XMLNode::SaveXMLNode apart
This CL removes the switch from SaveXMLNode and moves the required code into override methods in the child classes. The method is renamed from SaveXMLNode to just Save. Change-Id: I2011b80525e99635c573b4e0cf977e94f6b7cea6 Reviewed-on: https://pdfium-review.googlesource.com/26590 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core')
-rw-r--r--core/fxcrt/xml/cfx_xmlchardata.cpp8
-rw-r--r--core/fxcrt/xml/cfx_xmlchardata.h1
-rw-r--r--core/fxcrt/xml/cfx_xmlelement.cpp28
-rw-r--r--core/fxcrt/xml/cfx_xmlelement.h1
-rw-r--r--core/fxcrt/xml/cfx_xmlinstruction.cpp35
-rw-r--r--core/fxcrt/xml/cfx_xmlinstruction.h1
-rw-r--r--core/fxcrt/xml/cfx_xmlnode.cpp128
-rw-r--r--core/fxcrt/xml/cfx_xmlnode.h5
-rw-r--r--core/fxcrt/xml/cfx_xmltext.cpp4
-rw-r--r--core/fxcrt/xml/cfx_xmltext.h1
10 files changed, 101 insertions, 111 deletions
diff --git a/core/fxcrt/xml/cfx_xmlchardata.cpp b/core/fxcrt/xml/cfx_xmlchardata.cpp
index 902d139c99..7712b30dfe 100644
--- a/core/fxcrt/xml/cfx_xmlchardata.cpp
+++ b/core/fxcrt/xml/cfx_xmlchardata.cpp
@@ -20,3 +20,11 @@ FX_XMLNODETYPE CFX_XMLCharData::GetType() const {
std::unique_ptr<CFX_XMLNode> CFX_XMLCharData::Clone() {
return pdfium::MakeUnique<CFX_XMLCharData>(GetText());
}
+
+void CFX_XMLCharData::Save(
+ const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ WideString ws = L"<![CDATA[";
+ ws += GetText();
+ ws += L"]]>";
+ pXMLStream->WriteString(ws.AsStringView());
+}
diff --git a/core/fxcrt/xml/cfx_xmlchardata.h b/core/fxcrt/xml/cfx_xmlchardata.h
index b7e691495f..c702d9636f 100644
--- a/core/fxcrt/xml/cfx_xmlchardata.h
+++ b/core/fxcrt/xml/cfx_xmlchardata.h
@@ -20,6 +20,7 @@ class CFX_XMLCharData : public CFX_XMLText {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
};
#endif // CORE_FXCRT_XML_CFX_XMLCHARDATA_H_
diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp
index 39233190b1..4eb3900c2f 100644
--- a/core/fxcrt/xml/cfx_xmlelement.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement.cpp
@@ -101,3 +101,31 @@ void CFX_XMLElement::SetTextData(const WideString& wsText) {
return;
AppendChild(new CFX_XMLText(wsText));
}
+
+void CFX_XMLElement::Save(
+ const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ WideString ws(L"<");
+ ws += GetName();
+ pXMLStream->WriteString(ws.AsStringView());
+
+ for (auto it : GetAttributes()) {
+ pXMLStream->WriteString(
+ AttributeToString(it.first, it.second).AsStringView());
+ }
+
+ if (GetFirstChild()) {
+ ws = L"\n>";
+ pXMLStream->WriteString(ws.AsStringView());
+ CFX_XMLNode* pChild = GetFirstChild();
+ while (pChild) {
+ pChild->Save(pXMLStream);
+ pChild = pChild->GetNextSibling();
+ }
+ ws = L"</";
+ ws += GetName();
+ ws += L"\n>";
+ } else {
+ ws = L"\n/>";
+ }
+ pXMLStream->WriteString(ws.AsStringView());
+}
diff --git a/core/fxcrt/xml/cfx_xmlelement.h b/core/fxcrt/xml/cfx_xmlelement.h
index 59e3af6a44..e665e24a98 100644
--- a/core/fxcrt/xml/cfx_xmlelement.h
+++ b/core/fxcrt/xml/cfx_xmlelement.h
@@ -21,6 +21,7 @@ class CFX_XMLElement : public CFX_XMLAttributeNode {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
WideString GetLocalTagName() const;
WideString GetNamespacePrefix() const;
diff --git a/core/fxcrt/xml/cfx_xmlinstruction.cpp b/core/fxcrt/xml/cfx_xmlinstruction.cpp
index 75a4a2eec6..dabd538b4b 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction.cpp
+++ b/core/fxcrt/xml/cfx_xmlinstruction.cpp
@@ -8,6 +8,7 @@
#include <utility>
+#include "core/fxcrt/fx_codepage.h"
#include "core/fxcrt/fx_extension.h"
#include "third_party/base/ptr_util.h"
#include "third_party/base/stl_util.h"
@@ -36,3 +37,37 @@ void CFX_XMLInstruction::RemoveData(int32_t index) {
if (pdfium::IndexInBounds(m_TargetData, index))
m_TargetData.erase(m_TargetData.begin() + index);
}
+
+void CFX_XMLInstruction::Save(
+ const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ if (GetName().CompareNoCase(L"xml") == 0) {
+ WideString ws = L"<?xml version=\"1.0\" encoding=\"";
+ uint16_t wCodePage = pXMLStream->GetCodePage();
+ if (wCodePage == FX_CODEPAGE_UTF16LE)
+ ws += L"UTF-16";
+ else if (wCodePage == FX_CODEPAGE_UTF16BE)
+ ws += L"UTF-16be";
+ else
+ ws += L"UTF-8";
+
+ ws += L"\"?>";
+ pXMLStream->WriteString(ws.AsStringView());
+ return;
+ }
+
+ pXMLStream->WriteString(
+ WideString::Format(L"<?%ls", GetName().c_str()).AsStringView());
+ for (auto it : GetAttributes()) {
+ pXMLStream->WriteString(
+ AttributeToString(it.first, it.second).AsStringView());
+ }
+
+ for (auto target : m_TargetData) {
+ WideString ws = L" \"";
+ ws += target;
+ ws += L"\"";
+ pXMLStream->WriteString(ws.AsStringView());
+ }
+
+ pXMLStream->WriteString(WideStringView(L"?>"));
+}
diff --git a/core/fxcrt/xml/cfx_xmlinstruction.h b/core/fxcrt/xml/cfx_xmlinstruction.h
index ff27dae3a4..415a86a379 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction.h
+++ b/core/fxcrt/xml/cfx_xmlinstruction.h
@@ -21,6 +21,7 @@ class CFX_XMLInstruction : public CFX_XMLAttributeNode {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
const std::vector<WideString>& GetTargetData() const { return m_TargetData; }
void AppendData(const WideString& wsData);
diff --git a/core/fxcrt/xml/cfx_xmlnode.cpp b/core/fxcrt/xml/cfx_xmlnode.cpp
index 7cc506af9f..6135907557 100644
--- a/core/fxcrt/xml/cfx_xmlnode.cpp
+++ b/core/fxcrt/xml/cfx_xmlnode.cpp
@@ -8,7 +8,6 @@
#include <vector>
-#include "core/fxcrt/fx_codepage.h"
#include "core/fxcrt/xml/cfx_xmlchardata.h"
#include "core/fxcrt/xml/cfx_xmlelement.h"
#include "core/fxcrt/xml/cfx_xmlinstruction.h"
@@ -98,113 +97,22 @@ std::unique_ptr<CFX_XMLNode> CFX_XMLNode::Clone() {
return nullptr;
}
-void CFX_XMLNode::SaveXMLNode(
- const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
- CFX_XMLNode* pNode = (CFX_XMLNode*)this;
- switch (pNode->GetType()) {
- case FX_XMLNODE_Instruction: {
- WideString ws;
- CFX_XMLInstruction* pInstruction = (CFX_XMLInstruction*)pNode;
- if (pInstruction->GetName().CompareNoCase(L"xml") == 0) {
- ws = L"<?xml version=\"1.0\" encoding=\"";
- uint16_t wCodePage = pXMLStream->GetCodePage();
- if (wCodePage == FX_CODEPAGE_UTF16LE) {
- ws += L"UTF-16";
- } else if (wCodePage == FX_CODEPAGE_UTF16BE) {
- ws += L"UTF-16be";
- } else {
- ws += L"UTF-8";
- }
- ws += L"\"?>";
- pXMLStream->WriteString(ws.AsStringView());
- } else {
- ws = WideString::Format(L"<?%ls", pInstruction->GetName().c_str());
- pXMLStream->WriteString(ws.AsStringView());
-
- for (auto it : pInstruction->GetAttributes()) {
- WideString wsValue = it.second;
- wsValue.Replace(L"&", L"&amp;");
- wsValue.Replace(L"<", L"&lt;");
- wsValue.Replace(L">", L"&gt;");
- wsValue.Replace(L"\'", L"&apos;");
- wsValue.Replace(L"\"", L"&quot;");
-
- ws = L" ";
- ws += it.first;
- ws += L"=\"";
- ws += wsValue;
- ws += L"\"";
- pXMLStream->WriteString(ws.AsStringView());
- }
-
- for (auto target : pInstruction->GetTargetData()) {
- ws = L" \"";
- ws += target;
- ws += L"\"";
- pXMLStream->WriteString(ws.AsStringView());
- }
- ws = L"?>";
- pXMLStream->WriteString(ws.AsStringView());
- }
- break;
- }
- case FX_XMLNODE_Element: {
- WideString ws;
- ws = L"<";
- ws += static_cast<CFX_XMLElement*>(pNode)->GetName();
- pXMLStream->WriteString(ws.AsStringView());
-
- for (auto it : static_cast<CFX_XMLElement*>(pNode)->GetAttributes()) {
- WideString wsValue = it.second;
- wsValue.Replace(L"&", L"&amp;");
- wsValue.Replace(L"<", L"&lt;");
- wsValue.Replace(L">", L"&gt;");
- wsValue.Replace(L"\'", L"&apos;");
- wsValue.Replace(L"\"", L"&quot;");
-
- ws = L" ";
- ws += it.first;
- ws += L"=\"";
- ws += wsValue;
- ws += L"\"";
- pXMLStream->WriteString(ws.AsStringView());
- }
- if (pNode->first_child_) {
- ws = L"\n>";
- pXMLStream->WriteString(ws.AsStringView());
- CFX_XMLNode* pChild = pNode->first_child_;
- while (pChild) {
- pChild->SaveXMLNode(pXMLStream);
- pChild = pChild->next_sibling_;
- }
- ws = L"</";
- ws += static_cast<CFX_XMLElement*>(pNode)->GetName();
- ws += L"\n>";
- } else {
- ws = L"\n/>";
- }
- pXMLStream->WriteString(ws.AsStringView());
- break;
- }
- case FX_XMLNODE_Text: {
- WideString ws = static_cast<CFX_XMLText*>(pNode)->GetText();
- ws.Replace(L"&", L"&amp;");
- ws.Replace(L"<", L"&lt;");
- ws.Replace(L">", L"&gt;");
- ws.Replace(L"\'", L"&apos;");
- ws.Replace(L"\"", L"&quot;");
- pXMLStream->WriteString(ws.AsStringView());
- break;
- }
- case FX_XMLNODE_CharData: {
- WideString ws = L"<![CDATA[";
- ws += static_cast<CFX_XMLCharData*>(pNode)->GetText();
- ws += L"]]>";
- pXMLStream->WriteString(ws.AsStringView());
- break;
- }
- case FX_XMLNODE_Unknown:
- default:
- break;
- }
+void CFX_XMLNode::Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {}
+
+WideString CFX_XMLNode::EncodeEntities(WideString value) {
+ value.Replace(L"&", L"&amp;");
+ value.Replace(L"<", L"&lt;");
+ value.Replace(L">", L"&gt;");
+ value.Replace(L"\'", L"&apos;");
+ value.Replace(L"\"", L"&quot;");
+ return value;
+}
+
+WideString CFX_XMLNode::AttributeToString(WideString name, 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 55c79dcfe0..e2154c4f71 100644
--- a/core/fxcrt/xml/cfx_xmlnode.h
+++ b/core/fxcrt/xml/cfx_xmlnode.h
@@ -32,6 +32,7 @@ class CFX_XMLNode {
virtual FX_XMLNODETYPE GetType() const;
virtual std::unique_ptr<CFX_XMLNode> Clone();
+ virtual void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream);
CFX_XMLNode* GetRoot();
CFX_XMLNode* GetParent() const { return parent_; }
@@ -43,7 +44,9 @@ class CFX_XMLNode {
void RemoveChildNode(CFX_XMLNode* pNode);
void DeleteChildren();
- void SaveXMLNode(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream);
+ protected:
+ WideString AttributeToString(WideString name, WideString value);
+ WideString EncodeEntities(WideString value);
private:
CFX_XMLNode* parent_ = nullptr;
diff --git a/core/fxcrt/xml/cfx_xmltext.cpp b/core/fxcrt/xml/cfx_xmltext.cpp
index 83ad0434bf..05e2935440 100644
--- a/core/fxcrt/xml/cfx_xmltext.cpp
+++ b/core/fxcrt/xml/cfx_xmltext.cpp
@@ -20,3 +20,7 @@ FX_XMLNODETYPE CFX_XMLText::GetType() const {
std::unique_ptr<CFX_XMLNode> CFX_XMLText::Clone() {
return pdfium::MakeUnique<CFX_XMLText>(m_wsText);
}
+
+void CFX_XMLText::Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) {
+ pXMLStream->WriteString(EncodeEntities(GetText()).AsStringView());
+}
diff --git a/core/fxcrt/xml/cfx_xmltext.h b/core/fxcrt/xml/cfx_xmltext.h
index e9f35855f5..b26eef14ac 100644
--- a/core/fxcrt/xml/cfx_xmltext.h
+++ b/core/fxcrt/xml/cfx_xmltext.h
@@ -20,6 +20,7 @@ class CFX_XMLText : public CFX_XMLNode {
// CFX_XMLNode
FX_XMLNODETYPE GetType() const override;
std::unique_ptr<CFX_XMLNode> Clone() override;
+ void Save(const RetainPtr<CFX_SeekableStreamProxy>& pXMLStream) override;
WideString GetText() const { return m_wsText; }
void SetText(const WideString& wsText) { m_wsText = wsText; }