File: beerxmlreader.cpp

package info (click to toggle)
qbrew 0.4.1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 1,724 kB
  • ctags: 1,355
  • sloc: cpp: 7,503; sh: 414; makefile: 18
file content (728 lines) | stat: -rw-r--r-- 21,514 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/***************************************************************************
  beerxmlreader.cpp
  -------------------
  XML stream reader for BeerXML
  -------------------
  Copyright 2008, David Johnson
  Please see the header file for copyright and license information
 ***************************************************************************/

#include <QApplication>
#include <QInputDialog>
#include "data.h"
#include "recipe.h"
#include "resource.h"

#include "beerxmlreader.h"

using namespace Resource;

// BeerXmlReader /////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
// BeerXmlReader()
// ---------------
// Constructor

BeerXmlReader::BeerXmlReader(QIODevice *device)
    : QXmlStreamReader(device)
{
}

/////////////////////////////////////////////////////////////////////////////
// isBeerXmlFormat()
// -----------------
// Is xml BeerXML format?
// Defined as XML with "<RECIPES>" and "<RECIPE>", VERSION 1
// Note that BeerXML 1.0 is poorly designed format

bool BeerXmlReader::isBeerXmlFormat()
{
    // find root/first element
    do {
        readNext();
    } while (!atEnd() && !isStartElement());
    if (hasError()) return false;

    // check the document type
    if (name() != tagRECIPES) return false;

    // check for RECIPE
    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            if (name() == tagRECIPE) {
                // check VERSION
                while (!atEnd()) {
                    readNext();
                    if (isStartElement()) {
                        if (name() == tagVERSION) {
                            if (readElementText() == beerXMLVersion)
                                return true;
                        }
                    }
                }
            }
        }
    }

    return false;
}

/////////////////////////////////////////////////////////////////////////////
// readRecipe()
// ------------
// Read in recipe

bool BeerXmlReader::readRecipe(Recipe *recipe)
{
    QString buf;

    // BeerXML may have more than one recipe,
    // so we read each one into a different recipe object
    QMap<QString, Recipe> recipes;
    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            if (name() == tagRECIPES) {
                while (!atEnd()) {
                    readNext();
                    if (isStartElement()) {
                        if (name() == tagRECIPE) {
                            Recipe r = readSingleRecipe();
                            if (!r.title().isEmpty()) {
                                recipes[r.title()] = r;
                            }
                        } else {
                            skipElement();
                        }
                    }
                }
            } else {
                qWarning() << "Warning: Unknown tag" << name().toString();
                skipElement();
            }
        }
    }
    if (hasError()) return false;

    if (recipes.count() == 0) {
        raiseError("No recipes found");
        return false;
    }

    // select just one recipe
    if (recipes.count() > 1) {
        bool ok;
        QString name = QInputDialog::getItem(0, TITLE,
            QObject::tr("Multiple recipes found. Please select one:"),
            recipes.keys(), 0, false, &ok);
        if (ok) {
            // copy over recipe
            *recipe = recipes[name];
        }
    } else {
        // only one recipe
        *recipe = recipes.begin().value();
    }

    return true;
}

//////////////////////////////////////////////////////////////////////////////
// readSingleRecipe()
// ------------------
// Read an individual recipe from xml

