Torc  0.1
torcserialiser.cpp
Go to the documentation of this file.
1 /* Class TorcSerialiser
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2012-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torcxmlserialiser.h"
26 #include "torcserialiser.h"
27 
29 {
30  // first create a prioritised list from MimeType
31  QStringList rawtypes = MimeType.split(',', QString::SkipEmptyParts);
32  QMap<float, QPair<QString,QString>> types;
33  foreach (QString rawtype, rawtypes)
34  {
35  rawtype = rawtype.trimmed();
36  int pos = rawtype.indexOf('/');
37  if (pos > 0 && pos < (rawtype.size() - 1))
38  {
39  QString type = rawtype.left(pos).trimmed();
40  QString subtype = rawtype.mid(pos + 1).trimmed();
41  float priority = 1.0;
42 
43  int qpos1 = subtype.indexOf(';');
44  int qpos2 = subtype.indexOf('=');
45  if (qpos1 > -1 && qpos2 > 0)
46  {
47  QString prioritys = subtype.mid(qpos2 + 1).trimmed();
48  subtype = subtype.left(qpos1).trimmed();
49  bool ok = false;
50  priority = prioritys.toFloat(&ok);
51  priority = ok ? priority : 0.0;
52  // N.B. a fully specified mime type (e.g. text/plain) should have higher priority than partially
53  // specified (e.g. text/*)
54  if (subtype == QStringLiteral("*"))
55  priority -= 0.0001; // fudged - this COULD make it lower priority than intended
56  }
57 
58  types.insertMulti(priority, QPair<QString,QString>(type.toLower(), subtype.toLower()));
59  }
60  }
61 
62  QMapIterator<float, QPair<QString,QString>> it(types);
63  it.toBack();
64  while (it.hasPrevious())
65  {
66  it.previous();
68  for ( ; factory; factory = factory->NextTorcSerialiserFactory())
69  if (factory->Accepts(it.value()))
70  return factory->Create();
71  }
72 
73  // We don't check for */* - just default to XML as there is no serialiser priority.
74  LOG(VB_GENERAL, LOG_WARNING, QStringLiteral("Failed to find serialiser for '%1' - defaulting to XML").arg(MimeType));
75  return new TorcXMLSerialiser();
76 }
77 
78 void TorcSerialiser::Serialise(QByteArray &Dest, const QVariant &Data, const QString &Type)
79 {
80  Prepare(Dest);
81  Begin(Dest);
82  AddProperty(Dest, Type, Data);
83  End(Dest);
84 }
85 
87 
88 TorcSerialiserFactory::TorcSerialiserFactory(const QString &Type, const QString &SubType, const QString &Description)
89  : m_nextTorcSerialiserFactory(gTorcSerialiserFactory),
90  m_type(Type),
91  m_subtype(SubType),
92  m_description(Description)
93 {
95 }
96 
98 {
100 }
101 
103 {
105 }
106 
107 bool TorcSerialiserFactory::Accepts(const QPair<QString,QString> &MimeType) const
108 {
109  if (m_type == MimeType.first)
110  {
111  if (m_subtype == MimeType.second)
112  return true;
113  if ("*" == MimeType.second)
114  return true;
115  }
116  return false;
117 }
118 
119 const QString& TorcSerialiserFactory::Description(void) const
120 {
121  return m_description;
122 }
123 
125 {
126  return m_type + "/" + m_subtype;
127 }
bool Accepts(const QPair< QString, QString > &MimeType) const
virtual TorcSerialiser * Create(void)=0
TorcSerialiserFactory(const QString &Type, const QString &SubType, const QString &Description)
static TorcSerialiserFactory * GetTorcSerialiserFactory(void)
QString MimeType(void) const
TorcSerialiserFactory * NextTorcSerialiserFactory(void) const
void Serialise(QByteArray &Dest, const QVariant &Data, const QString &Type=QString())
virtual void Begin(QByteArray &Dest)=0
static TorcSerialiser * GetSerialiser(const QString &MimeType)
virtual void Prepare(QByteArray &Dest)=0
static TorcSerialiserFactory * gTorcSerialiserFactory
virtual void End(QByteArray &Dest)=0
virtual void AddProperty(QByteArray &Dest, const QString &Name, const QVariant &Value)=0
const QString & Description(void) const
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
TorcSerialiserFactory * m_nextTorcSerialiserFactory
A serialiser for XML formatted output.