1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the tools applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qmlstreamwriter.h"
43 
44 #include <QtCore/QBuffer>
45 #include <QtCore/QStringList>
46 
QmlStreamWriter(QByteArray * array)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 
writeStartDocument()56 void QmlStreamWriter::writeStartDocument()
57 {
58 }
59 
writeEndDocument()60 void QmlStreamWriter::writeEndDocument()
61 {
62 }
63 
writeLibraryImport(const QString & uri,int majorVersion,int minorVersion,const QString & as)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 
writeStartObject(const QString & component)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 
writeEndObject()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         flushPotentialLinesWithNewlines();
97         --m_indentDepth;
98         writeIndent();
99         m_stream->write("}\n");
100     }
101 }
102 
writeScriptBinding(const QString & name,const QString & rhs)103 void QmlStreamWriter::writeScriptBinding(const QString &name, const QString &rhs)
104 {
105     writePotentialLine(QString("%1: %2").arg(name, rhs).toUtf8());
106 }
107 
writeArrayBinding(const QString & name,const QStringList & elements)108 void QmlStreamWriter::writeArrayBinding(const QString &name, const QStringList &elements)
109 {
110     flushPotentialLinesWithNewlines();
111     writeIndent();
112     m_stream->write(QString("%1: [\n").arg(name).toUtf8());
113     ++m_indentDepth;
114     for (int i = 0; i < elements.size(); ++i) {
115         writeIndent();
116         m_stream->write(elements.at(i).toUtf8());
117         if (i != elements.size() - 1) {
118             m_stream->write(",\n");
119         } else {
120             m_stream->write("\n");
121         }
122     }
123     --m_indentDepth;
124     writeIndent();
125     m_stream->write("]\n");
126 }
127 
write(const QString & data)128 void QmlStreamWriter::write(const QString &data)
129 {
130     flushPotentialLinesWithNewlines();
131     m_stream->write(data.toUtf8());
132 }
133 
writeScriptObjectLiteralBinding(const QString & name,const QList<QPair<QString,QString>> & keyValue)134 void QmlStreamWriter::writeScriptObjectLiteralBinding(const QString &name, const QList<QPair<QString, QString> > &keyValue)
135 {
136     flushPotentialLinesWithNewlines();
137     writeIndent();
138     m_stream->write(QString("%1: {\n").arg(name).toUtf8());
139     ++m_indentDepth;
140     for (int i = 0; i < keyValue.size(); ++i) {
141         const QString key = keyValue.at(i).first;
142         const QString value = keyValue.at(i).second;
143         writeIndent();
144         m_stream->write(QString("%1: %2").arg(key, value).toUtf8());
145         if (i != keyValue.size() - 1) {
146             m_stream->write(",\n");
147         } else {
148             m_stream->write("\n");
149         }
150     }
151     --m_indentDepth;
152     writeIndent();
153     m_stream->write("}\n");
154 }
155 
writeIndent()156 void QmlStreamWriter::writeIndent()
157 {
158     m_stream->write(QByteArray(m_indentDepth * 4, ' '));
159 }
160 
writePotentialLine(const QByteArray & line)161 void QmlStreamWriter::writePotentialLine(const QByteArray &line)
162 {
163     m_pendingLines.append(line);
164     m_pendingLineLength += line.size();
165     if (m_pendingLineLength >= 80) {
166         flushPotentialLinesWithNewlines();
167     }
168 }
169 
flushPotentialLinesWithNewlines()170 void QmlStreamWriter::flushPotentialLinesWithNewlines()
171 {
172     if (m_maybeOneline)
173         m_stream->write("\n");
174     foreach (const QByteArray &line, m_pendingLines) {
175         writeIndent();
176         m_stream->write(line);
177         m_stream->write("\n");
178     }
179     m_pendingLines.clear();
180     m_pendingLineLength = 0;
181     m_maybeOneline = false;
182 }
183