Torc  0.1
torcplistserialiser.cpp
Go to the documentation of this file.
1 /* Class TorcPListSerialiser
2 *
3 * Copyright (C) Mark Kendall 2012-18
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 // Torc
21 #include "torcplistserialiser.h"
22 
25 {
26 }
27 
29 {
30  return HTTPResponsePList;
31 }
32 
33 void TorcPListSerialiser::Begin(QByteArray &)
34 {
35  m_xmlStream.setAutoFormatting(true);
36  m_xmlStream.setAutoFormattingIndent(4);
37  m_xmlStream.writeStartDocument(QStringLiteral("1.0"));
38  m_xmlStream.writeDTD(QStringLiteral("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"));
39  m_xmlStream.writeStartElement(QStringLiteral("plist"));
40  m_xmlStream.writeAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
41  m_xmlStream.writeStartElement(QStringLiteral("dict"));
42 }
43 
44 void TorcPListSerialiser::AddProperty(QByteArray &, const QString &Name, const QVariant &Value)
45 {
46  PListFromVariant(Name, Value);
47 }
48 
49 void TorcPListSerialiser::End(QByteArray &)
50 {
51  m_xmlStream.writeEndElement();
52  m_xmlStream.writeEndElement();
53  m_xmlStream.writeEndDocument();
54 }
55 
56 void TorcPListSerialiser::PListFromVariant(const QString &Name, const QVariant &Value, bool NeedKey)
57 {
58  if (Value.isNull())
59  {
60  if (NeedKey)
61  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
62  m_xmlStream.writeEmptyElement(QStringLiteral("null"));
63  return;
64  }
65 
66  switch ((int)Value.type())
67  {
68  case QMetaType::QVariantList: PListFromList(Name, Value.toList()); break;
69  case QMetaType::QStringList: PListFromStringList(Name, Value.toStringList()); break;
70  case QMetaType::QVariantMap: PListFromMap(Name, Value.toMap()); break;
71  case QMetaType::QDateTime:
72  {
73  if (Value.toDateTime().isValid())
74  {
75  if (NeedKey)
76  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
77  m_xmlStream.writeTextElement(QStringLiteral("date"), Value.toDateTime().toUTC().toString(QStringLiteral("yyyy-MM-ddThh:mm:ssZ")));
78  }
79  break;
80  }
81  case QMetaType::QByteArray:
82  {
83  if (!Value.toByteArray().isNull())
84  {
85  if (NeedKey)
86  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
87  m_xmlStream.writeTextElement(QStringLiteral("data"), Value.toByteArray().toBase64().data());
88  }
89  break;
90  }
91  case QMetaType::Bool:
92  {
93  if (NeedKey)
94  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
95  m_xmlStream.writeEmptyElement(Value.toBool() ? QStringLiteral("true") : QStringLiteral("false"));
96  break;
97  }
98  case QMetaType::Char:
99  {
100  if (NeedKey)
101  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
102  m_xmlStream.writeEmptyElement(QStringLiteral("fill"));
103  break;
104  }
105  break;
106  case QMetaType::UInt:
107  case QMetaType::UShort:
108  case QMetaType::ULong:
109  case QMetaType::ULongLong:
110  {
111  if (NeedKey)
112  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
113  m_xmlStream.writeTextElement(QStringLiteral("integer"), QString::number(Value.toULongLong()));
114  break;
115  }
116 
117  case QMetaType::Int:
118  case QMetaType::Short:
119  case QMetaType::Long:
120  case QMetaType::LongLong:
121  case QMetaType::Float:
122  case QMetaType::Double:
123  {
124  if (NeedKey)
125  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
126  m_xmlStream.writeTextElement(QStringLiteral("real"), QStringLiteral("%1").arg(Value.toDouble(), 0, 'f', 6));
127  break;
128  }
129 
130  case QMetaType::QString:
131  default:
132  {
133  if (NeedKey)
134  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
135  m_xmlStream.writeTextElement(QStringLiteral("string"), Value.toString());
136  break;
137  }
138  }
139 }
140 
141 void TorcPListSerialiser::PListFromList(const QString &Name, const QVariantList &Value)
142 {
143  if (!Value.isEmpty())
144  {
145  int type = Value[0].type();
146 
147  QVariantList::const_iterator it = Value.begin();
148  for ( ; it != Value.end(); ++it)
149  {
150  if ((int)(*it).type() != type)
151  {
152  PListFromVariant(QStringLiteral("Error"), QVARIANT_ERROR);
153  return;
154  }
155  }
156  }
157 
158  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
159  m_xmlStream.writeStartElement(QStringLiteral("array"));
160 
161  QVariantList::const_iterator it = Value.begin();
162  for ( ; it != Value.end(); ++it)
163  PListFromVariant(Name, (*it), false);
164 
165  m_xmlStream.writeEndElement();
166 }
167 
168 void TorcPListSerialiser::PListFromStringList(const QString &Name, const QStringList &Value)
169 {
170  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
171  m_xmlStream.writeStartElement(QStringLiteral("array"));
172 
173  QStringList::const_iterator it = Value.begin();
174  for ( ; it != Value.end(); ++it)
175  m_xmlStream.writeTextElement(QStringLiteral("string"), (*it));
176 
177  m_xmlStream.writeEndElement();
178 }
179 
180 void TorcPListSerialiser::PListFromMap(const QString &Name, const QVariantMap &Value)
181 {
182  m_xmlStream.writeTextElement(QStringLiteral("key"), Name);
183  m_xmlStream.writeStartElement(QStringLiteral("dict"));
184 
185  QVariantMap::const_iterator it = Value.begin();
186  for ( ; it != Value.end(); ++it)
187  PListFromVariant(it.key(), it.value());
188 
189  m_xmlStream.writeEndElement();
190 }
191 
193 {
194  public:
195  TorcApplePListSerialiserFactory() : TorcSerialiserFactory(QStringLiteral("text"), QStringLiteral("x-apple-plist+xml"), QStringLiteral("PList"))
196  {
197  }
198 
200  {
201  return new TorcPListSerialiser();
202  }
204 
206 {
207  public:
208  TorcPListSerialiserFactory() : TorcSerialiserFactory(QStringLiteral("application"), QStringLiteral("plist"), QStringLiteral("PList"))
209  {
210  }
211 
213  {
214  return new TorcPListSerialiser();
215  }
217 
218 
TorcPListSerialiserFactory TorcPListSerialiserFactory
void PListFromList(const QString &Name, const QVariantList &Value)
void PListFromMap(const QString &Name, const QVariantMap &Value)
void Begin(QByteArray &) override
void AddProperty(QByteArray &, const QString &Name, const QVariant &Value) override
TorcApplePListSerialiserFactory TorcApplePListSerialiserFactory
HTTPResponseType
void PListFromStringList(const QString &Name, const QStringList &Value)
HTTPResponseType ResponseType(void) override
void PListFromVariant(const QString &Name, const QVariant &Value, bool NeedKey=true)
void End(QByteArray &) override
TorcSerialiser * Create(void)
#define QVARIANT_ERROR
QXmlStreamWriter m_xmlStream
A serialiser for XML formatted output.