2 * Copyright (C) 2008 David Greaves <david@dgreaves.com>
4 * This software is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
9 * This software is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 //#define DEBUG_SHOPPER 1
22 #include "shopper.h" // automake, i8n, gettext
23 #include <QXmlAttributes>
24 #include <QXmlParseException>
25 #include <QXmlSimpleReader>
26 #include <QXmlInputSource>
35 #include "shopperList.h"
42 void abort (QString msg
){
50 ListParser::ListParser(List
*list
):
56 void ListParser::start_list_el(const QXmlAttributes
& attrs
)
58 // zero the id counters
59 Category::id_master
= 0;
62 for (int at
= 0; at
!= attrs
.count(); at
++) {
63 QString at_name
= attrs
.localName(at
);
64 QString at_value
= attrs
.value(at
);
65 if (at_name
.toLower() == "name") {
67 } else if (at_name
.toLower() == "state") { // save as int? or human readable?
69 l
->state
= sState(at_value
.toInt(&ok
));
71 abort("Converting state failed\n");
74 DEBUG("<list> Unknown attribute : " << at_name
<< "='" <<at_value
<<"'\n");
78 void ListParser::start_cat_el(const QXmlAttributes
& attrs
)
81 DEBUG("<category> Not inside <list>\n");
82 exit(1); // FIXME throw?
85 for (int at
= 0; at
!= attrs
.count(); at
++) {
86 QString at_name
= attrs
.localName(at
);
87 QString at_value
= attrs
.value(at
);
88 if (at_name
.toLower() == "name") {
90 } else if (at_name
.toLower() == "active") { // is this the category active?
91 if (at_value
.toLower() == "true" or at_value
== "1")
92 l
->active_category
= c
;
94 DEBUG("<category> Unknown attribute : " << at_name
<< "='" <<at_value
<<"'\n");
99 void ListParser::start_item_el(const QXmlAttributes
& attrs
)
102 DEBUG("<item> Not inside <category>\n");
107 i
->category
=c
; // current category
108 for (int at
= 0; at
!= attrs
.count(); at
++) {
109 QString at_name
= attrs
.localName(at
);
110 QString at_value
= attrs
.value(at
);
111 if (at_name
.toLower() == "desc") {
113 } else if (at_name
.toLower() == "note") {
115 } else if (at_name
.toLower() == "wanted") {
116 i
->set_wanted(at_value
.toLower() == "true" or at_value
== "1");
117 } else if (at_name
.toLower() == "bought") {
118 i
->set_bought(at_value
.toLower() == "true" or at_value
== "1");
120 DEBUG("<item> Unknown attribute : " << at_name
<< "='" <<at_value
<<"'\n");
126 bool ListParser::startElement (const QString
& namespaceURI
,
128 const QString
& qName
,
129 const QXmlAttributes
& attrs
)
131 Q_UNUSED(namespaceURI
)
133 DEBUG("adding " << el
<< "\n");
134 if (el
.toLower() == "list") {
135 start_list_el(attrs
);
136 } else if (el
.toLower() == "category") {
138 } else if (el
.toLower() == "item") {
139 start_item_el(attrs
);
146 bool ListParser::endElement (const QString
& namespaceURI
,
148 const QString
& qName
)
150 Q_UNUSED(namespaceURI
)
152 DEBUG("done " << el
<< "\n");
153 if (el
.toLower() == "list") {
154 l
->resequence(); // Ensure the id's are sensible FIXME : not needed?
155 l
= 0; // No current list
156 } else if (el
.toLower() == "category") {
157 // add the created cat to list
159 c
->items
.sort(cmpItem
);
160 c
=0; // No current category
161 } else if (el
.toLower() == "item") {
162 // add the created item to list
164 DEBUG("Assigned " << i
->desc
<< " to category " << i
->category
->name
<< "\n");
165 i
= 0; // No current item
171 bool ListParser::fatalError ( const QXmlParseException
& exception
)
174 DEBUG("Markup error\n");
178 void ListParser::from_string(QString xml
)
180 QXmlSimpleReader xmlReader
;
181 xmlReader
.setContentHandler(this);
182 xmlReader
.setErrorHandler(this);
183 QXmlInputSource source
;
185 bool ok
= xmlReader
.parse(&source
);
187 std::cout
<< "Parsing failed." << std::endl
;
188 DEBUG("Parsing EXIT\n");
191 ////////////////////////////////////////////////////////////////
192 XMLWriter::XMLWriter(const Shopper::List
*list
) :
197 ostream
& XMLWriter::write(ostream
&out
)
200 out
<< "<list name='"<< qPrintable(l
->name
)
201 << "' state='" << l
->state
203 for (List::pCategoryIter c
= l
->categories
.begin(); c
!= l
->categories
.end(); c
++) {
205 << ((l
->active_category
== *c
) ? " active='1' " : "")
206 << " name='" << qPrintable((*c
)->name
)
207 << "' id='" << QString::number((*c
)->id
)
209 for (Category::pItemIter i
= (*c
)->items
.begin(); i
!= (*c
)->items
.end(); i
++) {
211 << " wanted='" << (*i
)->wanted
212 << "' bought='" << (*i
)->bought
213 << "' desc='" << qPrintable((*i
)->desc
) // FIXME: Escape quotes etc
214 << "' note='" << qPrintable((*i
)->note
) // FIXME: Escape quotes etc
217 out
<< " </category>\n";
220 DEBUG("done write\n");