1
/* This file is part of the KDE project
2
Copyright (C) 2002 Ariya Hidayat <ariyahidayat@yahoo.de>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA.
23
#include <qvaluestack.h>
25
#include <wmlparser.h>
30
unsigned tableRow, tableCol;
31
WMLFormat currentFormat;
32
WMLFormatList formatList;
33
WMLLayout currentLayout;
35
WMLParseState( const WMLParseState& );
36
WMLParseState& operator=( const WMLParseState& );
37
void assign( const WMLParseState& );
40
WMLParseState::WMLParseState()
42
tableRow = tableCol = 0;
45
WMLParseState::WMLParseState( const WMLParseState& state )
50
WMLParseState& WMLParseState::operator=( const WMLParseState& state )
56
void WMLParseState::assign( const WMLParseState& state )
58
tableRow = state.tableRow;
59
tableCol = state.tableCol;
60
currentFormat = state.currentFormat;
61
formatList = state.formatList;
62
currentLayout = state.currentLayout;
65
// ContentHandler for use with the reader
66
class WMLHandler: public QXmlDefaultHandler
70
WMLHandler( WMLParser *parser ){ m_parser = parser; }
73
bool startElement( const QString&, const QString&, const QString& ,
74
const QXmlAttributes& );
75
bool endElement( const QString&, const QString&, const QString& );
76
bool characters( const QString& ch );
89
WMLParseState m_state;
90
QValueStack<WMLParseState> m_stateStack;
92
bool flushParagraph();
97
bool WMLHandler::startDocument()
108
bool WMLHandler::startElement( const QString&, const QString&,
109
const QString& qName,
110
const QXmlAttributes& attr )
112
QString tag = qName.lower();
115
return m_parser->doOpenDocument();
119
m_state = WMLParseState();
120
QString card_id = attr.value("id");
121
QString card_title = attr.value("title");
122
return m_parser->doOpenCard( card_id, card_title );
127
m_state.currentLayout = WMLLayout();
129
if( m_state.currentFormat.bold ||
130
m_state.currentFormat.italic ||
131
m_state.currentFormat.underline ||
132
(m_state.currentFormat.fontsize != WMLFormat::Normal) )
133
m_state.formatList.append( m_state.currentFormat );
135
QString align = attr.value("align").lower();
136
if( align == "right" )
137
m_state.currentLayout.align = WMLLayout::Right;
138
if( align == "center" )
139
m_state.currentLayout.align = WMLLayout::Center;
144
if(( tag == "b" ) || (tag == "strong") )
146
m_state.currentFormat.bold = TRUE;
147
m_state.currentFormat.pos = m_text.length();
148
m_state.formatList.append( m_state.currentFormat );
152
if(( tag == "i" ) || (tag == "em") )
154
m_state.currentFormat.italic = TRUE;
155
m_state.currentFormat.pos = m_text.length();
156
m_state.formatList.append( m_state.currentFormat );
162
m_state.currentFormat.underline = TRUE;
163
m_state.currentFormat.pos = m_text.length();
164
m_state.formatList.append( m_state.currentFormat );
170
m_state.currentFormat.fontsize = WMLFormat::Big;
171
m_state.currentFormat.pos = m_text.length();
172
m_state.formatList.append( m_state.currentFormat );
178
m_state.currentFormat.fontsize = WMLFormat::Small;
179
m_state.currentFormat.pos = m_text.length();
180
m_state.formatList.append( m_state.currentFormat );
186
QString href = attr.value("href");
187
if( !href.isEmpty() )
191
m_state.currentFormat.link = "";
192
m_state.currentFormat.href = href;
193
m_state.currentFormat.pos = m_text.length();
194
m_state.currentFormat.len = 1;
195
m_text.append( "#" ); // inline char
204
return m_parser->doBeginTable();
214
// open table cell, keep in sync with <p> above
218
m_state.currentLayout = WMLLayout();
220
m_state.formatList.append( m_state.currentFormat );
221
return m_parser->doTableCell( m_state.tableRow, m_state.tableCol );
228
bool WMLHandler::endElement( const QString&, const QString&,
229
const QString& qName )
231
QString tag = qName.lower();
234
return m_parser->doCloseDocument();
238
// forget </p> before </card> ?
240
if( !m_text.isEmpty() )
242
return m_parser->doCloseCard();
248
return flushParagraph();
251
if(( tag == "b" ) || (tag == "strong") )
253
m_state.currentFormat.bold = FALSE;
254
m_state.currentFormat.pos = m_text.length();
255
m_state.formatList.append( m_state.currentFormat );
259
if(( tag == "i" ) || (tag == "em") )
261
m_state.currentFormat.italic = FALSE;
262
m_state.currentFormat.pos = m_text.length();
263
m_state.formatList.append( m_state.currentFormat );
269
m_state.currentFormat.underline = FALSE;
270
m_state.currentFormat.pos = m_text.length();
271
m_state.formatList.append( m_state.currentFormat );
277
m_state.currentFormat.fontsize = WMLFormat::Normal;
278
m_state.currentFormat.pos = m_text.length();
279
m_state.formatList.append( m_state.currentFormat );
285
m_state.currentFormat.fontsize = WMLFormat::Normal;
286
m_state.currentFormat.pos = m_text.length();
287
m_state.formatList.append( m_state.currentFormat );
295
m_state.formatList.append( m_state.currentFormat );
303
return m_parser->doEndTable();
310
// close table cell, like </p>
314
return flushParagraph();
321
bool WMLHandler::characters( const QString& ch )
327
m_state.currentFormat.link.append( ch );
332
bool WMLHandler::flushParagraph()
334
// calc length of each format tag
335
for( unsigned i=0; i<m_state.formatList.count(); i++ )
338
WMLFormat& format = m_state.formatList[i];
339
if( i < m_state.formatList.count()-1 )
341
WMLFormat& nextformat = m_state.formatList[i+1];
342
nextpos = nextformat.pos;
344
else nextpos = m_text.length();
345
if( format.len <= 0 )
346
format.len = nextpos - format.pos;
349
bool result = m_parser->doParagraph( m_text, m_state.formatList, m_state.currentLayout );
351
// ready for next paragraph
353
m_state.formatList.clear();
354
m_state.currentLayout = WMLLayout();
356
// m_state.currentFormat = WMLFormat();
357
// FIXME should we reset formatting ?
362
void WMLHandler::pushState()
364
m_stateStack.push( m_state );
367
void WMLHandler::popState()
369
if( !m_stateStack.isEmpty() )
370
m_state = m_stateStack.pop();
373
// formatting for the text
374
WMLFormat::WMLFormat()
378
bold = italic = underline = FALSE;
383
void WMLFormat::assign( const WMLFormat& f )
389
underline = f.underline;
390
fontsize = f.fontsize;
395
WMLFormat::WMLFormat( const WMLFormat& f )
400
WMLFormat& WMLFormat::operator=( const WMLFormat& f )
406
// paragraph layout info
407
WMLLayout::WMLLayout()
412
void WMLLayout::assign( const WMLLayout& l )
417
WMLLayout::WMLLayout( const WMLLayout& l )
422
WMLLayout& WMLLayout::operator=( const WMLLayout& l )
428
// The basic WML parser
429
void WMLParser::parse( const char* filename )
432
QXmlInputSource source( &f );
433
QXmlSimpleReader reader;
434
WMLHandler handler( this );
435
reader.setContentHandler( &handler );
436
reader.parse( source );
439
bool WMLParser::doOpenDocument()
444
bool WMLParser::doCloseDocument()
449
bool WMLParser::doOpenCard( QString, QString )
454
bool WMLParser::doCloseCard()
459
bool WMLParser::doParagraph( QString, WMLFormatList, WMLLayout )
464
bool WMLParser::doBeginTable()
469
bool WMLParser::doEndTable()
474
bool WMLParser::doTableCell( unsigned, unsigned )