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