Debian Codegraph @

src/beerxmlreader.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 return false;
258 }
259
260 // set grain use according to recipe type
261 for (int n=0; n<recipe.grains()->count(); n++) {
262 if (mashed) (*recipe.grains())[n].setUse(Grain::MASHED_STRING);
263 else (*recipe.grains())[n].setUse(Grain::STEEPED_STRING);
264 }
265
266 return recipe;
267 }
268
269 // read grain
270 Grain BeerXmlReader::readFermentable()
271 {
272 QString buf;
273 Grain grain;
274
275 while (!atEnd()) {
276 readNext();
277 if (isStartElement()) {
278 // version
279 if (name() == tagVERSION) {
280 if (readElementText() != beerXMLVersion) {
281 qWarning() << "Error: Wrong BeerXML version";
282 return Grain();
283 }
284 }
285
286 // name
287 else if (name() == tagNAME) {
288 grain.setName(readElementText());
289 }
290
291 // amount
292 else if (name() == tagAMOUNT) {
293 buf = readElementText();
294 grain.setWeight(Weight(buf.toDouble(), Weight::kilogram));
295 }
296
297 // yield
298 else if (name() == tagYIELD) {
299 buf = readElementText();
300 grain.setExtract(Recipe::yieldToExtract(buf.toDouble() / 100.0));
301 }
302
303 // color
304 else if (name() == tagCOLOR) {
305 buf = readElementText();
306 grain.setColor(buf.toDouble());
307 }
308
309 // type (and guess at use)
310 else if (name() == tagTYPE) {
311 buf = readElementText();
312 grain.setType(Grain::OTHER_STRING);
313 grain.setUse(Grain::OTHER_STRING);
314 if (buf == "Grain") {
315 grain.setType(Grain::GRAIN_STRING);
316 }
317 if (buf == "Sugar") {
318 grain.setType(Grain::SUGAR_STRING);
319 grain.setUse(Grain::OTHER_STRING);
320 }
321 if ((buf == "Extract") ||
322 (buf == "Dry Extract")) {
323 grain.setType(Grain::EXTRACT_STRING);
324 grain.setUse(Grain::EXTRACT_STRING);
325 }
326 if (buf== "Adjunct") {
327 grain.setType(Grain::ADJUNCT_STRING);
328 grain.setUse(Grain::MASHED_STRING);
329 }
330 }
331 }
332
333 else if (isEndElement()) {
334 if (name() == tagFERMENTABLE) break;
335 }
336 }
337
338 return grain;
339 }
340
341 // read hop
342 Hop BeerXmlReader::readHop()
343 {
344 QString buf;
345 Hop hop;
346
347 // TODO: add USE to future hop notes
348
349 while (!atEnd()) {
350 readNext();
351 if (isStartElement()) {
352 // version
353 if (name() == tagVERSION) {
354 if (readElementText() != beerXMLVersion) {
355 qWarning() << "Error: Wrong BeerXML version";
356 return hop;
357 }
358 }
359
360 // name
361 if (name() == tagNAME) {
362 hop.setName(readElementText());
363 }
364
365 // amount
366 else if (name() == tagAMOUNT) {
367 buf = readElementText();
368 hop.setWeight(Weight(buf.toDouble(), Weight::kilogram));
369 }
370
371 // alpha
372 else if (name() == tagALPHA) {
373 buf = readElementText();
374 hop.setAlpha(buf.toDouble());
375 }
376
377 // time
378 else if (name() == tagTIME) {
379 buf = readElementText();
380 hop.setTime((int)buf.toDouble());
381 }
382
383 // form
384 else if (name() == tagFORM) {
385 buf = readElementText();
386 if (buf == "Pellet") {
387 hop.setType(Hop::PELLET_STRING);
388 } else if (buf == "Plug") {
389 hop.setType(Hop::PLUG_STRING);
390 } else {
391 hop.setType(Hop::WHOLE_STRING);
392 }
393 }
394
395
396
397 }
398 else if (isEndElement()) {
399 if (name() == tagHOP) break;
400 }
401 }
402
403 return hop;
404 }
405
406 // read misc ingredient
407 Misc BeerXmlReader::readMisc()
408 {
409 QString buf;
410 Misc misc;
411 double temp_amount = 0;
412 bool temp_aiw = false;
413
414 // TODO: put TYPE and USE in notes
415
416 while (!atEnd()) {
417 readNext();
418 if (isStartElement()) {
419 // version
420 if (name() == tagVERSION) {
421 if (readElementText() != beerXMLVersion) {
422 qWarning() << "Error: Wrong BeerXML version";
423 return misc;
424 }
425 }
426
427 // name
428 if (name() == tagNAME) {
429 misc.setName(readElementText());
430 }
431
432 // amount
433 else if (name() == tagAMOUNT) {
434 buf = readElementText();
435 temp_amount = buf.toDouble();
436 }
437
438 // amount is weight
439 else if (name() == tagAMOUNTISWEIGHT) {
440 buf = readElementText();
441 temp_aiw = (buf.toLower() == "true");
442 }
443
444 // notes
445 if (name() == tagNOTES) {
446 misc.setNotes(readElementText());
447 }
448 // type
449 else if (name() == tagTYPE) {
450 buf = readElementText();
451 misc.setType(Misc::OTHER_STRING);
452 if (buf == "Spice") misc.setType(Misc::SPICE_STRING);
453 if (buf == "Fining") misc.setType(Misc::FINING_STRING);
454 if (buf == "Herb") misc.setType(Misc::HERB_STRING);
455 if (buf == "Flavor") misc.setType(Misc::FLAVOR_STRING);
456 if (buf == "Water Agent") misc.setType(Misc::ADDITIVE_STRING);
457 }
458 }
459 else if (isEndElement()) {
460 if (name() == tagMISC) break;
461 }
462 }
463
464 if (temp_aiw) {
465 misc.setQuantity(Weight(temp_amount, Weight::kilogram));
466 } else {
467 misc.setQuantity(Volume(temp_amount, Volume::liter));
468 }
469
470 return misc;
471 }
472
473 // read yeast
474 Misc BeerXmlReader::readYeast()
475 {
476 QString buf, notes;
477 Misc misc;
478 double temp_amount = 0;
479 bool temp_aiw = false;
480 misc.setType(Misc::YEAST_STRING);
481
482 while (!atEnd()) {
483 readNext();
484 if (isStartElement()) {
485 // version
486 if (name() == tagVERSION) {
487 if (readElementText() != beerXMLVersion) {
488 qWarning() << "Error: Wrong BeerXML version";
489 return misc;
490 }
491 }
492
493 // name
494 if (name() == tagNAME) {
495 misc.setName(readElementText());
496 }
497
498 // amount
499 else if (name() == tagAMOUNT) {
500 buf = readElementText();
501 temp_amount = buf.toDouble();
502 }
503
504 // amount is weight
505 else if (name() == tagAMOUNTISWEIGHT) {
506 buf = readElementText();
507 temp_aiw = (buf.toLower() == "true");
508 }
509
510 // notes
511 if (name() == tagNOTES) {
512 if (!notes.isEmpty()) notes += ' ';
513 notes += readElementText();
514 }
515
516 // form
517 else if (name() == tagFORM) {
518 if (!notes.isEmpty()) notes += ' ';
519 notes += readElementText() + " yeast.";
520 }
521 }
522 else if (isEndElement()) {
523 if (name() == tagYEAST) break;
524 }
525 }
526
527 misc.setNotes(notes);
528 if (temp_aiw) {
529 misc.setQuantity(Weight(temp_amount, Weight::kilogram));
530 } else {
531 misc.setQuantity(Volume(temp_amount, Volume::liter));
532 }
533
534 return misc;
535 }
536
537 // read style from xml
538 QString BeerXmlReader::readStyle()
539 {
540 // the only thing I care about is name
541 QString stylename;
542 while (!atEnd()) {
543 readNext();
544 if (isStartElement()) {
545 if (name() == tagNAME) {
546 stylename = readElementText();
547 }
548 }
549
550 else if (isEndElement()) {
551 if (name() == tagSTYLE) break;
552 }
553 }
554 return stylename;
555 }
556
557 // skip over xml element
558 void BeerXmlReader::skipElement()
559 {
560 if (isStartElement()) {
561 int level = 0;
562 while (!atEnd()) {
563 readNext();
564
565 if (isStartElement()) level++;
566 if (isEndElement()) level--;
567
568 if (level < 0) break;
569 }
570 }
571 }
572
573 // BeerXmlWriter /////////////////////////////////////////////////////////////
574
575 //////////////////////////////////////////////////////////////////////////////
576 // RecipeWriter()
577 // --------------
578 // Constructor
579
580 BeerXmlWriter::BeerXmlWriter(QIODevice *device)
581 : QXmlStreamWriter(device)
582 {
583 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
584 setAutoFormatting(true);
585 setAutoFormattingIndent(2);
586 #endif
587 }
588
589 //////////////////////////////////////////////////////////////////////////////
590 // writeRecipe()
591 // -------------
592 // Write out recipe
593
594 bool BeerXmlWriter::writeRecipe(Recipe *recipe)
595 {
596 QString buf;
597
598 writeStartDocument(); // BeerXML 1 doesn't have a doctype
599 writeComment(QString("BeerXML generated by %1 %2")
600 .arg(PACKAGE).arg(VERSION));
601 writeStartElement(tagRECIPES);
602
603 // write recipe information
604 writeStartElement(tagRECIPE);
605 writeTextElement(tagVERSION, beerXMLVersion);
606 writeTextElement(tagNAME, recipe->title());
607 writeTextElement(tagTYPE, recipe->method());
608 writeTextElement(tagBREWER, recipe->brewer());
609 double size = recipe->size().amount(Volume::liter);
610 writeTextElement(tagBATCHSIZE, QString::number(size,'f',8));
611 writeTextElement(tagEFFICIENCY,
612 QString::number(Data::instance()->efficiency()*100.0,'f',1));
613
614 // write style
615 // TODO: unfinished...
616 // category
617 // category number
618 // style letter
619 // style guide
620 // type
621 // og
622 writeStartElement(tagSTYLE);
623 writeTextElement(tagVERSION, beerXMLVersion);
624 writeTextElement(tagNAME, recipe->style().name());
625 writeTextElement(tagOGMIN, QString::number(recipe->style().OGLow(),'f',4));
626 writeTextElement(tagOGMAX, QString::number(recipe->style().OGHi(),'f',4));
627 writeTextElement(tagFGMIN, QString::number(recipe->style().FGLow(),'f',4));
628 writeTextElement(tagFGMAX, QString::number(recipe->style().FGHi(),'f',4));
629 writeTextElement(tagIBUMIN, QString::number(recipe->style().IBULow(),'f',2));
630 writeTextElement(tagIBUMAX, QString::number(recipe->style().IBUHi(),'f',2));
631 writeTextElement(tagCOLORMIN, QString::number(recipe->style().SRMLow(),'f',2));
632 writeTextElement(tagCOLORMAX, QString::number(recipe->style().SRMHi(),'f',2));
633 writeEndElement(); // tagSTYLE
634
635 // fermentables list
636 writeStartElement(tagFERMENTABLES);
637 foreach (Grain grain, *recipe->grains()) {
638 writeStartElement(tagFERMENTABLE);
639 writeTextElement(tagVERSION, beerXMLVersion);
640 writeTextElement(tagNAME, grain.name());
641 size = grain.weight().amount(Weight::kilogram);
642 writeTextElement(tagAMOUNT, QString::number(size,'f',8));
643 writeTextElement(tagCOLOR, QString::number(grain.color(),'f',2));
644 double yield = recipe->extractToYield(grain.extract()) * 100.0;
645 writeTextElement(tagYIELD, QString::number(yield,'f',2));
646 writeTextElement(tagTYPE, grain.type());
647 writeEndElement(); // tagFERMENTABLE
648 }
649 writeEndElement(); // tagFERMENTABLES
650
651 // hop list
652 writeStartElement(tagHOPS);
653 foreach (Hop hop, *recipe->hops()) {
654 writeStartElement(tagHOP);
655 writeTextElement(tagVERSION, beerXMLVersion);
656 writeTextElement(tagNAME, hop.name());
657 size = hop.weight().amount(Weight::kilogram);
658 writeTextElement(tagAMOUNT, QString::number(size,'f',8));
659 writeTextElement(tagALPHA, QString::number(hop.alpha(),'f',2));
660 // TODO: note that I'm using "Boil" for all hops...
661 writeTextElement(tagUSE, "Boil");
662 writeTextElement(tagTIME, QString::number(hop.time()));
663 buf = "Leaf";
664 if (hop.type() == Hop::PELLET_STRING) buf = "Pellet";
665 if (hop.type() == Hop::PLUG_STRING) buf = "Plug";
666 writeTextElement(tagFORM, buf);
667 writeEndElement(); // tagHOP
668 }
669 writeEndElement(); // tagHOPS
670
671 // yeast list
672 writeStartElement(tagYEASTS);
673 foreach (Misc misc, *recipe->miscs()) {
674 // iterate through list looking for yeasts
675 if (misc.type() == Misc::YEAST_STRING) {
676 writeStartElement(tagYEAST);
677 writeTextElement(tagVERSION, beerXMLVersion);
678 writeTextElement(tagNAME, misc.name());
679 writeTextElement(tagAMOUNT, "0.00"); // TODO: fixup for miscs
680 writeTextElement(tagTYPE, Misc::YEAST_STRING);
681 // TODO: form???
682 writeTextElement(tagNOTES, misc.notes());
683 writeEndElement(); // tagYEAST
684 }
685 }
686 writeEndElement(); // tagYEASTS
687
688 // misc list
689 writeStartElement(tagMISCS);
690 foreach (Misc misc, *recipe->miscs()) {
691 // iterate through list looking for yeasts
692 if (misc.type() != Misc::YEAST_STRING) {
693 writeStartElement(tagMISC);
694 writeTextElement(tagVERSION, beerXMLVersion);
695 writeTextElement(tagNAME, misc.name());
696 writeTextElement(tagAMOUNT, "0.00"); // TODO: fixup for miscs
697 writeTextElement(tagTYPE, misc.type());
698 writeTextElement(tagNOTES, misc.notes());
699 // use ???
700 // time ???
701 writeEndElement(); // tagMISC
702 }
703 }
704 writeEndElement(); // tagMISCS
705
706 // waters
707 // NOTE: not currently supporting water
708 writeStartElement(tagWATERS);
709 writeEndElement();
710
711 // mash
712 // NOTE: not currently supporting mash
713
714 // notes
715 if (!(recipe->recipeNotes().isEmpty() && recipe->batchNotes().isEmpty())) {
716 buf = recipe->recipeNotes();
717 if (!recipe->batchNotes().isEmpty()) {
718 if (!buf.isEmpty()) buf += "\n\n";
719 buf += recipe->batchNotes();
720 }
721 writeTextElement(tagNOTES, buf);
722 }
723
724 writeEndElement(); // tagRECIPE
725 writeEndElement(); // tagRECIPES
726
727 return true;
728 }