OLD | NEW |
---|---|
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../include/fpdfdoc/fpdf_doc.h" | 7 #include "../../include/fpdfdoc/fpdf_doc.h" |
8 #include "../../include/fxcrt/fx_xml.h" | 8 #include "../../include/fxcrt/fx_xml.h" |
9 typedef struct _PDFDOC_METADATA { | 9 |
10 CPDF_Document* m_pDoc; | 10 CPDF_Metadata::CPDF_Metadata(CPDF_Document* pDoc) : m_pDoc(pDoc) { |
11 CXML_Element* m_pXmlElmnt; | |
12 CXML_Element* m_pElmntRdf; | |
13 CFX_CMapByteStringToPtr* m_pStringMap; | |
14 } PDFDOC_METADATA, *PDFDOC_LPMETADATA; | |
15 typedef PDFDOC_METADATA const* PDFDOC_LPCMETADATA; | |
16 const FX_CHAR* const gs_FPDFDOC_Metadata_Titles[] = { | |
17 "Title", "title", "Subject", "description", "Author", | |
18 "creator", "Keywords", "Keywords", "Producer", "Producer", | |
19 "Creator", "CreatorTool", "CreationDate", "CreateDate", "ModDate", | |
20 "ModifyDate", "MetadataDate", "MetadataDate"}; | |
21 CPDF_Metadata::CPDF_Metadata() { | |
22 m_pData = FX_Alloc(PDFDOC_METADATA, 1); | |
23 CFX_CMapByteStringToPtr*& pStringMap = | |
24 ((PDFDOC_LPMETADATA)m_pData)->m_pStringMap; | |
25 pStringMap = new CFX_CMapByteStringToPtr; | |
26 CFX_ByteString bstr; | |
27 for (int i = 0; i < 18; i += 2) { | |
28 bstr = gs_FPDFDOC_Metadata_Titles[i]; | |
29 pStringMap->AddValue(bstr, (void*)gs_FPDFDOC_Metadata_Titles[i + 1]); | |
30 } | |
31 } | |
32 CPDF_Metadata::~CPDF_Metadata() { | |
33 FXSYS_assert(m_pData != NULL); | |
34 CXML_Element*& p = ((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt; | |
35 delete p; | |
36 CFX_CMapByteStringToPtr* pStringMap = | |
37 ((PDFDOC_LPMETADATA)m_pData)->m_pStringMap; | |
38 if (pStringMap) { | |
39 pStringMap->RemoveAll(); | |
40 delete pStringMap; | |
41 } | |
42 FX_Free(m_pData); | |
43 } | |
44 void CPDF_Metadata::LoadDoc(CPDF_Document* pDoc) { | |
45 FXSYS_assert(pDoc != NULL); | |
46 ((PDFDOC_LPMETADATA)m_pData)->m_pDoc = pDoc; | |
47 CPDF_Dictionary* pRoot = pDoc->GetRoot(); | 11 CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
12 if (!pRoot) | |
Lei Zhang
2015/08/11 20:26:23
To be clear, this is the part that actually fixes
| |
13 return; | |
14 | |
48 CPDF_Stream* pStream = pRoot->GetStream(FX_BSTRC("Metadata")); | 15 CPDF_Stream* pStream = pRoot->GetStream(FX_BSTRC("Metadata")); |
49 if (!pStream) { | 16 if (!pStream) |
50 return; | 17 return; |
51 } | 18 |
52 CPDF_StreamAcc acc; | 19 CPDF_StreamAcc acc; |
53 acc.LoadAllData(pStream, FALSE); | 20 acc.LoadAllData(pStream, FALSE); |
54 int size = acc.GetSize(); | 21 m_pXmlElement.reset(CXML_Element::Parse(acc.GetData(), acc.GetSize())); |
55 const uint8_t* pBuf = acc.GetData(); | |
56 CXML_Element*& pXmlElmnt = ((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt; | |
57 pXmlElmnt = CXML_Element::Parse(pBuf, size); | |
58 if (!pXmlElmnt) { | |
59 return; | |
60 } | |
61 CXML_Element*& pElmntRdf = ((PDFDOC_LPMETADATA)m_pData)->m_pElmntRdf; | |
62 if (pXmlElmnt->GetTagName() == FX_BSTRC("RDF")) { | |
63 pElmntRdf = pXmlElmnt; | |
64 } else { | |
65 pElmntRdf = pXmlElmnt->GetElement(NULL, FX_BSTRC("RDF")); | |
66 } | |
67 } | 22 } |
68 int32_t CPDF_Metadata::GetString(const CFX_ByteStringC& bsItem, | 23 |
69 CFX_WideString& wsStr) { | 24 CPDF_Metadata::~CPDF_Metadata() {} |
70 if (!((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt) { | 25 |
71 return -1; | 26 const CXML_Element* CPDF_Metadata::GetRoot() const { |
72 } | 27 return m_pXmlElement.get(); |
73 if (!((PDFDOC_LPMETADATA)m_pData)->m_pStringMap) { | |
74 return -1; | |
75 } | |
76 void* szTag; | |
77 if (!((PDFDOC_LPMETADATA)m_pData)->m_pStringMap->Lookup(bsItem, szTag)) { | |
78 return -1; | |
79 } | |
80 CFX_ByteString bsTag = (const FX_CHAR*)szTag; | |
81 wsStr = L""; | |
82 CXML_Element* pElmntRdf = ((PDFDOC_LPMETADATA)m_pData)->m_pElmntRdf; | |
83 if (!pElmntRdf) { | |
84 return -1; | |
85 } | |
86 int nChild = pElmntRdf->CountChildren(); | |
87 for (int i = 0; i < nChild; i++) { | |
88 CXML_Element* pTag = | |
89 pElmntRdf->GetElement(NULL, FX_BSTRC("Description"), i); | |
90 if (!pTag) { | |
91 continue; | |
92 } | |
93 if (bsItem == FX_BSTRC("Title") || bsItem == FX_BSTRC("Subject")) { | |
94 CXML_Element* pElmnt = pTag->GetElement(NULL, bsTag); | |
95 if (!pElmnt) { | |
96 continue; | |
97 } | |
98 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("Alt")); | |
99 if (!pElmnt) { | |
100 continue; | |
101 } | |
102 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("li")); | |
103 if (!pElmnt) { | |
104 continue; | |
105 } | |
106 wsStr = pElmnt->GetContent(0); | |
107 return wsStr.GetLength(); | |
108 } | |
109 if (bsItem == FX_BSTRC("Author")) { | |
110 CXML_Element* pElmnt = pTag->GetElement(NULL, bsTag); | |
111 if (!pElmnt) { | |
112 continue; | |
113 } | |
114 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("Seq")); | |
115 if (!pElmnt) { | |
116 continue; | |
117 } | |
118 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("li")); | |
119 if (!pElmnt) { | |
120 continue; | |
121 } | |
122 wsStr = pElmnt->GetContent(0); | |
123 return wsStr.GetLength(); | |
124 } | |
125 CXML_Element* pElmnt = pTag->GetElement(NULL, bsTag); | |
126 if (!pElmnt) { | |
127 continue; | |
128 } | |
129 wsStr = pElmnt->GetContent(0); | |
130 return wsStr.GetLength(); | |
131 } | |
132 return -1; | |
133 } | 28 } |
134 CXML_Element* CPDF_Metadata::GetRoot() const { | |
135 return ((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt; | |
136 } | |
137 CXML_Element* CPDF_Metadata::GetRDF() const { | |
138 return ((PDFDOC_LPMETADATA)m_pData)->m_pElmntRdf; | |
139 } | |
OLD | NEW |