Recipe BeerXmlReader::readSingleRecipe()
{
    Recipe recipe(0);
    QString buf;
    bool mashed = false;
    bool grainsflag=false, hopsflag=false, miscsflag=false, yeastsflag=false;

    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            // version
            if (name() == tagVERSION) {
                if (readElementText() != beerXMLVersion) {
                    qWarning() << "Wrong BeerXML version";
                    return Recipe();
                }
            }

            // name
            else if (name() == tagNAME) {
                recipe.setTitle(readElementText());
            }

            // brewer
            else if (name() == tagBREWER) {
                recipe.setBrewer(readElementText());
            }

            // style
            else if (name() == tagSTYLE) {
                recipe.setStyle(readStyle());
            }

            // mashed
            else if (name() == tagTYPE) {
                mashed = (readElementText() != Grain::EXTRACT_STRING);
            }

            // batch size
            else if (name() == tagBATCHSIZE) {
                buf = readElementText();
                recipe.setSize(Volume(buf, Volume::liter));
            }

            // notes
            else if (name() == tagNOTES) {
                recipe.setRecipeNotes(readElementText());
            }

            else if (name() == tagTASTENOTES) {
                recipe.setBatchNotes(readElementText());
            }

            // fermentables
            else if (name() == tagFERMENTABLES) {
                grainsflag = true;
            }
            // fermentable
            else if (name() == tagFERMENTABLE) {
                if (!grainsflag) qWarning("Warning: mislocated fermentable tag");
                recipe.addGrain(readFermentable());
            }

            // hops
            else if (name() == tagHOPS) {
                hopsflag = true;
            }
            // hop
            else if (name() == tagHOP) {
                if (!hopsflag) qWarning("Warning: mislocated hop tag");
                recipe.addHop(readHop());
            }

            // miscs
            else if (name() == tagMISCS) {
                miscsflag = true;
            }
            // misc
            else if (name() == tagMISC) {
                if (!miscsflag) qWarning("Warning: mislocated misc tag");
                recipe.addMisc(readMisc());
            }

            // yeasts
            else if (name() == tagYEASTS) {
                yeastsflag = true;
            }
            // yeast
            else if (name() == tagYEAST) {
                if (!yeastsflag) qWarning("Warning: mislocated yeast tag");
                recipe.addMisc(readYeast());
            }

            else if (name() == tagEFFICIENCY) {
                // unused, ignore tag...
                skipElement();
            }

            else if (name() == tagWATERS) {
                // unused, ignore tag...
                skipElement();
            }

            // unknown tag, skip
            else {
                skipElement();
            }
        }

        else if (isEndElement()) {
            if (name() == tagRECIPE) break;
            // unset flags
            else if (name() == tagFERMENTABLES) grainsflag = false;
            else if (name() == tagHOPS) hopsflag = false;
            else if (name() == tagMISCS) miscsflag = false;
            else if (name() == tagYEASTS) yeastsflag = false;
        }
    }
    if (hasError()) {
        raiseError("Problem reading recipe");
        return false;
    }

    // set grain use according to recipe type
    for (int n=0; n<recipe.grains()->count(); n++) {
        if (mashed) (*recipe.grains())[n].setUse(Grain::MASHED_STRING);
        else        (*recipe.grains())[n].setUse(Grain::STEEPED_STRING);
    }

    return recipe;
}

// read grain
Grain BeerXmlReader::readFermentable()
{
    QString buf;
    Grain grain;

    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            // version
            if (name() == tagVERSION) {
                if (readElementText() != beerXMLVersion) {
                    qWarning() << "Error: Wrong BeerXML version";
                    return Grain();
                }
            }

            // name
            else if (name() == tagNAME) {
                grain.setName(readElementText());
            }

            // amount
            else if (name() == tagAMOUNT) {
                buf = readElementText();
                grain.setWeight(Weight(buf.toDouble(), Weight::kilogram));
            }

            // yield
            else if (name() == tagYIELD) {
                buf = readElementText();
                grain.setExtract(Recipe::yieldToExtract(buf.toDouble() / 100.0));
            }

            // color
            else if (name() == tagCOLOR) {
                buf = readElementText();
                grain.setColor(buf.toDouble());
            }

            // type (and guess at use)
            else if (name() == tagTYPE) {
                buf = readElementText();
                grain.setType(Grain::OTHER_STRING);
                grain.setUse(Grain::OTHER_STRING);
                if (buf == "Grain") {
                    grain.setType(Grain::GRAIN_STRING);
                }
                if (buf == "Sugar") {
                    grain.setType(Grain::SUGAR_STRING);
                    grain.setUse(Grain::OTHER_STRING);
                }
                if ((buf == "Extract") ||
                    (buf == "Dry Extract")) {
                    grain.setType(Grain::EXTRACT_STRING);
                    grain.setUse(Grain::EXTRACT_STRING);
                }
                if (buf== "Adjunct") {
                    grain.setType(Grain::ADJUNCT_STRING);
                    grain.setUse(Grain::MASHED_STRING);
                }
            }
        }

        else if (isEndElement()) {
            if (name() == tagFERMENTABLE) break;
        }
    }

    return grain;
}

