KWeatherCore

capparser.cpp
1 /*
2  * SPDX-FileCopyrightText: 2021 Anjani Kumar <[email protected]>
3  * SPDX-FileCopyrightText: 2021 Han Young <[email protected]>
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "capparser.h"
8 #include "kweathercore_p.h"
9 #include <KLocalizedString>
10 #include <QDateTime>
11 #include <QDebug>
12 
13 namespace KWeatherCore
14 {
15 CAPParser::CAPParser(const QByteArray &data)
16  : m_xml(data)
17 {
18  bool flag = false;
19  if (!data.isEmpty()) {
20  while (m_xml.readNextStartElement()) {
21  if (m_xml.name() == QStringLiteral("alert")) {
22  flag = true;
23  qDebug() << "valid cap file";
24  break;
25  }
26  }
27  if (!flag) {
28  qWarning() << "Not a CAP XML";
29  }
30  }
31 }
32 void CAPParser::setData(const QByteArray &data)
33 {
34  m_xml.clear();
35  m_xml.addData(data);
36 }
37 std::unique_ptr<AlertEntry> CAPParser::parse()
38 {
39  auto entry = std::make_unique<AlertEntry>();
40  while (m_xml.readNextStartElement()) {
41  switch (tags[m_xml.name().toString()]) {
42  case Tags::IDENTIFIER:
43  entry->setIdentifier(m_xml.readElementText());
44  break;
45  case Tags::SENDER:
46  entry->setSender(m_xml.readElementText());
47  break;
48  case Tags::SENT_TIME:
49  entry->setSentTime(QDateTime::fromString(m_xml.readElementText(), Qt::ISODate));
50  break;
51  case Tags::STATUS: {
52  AlertEntry::Status status;
53  auto elementText = m_xml.readElementText();
54  if (elementText == QStringLiteral("Actual")) {
55  status = AlertEntry::Status::Actual;
56  } else if (elementText == QStringLiteral("Excercise")) {
57  status = AlertEntry::Status::Exercise;
58  } else if (elementText == QStringLiteral("System")) {
59  status = AlertEntry::Status::System;
60  } else if (elementText == QStringLiteral("Test")) {
61  status = AlertEntry::Status::Test;
62  } else if (elementText == QStringLiteral("Draft")) {
63  status = AlertEntry::Status::Draft;
64  } else {
65  qWarning() << "Unknown status field";
66  }
67  entry->setStatus(status);
68  break;
69  }
70  case Tags::MSG_TYPE: {
71  AlertEntry::MsgType msgType;
72  auto elementText = m_xml.readElementText();
73  if (elementText == QStringLiteral("Alert")) {
74  msgType = AlertEntry::MsgType::Alert;
75  } else if (elementText == QStringLiteral("Update")) {
76  msgType = AlertEntry::MsgType::Update;
77  } else if (elementText == QStringLiteral("Cancel")) {
78  msgType = AlertEntry::MsgType::Cancel;
79  } else if (elementText == QStringLiteral("Ack")) {
80  msgType = AlertEntry::MsgType::Ack;
81  } else if (elementText == QStringLiteral("Error")) {
82  msgType = AlertEntry::MsgType::Error;
83  } else {
84  qWarning() << "Unknown msgType field";
85  }
86  entry->setMsgType(msgType);
87  break;
88  }
89  case Tags::SCOPE: {
90  AlertEntry::Scope scope;
91  auto elementText = m_xml.readElementText();
92  if (elementText == QStringLiteral("Public")) {
93  scope = AlertEntry::Scope::Public;
94  } else if (elementText == QStringLiteral("Private")) {
95  scope = AlertEntry::Scope::Private;
96  } else if (elementText == QStringLiteral("Restricted")) {
97  scope = AlertEntry::Scope::Restricted;
98  } else {
99  qWarning() << "Unknown scope field";
100  }
101 
102  entry->setScope(scope);
103  break;
104  }
105  case Tags::NOTE:
106  entry->setNote(m_xml.readElementText());
107  break;
108  case Tags::INFO: {
109  auto info = parseInfo();
110  entry->addInfo(info);
111  break;
112  }
113  default:
114  m_xml.skipCurrentElement();
115  }
116  }
117  return entry;
118 }
119 
120 AlertInfo CAPParser::parseInfo()
121 {
122  AlertInfo info;
123 
124  if (m_xml.name() == QStringLiteral("info")) {
125  while (!m_xml.atEnd() && !(m_xml.isEndElement() && m_xml.name() == QStringLiteral("info"))) {
126  m_xml.readNext();
127  if (m_xml.isStartElement() && infoTags.count(m_xml.name().toString())) {
128  qDebug() << m_xml.name();
129  switch (infoTags[m_xml.name().toString()]) {
130  case InfoTags::CATEGORY: {
131  auto s = m_xml.readElementText();
132  if (categoryMap.count(s)) {
133  info.addCategory(categoryMap[s]);
134  }
135  break;
136  }
137  case InfoTags::EVENT:
138  info.setEvent(m_xml.readElementText());
139  break;
140  case InfoTags::URGENCY:
141  info.setUrgency(self()->urgencyStringToEnum(m_xml.readElementText()));
142  break;
143  case InfoTags::SEVERITY:
144  info.setSeverity(self()->severityStringToEnum(m_xml.readElementText()));
145  break;
146  case InfoTags::CERTAINITY:
147  info.setCertainty(self()->certaintyStringToEnum(m_xml.readElementText()));
148  break;
149  case InfoTags::EFFECTIVE_TIME:
150  info.setEffectiveTime(QDateTime::fromString(m_xml.readElementText(), Qt::ISODate));
151  break;
152  case InfoTags::ONSET_TIME:
153  info.setOnsetTime(QDateTime::fromString(m_xml.readElementText(), Qt::ISODate));
154  break;
155  case InfoTags::EXPIRE_TIME:
156  info.setExpireTime(QDateTime::fromString(m_xml.readElementText(), Qt::ISODate));
157  break;
158  case InfoTags::HEADLINE:
159  info.setHeadline(m_xml.readElementText());
160  break;
161  case InfoTags::DESCRIPTION:
162  info.setDescription(m_xml.readElementText());
163  break;
164  case InfoTags::INSTRUCTION:
165  info.setInstruction(m_xml.readElementText());
166  break;
167  case InfoTags::PARAMETER: {
168  std::pair<QString, QString> p;
169  m_xml.readNextStartElement();
170  if (m_xml.name() == QStringLiteral("valueName")) {
171  p.first = m_xml.readElementText();
172  }
173  m_xml.readNextStartElement();
174  if (m_xml.name() == QStringLiteral("value")) {
175  p.second = m_xml.readElementText();
176  }
177  info.addParameter(p);
178  break;
179  }
180  case InfoTags::AREA: {
181  while (!(m_xml.isEndElement() && m_xml.name() == QStringLiteral("area"))) {
182  if (m_xml.name() == QStringLiteral("areaDesc") && !m_xml.isEndElement()) {
183  info.setAreaDesc(m_xml.readElementText());
184  } else if (m_xml.name() == QStringLiteral("geocode") && !m_xml.isEndElement()) {
185  std::pair<QString, QString> p;
186  m_xml.readNextStartElement();
187  if (m_xml.name() == QStringLiteral("valueName")) {
188  p.first = m_xml.readElementText();
189  }
190  m_xml.readNextStartElement();
191  if (m_xml.name() == QStringLiteral("value")) {
192  p.second = m_xml.readElementText();
193  }
194  info.addAreaCode(p);
195  } else if (m_xml.name() == QStringLiteral("polygon") && !m_xml.isEndElement()) {
196  info.addPolygon(self()->stringToPolygon(m_xml.readElementText()));
197  }
198  m_xml.readNext();
199  }
200  break;
201  }
202  case InfoTags::SENDERNAME: {
203  info.setSender(m_xml.readElementText());
204  break;
205  }
206  case InfoTags::LANGUAGE:
207  info.setLanguage(m_xml.readElementText());
208  break;
209  }
210  } else {
211  if (m_xml.isStartElement()) {
212  qWarning() << "unlnown element: " << m_xml.name();
213  }
214  }
215  }
216  }
217  return info;
218 }
219 }
bool isEmpty() const const
QDateTime fromString(const QString &string, Qt::DateFormat format)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:05:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.