summaryrefslogtreecommitdiff
path: root/core/fxcrt/xml/cxml_element.cpp
blob: 9d3e22c8f9eccaa06a2c77f4da5ebaaec80f46cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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/cxml_element.h"

#include "core/fxcrt/xml/cxml_content.h"
#include "core/fxcrt/xml/cxml_parser.h"

// static
std::unique_ptr<CXML_Element> CXML_Element::Parse(const void* pBuffer,
                                                  size_t size) {
  CXML_Parser parser;
  if (!parser.Init(static_cast<const uint8_t*>(pBuffer), size))
    return nullptr;
  return parser.ParseElement(nullptr, false);
}

CXML_Element::CXML_Element(const CXML_Element* pParent,
                           const ByteStringView& qSpace,
                           const ByteStringView& tagname)
    : m_pParent(pParent), m_QSpaceName(qSpace), m_TagName(tagname) {}

CXML_Element::~CXML_Element() {}

CXML_Element* CXML_Element::AsElement() {
  return this;
}

const CXML_Element* CXML_Element::AsElement() const {
  return this;
}

ByteString CXML_Element::GetTagName() const {
  return m_TagName;
}

ByteString CXML_Element::GetNamespaceURI(const ByteString& qName) const {
  const WideString* pwsSpace;
  const CXML_Element* pElement = this;
  do {
    if (qName.IsEmpty())
      pwsSpace = pElement->m_AttrMap.Lookup("", "xmlns");
    else
      pwsSpace = pElement->m_AttrMap.Lookup("xmlns", qName);
    if (pwsSpace)
      break;

    pElement = pElement->GetParent();
  } while (pElement);
  return pwsSpace ? pwsSpace->UTF8Encode() : ByteString();
}

void CXML_Element::GetAttrByIndex(int index,
                                  ByteString* space,
                                  ByteString* name,
                                  WideString* value) const {
  if (index < 0 || index >= m_AttrMap.GetSize())
    return;

  CXML_AttrItem& item = m_AttrMap.GetAt(index);
  *space = item.m_QSpaceName;
  *name = item.m_AttrName;
  *value = item.m_Value;
}

WideString CXML_Element::GetAttrValue(const ByteStringView& name) const {
  ByteStringView bsSpace;
  ByteStringView bsName;
  FX_XML_SplitQualifiedName(name, bsSpace, bsName);

  WideString attr;
  const WideString* pValue =
      m_AttrMap.Lookup(ByteString(bsSpace), ByteString(bsName));
  if (pValue)
    attr = *pValue;
  return attr;
}

int CXML_Element::GetAttrInteger(const ByteStringView& name) const {
  ByteStringView bsSpace;
  ByteStringView bsName;
  FX_XML_SplitQualifiedName(name, bsSpace, bsName);

  const WideString* pwsValue =
      m_AttrMap.Lookup(ByteString(bsSpace), ByteString(bsName));
  return pwsValue ? pwsValue->GetInteger() : 0;
}

uint32_t CXML_Element::CountElements(const ByteStringView& space,
                                     const ByteStringView& tag) const {
  int count = 0;
  for (const auto& pChild : m_Children) {
    const CXML_Element* pKid = pChild->AsElement();
    if (pKid && pKid->m_TagName == tag &&
        (space.IsEmpty() || pKid->m_QSpaceName == space)) {
      count++;
    }
  }
  return count;
}

CXML_Object* CXML_Element::GetChild(uint32_t index) const {
  return index < m_Children.size() ? m_Children[index].get() : nullptr;
}

CXML_Element* CXML_Element::GetElement(const ByteStringView& space,
                                       const ByteStringView& tag,
                                       int nth) const {
  if (nth < 0)
    return nullptr;

  for (const auto& pChild : m_Children) {
    CXML_Element* pKid = pChild->AsElement();
    if (pKid && pKid->m_TagName == tag &&
        (space.IsEmpty() || pKid->m_QSpaceName == space)) {
      if (nth-- == 0)
        return pKid;
    }
  }
  return nullptr;
}

void CXML_Element::SetAttribute(const ByteString& space,
                                const ByteString& name,
                                const WideString& value) {
  m_AttrMap.SetAt(space, name, value);
}