/src/libreoffice/tools/source/xml/XmlWalker.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | */ |
9 | | |
10 | | #include <tools/stream.hxx> |
11 | | #include <tools/XmlWalker.hxx> |
12 | | |
13 | | #include <libxml/tree.h> |
14 | | #include <libxml/parser.h> |
15 | | #include <libxml/xmlstring.h> |
16 | | #include <vector> |
17 | | |
18 | | namespace tools |
19 | | { |
20 | | struct XmlWalkerImpl |
21 | | { |
22 | | XmlWalkerImpl() |
23 | 0 | : mpDocPtr(nullptr) |
24 | 0 | , mpRoot(nullptr) |
25 | 0 | , mpCurrent(nullptr) |
26 | 0 | { |
27 | 0 | } |
28 | | |
29 | | xmlDocPtr mpDocPtr; |
30 | | xmlNodePtr mpRoot; |
31 | | xmlNodePtr mpCurrent; |
32 | | |
33 | | std::vector<xmlNodePtr> mpStack; |
34 | | }; |
35 | | |
36 | | XmlWalker::XmlWalker() |
37 | 0 | : mpImpl(std::make_unique<XmlWalkerImpl>()) |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | XmlWalker::~XmlWalker() |
42 | 0 | { |
43 | 0 | if (mpImpl) |
44 | 0 | xmlFreeDoc(mpImpl->mpDocPtr); |
45 | 0 | } |
46 | | |
47 | | bool XmlWalker::open(SvStream* pStream) |
48 | 0 | { |
49 | 0 | std::size_t nSize = pStream->remainingSize(); |
50 | 0 | if (nSize == 0) |
51 | 0 | return false; |
52 | 0 | std::vector<sal_uInt8> aBuffer(nSize + 1); |
53 | 0 | pStream->ReadBytes(aBuffer.data(), nSize); |
54 | 0 | aBuffer[nSize] = 0; |
55 | 0 | mpImpl->mpDocPtr = xmlParseDoc(reinterpret_cast<xmlChar*>(aBuffer.data())); |
56 | 0 | if (mpImpl->mpDocPtr == nullptr) |
57 | 0 | return false; |
58 | 0 | mpImpl->mpRoot = xmlDocGetRootElement(mpImpl->mpDocPtr); |
59 | 0 | mpImpl->mpCurrent = mpImpl->mpRoot; |
60 | 0 | mpImpl->mpStack.push_back(mpImpl->mpCurrent); |
61 | 0 | return true; |
62 | 0 | } |
63 | | |
64 | | std::string_view XmlWalker::name() |
65 | 0 | { |
66 | 0 | return reinterpret_cast<const char*>(mpImpl->mpCurrent->name); |
67 | 0 | } |
68 | | |
69 | | std::string_view XmlWalker::namespaceHref() |
70 | 0 | { |
71 | 0 | return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->href); |
72 | 0 | } |
73 | | |
74 | | std::string_view XmlWalker::namespacePrefix() |
75 | 0 | { |
76 | 0 | return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->prefix); |
77 | 0 | } |
78 | | |
79 | | OString XmlWalker::content() |
80 | 0 | { |
81 | 0 | OString aContent; |
82 | 0 | if (mpImpl->mpCurrent->xmlChildrenNode != nullptr) |
83 | 0 | { |
84 | 0 | xmlChar* pContent |
85 | 0 | = xmlNodeListGetString(mpImpl->mpDocPtr, mpImpl->mpCurrent->xmlChildrenNode, 1); |
86 | 0 | aContent = OString(reinterpret_cast<const char*>(pContent)); |
87 | 0 | xmlFree(pContent); |
88 | 0 | } |
89 | 0 | return aContent; |
90 | 0 | } |
91 | | |
92 | | void XmlWalker::children() |
93 | 0 | { |
94 | 0 | mpImpl->mpStack.push_back(mpImpl->mpCurrent); |
95 | 0 | mpImpl->mpCurrent = mpImpl->mpCurrent->xmlChildrenNode; |
96 | 0 | } |
97 | | |
98 | | void XmlWalker::parent() |
99 | 0 | { |
100 | 0 | mpImpl->mpCurrent = mpImpl->mpStack.back(); |
101 | 0 | mpImpl->mpStack.pop_back(); |
102 | 0 | } |
103 | | |
104 | | OString XmlWalker::attribute(const OString& sName) const |
105 | 0 | { |
106 | 0 | xmlChar* xmlAttribute |
107 | 0 | = xmlGetProp(mpImpl->mpCurrent, reinterpret_cast<const xmlChar*>(sName.getStr())); |
108 | 0 | OString aAttributeContent( |
109 | 0 | xmlAttribute == nullptr ? "" : reinterpret_cast<const char*>(xmlAttribute)); |
110 | 0 | xmlFree(xmlAttribute); |
111 | |
|
112 | 0 | return aAttributeContent; |
113 | 0 | } |
114 | | |
115 | 0 | void XmlWalker::next() { mpImpl->mpCurrent = mpImpl->mpCurrent->next; } |
116 | | |
117 | 0 | bool XmlWalker::isValid() const { return mpImpl->mpCurrent != nullptr; } |
118 | | |
119 | | } // end tools namespace |
120 | | |
121 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |