1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 // WARNING: This code is shared with the qmlplugindump tool code in Qt.
34 // Modifications to this file need to be applied there.
36 #include "qmlstreamwriter.h"
38 #include <QtCore/QBuffer>
39 #include <QtCore/QStringList>
41 QmlStreamWriter::QmlStreamWriter(QByteArray *array)
43 , m_pendingLineLength(0)
44 , m_maybeOneline(false)
45 , m_stream(new QBuffer(array))
47 m_stream->open(QIODevice::WriteOnly);
50 void QmlStreamWriter::writeStartDocument()
54 void QmlStreamWriter::writeEndDocument()
58 void QmlStreamWriter::writeLibraryImport(const QString &uri, int majorVersion, int minorVersion, const QString &as)
60 m_stream->write(QString("import %1 %2.%3").arg(uri, QString::number(majorVersion), QString::number(minorVersion)).toUtf8());
62 m_stream->write(QString(" as %1").arg(as).toUtf8());
63 m_stream->write("\n");
66 void QmlStreamWriter::writeStartObject(const QString &component)
68 flushPotentialLinesWithNewlines();
70 m_stream->write(QString("%1 {").arg(component).toUtf8());
72 m_maybeOneline = true;
75 void QmlStreamWriter::writeEndObject()
77 if (m_maybeOneline && !m_pendingLines.isEmpty()) {
79 for (int i = 0; i < m_pendingLines.size(); ++i) {
81 m_stream->write(m_pendingLines.at(i).trimmed());
82 if (i != m_pendingLines.size() - 1)
85 m_stream->write(" }\n");
86 m_pendingLines.clear();
87 m_pendingLineLength = 0;
88 m_maybeOneline = false;
91 flushPotentialLinesWithNewlines();
94 m_stream->write("}\n");
98 void QmlStreamWriter::writeScriptBinding(const QString &name, const QString &rhs)
100 writePotentialLine(QString("%1: %2").arg(name, rhs).toUtf8());
103 void QmlStreamWriter::writeArrayBinding(const QString &name, const QStringList &elements)
105 flushPotentialLinesWithNewlines();
107 m_stream->write(QString("%1: [\n").arg(name).toUtf8());
109 for (int i = 0; i < elements.size(); ++i) {
111 m_stream->write(elements.at(i).toUtf8());
112 if (i != elements.size() - 1) {
113 m_stream->write(",\n");
115 m_stream->write("\n");
120 m_stream->write("]\n");
123 void QmlStreamWriter::write(const QString &data)
125 flushPotentialLinesWithNewlines();
126 m_stream->write(data.toUtf8());
129 void QmlStreamWriter::writeScriptObjectLiteralBinding(const QString &name, const QList<QPair<QString, QString> > &keyValue)
131 flushPotentialLinesWithNewlines();
133 m_stream->write(QString("%1: {\n").arg(name).toUtf8());
135 for (int i = 0; i < keyValue.size(); ++i) {
136 const QString key = keyValue.at(i).first;
137 const QString value = keyValue.at(i).second;
139 m_stream->write(QString("%1: %2").arg(key, value).toUtf8());
140 if (i != keyValue.size() - 1) {
141 m_stream->write(",\n");
143 m_stream->write("\n");
148 m_stream->write("}\n");
151 void QmlStreamWriter::writeIndent()
153 m_stream->write(QByteArray(m_indentDepth * 4, ' '));
156 void QmlStreamWriter::writePotentialLine(const QByteArray &line)
158 m_pendingLines.append(line);
159 m_pendingLineLength += line.size();
160 if (m_pendingLineLength >= 80) {
161 flushPotentialLinesWithNewlines();
165 void QmlStreamWriter::flushPotentialLinesWithNewlines()
168 m_stream->write("\n");
169 foreach (const QByteArray &line, m_pendingLines) {
171 m_stream->write(line);
172 m_stream->write("\n");
174 m_pendingLines.clear();
175 m_pendingLineLength = 0;
176 m_maybeOneline = false;