aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scxmleditor/plugin_interface/scxmltag.cpp
blob: f7efa730d8d079b617967f31a92e505e5a0de379 (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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "scxmldocument.h"
#include "scxmleditorconstants.h"
#include "scxmleditortr.h"
#include "scxmltag.h"
#include "scxmltagutils.h"

#include <QCoreApplication>
#include <QDebug>

using namespace ScxmlEditor::PluginInterface;

ScxmlTag::ScxmlTag(TagType type, ScxmlDocument *document)
    : m_prefix(
          QLatin1String(type == Metadata || type == MetadataItem ? "qt" : ""))
{
    setDocument(document);
    init(type);
    m_tagName = QLatin1String(m_info->name);
}

ScxmlTag::ScxmlTag(const QString &prefix, const QString &name,
    ScxmlDocument *document)
    : m_tagName(name)
    , m_prefix(prefix)
{
    setDocument(document);
    TagType type = UnknownTag;
    for (int i = 0; i < Finalize; ++i) {
        if (QLatin1String(scxml_tags[i].name) == name) {
            type = (TagType)i;
            break;
        }
    }

    init(type);
}

ScxmlTag::ScxmlTag(const ScxmlTag *other, bool copyChildren)
{
    setDocument(other->m_document);
    m_tagType = other->m_tagType;
    m_tagName = other->m_tagName;
    m_content = other->m_content;
    m_prefix = other->m_prefix;
    m_info = &scxml_tags[m_tagType];
    m_attributeNames = other->m_attributeNames;
    m_attributeValues = other->m_attributeValues;
    m_editorInfo = other->m_editorInfo;

    if (copyChildren) {
        for (int i = 0; i < other->m_childTags.count(); ++i)
            appendChild(new ScxmlTag(other->m_childTags[i], copyChildren));
    }
}

ScxmlTag::~ScxmlTag()
{
    if (m_document)
        m_document->removeChild(this);

    m_attributeNames.clear();
    m_attributeValues.clear();
    m_childTags.clear();

    // m_parentTag = 0;
    m_document = nullptr;
    m_info = nullptr;
    m_tagType = UnknownTag;
}

void ScxmlTag::finalizeTagNames()
{
    switch (m_tagType) {
    case State: {
        if (hasAttribute("initial")) {
            QString oldInitial = attribute("initial");
            QString newInitial = m_document->m_idMap.value(oldInitial);
            setAttribute("initial", newInitial);
        }
        break;
    }
    default:
        break;
    }

    for (int i = m_childTags.count(); i--;) {
        ScxmlTag *t = m_childTags[i];
        switch (t->tagType()) {
        case InitialTransition:
        case Transition: {
            QString oldTarget = t->attribute("target");
            QString newTarget = m_document->m_idMap.value(oldTarget);
            if (!oldTarget.isEmpty() && newTarget.isEmpty())
                delete m_childTags.takeAt(i);
            else
                t->setAttribute("target", newTarget);
            break;
        }
        default:
            t->finalizeTagNames();
            break;
        }
    }
}

void ScxmlTag::print()
{
    qDebug() << "type            " << m_tagType;
    qDebug() << "name            " << m_tagName;
    qDebug() << "parent          " << (m_parentTag ? m_parentTag->m_tagName : "");
    qDebug() << "attributeNames  " << m_attributeNames;
    qDebug() << "attributeValues " << m_attributeValues;
    qDebug() << "childcount " << m_childTags.count();
    for (int i = 0; i < m_childTags.count(); ++i)
        qDebug() << " child           " << i << m_childTags[i]->m_tagName;
}

void ScxmlTag::init(TagType type)
{
    m_tagType = type;
    m_info = &scxml_tags[type];

    for (int i = 0; i < m_info->n_attributes; ++i) {
        if (m_info->attributes[i].value)
            setAttribute(
                QLatin1String(m_info->attributes[i].name),
                QString::fromLatin1(m_info->attributes[i].value).split(";").first());
    }

    initId();
}

void ScxmlTag::initId()
{
    // Init state IDs
    if (m_document) {
        switch (m_tagType) {
        case State:
            setAttribute("id", m_document->nextUniqueId("State"));
            break;
        case Parallel:
            setAttribute("id", m_document->nextUniqueId("Parallel"));
            break;
        case History:
            setAttribute("id", m_document->nextUniqueId("History"));
            break;
        case Final:
            setAttribute("id", m_document->nextUniqueId("Final"));
            break;
        default:
            break;
        }
    }
}

void ScxmlTag::setDocument(ScxmlDocument *document)
{
    if (m_document != document) {
        if (m_document)
            m_document->removeChild(this);

        m_document = document;
        if (m_document)
            m_document->addChild(this);
    }
}

ScxmlDocument *ScxmlTag::document() const
{
    return m_document;
}

const scxmltag_type_t *ScxmlTag::info() const
{
    return m_info;
}

TagType ScxmlTag::tagType() const
{
    return m_tagType;
}

QString ScxmlTag::prefix() const
{
    return m_prefix;
}

void ScxmlTag::setTagName(const QString &name)
{
    m_tagName = name;
}

QString ScxmlTag::tagName(bool addPrefix) const
{
    if (m_prefix.isEmpty() || !addPrefix)
        return m_tagName;
    else
        return QString::fromLatin1("%1:%2").arg(m_prefix).arg(m_tagName);
}

QString ScxmlTag::displayName() const
{
    switch (m_tagType) {
    case State:
    case Parallel:
    case Final:
        return attribute("id");
    case Transition:
    case InitialTransition:
        return attribute("event");
        break;
    default:
        return QString();
    }
}

QString ScxmlTag::stateNameSpace() const
{
    if (m_parentTag) {
        switch (m_parentTag->m_tagType) {
        case State:
        case Parallel:
            return QString::fromLatin1("%1%2%3")
                .arg(m_parentTag->stateNameSpace())
                .arg(m_parentTag->attribute(
                    m_parentTag->m_attributeNames.indexOf("id")))
                .arg(m_document->nameSpaceDelimiter());
        default:
            break;
        }
    }

    return QString();
}

QString ScxmlTag::content() const
{
    return m_content;
}

void ScxmlTag::setContent(const QString &content)
{
    m_content = content.trimmed();
}

bool ScxmlTag::hasData() const
{
    if (!m_attributeNames.isEmpty() || !m_content.isEmpty())
        return true;

    for (ScxmlTag *tag : std::as_const(m_childTags)) {
        if (tag->hasData())
            return true;
    }

    return false;
}

bool ScxmlTag::hasChild(TagType type) const
{
    for (ScxmlTag *tag : std::as_const(m_childTags)) {
        if (tag->tagType() == type)
            return true;
    }

    return false;
}

bool ScxmlTag::hasChild(const QString &name) const
{
    for (ScxmlTag *tag : std::as_const(m_childTags)) {
        if (tag->tagName() == name)
            return true;
    }

    return false;
}

void ScxmlTag::insertChild(int index, ScxmlTag *child)
{
    if (index >= 0 && index < m_childTags.count()) {
        m_childTags.insert(index, child);
        child->setParentTag(this);
    } else
        appendChild(child);
}

void ScxmlTag::appendChild(ScxmlTag *child)
{
    if (!m_childTags.contains(child)) {
        m_childTags.append(child);
        child->setParentTag(this);
    }
}

void ScxmlTag::removeChild(ScxmlTag *child)
{
    m_childTags.removeAll(child);
}

void ScxmlTag::moveChild(int oldPos, int newPos)
{
    m_childTags.insert(newPos, m_childTags.takeAt(oldPos));
}

QString ScxmlTag::attributeName(int ind) const
{
    if (ind >= 0 && ind < m_attributeNames.count())
        return m_attributeNames[ind];

    return QString();
}

QString ScxmlTag::attribute(int ind, const QString &defaultValue) const
{
    if (ind >= 0 && ind < m_attributeValues.count())
        return m_attributeValues[ind];

    return defaultValue;
}

void ScxmlTag::setEditorInfo(const QString &key, const QString &value)
{
    if (value.isEmpty())
        m_editorInfo.remove(key);
    else
        m_editorInfo[key] = value;
}

QString ScxmlTag::editorInfo(const QString &key) const
{
    return m_editorInfo.value(key);
}

bool ScxmlTag::hasEditorInfo(const QString &key) const
{
    return m_editorInfo.contains(key);
}

void ScxmlTag::setAttributeName(int ind, const QString &name)
{
    if (m_attributeNames.contains(name))
        return;

    if (ind >= 0 && ind < m_attributeValues.count()) {
        m_attributeNames[ind] = name;
    } else {
        m_attributeNames << name;
        m_attributeValues << Tr::tr("Unknown");
    }
}

void ScxmlTag::setAttribute(int ind, const QString &value)
{
    if (ind >= 0 && ind < m_attributeNames.count())
        setAttribute(m_attributeNames[ind], value);
    else {
        m_attributeNames << Tr::tr("Unknown");
        m_attributeValues << value;
    }
}

void ScxmlTag::setAttribute(const QString &name, const QString &value)
{
    if (value.isEmpty()) {
        int ind = m_attributeNames.indexOf(name);
        if (ind >= 0 && ind < m_attributeNames.count()) {
            m_attributeNames.removeAt(ind);
            m_attributeValues.removeAt(ind);
        }
    } else if (name.isEmpty()) {
        int ind = m_attributeValues.indexOf(value);
        if (ind >= 0 && ind < m_attributeValues.count()) {
            m_attributeNames.removeAt(ind);
            m_attributeValues.removeAt(ind);
        }
    } else {
        int ind = m_attributeNames.indexOf(name);
        if (ind >= 0 && ind < m_attributeNames.count()) {
            m_attributeNames[ind] = name;
            m_attributeValues[ind] = value;
        } else {
            m_attributeNames << name;
            m_attributeValues << value;
        }
    }
}

QStringList ScxmlTag::attributeNames() const
{
    return m_attributeNames;
}

QStringList ScxmlTag::attributeValues() const
{
    return m_attributeValues;
}

int ScxmlTag::attributeCount() const
{
    return m_attributeNames.count();
}

QString ScxmlTag::attribute(const QString &attr, bool useNameSpace,
    const QString &defaultValue) const
{
    QString value = attribute(m_attributeNames.indexOf(attr), defaultValue);
    if (useNameSpace && m_document->useFullNameSpace())
        return QString::fromLatin1("%1%2").arg(stateNameSpace()).arg(value);

    return value;
}

bool ScxmlTag::hasAttribute(const QString &key) const
{
    return m_attributeNames.contains(key);
}

int ScxmlTag::childCount() const
{
    return m_childTags.count();
}

int ScxmlTag::childIndex(const ScxmlTag *child) const
{
    return m_childTags.indexOf(const_cast<ScxmlTag*>(child));
}

void ScxmlTag::setParentTag(ScxmlTag *parentTag)
{
    m_parentTag = parentTag;
}

bool ScxmlTag::hasParentTag() const
{
    return m_parentTag != nullptr;
}

ScxmlTag *ScxmlTag::child(int index) const
{
    return index >= 0 && index < m_childTags.count()
        ? m_childTags.value(index)
        : 0;
}

ScxmlTag *ScxmlTag::parentTag() const
{
    return m_parentTag;
}

int ScxmlTag::index() const
{
    return m_parentTag ? m_parentTag->childIndex(const_cast<ScxmlTag*>(this))
                       : 0;
}

bool ScxmlTag::isRootTag() const
{
    return m_document->rootTag() == this;
}

ScxmlTag *ScxmlTag::tagForId(const QString &id) const
{
    for (ScxmlTag *child : m_childTags) {
        const TagType type = child->tagType();
        const bool typeOK = type == State || type == Parallel || type == History || type == Final;
        if (typeOK && child->attribute("id") == id)
            return child;
        ScxmlTag *foundTag = child->tagForId(id);
        if (foundTag)
            return foundTag;
    }
    return nullptr;
}

QVector<ScxmlTag*> ScxmlTag::allChildren() const
{
    return m_childTags;
}

QVector<ScxmlTag*> ScxmlTag::children(const QString &name) const
{
    QVector<ScxmlTag*> children;
    for (ScxmlTag *tag : std::as_const(m_childTags)) {
        if (tag->tagName() == name)
            children << tag;
    }

    return children;
}

ScxmlTag *ScxmlTag::child(const QString &name) const
{
    for (ScxmlTag *tag : std::as_const(m_childTags)) {
        if (tag->tagName() == name)
            return tag;
    }

    return nullptr;
}

void ScxmlTag::writeXml(QXmlStreamWriter &xml)
{
    // Start tag
    xml.writeStartElement(tagName());

    // Don't write attribute if type is Script and content is available
    if (m_tagType != Script || m_content.isEmpty()) {
        for (int i = 0; i < m_attributeNames.count(); ++i) {
            if (m_attributeNames[i] == "id" && m_document->useFullNameSpace())
                xml.writeAttribute("id", attribute("id", true));
            else
                xml.writeAttribute(m_attributeNames[i], m_attributeValues[i]);
        }
    }

    // If tag is Scxml (root), we need to fine-tune initial-state
    if (m_tagType == Scxml) {
        setEditorInfo("initialGeometry", QString());
        setEditorInfo("transitionGeometry", QString());

        ScxmlTag *initialTag = child("initial");
        if (initialTag) {
            setEditorInfo("initialGeometry", initialTag->editorInfo(Constants::C_SCXML_EDITORINFO_GEOMETRY));

            ScxmlTag *initialTransitionTag = initialTag->child("transition");
            if (initialTransitionTag) {
                xml.writeAttribute("initial",
                    initialTransitionTag->attribute("target"));
                setEditorInfo("transitionGeometry",
                    initialTransitionTag->editorInfo(Constants::C_SCXML_EDITORINFO_GEOMETRY));
            }
        }
    }

    // Write content if necessary
    if (m_info->canIncludeContent && !m_content.isEmpty())
        xml.writeCharacters(m_content);

    // Write editorinfo if necessary
    if (!m_editorInfo.isEmpty()) {
        xml.writeStartElement("qt:editorinfo");
        for (auto i = m_editorInfo.cbegin(), end = m_editorInfo.cend(); i != end; ++i)
            xml.writeAttribute(i.key(), i.value());
        xml.writeEndElement();
    }

    // Write children states
    for (int i = 0; i < m_childTags.count(); ++i) {
        if (m_tagType == Scxml && m_childTags[i]->tagType() == Initial)
            continue;

        if ((m_childTags[i]->tagType() == Metadata || m_childTags[i]->tagType() == MetadataItem) && !m_childTags[i]->hasData())
            continue;

        m_childTags[i]->writeXml(xml);
    }

    // End tag
    xml.writeEndElement();
}

void ScxmlTag::readXml(QXmlStreamReader &xml, bool checkCopyId)
{
    QString scxmlInitial;

    // Read and set attributes
    QXmlStreamAttributes attributes = xml.attributes();
    for (int i = 0; i < attributes.count(); ++i) {
        if (m_tagType == Scxml && attributes[i].qualifiedName() == QLatin1String("initial"))
            scxmlInitial = attributes[i].value().toString();
        else {
            QString key = attributes[i].qualifiedName().toString();
            QString value = attributes[i].value().toString();

            // Modify id-attribute if necessary
            switch (m_tagType) {
            case History:
            case Final:
            case State:
            case Parallel:
            case Initial: {
                if (key == "id") {
                    setAttribute(key, value);

                    QString oldId = value;
                    QString parentNS = stateNameSpace();
                    if (value.startsWith(parentNS))
                        value.remove(parentNS);

                    if (checkCopyId)
                        value = m_document->getUniqueCopyId(this);
                    if (m_document->useFullNameSpace())
                        m_document->m_idMap[oldId] = QString::fromLatin1("%1%2").arg(stateNameSpace()).arg(value);
                    else
                        m_document->m_idMap[oldId] = value;
                }
                break;
            }
            default:
                break;
            }

            setAttribute(key, value);
        }
    }

    // Read children states
    QXmlStreamReader::TokenType token = xml.readNext();
    while (token != QXmlStreamReader::EndElement) {
        if (token == QXmlStreamReader::Characters)
            m_content = xml.text().toString().trimmed();
        else if (token == QXmlStreamReader::StartElement) {
            if (m_tagType != Metadata && m_tagType != MetadataItem && xml.qualifiedName().toString() == "qt:editorinfo") {
                // Read editorinfos
                const QXmlStreamAttributes attributes = xml.attributes();
                for (QXmlStreamAttribute attr : attributes) {
                    m_editorInfo[attr.name().toString()] = attr.value().toString();
                }

                // Update fullnamespace-type of the scxmldocument
                if (m_tagType == Scxml) {
                    m_document->m_useFullNameSpace = m_editorInfo.value("fullnamespace", "false") == "true";
                    m_document->m_idDelimiter = m_editorInfo.value("fullnamespacedelimiter", "::");
                }

                xml.readNext();
            } else {
                ScxmlTag *childTag = nullptr;
                if ((m_tagType == Initial || m_tagType == History) && xml.name().toString() == "transition")
                    childTag = new ScxmlTag(InitialTransition, m_document);
                else
                    childTag = new ScxmlTag(xml.prefix().toString(),
                        xml.name().toString(), m_document);

                appendChild(childTag);
                childTag->readXml(xml, checkCopyId);
            }
        } else if (token == QXmlStreamReader::Invalid) {
            qDebug() << Tr::tr("Error in reading XML ") << xml.error() << ":"
                     << xml.errorString();
            break;
        }

        token = xml.readNext();
    }

    // Create initial state of the scxml (root)
    if (m_tagType == Scxml && !scxmlInitial.isEmpty()) {
        auto initialTag = new ScxmlTag(Initial, m_document);
        auto initialTransitionTag = new ScxmlTag(InitialTransition, m_document);
        initialTransitionTag->setAttribute("target", scxmlInitial);
        initialTag->setEditorInfo(Constants::C_SCXML_EDITORINFO_GEOMETRY,
            m_editorInfo.value("initialGeometry"));
        initialTransitionTag->setEditorInfo(
            Constants::C_SCXML_EDITORINFO_GEOMETRY, m_editorInfo.value("transitionGeometry"));

        appendChild(initialTag);
        initialTag->appendChild(initialTransitionTag);
    }
}