// read hop
Hop BeerXmlReader::readHop()
{
    QString buf;
    Hop hop;

    // TODO: add USE to future hop notes

    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            // version
            if (name() == tagVERSION) {
                if (readElementText() != beerXMLVersion) {
                    qWarning() << "Error: Wrong BeerXML version";
                    return hop;
                }
            }

            // name
            if (name() == tagNAME) {
                hop.setName(readElementText());
            }

            // amount
            else if (name() == tagAMOUNT) {
                buf = readElementText();
                hop.setWeight(Weight(buf.toDouble(), Weight::kilogram));
            }

            // alpha
            else if (name() == tagALPHA) {
                buf = readElementText();
                hop.setAlpha(buf.toDouble());
            }

            // time
            else if (name() == tagTIME) {
                buf = readElementText();
                hop.setTime((int)buf.toDouble());
            }

            // form
            else if (name() == tagFORM) {
                buf = readElementText();
                if (buf == "Pellet") {
                    hop.setType(Hop::PELLET_STRING);
                } else if (buf == "Plug") {
                    hop.setType(Hop::PLUG_STRING);
                } else {
                    hop.setType(Hop::WHOLE_STRING);
                }
            }



        }
        else if (isEndElement()) {
            if (name() == tagHOP) break;
        }
    }

    return hop;
}

// read misc ingredient
Misc BeerXmlReader::readMisc()
{
    QString buf;
    Misc misc;
    double temp_amount = 0;
    bool temp_aiw = false;

    // TODO: put TYPE and USE in notes

    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            // version
            if (name() == tagVERSION) {
                if (readElementText() != beerXMLVersion) {
                    qWarning() << "Error: Wrong BeerXML version";
                    return misc;
                }
            }

            // name
            if (name() == tagNAME) {
                misc.setName(readElementText());
            }

            // amount
            else if (name() == tagAMOUNT) {
                buf = readElementText();
                temp_amount = buf.toDouble();
            }

            // amount is weight
            else if (name() == tagAMOUNTISWEIGHT) {
                buf = readElementText();
                temp_aiw = (buf.toLower() == "true");
            }

            // notes
            if (name() == tagNOTES) {
                misc.setNotes(readElementText());
            }
            // type
            else if (name() == tagTYPE) {
                buf = readElementText();
                misc.setType(Misc::OTHER_STRING);
                if (buf == "Spice") misc.setType(Misc::SPICE_STRING);
                if (buf == "Fining") misc.setType(Misc::FINING_STRING);
                if (buf == "Herb") misc.setType(Misc::HERB_STRING);
                if (buf == "Flavor") misc.setType(Misc::FLAVOR_STRING);
                if (buf == "Water Agent") misc.setType(Misc::ADDITIVE_STRING);
            }
        }
        else if (isEndElement()) {
            if (name() == tagMISC) break;
        }
    }

    if (temp_aiw) {
        misc.setQuantity(Weight(temp_amount, Weight::kilogram));
    } else {
        misc.setQuantity(Volume(temp_amount, Volume::liter));
    }

    return misc;
}

