| 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 "xml_int.h" | 7 #include "xml_int.h" |
| 8 | 8 |
| 9 #include "core/include/fxcrt/fx_ext.h" | 9 #include "core/include/fxcrt/fx_ext.h" |
| 10 #include "core/include/fxcrt/fx_xml.h" | 10 #include "core/include/fxcrt/fx_xml.h" |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 content.Clear(); | 460 content.Clear(); |
| 461 decoder.Clear(); | 461 decoder.Clear(); |
| 462 bCDATA = FALSE; | 462 bCDATA = FALSE; |
| 463 iState = 0; | 463 iState = 0; |
| 464 m_dwIndex--; | 464 m_dwIndex--; |
| 465 CXML_Element* pSubElement = ParseElement(pElement, TRUE); | 465 CXML_Element* pSubElement = ParseElement(pElement, TRUE); |
| 466 if (!pSubElement) { | 466 if (!pSubElement) { |
| 467 break; | 467 break; |
| 468 } | 468 } |
| 469 pSubElement->m_pParent = pElement; | 469 pSubElement->m_pParent = pElement; |
| 470 pElement->m_Children.Add((void*)CXML_Element::Element); | 470 pElement->m_Children.push_back( |
| 471 pElement->m_Children.Add(pSubElement); | 471 {CXML_Element::Element, pSubElement}); |
| 472 SkipWhiteSpaces(); | 472 SkipWhiteSpaces(); |
| 473 } | 473 } |
| 474 break; | 474 break; |
| 475 case 2: | 475 case 2: |
| 476 if (ch == '[') { | 476 if (ch == '[') { |
| 477 SkipLiterals("]]>"); | 477 SkipLiterals("]]>"); |
| 478 } else if (ch == '-') { | 478 } else if (ch == '-') { |
| 479 m_dwIndex++; | 479 m_dwIndex++; |
| 480 SkipLiterals("-->"); | 480 SkipLiterals("-->"); |
| 481 } else { | 481 } else { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 507 return pElement; | 507 return pElement; |
| 508 } | 508 } |
| 509 void CXML_Parser::InsertContentSegment(FX_BOOL bCDATA, | 509 void CXML_Parser::InsertContentSegment(FX_BOOL bCDATA, |
| 510 const CFX_WideStringC& content, | 510 const CFX_WideStringC& content, |
| 511 CXML_Element* pElement) { | 511 CXML_Element* pElement) { |
| 512 if (content.IsEmpty()) { | 512 if (content.IsEmpty()) { |
| 513 return; | 513 return; |
| 514 } | 514 } |
| 515 CXML_Content* pContent = new CXML_Content; | 515 CXML_Content* pContent = new CXML_Content; |
| 516 pContent->Set(bCDATA, content); | 516 pContent->Set(bCDATA, content); |
| 517 pElement->m_Children.Add((void*)CXML_Element::Content); | 517 pElement->m_Children.push_back({CXML_Element::Content, pContent}); |
| 518 pElement->m_Children.Add(pContent); | |
| 519 } | 518 } |
| 520 static CXML_Element* XML_ContinueParse(CXML_Parser& parser, | 519 static CXML_Element* XML_ContinueParse(CXML_Parser& parser, |
| 521 FX_BOOL bSaveSpaceChars, | 520 FX_BOOL bSaveSpaceChars, |
| 522 FX_FILESIZE* pParsedSize) { | 521 FX_FILESIZE* pParsedSize) { |
| 523 parser.m_bSaveSpaceChars = bSaveSpaceChars; | 522 parser.m_bSaveSpaceChars = bSaveSpaceChars; |
| 524 CXML_Element* pElement = parser.ParseElement(NULL, FALSE); | 523 CXML_Element* pElement = parser.ParseElement(NULL, FALSE); |
| 525 if (pParsedSize) { | 524 if (pParsedSize) { |
| 526 *pParsedSize = parser.m_nOffset; | 525 *pParsedSize = parser.m_nOffset; |
| 527 } | 526 } |
| 528 return pElement; | 527 return pElement; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 : m_pParent(NULL), m_QSpaceName(), m_TagName(), m_AttrMap() { | 565 : m_pParent(NULL), m_QSpaceName(), m_TagName(), m_AttrMap() { |
| 567 SetTag(qTagName); | 566 SetTag(qTagName); |
| 568 } | 567 } |
| 569 CXML_Element::~CXML_Element() { | 568 CXML_Element::~CXML_Element() { |
| 570 Empty(); | 569 Empty(); |
| 571 } | 570 } |
| 572 void CXML_Element::Empty() { | 571 void CXML_Element::Empty() { |
| 573 RemoveChildren(); | 572 RemoveChildren(); |
| 574 } | 573 } |
| 575 void CXML_Element::RemoveChildren() { | 574 void CXML_Element::RemoveChildren() { |
| 576 for (int i = 0; i < m_Children.GetSize(); i += 2) { | 575 for (const ChildRecord& record : m_Children) { |
| 577 ChildType type = (ChildType)(uintptr_t)m_Children.GetAt(i); | 576 if (record.type == Content) { |
| 578 if (type == Content) { | 577 delete static_cast<CXML_Content*>(record.child); |
| 579 CXML_Content* content = (CXML_Content*)m_Children.GetAt(i + 1); | 578 } else if (record.type == Element) { |
| 580 delete content; | 579 CXML_Element* child = static_cast<CXML_Element*>(record.child); |
| 581 } else if (type == Element) { | |
| 582 CXML_Element* child = (CXML_Element*)m_Children.GetAt(i + 1); | |
| 583 child->RemoveChildren(); | 580 child->RemoveChildren(); |
| 584 delete child; | 581 delete child; |
| 585 } | 582 } |
| 586 } | 583 } |
| 587 m_Children.RemoveAll(); | 584 m_Children.clear(); |
| 588 } | 585 } |
| 589 CFX_ByteString CXML_Element::GetTagName(FX_BOOL bQualified) const { | 586 CFX_ByteString CXML_Element::GetTagName(FX_BOOL bQualified) const { |
| 590 if (!bQualified || m_QSpaceName.IsEmpty()) { | 587 if (!bQualified || m_QSpaceName.IsEmpty()) { |
| 591 return m_TagName; | 588 return m_TagName; |
| 592 } | 589 } |
| 593 CFX_ByteString bsTag = m_QSpaceName; | 590 CFX_ByteString bsTag = m_QSpaceName; |
| 594 bsTag += ":"; | 591 bsTag += ":"; |
| 595 bsTag += m_TagName; | 592 bsTag += m_TagName; |
| 596 return bsTag; | 593 return bsTag; |
| 597 } | 594 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 FX_BOOL CXML_Element::GetAttrFloat(const CFX_ByteStringC& space, | 678 FX_BOOL CXML_Element::GetAttrFloat(const CFX_ByteStringC& space, |
| 682 const CFX_ByteStringC& name, | 679 const CFX_ByteStringC& name, |
| 683 FX_FLOAT& attribute) const { | 680 FX_FLOAT& attribute) const { |
| 684 const CFX_WideString* pValue = m_AttrMap.Lookup(space, name); | 681 const CFX_WideString* pValue = m_AttrMap.Lookup(space, name); |
| 685 if (pValue) { | 682 if (pValue) { |
| 686 attribute = pValue->GetFloat(); | 683 attribute = pValue->GetFloat(); |
| 687 return TRUE; | 684 return TRUE; |
| 688 } | 685 } |
| 689 return FALSE; | 686 return FALSE; |
| 690 } | 687 } |
| 691 FX_DWORD CXML_Element::CountChildren() const { | |
| 692 return m_Children.GetSize() / 2; | |
| 693 } | |
| 694 CXML_Element::ChildType CXML_Element::GetChildType(FX_DWORD index) const { | 688 CXML_Element::ChildType CXML_Element::GetChildType(FX_DWORD index) const { |
| 695 index <<= 1; | 689 return index < m_Children.size() ? m_Children[index].type : Invalid; |
| 696 if (index >= (FX_DWORD)m_Children.GetSize()) { | |
| 697 return Invalid; | |
| 698 } | |
| 699 return (ChildType)(uintptr_t)m_Children.GetAt(index); | |
| 700 } | 690 } |
| 701 CFX_WideString CXML_Element::GetContent(FX_DWORD index) const { | 691 CFX_WideString CXML_Element::GetContent(FX_DWORD index) const { |
| 702 index <<= 1; | 692 if (index < m_Children.size() && m_Children[index].type == Content) { |
| 703 if (index >= (FX_DWORD)m_Children.GetSize() || | 693 CXML_Content* pContent = |
| 704 (ChildType)(uintptr_t)m_Children.GetAt(index) != Content) { | 694 static_cast<CXML_Content*>(m_Children[index].child); |
| 705 return CFX_WideString(); | 695 if (pContent) |
| 706 } | 696 return pContent->m_Content; |
| 707 CXML_Content* pContent = (CXML_Content*)m_Children.GetAt(index + 1); | |
| 708 if (pContent) { | |
| 709 return pContent->m_Content; | |
| 710 } | 697 } |
| 711 return CFX_WideString(); | 698 return CFX_WideString(); |
| 712 } | 699 } |
| 713 CXML_Element* CXML_Element::GetElement(FX_DWORD index) const { | 700 CXML_Element* CXML_Element::GetElement(FX_DWORD index) const { |
| 714 index <<= 1; | 701 if (index < m_Children.size() && m_Children[index].type == Element) { |
| 715 if (index >= (FX_DWORD)m_Children.GetSize() || | 702 return static_cast<CXML_Element*>(m_Children[index].child); |
| 716 (ChildType)(uintptr_t)m_Children.GetAt(index) != Element) { | |
| 717 return NULL; | |
| 718 } | 703 } |
| 719 return (CXML_Element*)m_Children.GetAt(index + 1); | 704 return nullptr; |
| 720 } | 705 } |
| 721 FX_DWORD CXML_Element::CountElements(const CFX_ByteStringC& space, | 706 FX_DWORD CXML_Element::CountElements(const CFX_ByteStringC& space, |
| 722 const CFX_ByteStringC& tag) const { | 707 const CFX_ByteStringC& tag) const { |
| 723 int count = 0; | 708 int count = 0; |
| 724 for (int i = 0; i < m_Children.GetSize(); i += 2) { | 709 for (const ChildRecord& record : m_Children) { |
| 725 ChildType type = (ChildType)(uintptr_t)m_Children.GetAt(i); | 710 if (record.type != Element) |
| 726 if (type != Element) { | |
| 727 continue; | 711 continue; |
| 728 } | 712 |
| 729 CXML_Element* pKid = (CXML_Element*)m_Children.GetAt(i + 1); | 713 CXML_Element* pKid = static_cast<CXML_Element*>(record.child); |
| 730 if ((space.IsEmpty() || pKid->m_QSpaceName == space) && | 714 if ((space.IsEmpty() || pKid->m_QSpaceName == space) && |
| 731 pKid->m_TagName == tag) { | 715 pKid->m_TagName == tag) { |
| 732 count++; | 716 count++; |
| 733 } | 717 } |
| 734 } | 718 } |
| 735 return count; | 719 return count; |
| 736 } | 720 } |
| 737 CXML_Element* CXML_Element::GetElement(const CFX_ByteStringC& space, | 721 CXML_Element* CXML_Element::GetElement(const CFX_ByteStringC& space, |
| 738 const CFX_ByteStringC& tag, | 722 const CFX_ByteStringC& tag, |
| 739 int index) const { | 723 int index) const { |
| 740 if (index < 0) { | 724 if (index < 0) |
| 741 return NULL; | 725 return nullptr; |
| 742 } | 726 |
| 743 for (int i = 0; i < m_Children.GetSize(); i += 2) { | 727 for (const ChildRecord& record : m_Children) { |
| 744 ChildType type = (ChildType)(uintptr_t)m_Children.GetAt(i); | 728 if (record.type != Element) |
| 745 if (type != Element) { | |
| 746 continue; | 729 continue; |
| 747 } | 730 |
| 748 CXML_Element* pKid = (CXML_Element*)m_Children.GetAt(i + 1); | 731 CXML_Element* pKid = static_cast<CXML_Element*>(record.child); |
| 749 if ((!space.IsEmpty() && pKid->m_QSpaceName != space) || | 732 if ((space.IsEmpty() || pKid->m_QSpaceName == space) && |
| 750 pKid->m_TagName != tag) { | 733 pKid->m_TagName == tag) { |
| 751 continue; | 734 if (index-- == 0) |
| 752 } | 735 return pKid; |
| 753 if (index-- == 0) { | |
| 754 return pKid; | |
| 755 } | 736 } |
| 756 } | 737 } |
| 757 return NULL; | 738 return NULL; |
| 758 } | 739 } |
| 759 FX_DWORD CXML_Element::FindElement(CXML_Element* pChild) const { | 740 FX_DWORD CXML_Element::FindElement(CXML_Element* pChild) const { |
| 760 for (int i = 0; i < m_Children.GetSize(); i += 2) { | 741 int index = 0; |
| 761 if ((ChildType)(uintptr_t)m_Children.GetAt(i) == Element && | 742 for (const ChildRecord& record : m_Children) { |
| 762 (CXML_Element*)m_Children.GetAt(i + 1) == pChild) { | 743 if (record.type == Element && |
| 763 return (FX_DWORD)(i >> 1); | 744 static_cast<CXML_Element*>(record.child) == pChild) { |
| 745 return index; |
| 764 } | 746 } |
| 747 ++index; |
| 765 } | 748 } |
| 766 return (FX_DWORD)-1; | 749 return (FX_DWORD)-1; |
| 767 } | 750 } |
| 768 const CFX_WideString* CXML_AttrMap::Lookup(const CFX_ByteStringC& space, | 751 const CFX_WideString* CXML_AttrMap::Lookup(const CFX_ByteStringC& space, |
| 769 const CFX_ByteStringC& name) const { | 752 const CFX_ByteStringC& name) const { |
| 770 if (!m_pMap) { | 753 if (!m_pMap) { |
| 771 return NULL; | 754 return NULL; |
| 772 } | 755 } |
| 773 for (int i = 0; i < m_pMap->GetSize(); i++) { | 756 for (int i = 0; i < m_pMap->GetSize(); i++) { |
| 774 CXML_AttrItem& item = GetAt(i); | 757 CXML_AttrItem& item = GetAt(i); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 return (*m_pMap)[index]; | 805 return (*m_pMap)[index]; |
| 823 } | 806 } |
| 824 void CXML_AttrMap::RemoveAll() { | 807 void CXML_AttrMap::RemoveAll() { |
| 825 if (!m_pMap) { | 808 if (!m_pMap) { |
| 826 return; | 809 return; |
| 827 } | 810 } |
| 828 m_pMap->RemoveAll(); | 811 m_pMap->RemoveAll(); |
| 829 delete m_pMap; | 812 delete m_pMap; |
| 830 m_pMap = NULL; | 813 m_pMap = NULL; |
| 831 } | 814 } |
| OLD | NEW |