aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/xml/document/xml-textreader.cpp
blob: 5ea6c75fe75ccd0cacdbf1a727ce2afa1eec7031 (plain) (blame)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include "xml-textreader.h"

#include <contrib/libs/libxml/include/libxml/xmlreader.h>

#include <util/generic/yexception.h>
#include <util/string/strip.h>
#include <util/system/compiler.h>

namespace NXml {
    TTextReader::TTextReader(IInputStream& stream, const TOptions& options)
        : Stream(stream)
        , IsError(false)
    {
        Impl.Reset(xmlReaderForIO(ReadFromInputStreamCallback, nullptr, this, nullptr, nullptr, options.GetMask()));

        if (!Impl) {
            ythrow yexception() << "cannot instantiate underlying xmlTextReader structure";
        }
        SetupErrorHandler();
        CheckForExceptions();
    }

    TTextReader::~TTextReader() {
    }

    bool TTextReader::Read() {
        return BoolResult(xmlTextReaderRead(Impl.Get()));
    }

    TString TTextReader::ReadInnerXml() const {
        return TempStringOrEmptyResult(xmlTextReaderReadInnerXml(Impl.Get()));
    }

    TString TTextReader::ReadOuterXml() const {
        return TempStringOrEmptyResult(xmlTextReaderReadOuterXml(Impl.Get()));
    }

    TString TTextReader::ReadString() const {
        return TempStringOrEmptyResult(xmlTextReaderReadString(Impl.Get()));
    }

    bool TTextReader::ReadAttributeValue() const {
        return BoolResult(xmlTextReaderReadAttributeValue(Impl.Get()));
    }

    int TTextReader::GetAttributeCount() const {
        return IntResult(xmlTextReaderAttributeCount(Impl.Get()));
    }

    TStringBuf TTextReader::GetBaseUri() const {
        return ConstStringOrEmptyResult(xmlTextReaderConstBaseUri(Impl.Get()));
    }

    int TTextReader::GetDepth() const {
        return IntResult(xmlTextReaderDepth(Impl.Get()));
    }

    bool TTextReader::HasAttributes() const {
        return BoolResult(xmlTextReaderHasAttributes(Impl.Get()));
    }

    bool TTextReader::HasValue() const {
        return BoolResult(xmlTextReaderHasValue(Impl.Get()));
    }

    bool TTextReader::IsDefault() const {
        return BoolResult(xmlTextReaderIsDefault(Impl.Get()));
    }

    bool TTextReader::IsEmptyElement() const {
        return BoolResult(xmlTextReaderIsEmptyElement(Impl.Get()));
    }

    TStringBuf TTextReader::GetLocalName() const {
        return ConstStringOrEmptyResult(xmlTextReaderConstLocalName(Impl.Get()));
    }

    TStringBuf TTextReader::GetName() const {
        return ConstStringOrEmptyResult(xmlTextReaderConstName(Impl.Get()));
    }

    TStringBuf TTextReader::GetNamespaceUri() const {
        return ConstStringOrEmptyResult(xmlTextReaderConstNamespaceUri(Impl.Get()));
    }

    TTextReader::ENodeType TTextReader::GetNodeType() const {
        return static_cast<ENodeType>(IntResult(xmlTextReaderNodeType(Impl.Get())));
    }

    TStringBuf TTextReader::GetPrefix() const {
        return ConstStringOrEmptyResult(xmlTextReaderConstPrefix(Impl.Get()));
    }

    char TTextReader::GetQuoteChar() const {
        return CharResult(xmlTextReaderQuoteChar(Impl.Get()));
    }

    TStringBuf TTextReader::GetValue() const {
        return ConstStringOrEmptyResult(xmlTextReaderConstValue(Impl.Get()));
    }

    TTextReader::EReadState TTextReader::GetReadState() const {
        return static_cast<EReadState>(IntResult(xmlTextReaderReadState(Impl.Get())));
    }

    void TTextReader::Close() {
        if (xmlTextReaderClose(Impl.Get()) == -1) {
            ThrowException();
        }
    }

    TString TTextReader::GetAttribute(int number) const {
        return TempStringResult(xmlTextReaderGetAttributeNo(Impl.Get(), number));
    }

    TString TTextReader::GetAttribute(TZtStringBuf name) const {
        return TempStringResult(xmlTextReaderGetAttribute(Impl.Get(), XMLCHAR(name.data()))); 
    }

    TString TTextReader::GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const {
        return TempStringResult(xmlTextReaderGetAttributeNs(Impl.Get(), XMLCHAR(localName.data()), XMLCHAR(nsUri.data()))); 
    }

    TString TTextReader::LookupNamespace(TZtStringBuf prefix) const {
        return TempStringResult(xmlTextReaderLookupNamespace(Impl.Get(), XMLCHAR(prefix.data()))); 
    }

    bool TTextReader::MoveToAttribute(int number) {
        return BoolResult(xmlTextReaderMoveToAttributeNo(Impl.Get(), number));
    }

    bool TTextReader::MoveToAttribute(TZtStringBuf name) {
        return BoolResult(xmlTextReaderMoveToAttribute(Impl.Get(), XMLCHAR(name.data()))); 
    }

