summaryrefslogtreecommitdiff
path: root/core/fxcrt/xml/cfx_saxreaderhandler.cpp
blob: 9967f3eea87e95b94040b6c7ab2a51b8d3f944d4 (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
// 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/cfx_saxreaderhandler.h"

#include "core/fxcrt/cfx_checksumcontext.h"

CFX_SAXReaderHandler::CFX_SAXReaderHandler(CFX_ChecksumContext* pContext)
    : m_pContext(pContext) {
  ASSERT(m_pContext);
}

CFX_SAXReaderHandler::~CFX_SAXReaderHandler() {}

CFX_SAXContext* CFX_SAXReaderHandler::OnTagEnter(
    const CFX_ByteStringC& bsTagName,
    CFX_SAXItem::Type eType,
    uint32_t dwStartPos) {
  UpdateChecksum(true);
  if (eType != CFX_SAXItem::Type::Tag &&
      eType != CFX_SAXItem::Type::Instruction) {
    return nullptr;
  }

  m_SAXContext.m_eNode = eType;
  m_SAXContext.m_TextBuf << "<";
  if (eType == CFX_SAXItem::Type::Instruction)
    m_SAXContext.m_TextBuf << "?";

  m_SAXContext.m_TextBuf << bsTagName.c_str();
  m_SAXContext.m_bsTagName = bsTagName;
  return &m_SAXContext;
}

void CFX_SAXReaderHandler::OnTagAttribute(CFX_SAXContext* pTag,
                                          const CFX_ByteStringC& bsAttri,
                                          const CFX_ByteStringC& bsValue) {
  if (!pTag)
    return;
  pTag->m_TextBuf << " " << bsAttri.c_str() << "=\"" << bsValue.c_str() << "\"";
}

void CFX_SAXReaderHandler::OnTagBreak(CFX_SAXContext* pTag) {
  if (!pTag)
    return;

  pTag->m_TextBuf << ">";
  UpdateChecksum(false);
}

void CFX_SAXReaderHandler::OnTagData(CFX_SAXContext* pTag,
                                     CFX_SAXItem::Type eType,
                                     const CFX_ByteStringC& bsData,
                                     uint32_t dwStartPos) {
  if (!pTag)
    return;

  if (eType == CFX_SAXItem::Type::CharData)
    pTag->m_TextBuf << "<![CDATA[";

  pTag->m_TextBuf << bsData.c_str();
  if (eType == CFX_SAXItem::Type::CharData)
    pTag->m_TextBuf << "]]>";
}

void CFX_SAXReaderHandler::OnTagClose(CFX_SAXContext* pTag, uint32_t dwEndPos) {
  if (!pTag)
    return;

  if (pTag->m_eNode == CFX_SAXItem::Type::Instruction)
    pTag->m_TextBuf << "?>";
  else if (pTag->m_eNode == CFX_SAXItem::Type::Tag)
    pTag->m_TextBuf << "></" << pTag->m_bsTagName.AsStringC().c_str() << ">";

  UpdateChecksum(false);
}

void CFX_SAXReaderHandler::OnTagEnd(CFX_SAXContext* pTag,
                                    const CFX_ByteStringC& bsTagName,
                                    uint32_t dwEndPos) {
  if (!pTag)
    return;

  pTag->m_TextBuf << "</" << bsTagName.c_str() << ">";
  UpdateChecksum(false);
}

void CFX_SAXReaderHandler::OnTargetData(CFX_SAXContext* pTag,
                                        CFX_SAXItem::Type eType,
                                        const CFX_ByteStringC& bsData,
                                        uint32_t dwStartPos) {
  if (!pTag && eType != CFX_SAXItem::Type::Comment)
    return;

  if (eType == CFX_SAXItem::Type::Comment) {
    m_SAXContext.m_TextBuf << "<!--" << bsData.c_str() << "-->";
    UpdateChecksum(false);
  } else {
    pTag->m_TextBuf << " " << bsData.c_str();
  }
}

void CFX_SAXReaderHandler::UpdateChecksum(bool bCheckSpace) {
  int32_t iLength = m_SAXContext.m_TextBuf.tellp();
  if (iLength < 1)
    return;

  const uint8_t* pBuffer =
      reinterpret_cast<const uint8_t*>(m_SAXContext.m_TextBuf.str().c_str());
  bool bUpdata = true;
  if (bCheckSpace) {
    bUpdata = false;
    for (int32_t i = 0; i < iLength; i++) {
      bUpdata = (pBuffer[i] > 0x20);
      if (bUpdata)
        break;
    }
  }
  if (bUpdata)
    m_pContext->Update(CFX_ByteStringC(pBuffer, iLength));

  m_SAXContext.m_TextBuf.str("");
}