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
|
/*
Copyright (C) 2003 Richard L�rk�ng
Copyright (C) 2003 Jason Kivlighn
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston, MA 02111-1307 USA
*/
#include "mx2importer.h"
#include <tdelocale.h>
#include <kdebug.h>
#include <tqfile.h>
#include <tqstringlist.h>
#include <tqtextstream.h>
#include <tqdatetime.h>
#include "datablocks/recipe.h"
MX2Importer::MX2Importer()
{}
void MX2Importer::parseFile( const TQString& filename )
{
TQFile file( filename );
kdDebug() << "loading file: " << filename << endl;
if ( file.open( IO_ReadOnly ) ) {
kdDebug() << "file opened" << endl;
TQDomDocument doc;
//hopefully a temporary hack, since MasterCook creates invalid xml declarations
TQTextStream stream( &file );
TQString all_data = stream.read();
if ( all_data.startsWith( "<?xml" ) )
all_data.remove( 0, all_data.find( "?>" ) + 2 );
TQString error;
int line;
int column;
if ( !doc.setContent( all_data, &error, &line, &column ) ) {
kdDebug() << TQString( "error: \"%1\" at line %2, column %3" ).arg( error ).arg( line ).arg( column ) << endl;
setErrorMsg( TQString( i18n( "\"%1\" at line %2, column %3. This may not be a *.mx2 file." ) ).arg( error ).arg( line ).arg( column ) );
return ;
}
TQDomElement mx2 = doc.documentElement();
// TODO Check if there are changes between versions
if ( mx2.tagName() != "mx2" /*|| mx2.attribute("source") != "MasterCook 5.0"*/ ) {
setErrorMsg( i18n( "This file does not appear to be a *.mx2 file" ) );
return ;
}
TQDomNodeList l = mx2.childNodes();
for ( unsigned i = 0; i < l.count(); i++ ) {
TQDomElement el = l.item( i ).toElement();
if ( el.tagName() == "RcpE" ) {
Recipe recipe;
recipe.title = el.attribute( "name" );
Element author( el.attribute( "author" ) );
recipe.authorList.append( author );
readRecipe( el.childNodes(), &recipe );
add
( recipe );
}
}
}
else
setErrorMsg( i18n( "Unable to open file." ) );
}
MX2Importer::~MX2Importer()
{
}
void MX2Importer::readRecipe( const TQDomNodeList& l, Recipe *recipe )
{
for ( unsigned i = 0; i < l.count(); i++ ) {
TQDomElement el = l.item( i ).toElement();
TQString tagName = el.tagName();
if ( tagName == "Serv" ) {
recipe->yield.amount = el.attribute( "qty" ).toInt();
recipe->yield.type = i18n("servings");
}
else if ( tagName == "PrpT" )
recipe->prepTime = TQTime::fromString( el.attribute( "elapsed" ) );
else if ( tagName == "CatS" ) {
TQDomNodeList categories = el.childNodes();
for ( unsigned j = 0; j < categories.count(); j++ ) {
TQDomElement c = categories.item( j ).toElement();
if ( c.tagName() == "CatT" ) {
if ( c.text().length() > 0 ) {
Element cat( c.text().stripWhiteSpace() );
recipe->categoryList.append( cat );
}
}
}
}
else if ( tagName == "IngR" ) {
Ingredient new_ing( el.attribute( "name" ),
el.attribute( "qty" ).toDouble(),
Unit( el.attribute( "unit" ), el.attribute( "qty" ).toDouble() ) );
if ( el.hasChildNodes() ) {
TQDomNodeList iChilds = el.childNodes();
for ( unsigned j = 0; j < iChilds.count(); j++ ) {
TQDomElement iChild = iChilds.item( j ).toElement();
if ( iChild.tagName() == "IPrp" )
new_ing.prepMethodList.append( Element(iChild.text().stripWhiteSpace()) );
else if ( iChild.tagName() == "INtI" )
; // TODO: What does it mean?... ingredient nutrient info?
}
}
recipe->ingList.append( new_ing );
}
else if ( tagName == "DirS" ) {
TQStringList directions;
TQDomNodeList dirs = el.childNodes();
for ( unsigned j = 0; j < dirs.count(); j++ ) {
TQDomElement dir = dirs.item( j ).toElement();
if ( dir.tagName() == "DirT" )
directions.append( dir.text().stripWhiteSpace() );
}
TQString directionsText;
// TODO This is copied from RecipeML, maybe a TQStringList
// for directions in Recipe instead?
if ( directions.count() > 1 ) {
for ( unsigned i = 1; i <= directions.count(); i++ ) {
if ( i != 1 ) {
directionsText += "\n\n";
}
TQString sWith = TQString( "%1. " ).arg( i );
TQString text = directions[ i - 1 ];
if ( !text.stripWhiteSpace().startsWith( sWith ) )
directionsText += sWith;
directionsText += text;
}
}
else
directionsText = directions[ 0 ];
recipe->instructions = directionsText;
}
else if ( tagName == "SrvI" ) {
// Don't know what to do with it, for now add it to directions
// btw lets hope this is read after the directions
recipe->instructions += "\n\n" + el.text().stripWhiteSpace();
}
else if ( tagName == "Note" ) {
// Don't know what to do with it, for now add it to directions
// btw lets hope this is read after the directions
recipe->instructions += "\n\n" + el.text().stripWhiteSpace();
}
else if ( tagName == "Nutr" ) {
//example: <Nutr>Per Serving (excluding unknown items): 51 Calories; 6g Fat (99.5% calories from fat); trace Protein; trace Carbohydrate; 0g Dietary Fiber; 16mg Cholesterol; 137mg Sodium. Exchanges: 1 Fat.</Nutr>
// Don't know what to do with it, for now add it to directions
// btw lets hope this is read after the directions
recipe->instructions += "\n\n" + el.text().stripWhiteSpace();
}
/* tags to check for (example follows:
<Srce>SARA'S SECRETS with Sara Moulton - (Show # SS-1B43) - from the TV FOOD NETWORK</Srce>
<AltS label="Formatted for MC7" source="07-11-2003 by Joe Comiskey - Mad's Recipe Emporium"/>
*/
// TODO Have i missed some tag?
}
}
|