    bool TTextReader::MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri) {
        return BoolResult(xmlTextReaderMoveToAttributeNs(Impl.Get(), XMLCHAR(localName.data()), XMLCHAR(nsUri.data()))); 
    }

    bool TTextReader::MoveToFirstAttribute() {
        return BoolResult(xmlTextReaderMoveToFirstAttribute(Impl.Get()));
    }

    bool TTextReader::MoveToNextAttribute() {
        return BoolResult(xmlTextReaderMoveToNextAttribute(Impl.Get()));
    }

    bool TTextReader::MoveToElement() {
        return BoolResult(xmlTextReaderMoveToElement(Impl.Get()));
    }

    TConstNode TTextReader::Expand() const {
        const xmlNodePtr node = xmlTextReaderExpand(Impl.Get());
        if (node == nullptr) {
            ThrowException();
        }
        return TConstNode(TNode(node->doc, node));
    }

    bool TTextReader::Next() {
        return BoolResult(xmlTextReaderNext(Impl.Get()));
    }

    bool TTextReader::IsValid() const {
        return BoolResult(xmlTextReaderIsValid(Impl.Get()));
    }

    // Callback for xmlReaderForIO() to read more data.
    // It is almost "noexcept" (std::bad_alloc may happen when saving exception message to new TString).
    // Waiting for std::exception_ptr and std::rethrow_exception from C++11 in Arcadia to make it really "noexcept".
    int TTextReader::ReadFromInputStreamCallback(void* context, char* buffer, int len) {
        Y_ASSERT(len >= 0);
        TTextReader* reader = static_cast<TTextReader*>(context);

        int result = -1;

        // Exception may be thrown by IInputStream::Read().
        // It is caught unconditionally because exceptions cannot safely pass through libxml2 plain C code
        // (no destructors, no RAII, raw pointers, so in case of stack unwinding some memory gets leaked).

        try {
            result = reader->Stream.Read(buffer, len);
        } catch (const yexception& ex) {
            reader->LogError() << "read from input stream failed: " << ex;
        } catch (...) {
            reader->LogError() << "read from input stream failed";
        }

        return result;
    }

    void TTextReader::OnLibxmlError(void* arg, const char* msg, xmlParserSeverities severity, xmlTextReaderLocatorPtr locator) {
        TTextReader* reader = static_cast<TTextReader*>(arg);
        Y_ASSERT(reader != nullptr);

        TStringStream& out = reader->LogError();

        if (severity == XML_PARSER_SEVERITY_ERROR) {
            out << "libxml parse error";
        } else if (severity == XML_PARSER_SEVERITY_VALIDITY_ERROR) {
            out << "libxml validity error";
        } else {
            out << "libxml error";
        }

        if (locator != nullptr) {
            const int line = xmlTextReaderLocatorLineNumber(locator);
            const TCharPtr baseUri = xmlTextReaderLocatorBaseURI(locator);
            out << " (";
            if (line != -1) {
                out << "at line " << line;
                if (baseUri) {
                    out << ", ";
                }
            }
            if (baseUri) {
                out << "base URI " << CAST2CHAR(baseUri.Get());
            }
            out << ")";
        }

        TStringBuf message = (msg != nullptr) ? msg : "unknown";
        message = StripStringRight(message); // remove trailing \n that is added by libxml
        if (!message.empty()) {
            out << ": " << message;
        }
    }

    void TTextReader::SetupErrorHandler() {
        xmlTextReaderErrorFunc func = nullptr;
        void* arg = nullptr;

        // We respect any other error handlers already set up:
        xmlTextReaderGetErrorHandler(Impl.Get(), &func, &arg);
        if (!func) {
            func = TTextReader::OnLibxmlError;
            xmlTextReaderSetErrorHandler(Impl.Get(), func, this);
        }
    }

    TStringStream& TTextReader::LogError() const {
        if (IsError) { // maybe there are previous errors
            ErrorBuffer << Endl;
        }
        IsError = true;
        return ErrorBuffer;
    }

    void TTextReader::CheckForExceptions() const {
        if (Y_LIKELY(!IsError)) {
            return;
        }

        const TString message = ErrorBuffer.Str();
        ErrorBuffer.clear();
        IsError = false;

        ythrow yexception() << message;
    }

    void TTextReader::ThrowException() const {
        CheckForExceptions();
        // Probably CheckForExceptions() would throw an exception with more verbose message. As the last resort
        // (we do not even know the name of the failed libxml function, but it's possible to deduce it from stacktrace):
        ythrow yexception() << "libxml function returned error exit code";
    }

    bool TTextReader::BoolResult(int value) const {
        if (Y_UNLIKELY(value == -1)) {
            ThrowException();
        }
        return (value != 0);
    }

    int TTextReader::IntResult(int value) const {
        if (Y_UNLIKELY(value == -1)) {
            ThrowException();
        }
        return value;
    }

    char TTextReader::CharResult(int value) const {
        if (Y_UNLIKELY(value == -1)) {
            ThrowException();
        }
        return static_cast<char>(value);
    }

    TStringBuf TTextReader::ConstStringResult(const xmlChar* value) const {
        if (Y_UNLIKELY(value == nullptr)) {
            ThrowException();
        }
        return CAST2CHAR(value);
    }

    TStringBuf TTextReader::ConstStringOrEmptyResult(const xmlChar* value) const {
        CheckForExceptions();
        return (value != nullptr) ? TStringBuf(CAST2CHAR(value)) : TStringBuf();
    }

    TString TTextReader::TempStringResult(TCharPtr value) const {
        if (Y_UNLIKELY(value == nullptr)) {
            ThrowException();
        }
        return TString(CAST2CHAR(value.Get()));
    }

    TString TTextReader::TempStringOrEmptyResult(TCharPtr value) const {
        CheckForExceptions();
        return (value != nullptr) ? TString(CAST2CHAR(value.Get())) : TString();
    }

    struct TTextReader::TDeleter {
        static inline void Destroy(xmlTextReaderPtr handle) {
            xmlFreeTextReader(handle);
        }
    };
}