// read yeast
Misc BeerXmlReader::readYeast()
{
    QString buf, notes;
    Misc misc;
    double temp_amount = 0;
    bool temp_aiw = false;
    misc.setType(Misc::YEAST_STRING);

    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            // version
            if (name() == tagVERSION) {
                if (readElementText() != beerXMLVersion) {
                    qWarning() << "Error: Wrong BeerXML version";
                    return misc;
                }
            }

            // name
            if (name() == tagNAME) {
                misc.setName(readElementText());
            }

            // amount
            else if (name() == tagAMOUNT) {
                buf = readElementText();
                temp_amount = buf.toDouble();
            }

            // amount is weight
            else if (name() == tagAMOUNTISWEIGHT) {
                buf = readElementText();
                temp_aiw = (buf.toLower() == "true");
            }

            // notes
            if (name() == tagNOTES) {
                if (!notes.isEmpty()) notes += ' ';
                notes += readElementText();
            }

            // form
            else if (name() == tagFORM) {
                if (!notes.isEmpty()) notes += ' ';
                notes += readElementText() + " yeast.";
            }
        }
        else if (isEndElement()) {
            if (name() == tagYEAST) break;
        }
    }

    misc.setNotes(notes);
    if (temp_aiw) {
        misc.setQuantity(Weight(temp_amount, Weight::kilogram));
    } else {
        misc.setQuantity(Volume(temp_amount, Volume::liter));
    }

    return misc;
}

// read style from xml
QString BeerXmlReader::readStyle()
{
    // the only thing I care about is name
    QString stylename;
    while (!atEnd()) {
        readNext();
        if (isStartElement()) {
            if (name() == tagNAME) {
                stylename = readElementText();
            }
        }

        else if (isEndElement()) {
            if (name() == tagSTYLE) break;
        }
    }
    return stylename;
}

// skip over xml element
void BeerXmlReader::skipElement()
{
    if (isStartElement()) {
        int level = 0;
        while (!atEnd()) {
            readNext();

            if (isStartElement()) level++;
            if (isEndElement()) level--;

            if (level < 0) break;
        }
    }
}

// BeerXmlWriter /////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// RecipeWriter()
// --------------
// Constructor

BeerXmlWriter::BeerXmlWriter(QIODevice *device)
    : QXmlStreamWriter(device)
{
#if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
    setAutoFormatting(true);
    setAutoFormattingIndent(2);
#endif
}

//////////////////////////////////////////////////////////////////////////////
// writeRecipe()
// -------------
// Write out recipe

