summaryrefslogtreecommitdiffstats
path: root/src/Authoring/QT3DSDM/Systems/Qt3DSDMXML.h
blob: 5fe722f9c221dc4821d685209e10b3c4b8ad160d (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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/****************************************************************************
**
** Copyright (C) 1993-2009 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#ifndef QT3DS_IMPORT_XML_H
#define QT3DS_IMPORT_XML_H
#include "foundation/Qt3DS.h"
#include "foundation/Qt3DSMemoryBuffer.h"
#include "Qt3DSDMDataTypes.h"
#include <string>
#include "Qt3DSDMWStrOps.h" //Conversion between string and various datatypes.
#include "foundation/Qt3DSFlags.h"
#include "EASTL/algorithm.h"
#include "foundation/Qt3DSSimpleTypes.h"
#include "foundation/IOStreams.h"
#include "Qt3DSDMValue.h"
#include <memory>

namespace qt3dsdm {
using qt3ds::foundation::MemoryBuffer;
using qt3ds::foundation::RawAllocator;
class IStringTable;

class IDOMFactory;
struct SDOMAttribute;
struct SDOMElement;

typedef const char8_t *TXMLCharPtr;
typedef const wchar_t *TWideXMLCharPtr;

typedef TCharStr TXMLWideStr;
typedef eastl::basic_string<char8_t> TXMLStr;

using qt3ds::QT3DSU32;
using qt3ds::NVFlags;
using qt3ds::foundation::NVDataRef;
using qt3ds::foundation::NVConstDataRef;
using qt3ds::foundation::IOutStream;

class IDOMFactory
{
protected:
    virtual ~IDOMFactory() {}
public:
    friend class std::shared_ptr<IDOMFactory>;

    virtual void Release() = 0;
    // Str does not need to be null terminated.
    virtual void AppendStrBuf(TXMLCharPtr str, QT3DSU32 len) = 0;
    virtual void AppendStrBuf(TWideXMLCharPtr str, QT3DSU32 len) = 0;
    // Null terminate what is there and return the buffer.
    // This pointer needs to be persistent.
    virtual TXMLCharPtr FinalizeStrBuf() = 0;
    virtual void IgnoreStrBuf() = 0;

    virtual std::shared_ptr<IStringTable> GetStringTable() = 0;

    virtual SDOMAttribute *NextAttribute(TWideXMLCharPtr name, TWideXMLCharPtr val) = 0;
    virtual SDOMElement *NextElement(TWideXMLCharPtr name) = 0;

    virtual SDOMAttribute *NextAttribute(TXMLCharPtr name, TXMLCharPtr val) = 0;
    virtual SDOMElement *NextElement(TXMLCharPtr name) = 0;

    TXMLCharPtr RegisterValue(TXMLCharPtr inValue);
    TXMLCharPtr RegisterValue(TWideXMLCharPtr inValue);

    static std::shared_ptr<IDOMFactory>
    CreateDOMFactory(std::shared_ptr<IStringTable> inStrTable);
};

struct DOMFlagValues
{
    enum Enum {
        CaselessElementNames = 1 << 0,
        CaselessAttributeNames = 1 << 1,
    };
};

struct SDOMFlags : NVFlags<DOMFlagValues::Enum, int>
{
    SDOMFlags()
        : NVFlags<DOMFlagValues::Enum, int>(DOMFlagValues::CaselessElementNames
                                            | DOMFlagValues::CaselessAttributeNames)
    {
    }
    bool CaselessAttributes() const { return *this & DOMFlagValues::CaselessAttributeNames; }
    bool CaselessElements() const { return *this & DOMFlagValues::CaselessElementNames; }
};

class IDOMReader
{
protected:
    virtual ~IDOMReader() {}
public:
    friend class std::shared_ptr<IDOMReader>;

    // Stack object to save the reader's state.
    struct Scope
    {
        IDOMReader &m_Reader;
        Scope(IDOMReader &reader)
            : m_Reader(reader)
        {
            m_Reader.PushScope();
        }
        ~Scope() { m_Reader.PopScope(); }
    };

    // Parse buffer
    MemoryBuffer<RawAllocator> m_TempBuf;
    std::shared_ptr<IStringTable> m_StringTable;

    IDOMReader(std::shared_ptr<IStringTable> inStringTable)
        : m_StringTable(inStringTable)
    {
    }

    std::shared_ptr<IStringTable> GetStringTable() { return m_StringTable; }

    // DOM reader flags change the nature of the DOM reader.
    // see DOMFlagValues
    // Defaults to caseless comparisons for attributes and for
    // element names.
    virtual void SetDOMFlags(SDOMFlags inFlags) = 0;
    virtual SDOMFlags GetDOMFlags() const = 0;

    // Pushing scope saves your state so no matter where you are when
    // you next pop scope, you come back to the same state.
    virtual void PushScope() = 0;
    virtual void PopScope() = 0;

    // Sometimes pushing and popping scope isn't enough and you need
    // somewhat random access to scope.
    // This scope does not remember the current attribute.
    virtual void *GetScope() = 0;
    virtual void SetScope(void *inScope) = 0;
    // Return an attribute whose value is *not* registered with the string table.
    // You can't hold onto this value for any length of time, but when you need to
    // immediately convert to a different value this is the most efficient way.
    virtual bool UnregisteredAtt(TWideXMLCharPtr name, TWideXMLCharPtr &outValue) = 0;
    virtual bool UnregisteredAtt(TXMLCharPtr name, TXMLCharPtr &outValue) = 0;
    // Return an attribute whose value *is* registered with the string table.
    virtual bool Att(TWideXMLCharPtr name, TWideXMLCharPtr &outValue) = 0;
    virtual bool Att(TXMLCharPtr name, TXMLCharPtr &outValue) = 0;
    virtual eastl::pair<TWideXMLCharPtr, TWideXMLCharPtr> GetFirstAttribute() = 0;
    virtual eastl::pair<TWideXMLCharPtr, TWideXMLCharPtr> GetNextAttribute() = 0;
    virtual eastl::pair<TXMLCharPtr, TXMLCharPtr> GetNarrowFirstAttribute() = 0;
    virtual eastl::pair<TXMLCharPtr, TXMLCharPtr> GetNarrowNextAttribute() = 0;
    virtual QT3DSU32 CountChildren() = 0;
    virtual QT3DSU32 CountChildren(TWideXMLCharPtr childName) = 0;
    virtual QT3DSU32 CountChildren(TXMLCharPtr childName) = 0;
    virtual bool MoveToFirstChild(TWideXMLCharPtr childName) = 0;
    virtual bool MoveToFirstChild(TXMLCharPtr childName) = 0;
    virtual bool MoveToFirstChild() = 0;
    virtual bool MoveToNextSibling(TWideXMLCharPtr siblingName) = 0;
    virtual bool MoveToNextSibling(TXMLCharPtr siblingName) = 0;
    virtual bool MoveToNextSibling() = 0;
    // Leave element means go to its parent.
    virtual void Leave() = 0;
    virtual TWideXMLCharPtr GetElementName() const = 0;
    virtual TXMLCharPtr GetNarrowElementName() const = 0;

    // Value is the concatentated text node values inside the element
    virtual bool Value(TWideXMLCharPtr &outValue) = 0;
    virtual bool Value(TXMLCharPtr &outValue) = 0;

    // Get the element this reader was created with
    virtual SDOMElement *GetTopElement() = 0;

    bool Att(TWideXMLCharPtr name, TXMLWideStr &outValue)
    {
        TWideXMLCharPtr temp;
        if (UnregisteredAtt(name, temp)) {
            outValue.assign(temp);
            return true;
        }
        return false;
    }

    bool Att(TXMLCharPtr name, TXMLStr &outValue)
    {
        TXMLCharPtr temp;
        if (UnregisteredAtt(name, temp)) {
            outValue.assign(temp);
            return true;
        }
        return false;
    }

    // Helpers to help make the parsing a bit easier.
    template <typename TDataType>
    bool Att(TWideXMLCharPtr name, TDataType &outValue)
    {
        TWideXMLCharPtr temp;
        if (UnregisteredAtt(name, temp)) {
            WStrOps<TDataType>().StrTo(temp, outValue);
            return true;
        } else {
            return false;
        }
    }
    template <typename TDataType>
    bool Att(TXMLCharPtr name, TDataType &outValue)
    {
        TXMLCharPtr temp;
        if (UnregisteredAtt(name, temp)) {
            WStrOps<TDataType>().StrTo(temp, outValue);
            return true;
        } else {
            return false;
        }
    }

    bool ChildValue(TXMLCharPtr name, TXMLCharPtr &value)
    {
        if (MoveToFirstChild(name)) {
            Value(value);
            Leave();
            return true;
        }
        return false;
    }

    bool ChildValue(TWideXMLCharPtr name, TWideXMLCharPtr &value)
    {
        if (MoveToFirstChild(name)) {
            Value(value);
            Leave();
            return true;
        }
        return false;
    }

    bool RegisteredChildValue(TWideXMLCharPtr name, TWideXMLCharPtr &value)
    {
        if (MoveToFirstChild(name)) {
            RegisteredValue(value);
            Leave();
            return true;
        }
        return false;
    }
    bool RegisteredChildValue(TXMLCharPtr name, TXMLCharPtr &value)
    {
        if (MoveToFirstChild(name)) {
            RegisteredValue(value);
            Leave();
            return true;
        }
        return false;
    }
    bool RegisteredValue(TWideXMLCharPtr &outValue)
    {
        bool retval = Value(outValue);
        if (retval)
            outValue = m_StringTable->RegisterStr(outValue);
        return retval;
    }
    bool RegisteredValue(TXMLCharPtr &outValue)
    {
        bool retval = Value(outValue);
        if (retval)
            outValue = m_StringTable->RegisterStr(outValue);
        return retval;
    }
    bool Value(DataModelDataType::Value type, SValue &outValue);

    template <typename TDataType>
    bool Value(TDataType &outValue)
    {
        TXMLCharPtr value;
        if (Value(value)) {
            WStrOps<TDataType>().StrTo(value, outValue);
            return true;
        }
        return false;
    }

    // Functions below implemented in WStrOpsImpl
    // to avoid circular dependencies
    // Returns the number of items read
    // Destructive parsing
    template <typename TDataType>
    bool ValueList(NVDataRef<TDataType> data);

    // Destructive operation because we can't trust
    // strtod to do the right thing.  On windows, for long strings,
    // it calls strlen every operation thus leading to basically N^2
    // behavior
    template <typename TDataType>
    NVConstDataRef<TDataType> ChildValueList(TWideXMLCharPtr listName);

    template <typename TDataType>
    NVConstDataRef<TDataType> ChildValueList(TXMLCharPtr listName);

    template <typename TSerializer>
    void Serialize(const wchar_t *elemName, TSerializer &serializer)
    {
        IDOMReader::Scope __theScope(*this);
        if (MoveToFirstChild(elemName)) {
            serializer.Serialize(*this);
        }
    }
    // Optionally hold on to the factory to keep our elements in memory as long as we are.
    static std::shared_ptr<IDOMReader>
    CreateDOMReader(SDOMElement &inRootElement, std::shared_ptr<IStringTable> inStringTable,
                    std::shared_ptr<IDOMFactory> inFactory = std::shared_ptr<IDOMFactory>());
};

// Write out data in an xml-like fasion without specifying exactly
// where that data is being written.
class IDOMWriter
{
protected:
    virtual ~IDOMWriter() {}
public:
    friend class std::shared_ptr<IDOMWriter>;

    // Control the element scope.
    struct Scope
    {
        IDOMWriter &m_Writer;
        Scope(IDOMWriter &writer, TWideXMLCharPtr inElemName)
            : m_Writer(writer)
        {
            m_Writer.Begin(inElemName);
        }
        Scope(IDOMWriter &writer, TXMLCharPtr inElemName)
            : m_Writer(writer)
        {
            m_Writer.Begin(inElemName);
        }
        ~Scope() { m_Writer.End(); }
    };

    wchar_t m_PrintBuf[256];
    char8_t m_NarrowBuf[256];

    virtual void SetDOMFlags(SDOMFlags inFlags) = 0;
    virtual SDOMFlags GetDOMFlags() const = 0;
    // There tend to be two types of elements.
    // Containers (<a>\n\t<b/><b/>\n</a>)
    // and Values <b>onetwothree</b>
    virtual void Begin(TWideXMLCharPtr inElemName) = 0;
    virtual void Begin(TXMLCharPtr inElemName) = 0;
    // Attributes.  They may be sorted just before write
    virtual void Att(TWideXMLCharPtr name, TWideXMLCharPtr value) = 0;
    virtual void Att(TXMLCharPtr name, TXMLCharPtr value) = 0;
    virtual void Value(TWideXMLCharPtr value) = 0;
    virtual void Value(TXMLCharPtr value) = 0;
    virtual void End() = 0;
    virtual void RemoveCurrent() = 0;
    virtual void ReplaceCurrent(SDOMElement &inElement) = 0;
    // Append all the children of inElement to the child list of the current items.
    virtual void AppendChildren(SDOMElement &inElement) = 0;
    virtual void RemoveAttribute(TWideXMLCharPtr inItem) = 0;
    virtual void RemoveAttribute(TXMLCharPtr inItem) = 0;
    // Get the number of tabs required to line up the next line
    // with the opening of the previous line
    virtual QT3DSU32 GetTabs() = 0;
    virtual SDOMElement *GetTopElement() = 0;
    virtual std::shared_ptr<IDOMFactory> GetFactory() = 0;
    // Move this item before this sibling.  Function does not rearrange the
    // tree in any major way and will not work if inItem and inSibling aren't
    // siblings.
    virtual void MoveBefore(TWideXMLCharPtr inItem, TWideXMLCharPtr inSibling) = 0;
    virtual void MoveBefore(TXMLCharPtr inItem, TXMLCharPtr inSibling) = 0;

    virtual void ChildValue(TWideXMLCharPtr name, TWideXMLCharPtr value)
    {
        Begin(name);
        Value(value);
        End();
    }

    virtual void ChildValue(TXMLCharPtr name, TXMLCharPtr value)
    {
        Begin(name);
        Value(value);
        End();
    }

    TWideXMLCharPtr ToStr(wchar_t val)
    {
        m_PrintBuf[0] = val;
        m_PrintBuf[1] = 0;
        return m_PrintBuf;
    }
    TXMLCharPtr ToStr(char8_t val)
    {
        m_NarrowBuf[0] = val;
        m_NarrowBuf[1] = 0;
        return m_NarrowBuf;
    }
    template <typename TDataType>
    TWideXMLCharPtr ToStr(TDataType val)
    {
        WStrOps<TDataType>().ToStr(val, NVDataRef<wchar_t>(m_PrintBuf, 256));
        return m_PrintBuf;
    }

    template <typename TDataType>
    TXMLCharPtr ToNarrowStr(TDataType val)
    {
        WStrOps<TDataType>().ToStr(val, NVDataRef<char8_t>(m_NarrowBuf, 256));
        return m_NarrowBuf;
    }

    void Att(TWideXMLCharPtr name, const TXMLWideStr &inValue)
    {
        return Att(name, inValue.wide_str());
    }

    void Att(TXMLCharPtr name, const TXMLStr &inValue) { return Att(name, inValue.c_str()); }

    template <typename TData>
    void Att(TWideXMLCharPtr name, TData value)
    {
        Att(name, ToStr(value));
    }

    template <typename TData>
    void Att(TXMLCharPtr name, TData value)
    {
        Att(name, ToNarrowStr(value));
    }

    template <typename TSerializer>
    void Serialize(const wchar_t *elemName, TSerializer &serializer)
    {
        IDOMWriter::Scope __theScope(*this, elemName);
        serializer.Serialize(*this);
    }

    template <typename TSerializer>
    void Serialize(const char8_t *elemName, TSerializer &serializer)
    {
        IDOMWriter::Scope __theScope(*this, elemName);
        serializer.Serialize(*this);
    }

    std::shared_ptr<IDOMReader> CreateDOMReader()
    {
        std::shared_ptr<IDOMFactory> theFactory(GetFactory());
        return IDOMReader::CreateDOMReader(*GetTopElement(), theFactory->GetStringTable(),
                                           theFactory);
    }

    // Note that the default method of creating a writer also creates a reader; they can
    // both manipulation the DOM hierarch.
    static eastl::pair<std::shared_ptr<IDOMWriter>, std::shared_ptr<IDOMReader>>
    CreateDOMWriter(std::shared_ptr<IDOMFactory> inFactory, SDOMElement &inRootElement,
                    std::shared_ptr<IStringTable> inStringTable);

    static eastl::pair<std::shared_ptr<IDOMWriter>, std::shared_ptr<IDOMReader>>
    CreateDOMWriter(const wchar_t *inTopElemName, std::shared_ptr<IStringTable> inStringTable)
    {
        std::shared_ptr<IDOMFactory> theFactory(IDOMFactory::CreateDOMFactory(inStringTable));
        SDOMElement *theRoot = theFactory->NextElement(inTopElemName);
        return CreateDOMWriter(theFactory, *theRoot, inStringTable);
    }
};

class CXmlErrorHandler
{
protected:
    virtual ~CXmlErrorHandler() {}
public:
    virtual void OnXmlError(const QString &errorName, int line, int column) = 0;
};

class CDOMSerializer
{
public:
    static void WriteXMLHeader(IOutStream &inStream);
    static void Write(SDOMElement &inElement, IOutStream &inStream, QT3DSU32 inTabs = 0);
    static SDOMElement *Read(IDOMFactory &inFactory, qt3ds::foundation::IInStream &inStream,
                             CXmlErrorHandler *inErrorHandler = NULL);
};
}
#endif