2 * Copyright (C) 2008 David Greaves
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
31 #include "shopperList.h"
38 void abort (QString msg
){
43 ListParser::ListParser(List
*list
):
48 void ListParser::start_list_el(const QXmlAttributes
& attrs
)
50 // zero the id counters
51 Category::id_master
= 0;
54 for (int at
= 0; at
!= attrs
.count(); at
++) {
55 QString at_name
= attrs
.localName(at
);
56 QString at_value
= attrs
.value(at
);
57 if (at_name
.toLower() == "name") {
59 } else if (at_name
.toLower() == "state") { // save as int? or human readable?
61 l
->state
= sState(at_value
.toInt(&ok
));
63 abort("Converting state failed\n");
66 DEBUG("<list> Unknown attribute : " << at_name
<< "='" <<at_value
<<"'\n");
70 void ListParser::start_cat_el(const QXmlAttributes
& attrs
)
73 DEBUG("<category> Not inside <list>\n");
74 exit(1); // FIXME throw?
77 for (int at
= 0; at
!= attrs
.count(); at
++) {
78 QString at_name
= attrs
.localName(at
);
79 QString at_value
= attrs
.value(at
);
80 if (at_name
.toLower() == "name") {
82 } else if (at_name
.toLower() == "active") { // is this the category active?
83 if (at_value
.toLower() == "true" or at_value
== "1")
84 l
->active_category
= c
;
86 DEBUG("<category> Unknown attribute : " << at_name
<< "='" <<at_value
<<"'\n");
91 void ListParser::start_item_el(const QXmlAttributes
& attrs
)
94 DEBUG("<item> Not inside <category>\n");
99 i
->category
=c
; // current category
100 for (int at
= 0; at
!= attrs
.count(); at
++) {
101 QString at_name
= attrs
.localName(at
);
102 QString at_value
= attrs
.value(at
);
103 if (at_name
.toLower() == "desc") {
105 } else if (at_name
.toLower() == "note") {
107 } else if (at_name
.toLower() == "wanted") {
108 i
->set_wanted(at_value
.toLower() == "true" or at_value
== "1");
109 } else if (at_name
.toLower() == "bought") {
110 i
->set_bought(at_value
.toLower() == "true" or at_value
== "1");
112 DEBUG("<item> Unknown attribute : " << at_name
<< "='" <<at_value
<<"'\n");
118 bool ListParser::startElement (const QString
& namespaceURI
,
120 const QString
& qName
,
121 const QXmlAttributes
& attrs
)
123 DEBUG("adding " << el
<< "\n");
124 if (el
.toLower() == "list") {
125 start_list_el(attrs
);
126 } else if (el
.toLower() == "category") {
128 } else if (el
.toLower() == "item") {
129 start_item_el(attrs
);
136 bool ListParser::endElement (const QString
& namespaceURI
,
138 const QString
& qName
)
140 DEBUG("done " << el
<< "\n");
141 if (el
.toLower() == "list") {
142 l
->resequence(); // Ensure the id's are sensible FIXME : not needed?
143 l
= NULL
; // No current list
144 } else if (el
.toLower() == "category") {
145 // add the created cat to list
147 c
=NULL
; // No current category
148 } else if (el
.toLower() == "item") {
149 // add the created item to list
151 DEBUG("Assigned " << i
->desc
<< " to category " << i
->category
->name
<< "\n");
152 i
= NULL
; // No current item
158 bool ListParser::fatalError ( const QXmlParseException
& exception
)
160 DEBUG("Markup error\n");
164 void ListParser::from_string(QString xml
)
166 QXmlSimpleReader xmlReader
;
167 xmlReader
.setContentHandler(this);
168 xmlReader
.setErrorHandler(this);
169 QXmlInputSource source
;
171 bool ok
= xmlReader
.parse(&source
);
173 std::cout
<< "Parsing failed." << std::endl
;
174 DEBUG("Parsing EXIT\n");
177 ////////////////////////////////////////////////////////////////
178 XMLWriter::XMLWriter(const Shopper::List
*list
) :
183 ostream
& XMLWriter::write(ostream
&out
)
186 out
<< "<list name='"<< qPrintable(l
->name
)
187 << "' state='" << l
->state
189 for (List::pCategoryIter c
= l
->categories
.begin(); c
!= l
->categories
.end(); c
++) {
191 << ((l
->active_category
== *c
) ? " active='1' " : "")
192 << " name='" << qPrintable((*c
)->name
)
194 for (Category::pItemIter i
= (*c
)->items
.begin(); i
!= (*c
)->items
.end(); i
++) {
196 << " wanted='" << (*i
)->wanted
197 << "' bought='" << (*i
)->bought
198 << "' desc='" << qPrintable((*i
)->desc
) // FIXME: Escape quotes etc
199 << "' note='" << qPrintable((*i
)->note
) // FIXME: Escape quotes etc
202 out
<< " </category>\n";
205 DEBUG("done write\n");