| 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 if (pStringMap != NULL) { | |
| 27 CFX_ByteString bstr; | |
| 28 for (int i = 0; i < 18; i += 2) { | |
| 29 bstr = gs_FPDFDOC_Metadata_Titles[i]; | |
| 30 pStringMap->AddValue(bstr, (void*)gs_FPDFDOC_Metadata_Titles[i + 1]); | |
| 31 } | |
| 32 } | |
| 33 } | |
| 34 CPDF_Metadata::~CPDF_Metadata() { | |
| 35 FXSYS_assert(m_pData != NULL); | |
| 36 CXML_Element*& p = ((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt; | |
| 37 delete p; | |
| 38 CFX_CMapByteStringToPtr* pStringMap = | |
| 39 ((PDFDOC_LPMETADATA)m_pData)->m_pStringMap; | |
| 40 if (pStringMap) { | |
| 41 pStringMap->RemoveAll(); | |
| 42 delete pStringMap; | |
| 43 } | |
| 44 FX_Free(m_pData); | |
| 45 } | |
| 46 void CPDF_Metadata::LoadDoc(CPDF_Document* pDoc) { | |
| 47 FXSYS_assert(pDoc != NULL); | |
| 48 ((PDFDOC_LPMETADATA)m_pData)->m_pDoc = pDoc; | |
| 49 CPDF_Dictionary* pRoot = pDoc->GetRoot(); | 11 CPDF_Dictionary* pRoot = pDoc->GetRoot(); |
| 12 if (!pRoot) |
| 13 return; |
| 14 |
| 50 CPDF_Stream* pStream = pRoot->GetStream(FX_BSTRC("Metadata")); | 15 CPDF_Stream* pStream = pRoot->GetStream(FX_BSTRC("Metadata")); |
| 51 if (!pStream) { | 16 if (!pStream) |
| 52 return; | 17 return; |
| 53 } | 18 |
| 54 CPDF_StreamAcc acc; | 19 CPDF_StreamAcc acc; |
| 55 acc.LoadAllData(pStream, FALSE); | 20 acc.LoadAllData(pStream, FALSE); |
| 56 int size = acc.GetSize(); | 21 m_pXmlElement.reset(CXML_Element::Parse(acc.GetData(), acc.GetSize())); |
| 57 const uint8_t* pBuf = acc.GetData(); | |
| 58 CXML_Element*& pXmlElmnt = ((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt; | |
| 59 pXmlElmnt = CXML_Element::Parse(pBuf, size); | |
| 60 if (!pXmlElmnt) { | |
| 61 return; | |
| 62 } | |
| 63 CXML_Element*& pElmntRdf = ((PDFDOC_LPMETADATA)m_pData)->m_pElmntRdf; | |
| 64 if (pXmlElmnt->GetTagName() == FX_BSTRC("RDF")) { | |
| 65 pElmntRdf = pXmlElmnt; | |
| 66 } else { | |
| 67 pElmntRdf = pXmlElmnt->GetElement(NULL, FX_BSTRC("RDF")); | |
| 68 } | |
| 69 } | 22 } |
| 70 int32_t CPDF_Metadata::GetString(const CFX_ByteStringC& bsItem, | 23 |
| 71 CFX_WideString& wsStr) { | 24 CPDF_Metadata::~CPDF_Metadata() {} |
| 72 if (!((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt) { | 25 |
| 73 return -1; | 26 const CXML_Element* CPDF_Metadata::GetRoot() const { |
| 74 } | 27 return m_pXmlElement.get(); |
| 75 if (!((PDFDOC_LPMETADATA)m_pData)->m_pStringMap) { | |
| 76 return -1; | |
| 77 } | |
| 78 void* szTag; | |
| 79 if (!((PDFDOC_LPMETADATA)m_pData)->m_pStringMap->Lookup(bsItem, szTag)) { | |
| 80 return -1; | |
| 81 } | |
| 82 CFX_ByteString bsTag = (const FX_CHAR*)szTag; | |
| 83 wsStr = L""; | |
| 84 CXML_Element* pElmntRdf = ((PDFDOC_LPMETADATA)m_pData)->m_pElmntRdf; | |
| 85 if (!pElmntRdf) { | |
| 86 return -1; | |
| 87 } | |
| 88 int nChild = pElmntRdf->CountChildren(); | |
| 89 for (int i = 0; i < nChild; i++) { | |
| 90 CXML_Element* pTag = | |
| 91 pElmntRdf->GetElement(NULL, FX_BSTRC("Description"), i); | |
| 92 if (!pTag) { | |
| 93 continue; | |
| 94 } | |
| 95 if (bsItem == FX_BSTRC("Title") || bsItem == FX_BSTRC("Subject")) { | |
| 96 CXML_Element* pElmnt = pTag->GetElement(NULL, bsTag); | |
| 97 if (!pElmnt) { | |
| 98 continue; | |
| 99 } | |
| 100 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("Alt")); | |
| 101 if (!pElmnt) { | |
| 102 continue; | |
| 103 } | |
| 104 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("li")); | |
| 105 if (!pElmnt) { | |
| 106 continue; | |
| 107 } | |
| 108 wsStr = pElmnt->GetContent(0); | |
| 109 return wsStr.GetLength(); | |
| 110 } | |
| 111 if (bsItem == FX_BSTRC("Author")) { | |
| 112 CXML_Element* pElmnt = pTag->GetElement(NULL, bsTag); | |
| 113 if (!pElmnt) { | |
| 114 continue; | |
| 115 } | |
| 116 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("Seq")); | |
| 117 if (!pElmnt) { | |
| 118 continue; | |
| 119 } | |
| 120 pElmnt = pElmnt->GetElement(NULL, FX_BSTRC("li")); | |
| 121 if (!pElmnt) { | |
| 122 continue; | |
| 123 } | |
| 124 wsStr = pElmnt->GetContent(0); | |
| 125 return wsStr.GetLength(); | |
| 126 } | |
| 127 CXML_Element* pElmnt = pTag->GetElement(NULL, bsTag); | |
| 128 if (!pElmnt) { | |
| 129 continue; | |
| 130 } | |
| 131 wsStr = pElmnt->GetContent(0); | |
| 132 return wsStr.GetLength(); | |
| 133 } | |
| 134 return -1; | |
| 135 } | 28 } |
| 136 CXML_Element* CPDF_Metadata::GetRoot() const { | |
| 137 return ((PDFDOC_LPMETADATA)m_pData)->m_pXmlElmnt; | |
| 138 } | |
| 139 CXML_Element* CPDF_Metadata::GetRDF() const { | |
| 140 return ((PDFDOC_LPMETADATA)m_pData)->m_pElmntRdf; | |
| 141 } | |
| OLD | NEW |