Debian Codegraph @

src/beerxmlreader.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /***************************************************************************
2 beerxmlreader.cpp
3 -------------------
4 XML stream reader for BeerXML
5 -------------------
6 Copyright 2008, David Johnson
7 Please see the header file for copyright and license information
8 ***************************************************************************/
9
10 #include <QApplication>
11 #include <QInputDialog>
12 #include "data.h"
13 #include "recipe.h"
14 #include "resource.h"
15
16 #include "beerxmlreader.h"
17
18 using namespace Resource;
19
20 // BeerXmlReader /////////////////////////////////////////////////////////////
21
22 /////////////////////////////////////////////////////////////////////////////
23 // BeerXmlReader()
24 // ---------------
25 // Constructor
26
27 BeerXmlReader::BeerXmlReader(QIODevice *device)
28 : QXmlStreamReader(device)
29 {
30 }
31
32 /////////////////////////////////////////////////////////////////////////////
33 // isBeerXmlFormat()
34 // -----------------
35 // Is xml BeerXML format?
36 // Defined as XML with "<RECIPES>" and "<RECIPE>", VERSION 1
37 // Note that BeerXML 1.0 is poorly designed format
38
39 bool BeerXmlReader::isBeerXmlFormat()
40 {
41 // find root/first element
42 do {
43 readNext();
44 } while (!atEnd() && !isStartElement());
45 if (hasError()) return false;
46
47 // check the document type
48 if (name() != tagRECIPES) return false;
49
50 // check for RECIPE
51 while (!atEnd()) {
52 readNext();
53 if (isStartElement()) {
54 if (name() == tagRECIPE) {
55 // check VERSION
56 while (!atEnd()) {
57 readNext();
58 if (isStartElement()) {
59 if (name() == tagVERSION) {
60 if (readElementText() == beerXMLVersion)
61 return true;
62 }
63 }
64 }
65 }
66 }
67 }
68
69 return false;
70 }
71
72 /////////////////////////////////////////////////////////////////////////////
73 // readRecipe()
74 // ------------
75 // Read in recipe
76
77 bool BeerXmlReader::readRecipe(Recipe *recipe)
78 {
79 QString buf;
80
81 // BeerXML may have more than one recipe,
82 // so we read each one into a different recipe object
83 QMap<QString, Recipe> recipes;
84 while (!atEnd()) {
85 readNext();
86 if (isStartElement()) {
87 if (name() == tagRECIPES) {
88 while (!atEnd()) {
89 readNext();
90 if (isStartElement()) {
91 if (name() == tagRECIPE) {
92 Recipe r = readSingleRecipe();
93 if (!r.title().isEmpty()) {
94 recipes[r.title()] = r;
95 }
96 } else {
97 skipElement();
98 }
99 }
100 }
101 } else {
102 qWarning() << "Warning: Unknown tag" << name().toString();
103 skipElement();
104 }
105 }
106 }
107 if (hasError()) return false;
108
109 if (recipes.count() == 0) {
110 raiseError("No recipes found");
111 return false;
112 }
113
114 // select just one recipe
115 if (recipes.count() > 1) {
116 bool ok;
117 QString name = QInputDialog::getItem(0, TITLE,
118 QObject::tr("Multiple recipes found. Please select one:"),
119 recipes.keys(), 0, false, &ok);
120 if (ok) {
121 // copy over recipe
122 *recipe = recipes[name];
123 }
124 } else {
125 // only one recipe
126 *recipe = recipes.begin().value();
127 }
128
129 return true;
130 }
131
132 //////////////////////////////////////////////////////////////////////////////
133 // readSingleRecipe()
134 // ------------------
135 // Read an individual recipe from xml
136
137 Recipe BeerXmlReader::readSingleRecipe()
138 {
139 Recipe recipe(0);
140 QString buf;
141 bool mashed = false;
142 bool grainsflag=false, hopsflag=false, miscsflag=false, yeastsflag=false;
143
144 while (!atEnd()) {
145 readNext();
146 if (isStartElement()) {
147 // version
148 if (name() == tagVERSION) {
149 if (readElementText() != beerXMLVersion) {
150 qWarning() << "Wrong BeerXML version";
151 return Recipe();
152 }
153 }
154
155 // name
156 else if (name() == tagNAME) {
157 recipe.setTitle(readElementText());
158 }
159
160 // brewer
161 else if (name() == tagBREWER) {
162 recipe.setBrewer(readElementText());
163 }
164
165 // style
166 else if (name() == tagSTYLE) {
167 recipe.setStyle(readStyle());
168 }
169
170 // mashed
171 else if (name() == tagTYPE) {
172 mashed = (readElementText() != Grain::EXTRACT_STRING);
173 }
174
175 // batch size
176 else if (name() == tagBATCHSIZE) {
177 buf = readElementText();
178 recipe.setSize(Volume(buf, Volume::liter));
179 }
180
181 // notes
182 else if (name() == tagNOTES) {
183 recipe.setRecipeNotes(readElementText());
184 }
185
186 else if (name() == tagTASTENOTES) {
187 recipe.setBatchNotes(readElementText());
188 }
189
190 // fermentables
191 else if (name() == tagFERMENTABLES) {
192 grainsflag = true;
193 }
194 // fermentable
195 else if (name() == tagFERMENTABLE) {
196 if (!grainsflag) qWarning("Warning: mislocated fermentable tag");
197 recipe.addGrain(readFermentable());
198 }
199
200 // hops
201 else if (name() == tagHOPS) {
202 hopsflag = true;
203 }
204 // hop
205 else if (name() == tagHOP) {
206 if (!hopsflag) qWarning("Warning: mislocated hop tag");
207 recipe.addHop(readHop());
208 }
209
210 // miscs
211 else if (name() == tagMISCS) {
212 miscsflag = true;
213 }
214 // misc
215 else if (name() == tagMISC) {
216 if (!miscsflag) qWarning("Warning: mislocated misc tag");
217 recipe.addMisc(readMisc());
218 }
219
220 // yeasts
221 else if (name() == tagYEASTS) {
222 yeastsflag = true;
223 }
224 // yeast
225 else if (name() == tagYEAST) {
226 if (!yeastsflag) qWarning("Warning: mislocated yeast tag");
227 recipe.addMisc(readYeast());
228 }
229
230 else if (name() == tagEFFICIENCY) {
231 // unused, ignore tag...
232 skipElement();
233 }
234
235 else if (name() == tagWATERS) {
236 // unused, ignore tag...
237 skipElement();
238 }
239
240 // unknown tag, skip
241 else {
242 skipElement();
243 }
244 }
245
246 else if (isEndElement()) {
247 if (name() == tagRECIPE) break;
248 // unset flags
249 else if (name() == tagFERMENTABLES) grainsflag = false;
250 else if (name() == tagHOPS) hopsflag = false;
251 else if (name() == tagMISCS) miscsflag = false;
252 else if (name() == tagYEASTS) yeastsflag = false;
253 }
254 }
255 if (hasError()) {
256 raiseError("Problem reading recipe");
•257 // Ensure an empty title, so that the recipe does not get included
258 recipe.setTitle("");
259 return recipe;
260 }
261
262 // set grain use according to recipe type
263 for (int n=0; n<recipe.grains()->count(); n++) {
264 if (mashed) (*recipe.grains())[n].setUse(Grain::MASHED_STRING);
265 else (*recipe.grains())[n].setUse(Grain::STEEPED_STRING);
266 }
267
268 return recipe;
269 }
270
271 // read grain
272 Grain BeerXmlReader::readFermentable()
273 {
274 QString buf;
275 Grain grain;
276
277 while (!atEnd()) {
278 readNext();
279 if (isStartElement()) {
280 // version
281 if (name() == tagVERSION) {
282 if (readElementText() != beerXMLVersion) {
283 qWarning() << "Error: Wrong BeerXML version";
284 return Grain();
285 }
286 }
287
288 // name
289 else if (name() == tagNAME) {
290 grain.setName(readElementText());
291 }
292
293 // amount
294 else if (name() == tagAMOUNT) {
295 buf = readElementText();
296 grain.setWeight(Weight(buf.toDouble(), Weight::kilogram));
297 }
298
299 // yield
300 else if (name() == tagYIELD) {
301 buf = readElementText();
302 grain.setExtract(Recipe::yieldToExtract(buf.toDouble() / 100.0));
303 }
304
305 // color
306 else if (name() == tagCOLOR) {
307 buf = readElementText();
308 grain.setColor(buf.toDouble());
309 }
310
311 // type (and guess at use)
312 else if (name() == tagTYPE) {
313 buf = readElementText();
314 grain.setType(Grain::OTHER_STRING);
315 grain.setUse(Grain::OTHER_STRING);
316 if (buf == "Grain") {
317 grain.setType(Grain::GRAIN_STRING);
318 }
319 if (buf == "Sugar") {
320 grain.setType(Grain::SUGAR_STRING);
321 grain.setUse(Grain::OTHER_STRING);
322 }
323 if ((buf == "Extract") ||
324 (buf == "Dry Extract")) {
325 grain.setType(Grain::EXTRACT_STRING);
326 grain.setUse(Grain::EXTRACT_STRING);
327 }
328 if (buf== "Adjunct") {
329 grain.setType(Grain::ADJUNCT_STRING);
330 grain.setUse(Grain::MASHED_STRING);
331 }
332 }
333 }
334
335 else if (isEndElement()) {
336 if (name() == tagFERMENTABLE) break;
337 }
338 }
339
340 return grain;
341 }
342
343 // read hop
344 Hop BeerXmlReader::readHop()
345 {
346 QString buf;
347 Hop hop;
348
349 // TODO: add USE to future hop notes
350
351 while (!atEnd()) {
352 readNext();
353 if (isStartElement()) {
354 // version
355 if (name() == tagVERSION) {
356 if (readElementText() != beerXMLVersion) {
357 qWarning() << "Error: Wrong BeerXML version";
358 return hop;
359 }
360 }
361
362 // name
363 if (name() == tagNAME) {
364 hop.setName(readElementText());
365 }
366
367 // amount
368 else if (name() == tagAMOUNT) {
369 buf = readElementText();
370 hop.setWeight(Weight(buf.toDouble(), Weight::kilogram));
371 }
372
373 // alpha
374 else if (name() == tagALPHA) {
375 buf = readElementText();
376 hop.setAlpha(buf.toDouble());
377 }
378
379 // time
380 else if (name() == tagTIME) {
381 buf = readElementText();
382 hop.setTime((int)buf.toDouble());
383 }
384
385 // form
386 else if (name() == tagFORM) {
387 buf = readElementText();
388 if (buf == "Pellet") {
389 hop.setType(Hop::PELLET_STRING);
390 } else if (buf == "Plug") {
391 hop.setType(Hop::PLUG_STRING);
392 } else {
393 hop.setType(Hop::WHOLE_STRING);
394 }
395 }
396
397
398
399 }
400 else if (isEndElement()) {
401 if (name() == tagHOP) break;
402 }
403 }
404
405 return hop;
406 }
407
408 // read misc ingredient
409 Misc BeerXmlReader::readMisc()
410 {
411 QString buf;
412 Misc misc;
413 double temp_amount = 0;
414 bool temp_aiw = false;
415
416 // TODO: put TYPE and USE in notes
417
418 while (!atEnd()) {
419 readNext();
420 if (isStartElement()) {
421 // version
422 if (name() == tagVERSION) {
423 if (readElementText() != beerXMLVersion) {
424 qWarning() << "Error: Wrong BeerXML version";
425 return misc;
426 }
427 }
428
429 // name
430 if (name() == tagNAME) {
431 misc.setName(readElementText());
432 }
433
434 // amount
435 else if (name() == tagAMOUNT) {
436 buf = readElementText();
437 temp_amount = buf.toDouble();
438 }
439
440 // amount is weight
441 else if (name() == tagAMOUNTISWEIGHT) {
442 buf = readElementText();
443 temp_aiw = (buf.toLower() == "true");
444 }
445
446 // notes
447 if (name() == tagNOTES) {
448 misc.setNotes(readElementText());
449 }
450 // type
451 else if (name() == tagTYPE) {
452 buf = readElementText();
453 misc.setType(Misc::OTHER_STRING);
454 if (buf == "Spice") misc.setType(Misc::SPICE_STRING);
455 if (buf == "Fining") misc.setType(Misc::FINING_STRING);
456 if (buf == "Herb") misc.setType(Misc::HERB_STRING);
457 if (buf == "Flavor") misc.setType(Misc::FLAVOR_STRING);
458 if (buf == "Water Agent") misc.setType(Misc::ADDITIVE_STRING);
459 }
460 }
461 else if (isEndElement()) {
462 if (name() == tagMISC) break;
463 }
464 }
465
466 if (temp_aiw) {
467 misc.setQuantity(Weight(temp_amount, Weight::kilogram));
468 } else {
469 misc.setQuantity(Volume(temp_amount, Volume::liter));
470 }
471
472 return misc;
473 }
474
475 // read yeast
476 Misc BeerXmlReader::readYeast()
477 {
478 QString buf, notes;
479 Misc misc;
480 double temp_amount = 0;
481 bool temp_aiw = false;
482 misc.setType(Misc::YEAST_STRING);
483
484 while (!atEnd()) {
485 readNext();
486 if (isStartElement()) {
487 // version
488 if (name() == tagVERSION) {
489 if (readElementText() != beerXMLVersion) {
490 qWarning() << "Error: Wrong BeerXML version";
491 return misc;
492 }
493 }
494
495 // name
496 if (name() == tagNAME) {
497 misc.setName(readElementText());
498 }
499
500 // amount
501 else if (name() == tagAMOUNT) {
502 buf = readElementText();
503 temp_amount = buf.toDouble();
504 }
505
506 // amount is weight
507 else if (name() == tagAMOUNTISWEIGHT) {
508 buf = readElementText();
509 temp_aiw = (buf.toLower() == "true");
510 }
511
512 // notes
513 if (name() == tagNOTES) {
514 if (!notes.isEmpty()) notes += ' ';
515 notes += readElementText();
516 }
517
518 // form
519 else if (name() == tagFORM) {
520 if (!notes.isEmpty()) notes += ' ';
521 notes += readElementText() + " yeast.";
522 }
523 }
524 else if (isEndElement()) {
525 if (name() == tagYEAST) break;
526 }
527 }
528
529 misc.setNotes(notes);
530 if (temp_aiw) {
531 misc.setQuantity(Weight(temp_amount, Weight::kilogram));
532 } else {
533 misc.setQuantity(Volume(temp_amount, Volume::liter));
534 }
535
536 return misc;
537 }
538
539 // read style from xml
540 QString BeerXmlReader::readStyle()
541 {
542 // the only thing I care about is name
543 QString stylename;
544 while (!atEnd()) {
545 readNext();
546 if (isStartElement()) {
547 if (name() == tagNAME) {
548 stylename = readElementText();
549 }
550 }
551
552 else if (isEndElement()) {
553 if (name() == tagSTYLE) break;
554 }
555 }
556 return stylename;
557 }
558
559 // skip over xml element
560 void BeerXmlReader::skipElement()
561 {
562 if (isStartElement()) {
563 int level = 0;
564 while (!atEnd()) {
565 readNext();
566
567 if (isStartElement()) level++;
568 if (isEndElement()) level--;
569
570 if (level < 0) break;
571 }
572 }
573 }
574
575 // BeerXmlWriter /////////////////////////////////////////////////////////////
576
577 //////////////////////////////////////////////////////////////////////////////
578 // RecipeWriter()
579 // --------------
580 // Constructor
581
582 BeerXmlWriter::BeerXmlWriter(QIODevice *device)
583 : QXmlStreamWriter(device)
584 {
585 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
586 setAutoFormatting(true);
587 setAutoFormattingIndent(2);
588 #endif
589 }
590
591 //////////////////////////////////////////////////////////////////////////////
592 // writeRecipe()
593 // -------------
594 // Write out recipe
595
596 bool BeerXmlWriter::writeRecipe(Recipe *recipe)
597 {
598 QString buf;
599
600 writeStartDocument(); // BeerXML 1 doesn't have a doctype
601 writeComment(QString("BeerXML generated by %1 %2")
602 .arg(PACKAGE).arg(VERSION));
603 writeStartElement(tagRECIPES);
604
605 // write recipe information
606 writeStartElement(tagRECIPE);
607 writeTextElement(tagVERSION, beerXMLVersion);
608 writeTextElement(tagNAME, recipe->title());
609 writeTextElement(tagTYPE, recipe->method());
610 writeTextElement(tagBREWER, recipe->brewer());
611 double size = recipe->size().amount(Volume::liter);
612 writeTextElement(tagBATCHSIZE, QString::number(size,'f',8));
613 writeTextElement(tagEFFICIENCY,
614 QString::number(Data::instance()->efficiency()*100.0,'f',1));
615
616 // write style
617 // TODO: unfinished...
618 // category
619 // category number
620 // style letter
621 // style guide
622 // type
623 // og
624 writeStartElement(tagSTYLE);
625 writeTextElement(tagVERSION, beerXMLVersion);
626 writeTextElement(tagNAME, recipe->style().name());
627 writeTextElement(tagOGMIN, QString::number(recipe->style().OGLow(),'f',4));
628 writeTextElement(tagOGMAX, QString::number(recipe->style().OGHi(),'f',4));
629 writeTextElement(tagFGMIN, QString::number(recipe->style().FGLow(),'f',4));
630 writeTextElement(tagFGMAX, QString::number(recipe->style().FGHi(),'f',4));
631 writeTextElement(tagIBUMIN, QString::number(recipe->style().IBULow(),'f',2));
632 writeTextElement(tagIBUMAX, QString::number(recipe->style().IBUHi(),'f',2));
633 writeTextElement(tagCOLORMIN, QString::number(recipe->style().SRMLow(),'f',2));
634 writeTextElement(tagCOLORMAX, QString::number(recipe->style().SRMHi(),'f',2));
635 writeEndElement(); // tagSTYLE
636
637 // fermentables list
638 writeStartElement(tagFERMENTABLES);
639 foreach (Grain grain, *recipe->grains()) {
640 writeStartElement(tagFERMENTABLE);
641 writeTextElement(tagVERSION, beerXMLVersion);
642 writeTextElement(tagNAME, grain.name());
643 size = grain.weight().amount(Weight::kilogram);
644 writeTextElement(tagAMOUNT, QString::number(size,'f',8));
645 writeTextElement(tagCOLOR, QString::number(grain.color(),'f',2));
646 double yield = recipe->extractToYield(grain.extract()) * 100.0;
647 writeTextElement(tagYIELD, QString::number(yield,'f',2));
648 writeTextElement(tagTYPE, grain.type());
649 writeEndElement(); // tagFERMENTABLE
650 }
651 writeEndElement(); // tagFERMENTABLES
652
653 // hop list
654 writeStartElement(tagHOPS);
655 foreach (Hop hop, *recipe->hops()) {
656 writeStartElement(tagHOP);
657 writeTextElement(tagVERSION, beerXMLVersion);
658 writeTextElement(tagNAME, hop.name());
659 size = hop.weight().amount(Weight::kilogram);
660 writeTextElement(tagAMOUNT, QString::number(size,'f',8));
661 writeTextElement(tagALPHA, QString::number(hop.alpha(),'f',2));
662 // TODO: note that I'm using "Boil" for all hops...
663 writeTextElement(tagUSE, "Boil");
664 writeTextElement(tagTIME, QString::number(hop.time()));
665 buf = "Leaf";
666 if (hop.type() == Hop::PELLET_STRING) buf = "Pellet";
667 if (hop.type() == Hop::PLUG_STRING) buf = "Plug";
668 writeTextElement(tagFORM, buf);
669 writeEndElement(); // tagHOP
670 }
671 writeEndElement(); // tagHOPS
672
673 // yeast list
674 writeStartElement(tagYEASTS);
675 foreach (Misc misc, *recipe->miscs()) {
676 // iterate through list looking for yeasts
677 if (misc.type() == Misc::YEAST_STRING) {
678 writeStartElement(tagYEAST);
679 writeTextElement(tagVERSION, beerXMLVersion);
680 writeTextElement(tagNAME, misc.name());
681 writeTextElement(tagAMOUNT, "0.00"); // TODO: fixup for miscs
682 writeTextElement(tagTYPE, Misc::YEAST_STRING);
683 // TODO: form???
684 writeTextElement(tagNOTES, misc.notes());
685 writeEndElement(); // tagYEAST
686 }
687 }
688 writeEndElement(); // tagYEASTS
689
690 // misc list
691 writeStartElement(tagMISCS);
692 foreach (Misc misc, *recipe->miscs()) {
693 // iterate through list looking for yeasts
694 if (misc.type() != Misc::YEAST_STRING) {
695 writeStartElement(tagMISC);
696 writeTextElement(tagVERSION, beerXMLVersion);
697 writeTextElement(tagNAME, misc.name());
698 writeTextElement(tagAMOUNT, "0.00"); // TODO: fixup for miscs
699 writeTextElement(tagTYPE, misc.type());
700 writeTextElement(tagNOTES, misc.notes());
701 // use ???
702 // time ???
703 writeEndElement(); // tagMISC
704 }
705 }
706 writeEndElement(); // tagMISCS
707
708 // waters
709 // NOTE: not currently supporting water
710 writeStartElement(tagWATERS);
711 writeEndElement();
712
713 // mash
714 // NOTE: not currently supporting mash
715
716 // notes
717 if (!(recipe->recipeNotes().isEmpty() && recipe->batchNotes().isEmpty())) {
718 buf = recipe->recipeNotes();
719 if (!recipe->batchNotes().isEmpty()) {
720 if (!buf.isEmpty()) buf += "\n\n";
721 buf += recipe->batchNotes();
722 }
723 writeTextElement(tagNOTES, buf);
724 }
725
726 writeEndElement(); // tagRECIPE
727 writeEndElement(); // tagRECIPES
728
729 return true;
730 }