bool BeerXmlWriter::writeRecipe(Recipe *recipe)
{
    QString buf;

    writeStartDocument(); // BeerXML 1 doesn't have a doctype
    writeComment(QString("BeerXML generated by %1 %2")
                     .arg(PACKAGE).arg(VERSION));
    writeStartElement(tagRECIPES);

    // write recipe information
    writeStartElement(tagRECIPE);
    writeTextElement(tagVERSION, beerXMLVersion);
    writeTextElement(tagNAME, recipe->title());
    writeTextElement(tagTYPE, recipe->method());
    writeTextElement(tagBREWER, recipe->brewer());
    double size = recipe->size().amount(Volume::liter);
    writeTextElement(tagBATCHSIZE, QString::number(size,'f',8));
    writeTextElement(tagEFFICIENCY,
        QString::number(Data::instance()->efficiency()*100.0,'f',1));

    // write style
    // TODO: unfinished...
    // category
    // category number
    // style letter
    // style guide
    // type
    // og
    writeStartElement(tagSTYLE);
    writeTextElement(tagVERSION, beerXMLVersion);
    writeTextElement(tagNAME, recipe->style().name());
    writeTextElement(tagOGMIN, QString::number(recipe->style().OGLow(),'f',4));
    writeTextElement(tagOGMAX, QString::number(recipe->style().OGHi(),'f',4));
    writeTextElement(tagFGMIN, QString::number(recipe->style().FGLow(),'f',4));
    writeTextElement(tagFGMAX, QString::number(recipe->style().FGHi(),'f',4));
    writeTextElement(tagIBUMIN, QString::number(recipe->style().IBULow(),'f',2));
    writeTextElement(tagIBUMAX, QString::number(recipe->style().IBUHi(),'f',2));
    writeTextElement(tagCOLORMIN, QString::number(recipe->style().SRMLow(),'f',2));
    writeTextElement(tagCOLORMAX, QString::number(recipe->style().SRMHi(),'f',2));
    writeEndElement(); // tagSTYLE

    // fermentables list
    writeStartElement(tagFERMENTABLES);
    foreach (Grain grain, *recipe->grains()) {
        writeStartElement(tagFERMENTABLE);
        writeTextElement(tagVERSION, beerXMLVersion);
        writeTextElement(tagNAME, grain.name());
        size = grain.weight().amount(Weight::kilogram);
        writeTextElement(tagAMOUNT, QString::number(size,'f',8));
        writeTextElement(tagCOLOR, QString::number(grain.color(),'f',2));
        double yield = recipe->extractToYield(grain.extract()) * 100.0;
        writeTextElement(tagYIELD, QString::number(yield,'f',2));
        writeTextElement(tagTYPE, grain.type());
        writeEndElement(); // tagFERMENTABLE
    }
    writeEndElement(); // tagFERMENTABLES

    // hop list
    writeStartElement(tagHOPS);
    foreach (Hop hop, *recipe->hops()) {
        writeStartElement(tagHOP);
        writeTextElement(tagVERSION, beerXMLVersion);
        writeTextElement(tagNAME, hop.name());
        size = hop.weight().amount(Weight::kilogram);
        writeTextElement(tagAMOUNT, QString::number(size,'f',8));
        writeTextElement(tagALPHA, QString::number(hop.alpha(),'f',2));
        // TODO: note that I'm using "Boil" for all hops...
        writeTextElement(tagUSE, "Boil");
        writeTextElement(tagTIME, QString::number(hop.time()));
        buf = "Leaf";
        if (hop.type() == Hop::PELLET_STRING) buf = "Pellet";
        if (hop.type() == Hop::PLUG_STRING) buf = "Plug";
        writeTextElement(tagFORM, buf);
        writeEndElement(); // tagHOP
    }
    writeEndElement(); // tagHOPS

    // yeast list
    writeStartElement(tagYEASTS);
    foreach (Misc misc, *recipe->miscs()) {
        // iterate through list looking for yeasts
        if (misc.type() == Misc::YEAST_STRING) {
            writeStartElement(tagYEAST);
            writeTextElement(tagVERSION, beerXMLVersion);
            writeTextElement(tagNAME, misc.name());
            writeTextElement(tagAMOUNT, "0.00"); // TODO: fixup for miscs
            writeTextElement(tagTYPE, Misc::YEAST_STRING);
            // TODO: form???
            writeTextElement(tagNOTES, misc.notes());
            writeEndElement(); // tagYEAST
        }
    }
    writeEndElement(); // tagYEASTS

    // misc list
    writeStartElement(tagMISCS);
    foreach (Misc misc, *recipe->miscs()) {
        // iterate through list looking for yeasts
        if (misc.type() != Misc::YEAST_STRING) {
            writeStartElement(tagMISC);
            writeTextElement(tagVERSION, beerXMLVersion);
            writeTextElement(tagNAME, misc.name());
            writeTextElement(tagAMOUNT, "0.00"); // TODO: fixup for miscs
            writeTextElement(tagTYPE, misc.type());
            writeTextElement(tagNOTES, misc.notes());
            // use ???
            // time ???
            writeEndElement(); // tagMISC
        }
    }
    writeEndElement(); // tagMISCS

    // waters
    // NOTE: not currently supporting water
    writeStartElement(tagWATERS);
    writeEndElement();

    // mash
    // NOTE: not currently supporting mash

    // notes
    if (!(recipe->recipeNotes().isEmpty() && recipe->batchNotes().isEmpty())) {
        buf = recipe->recipeNotes();
        if (!recipe->batchNotes().isEmpty()) {
            if (!buf.isEmpty()) buf += "\n\n";
            buf += recipe->batchNotes();
        }
        writeTextElement(tagNOTES, buf);
    }

    writeEndElement(); // tagRECIPE
    writeEndElement(); // tagRECIPES

    return true;
}