Debian Codegraph @

src/recipereader.cpp

raw symbols SCIP
1 /***************************************************************************
2 recipereader.cpp
3 -------------------
4 XML stream reader for recipe
5 -------------------
6 Copyright 2008, David Johnson
7 Please see the header file for copyright and license information
8 ***************************************************************************/
9
10 #include <QApplication>
11 #include "recipe.h"
12 #include "resource.h"
13
14 #include "recipereader.h"
15
16 using namespace Resource;
17
18 // RecipeReader ///////////////////////////////////////////////////////////////
19
20 /////////////////////////////////////////////////////////////////////////////
21 // RecipeReader()
22 // --------------
23 // Constructor
24
25 RecipeReader::RecipeReader(QIODevice *device)
26 : QXmlStreamReader(device)
27 {
28 }
29
30 /////////////////////////////////////////////////////////////////////////////
31 // isRecipeFormat()
32 // ----------------
33 // Is xml native Recipe format?
34 // Defined as "recipe" doctype with generator or application as "qbrew"
35
36 bool RecipeReader::isRecipeFormat()
37 {
38 // find root/first element
39 do {
40 readNext();
41 } while (!atEnd() && !isStartElement());
42
43 if (hasError()) return false;
44
45 // check the document type
46 if (name() != tagRecipe) return false;
47
48 // check application
49 if (attributes().value(attrApplication) != PACKAGE) {
50 // check generator if no application
51 if (attributes().value(attrGenerator) != PACKAGE) {
52 return false;
53 }
54 }
55
56 return true;
57 }
58
59 /////////////////////////////////////////////////////////////////////////////
60 // readRecipe()
61 // ------------
62 // Read in recipe
63
64 bool RecipeReader::readRecipe(Recipe *recipe)
65 {
66 QString buf;
67 bool grainsflag=false, hopsflag=false, miscsflag=false;
68
69 // parse file
70 while (!atEnd()) {
71 readNext();
72 if (isStartElement()) {
73
74 // recipe root tag
75 if (name() == tagRecipe) {
76 // check application or generator
77 if (attributes().value(attrApplication) != PACKAGE) {
78 if (attributes().value(attrGenerator) != PACKAGE) {
79 raiseError(QObject::tr("Not a recipe for qbrew"));
80 return false;
81 }
82 }
83
84 // check file version
85 buf = attributes().value(attrVersion).toString();
86 if (buf < RECIPE_PREVIOUS) {
87 // too old of a version
88 raiseError(QObject::tr("Unsupported file version"));
89 return false;
90 }
91 }
92
93 // title
94 else if (name() == tagTitle) {
95 recipe->setTitle(readElementText());
96 }
97
98 // brewer
99 else if (name() == tagBrewer) {
100 recipe->setBrewer(readElementText());
101 }
102
103 // style
104 // TODO: load/save entire style
105 else if (name() == tagStyle) {
106 recipe->setStyle(readElementText());
107 }
108
109 // get batch settings
110 // TODO: eliminate this tag, use quantity, efficiency
111 else if (name() == tagBatch) {
112 if (!attributes().value(attrQuantity).isEmpty()) {
113 buf = attributes().value(attrQuantity).toString();
114 recipe->setSize(Volume(buf, Volume::gallon));
115 } else if (!attributes().value(attrSize).isEmpty()) {
116 // deprecated tag
117 buf = attributes().value(attrSize).toString();
118 recipe->setSize(Volume(buf, Volume::gallon));
119 }
120 }
121
122 // get notes
123 else if (name() == tagNotes) {
124 if (attributes().value(attrClass) == classRecipe) {
125 buf = recipe->recipeNotes();
126 if (!buf.isEmpty()) buf += '\n';
127 buf += readElementText();
128 recipe->setRecipeNotes(buf);
129 } else if (attributes().value(attrClass) == classBatch) {
130 buf = recipe->batchNotes();
131 if (!buf.isEmpty()) buf += '\n';
132 buf += readElementText();
133 recipe->setBatchNotes(buf);
134 }
135 }
136
137 // grains
138 else if (name() == tagGrains) {
139 grainsflag = true;
140 }
141
142 // grain
143 else if (name() == tagGrain) {
144 if (!grainsflag) qWarning("Warning: mislocated grain tag");
145 Grain grain;
146 buf = attributes().value(attrQuantity).toString();
147 grain.setWeight(Weight(buf, Weight::pound));
148 buf = attributes().value(attrExtract).toString();
149 grain.setExtract(buf.toDouble());
150 buf = attributes().value(attrColor).toString();
151 grain.setColor(buf.toDouble());
152 grain.setType(attributes().value(attrType).toString());
153 grain.setUse(attributes().value(attrUse).toString());
154 grain.setName(readElementText());
155 recipe->addGrain(grain);
156 }
157
158 // hops
159 else if (name() == tagHops) {
160 hopsflag = true;
161 }
162
163 // hop
164 else if (name() == tagHop) {
165 // TODO: attrForm is deprecated 0.4.0
166 if (!hopsflag) qWarning("Warning: mislocated hop tag");
167 Hop hop;
168 buf = attributes().value(attrQuantity).toString();
169 hop.setWeight(Weight(buf, Weight::ounce));
170 buf = attributes().value(attrType).toString();
171 if (buf.isEmpty()) buf = attributes().value(attrForm).toString();
172 hop.setType(buf);
173 buf = attributes().value(attrAlpha).toString();
174 hop.setAlpha(buf.toDouble());
175 buf = attributes().value(attrTime).toString();
176 hop.setTime(buf.toUInt());
177 hop.setName(readElementText());
178 recipe->addHop(hop);
179 }
180
181 // miscs
182 else if (name() == tagMiscs) {
183 miscsflag = true;
184 }
185
186 // misc
187 else if (name() == tagMisc) {
188 if (!miscsflag) qWarning("Warning: mislocated misc tag");
189 Misc misc;
190 buf = attributes().value(attrQuantity).toString();
191 misc.setQuantity(Quantity(buf, Quantity::generic));
192 misc.setType(attributes().value(attrType).toString());
193 misc.setNotes(attributes().value(attrNotes).toString());
194 misc.setName(readElementText());
195 recipe->addMisc(misc);
196 }
197
198 // unknown tag, skip
199 else {
200 qWarning() << "Warning: Unknown tag" << name().toString();
201 skipElement();
202 }
203 }
204
205 else if (isEndElement()) {
206 // unset flags
207 if (name() == tagGrains) grainsflag = false;
208 else if (name() == tagHops) hopsflag = false;
209 else if (name() == tagMiscs) miscsflag = false;
210 }
211 }
212 return true;
213 }
214
215 // skip over xml element
216 void RecipeReader::skipElement()
217 {
218 if (isStartElement()) {
219 int level = 0;
220 while (!atEnd()) {
221 readNext();
222
223 if (isStartElement()) level++;
224 if (isEndElement()) level--;
225
226 if (level < 0) break;
227 }
228 }
229 }
230
231 // RecipeWriter ///////////////////////////////////////////////////////////////
232
233 /////////////////////////////////////////////////////////////////////////////
234 // RecipeWriter()
235 // --------------
236 // Constructor
237
238 RecipeWriter::RecipeWriter(QIODevice *device)
239 : QXmlStreamWriter(device)
240 {
241 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
242 setAutoFormatting(true);
243 setAutoFormattingIndent(2);
244 #endif
245 }
246
247 /////////////////////////////////////////////////////////////////////////////
248 // writeRecipe()
249 // -------------
250 // Write out recipe
251
252 bool RecipeWriter::writeRecipe(Recipe *recipe)
253 {
254 writeStartDocument();
255 writeDTD(QString("<!DOCTYPE %1>").arg(tagRecipe));
256
257 // write root element
258 writeStartElement(tagRecipe);
259 writeAttribute(attrApplication, PACKAGE);
260 writeAttribute(attrVersion, VERSION);
261
262 // write recipe information
263 writeTextElement(tagTitle, recipe->title());
264 writeTextElement(tagBrewer, recipe->brewer());
265 // TODO: save entire style
266 writeTextElement(tagStyle, recipe->style().name());
267 writeEmptyElement(tagBatch);
268 writeAttribute(attrQuantity, recipe->size().toString());
269
270 // write notes
271 if (!recipe->recipeNotes().isEmpty()) {
272 writeStartElement(tagNotes);
273 writeAttribute(attrClass, classRecipe);
274 writeCharacters(recipe->recipeNotes());
275 writeEndElement();
276 }
277 if (!recipe->batchNotes().isEmpty()) {
278 writeStartElement(tagNotes);
279 writeAttribute(attrClass, classBatch);
280 writeCharacters(recipe->batchNotes());
281 writeEndElement();
282 }
283
284 // write grains
285 writeStartElement(tagGrains);
286 foreach(Grain grain, *recipe->grains()) {
287 // iterate through grain list
288 writeStartElement(tagGrain);
289 writeAttribute(attrQuantity, grain.weight().toString());
290 writeAttribute(attrExtract, QString::number(grain.extract()));
291 writeAttribute(attrColor, QString::number(grain.color()));
292 writeAttribute(attrType, grain.type());
293 writeAttribute(attrUse, grain.use());
294 writeCharacters(grain.name());
295 writeEndElement();
296 }
297 writeEndElement(); // tagGrains
298
299 // write hops
300 writeStartElement(tagHops);
301 foreach(Hop hop, *recipe->hops()) {
302 // iterate through hop list
303 writeStartElement(tagHop);
304 writeAttribute(attrQuantity, hop.weight().toString());
305 writeAttribute(attrAlpha, QString::number(hop.alpha()));
306 writeAttribute(attrTime, QString::number(hop.time()));
307 writeAttribute(attrType, hop.type());
308 writeCharacters(hop.name());
309 writeEndElement();
310 }
311 writeEndElement(); // tagHops
312
313 // write misc ingredients
314 writeStartElement(tagMiscs);
315 foreach(Misc misc, *recipe->miscs()) {
316 // iterate through misc list
317 writeStartElement(tagMisc);
318 writeAttribute(attrQuantity, misc.quantity().toString());
319 writeAttribute(attrType, misc.type());
320 writeAttribute(attrNotes, misc.notes());
321 writeCharacters(misc.name());
322 writeEndElement();
323 }
324 writeEndElement(); // tagMiscs
325
326 writeEndElement(); // tagRecipe
327 writeEndDocument();
328
329 return true;
330 }