XML To FastInfoset Converter Example

#include <QtWidgets>
#include <QXmlStreamReader>
#include <DevMachines/QtitanBase>

#include "mainwindow.h"
#include "xmlsyntaxhighlighter.h"

MainWindow::MainWindow()
    : DemoMainWindow("QtitanFastInfoset", QStringLiteral(QTN_VERSION_FASTINFOSET_STR), tr("FastInfoset/XML Converter"))

{
    QWidget* panel = new QWidget(this);
    m_formUI.setupUi(panel);
    new XmlSyntaxHighlighter(m_formUI.xmlEdit->document());

    connect(m_formUI.loadXML, SIGNAL(clicked()), this, SLOT(loadXML()));
    connect(m_formUI.loadFastInfoset, SIGNAL(clicked()), this, SLOT(loadFastInfoset()));
    connect(m_formUI.saveXML, SIGNAL(clicked()), this, SLOT(saveAsXML()));
    connect(m_formUI.saveFastInfoset, SIGNAL(clicked()), this, SLOT(saveAsFastInfoset()));

    setDemoWidget(panel, 0, Qt::AlignTop, false);
}

void xml_converter(IXmlStreamReader* reader, IXmlStreamWriter* writer)
{
    while (reader->readNext() != QXmlStreamReader::Invalid)
    {
        switch (reader->tokenType())
        {
        case QXmlStreamReader::NoToken:
        {
            Q_ASSERT(false);
        }
        break;
        case QXmlStreamReader::StartDocument:
        {
            QString version = reader->documentVersion().toString();
            if (version.isEmpty())
                version = "1.0";
            if (reader->isStandaloneDocument())
                writer->writeStartDocument(version, reader->isStandaloneDocument());
            else
                writer->writeStartDocument(version);
        }
        break;
        case QXmlStreamReader::EndDocument:
        {
            writer->writeEndDocument();
        }
        break;
        case QXmlStreamReader::StartElement:
        {
            QXmlStreamNamespaceDeclarations namespaceDeclarations = reader->namespaceDeclarations();
            for (int i = 0; i < namespaceDeclarations.size(); ++i) {
                const QXmlStreamNamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(i);
                writer->writeNamespace(namespaceDeclaration.namespaceUri().toString(),
                    namespaceDeclaration.prefix().toString());
            }
            writer->writeStartElement(reader->namespaceUri().toString(), reader->name().toString());
            writer->writeAttributes(reader->attributes());
        }
        break;
        case QXmlStreamReader::EndElement:
        {
            writer->writeEndElement();
        }
        break;
        case QXmlStreamReader::Characters:
        {

            if (reader->isCDATA())
            {
                writer->writeCDATA(reader->text().toString());
            }
            else
            {
                writer->writeCharacters(reader->text().toString());
            }
        }
        break;
        case QXmlStreamReader::Comment:
        {
            writer->writeComment(reader->text().toString());
        }
        break;
        case QXmlStreamReader::DTD:
        {
            writer->writeDTD(reader->dtdName().toString(), reader->dtdSystemId().toString(), reader->dtdPublicId().toString());
        }
        break;
        case QXmlStreamReader::EntityReference:
        {
        }
        break;
        case QXmlStreamReader::ProcessingInstruction:
        {
            writer->writeProcessingInstruction(reader->processingInstructionTarget().toString(), reader->processingInstructionData().toString());
        }
        break;

        default:
            break;
        }

        if (reader->atEnd())
            break;
    }
    if (reader->hasError())
    {
        QMessageBox::critical(qApp->activeWindow(), "Stream read error", reader->errorString());
        return;
    }
}

void MainWindow::loadXML()
{
    QString format = "XML (*.xml)";
    QString fileName = QFileDialog::getOpenFileName(this, "Open XML File", QDir::currentPath(), format);
    if (fileName.isEmpty())
        return;
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::critical(this, "File open error", file.errorString());
        return;
    }

    XmlStreamReader xmlreader(&file);
    QByteArray text;
    QBuffer buffer(&text);
    buffer.open(QIODevice::WriteOnly);
    XmlStreamWriter xmlwriter(&buffer);
    xml_converter(&xmlreader, &xmlwriter);
    buffer.close();

    m_formUI.xmlEdit->setPlainText(text);
}

void MainWindow::loadFastInfoset()
{
    QString format = "FastInfoset (*.fi)";
    QString fileName = QFileDialog::getOpenFileName(this, "Open FastInfoset File", QDir::currentPath(), format);
    if (fileName.isEmpty())
        return;
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly))
    {
        QMessageBox::critical(this, "File open error", file.errorString());
        return;
    }

    FastInfosetStreamReader xmlreader(&file);
    QByteArray text;
    QBuffer buffer(&text);
    buffer.open(QIODevice::WriteOnly);
    XmlStreamWriter xmlwriter(&buffer);

    xml_converter(&xmlreader, &xmlwriter);
    buffer.close();
    m_formUI.xmlEdit->setPlainText(text);
}

void MainWindow::saveAsXML()
{
    QString format = "XML (*.xml)";
    QString fileName = QFileDialog::getSaveFileName(this, "Save As XML File", QDir::currentPath(), format);
    if (fileName.isEmpty())
        return;

    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly))
    {
        QMessageBox::critical(this, "File open error", file.errorString());
        return;
    }

    QByteArray text = m_formUI.xmlEdit->toPlainText().toUtf8();
    QBuffer buffer(&text);
    buffer.open(QIODevice::ReadOnly);
    XmlStreamReader xmlreader(&buffer);
    XmlStreamWriter xmlwriter(&file);
    xml_converter(&xmlreader, &xmlwriter);
    buffer.close();
}

void MainWindow::saveAsFastInfoset()
{
    QString format = "FastInfoset (*.fi)";
    QString fileName = QFileDialog::getSaveFileName(this, "Save As FastInfoset File", QDir::currentPath(), format);
    if (fileName.isEmpty())
        return;

    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly))
    {
        QMessageBox::critical(this, "File open error", file.errorString());
        return;
    }

    QByteArray text = m_formUI.xmlEdit->toPlainText().toUtf8();
    QBuffer buffer(&text);
    buffer.open(QIODevice::ReadOnly);
    XmlStreamReader xmlreader(&buffer);
    FastInfosetStreamWriter xmlwriter(&file);
    xml_converter(&xmlreader, &xmlwriter);
    buffer.close();
}