1
/***************************************************************************
2
* Copyright (C) 2008 by Łukasz Jernaś *
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. *
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. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
***************************************************************************/
20
#include "xmlstreamreader.h"
22
namespace NameFinder {
24
XmlStreamReader::XmlStreamReader(QIODevice *device) {
29
XmlStreamReader::~XmlStreamReader() {
32
bool XmlStreamReader::read() {
33
myDevice->open(QIODevice::ReadOnly | QIODevice::Text);
34
reader.setDevice(myDevice);
36
while (!reader.atEnd()) {
37
if (reader.isStartElement()) {
38
if (reader.name() == "searchresults") {
39
readSearchResultsElement();
41
reader.raiseError(QObject::tr("Not a proper results stream!"));
48
// Implement error handling
51
void XmlStreamReader::readSearchResultsElement() {
53
while (!reader.atEnd()) {
54
if (reader.isEndElement()) {
59
if (reader.isStartElement()) {
60
if (reader.name() == "named") {
61
readNamedElement(&myResults);
71
void XmlStreamReader::readNamedElement(QList<NameFinderResult> *results) {
72
NameFinderResult myResult;
73
myResult.name = reader.attributes().value("name").toString();
74
myResult.type = reader.attributes().value("type").toString();
75
myResult.zoom = reader.attributes().value("zoom").toString().toInt();
76
myResult.lon = reader.attributes().value("lon").toString().toDouble();
77
myResult.lat = reader.attributes().value("lat").toString().toDouble();
78
myResult.category = reader.attributes().value("category").toString();
79
myResult.info = reader.attributes().value("info").toString();
82
while (!reader.atEnd()) {
83
if (reader.isEndElement()) {
88
if (reader.isStartElement()) {
89
if (reader.name() == "named") {
90
readNamedElement(results);
91
} else if (reader.name() == "description") {
92
readDescriptionElement(&myResult);
93
} else if (reader.name() == "nearestplaces") {
94
readNearestPlacesElement(&myResult);
103
results->append(myResult);
106
void XmlStreamReader::skipElement() {
108
while (!reader.atEnd()) {
109
if (reader.isEndElement()) {
113
if (reader.isStartElement()) {
121
void XmlStreamReader::readDescriptionElement(NameFinderResult *result) {
122
result->description = reader.readElementText();
123
while (!reader.atEnd()) {
124
if (reader.isEndElement()) {
133
void XmlStreamReader::readNearestPlacesElement(NameFinderResult *result) {
135
while (!reader.atEnd()) {
136
if (reader.isEndElement()) {
140
if (reader.isStartElement()) {
141
if (reader.name() == "named") {
142
QList<NameFinderResult> nearResults;
143
readNamedElement(&nearResults);
144
result->near = nearResults;
154
QList<NameFinderResult> XmlStreamReader::getResults() {