| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the tools applications of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include "qmlstreamwriter.h" |
| 30 | |
| 31 | #include <QtCore/QBuffer> |
| 32 | #include <QtCore/QStringList> |
| 33 | |
| 34 | QmlStreamWriter::QmlStreamWriter(QByteArray *array) |
| 35 | : m_indentDepth(0) |
| 36 | , m_pendingLineLength(0) |
| 37 | , m_maybeOneline(false) |
| 38 | , m_stream(new QBuffer(array)) |
| 39 | { |
| 40 | m_stream->open(mode: QIODevice::WriteOnly); |
| 41 | } |
| 42 | |
| 43 | void QmlStreamWriter::writeStartDocument() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void QmlStreamWriter::writeEndDocument() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | void QmlStreamWriter::writeLibraryImport(const QString &uri, int majorVersion, int minorVersion, const QString &as) |
| 52 | { |
| 53 | m_stream->write(data: QString::fromLatin1(str: "import %1 %2.%3" ).arg(args: uri, args: QString::number(majorVersion), args: QString::number(minorVersion)).toUtf8()); |
| 54 | if (!as.isEmpty()) |
| 55 | m_stream->write(data: QString::fromLatin1(str: " as %1" ).arg(a: as).toUtf8()); |
| 56 | m_stream->write(data: "\n" ); |
| 57 | } |
| 58 | |
| 59 | void QmlStreamWriter::writeStartObject(const QString &component) |
| 60 | { |
| 61 | flushPotentialLinesWithNewlines(); |
| 62 | writeIndent(); |
| 63 | m_stream->write(data: QString::fromLatin1(str: "%1 {" ).arg(a: component).toUtf8()); |
| 64 | ++m_indentDepth; |
| 65 | m_maybeOneline = true; |
| 66 | } |
| 67 | |
| 68 | void QmlStreamWriter::writeEndObject() |
| 69 | { |
| 70 | if (m_maybeOneline && !m_pendingLines.isEmpty()) { |
| 71 | --m_indentDepth; |
| 72 | for (int i = 0; i < m_pendingLines.size(); ++i) { |
| 73 | m_stream->write(data: " " ); |
| 74 | m_stream->write(data: m_pendingLines.at(i).trimmed()); |
| 75 | if (i != m_pendingLines.size() - 1) |
| 76 | m_stream->write(data: ";" ); |
| 77 | } |
| 78 | m_stream->write(data: " }\n" ); |
| 79 | m_pendingLines.clear(); |
| 80 | m_pendingLineLength = 0; |
| 81 | m_maybeOneline = false; |
| 82 | } else { |
| 83 | flushPotentialLinesWithNewlines(); |
| 84 | --m_indentDepth; |
| 85 | writeIndent(); |
| 86 | m_stream->write(data: "}\n" ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void QmlStreamWriter::writeScriptBinding(const QString &name, const QString &rhs) |
| 91 | { |
| 92 | writePotentialLine(line: QString::fromLatin1(str: "%1: %2" ).arg(a1: name, a2: rhs).toUtf8()); |
| 93 | } |
| 94 | |
| 95 | void QmlStreamWriter::writeBooleanBinding(const QString &name, bool value) |
| 96 | { |
| 97 | writeScriptBinding(name, rhs: value ? QLatin1String("true" ) : QLatin1String("false" )); |
| 98 | } |
| 99 | |
| 100 | void QmlStreamWriter::writeArrayBinding(const QString &name, const QStringList &elements) |
| 101 | { |
| 102 | flushPotentialLinesWithNewlines(); |
| 103 | writeIndent(); |
| 104 | |
| 105 | // try to use a single line |
| 106 | QString singleLine; |
| 107 | singleLine += QString::fromLatin1(str: "%1: [" ).arg(a: name); |
| 108 | for (int i = 0; i < elements.size(); ++i) { |
| 109 | singleLine += elements.at(i); |
| 110 | if (i != elements.size() - 1) |
| 111 | singleLine += QLatin1String(", " ); |
| 112 | } |
| 113 | singleLine += QLatin1String("]\n" ); |
| 114 | if (singleLine.size() + m_indentDepth * 4 < 80) { |
| 115 | m_stream->write(data: singleLine.toUtf8()); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | // write multi-line |
| 120 | m_stream->write(data: QString::fromLatin1(str: "%1: [\n" ).arg(a: name).toUtf8()); |
| 121 | ++m_indentDepth; |
| 122 | for (int i = 0; i < elements.size(); ++i) { |
| 123 | writeIndent(); |
| 124 | m_stream->write(data: elements.at(i).toUtf8()); |
| 125 | if (i != elements.size() - 1) { |
| 126 | m_stream->write(data: ",\n" ); |
| 127 | } else { |
| 128 | m_stream->write(data: "\n" ); |
| 129 | } |
| 130 | } |
| 131 | --m_indentDepth; |
| 132 | writeIndent(); |
| 133 | m_stream->write(data: "]\n" ); |
| 134 | } |
| 135 | |
| 136 | void QmlStreamWriter::write(const QString &data) |
| 137 | { |
| 138 | flushPotentialLinesWithNewlines(); |
| 139 | m_stream->write(data: data.toUtf8()); |
| 140 | } |
| 141 | |
| 142 | void QmlStreamWriter::writeScriptObjectLiteralBinding(const QString &name, const QList<QPair<QString, QString> > &keyValue) |
| 143 | { |
| 144 | flushPotentialLinesWithNewlines(); |
| 145 | writeIndent(); |
| 146 | m_stream->write(data: QString::fromLatin1(str: "%1: {\n" ).arg(a: name).toUtf8()); |
| 147 | ++m_indentDepth; |
| 148 | for (int i = 0; i < keyValue.size(); ++i) { |
| 149 | const QString key = keyValue.at(i).first; |
| 150 | const QString value = keyValue.at(i).second; |
| 151 | writeIndent(); |
| 152 | m_stream->write(data: QString::fromLatin1(str: "%1: %2" ).arg(a1: key, a2: value).toUtf8()); |
| 153 | if (i != keyValue.size() - 1) { |
| 154 | m_stream->write(data: ",\n" ); |
| 155 | } else { |
| 156 | m_stream->write(data: "\n" ); |
| 157 | } |
| 158 | } |
| 159 | --m_indentDepth; |
| 160 | writeIndent(); |
| 161 | m_stream->write(data: "}\n" ); |
| 162 | } |
| 163 | |
| 164 | void QmlStreamWriter::writeIndent() |
| 165 | { |
| 166 | m_stream->write(data: QByteArray(m_indentDepth * 4, ' ')); |
| 167 | } |
| 168 | |
| 169 | void QmlStreamWriter::writePotentialLine(const QByteArray &line) |
| 170 | { |
| 171 | m_pendingLines.append(t: line); |
| 172 | m_pendingLineLength += line.size(); |
| 173 | if (m_pendingLineLength >= 80) { |
| 174 | flushPotentialLinesWithNewlines(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void QmlStreamWriter::flushPotentialLinesWithNewlines() |
| 179 | { |
| 180 | if (m_maybeOneline) |
| 181 | m_stream->write(data: "\n" ); |
| 182 | for (const QByteArray &line : qAsConst(t&: m_pendingLines)) { |
| 183 | writeIndent(); |
| 184 | m_stream->write(data: line); |
| 185 | m_stream->write(data: "\n" ); |
| 186 | } |
| 187 | m_pendingLines.clear(); |
| 188 | m_pendingLineLength = 0; |
| 189 | m_maybeOneline = false; |
| 190 | } |
| 191 | |