22#include "kword13formatone.h"
23#include "kword13formatother.h"
24#include "kword13layout.h"
25#include "kword13frameset.h"
26#include "kword13picture.h"
27#include "kword13document.h"
28#include "kword13parser.h"
30KWord13StackItem::KWord13StackItem() : elementType( KWord13TypeUnknown ), m_currentFrameset( 0 )
34KWord13StackItem::~KWord13StackItem()
38KWord13Parser::KWord13Parser( KWord13Document* kwordDocument )
39 : m_kwordDocument(kwordDocument), m_currentParagraph( 0 ),
40 m_currentLayout( 0 ), m_currentFormat( 0 )
42 parserStack.setAutoDelete(
true );
43 KWord13StackItem* bottom =
new KWord13StackItem;
44 bottom->elementType = KWord13TypeBottom;
45 parserStack.push( bottom );
48KWord13Parser::~KWord13Parser(
void )
51 delete m_currentParagraph;
52 delete m_currentLayout;
53 delete m_currentFormat;
56bool KWord13Parser::startElementFormatOneProperty(
const TQString& name,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem)
59 if ( stackItem->elementType == KWord13TypeLayoutFormatOne )
61 if ( ! m_currentLayout )
63 kdError(30520) <<
"No current LAYOUT for storing FORMAT property: " << name << endl;
66 for (
int i = 0; i < attributes.count(); ++i )
68 TQString attrName ( name );
70 attrName += attributes.qName( i );
71 m_currentLayout->m_format.m_properties[ attrName ] = attributes.value( i );
72 kdDebug(30520) <<
"Format Property (for LAYOUT): " << attrName <<
" = " << attributes.value( i ) << endl;
74 stackItem->elementType = KWord13TypeEmpty;
77 else if ( stackItem->elementType == KWord13TypeFormat )
79 if ( ! m_currentFormat )
81 kdError(30520) <<
"No current FORMAT for storing FORMAT property: " << name << endl;
88 kdError(30520) <<
"Current FORMAT cannot store FORMAT text property: " << name << endl;
92 for (
int i = 0; i < attributes.count(); ++i )
94 TQString attrName ( name );
96 attrName += attributes.qName( i );
97 data->m_properties[ attrName ] = attributes.value( i );
98 kdDebug(30520) <<
"Format Property (for FORMATS): " << attrName <<
" = " << attributes.value( i ) << endl;
100 stackItem->elementType = KWord13TypeEmpty;
103 else if ( stackItem->elementType == KWord13TypeIgnore )
109 kdError(30520) <<
"Wrong parents for FORMAT property: " << name << endl;
114bool KWord13Parser::startElementLayoutProperty(
const TQString& name,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem)
117 if ( stackItem->elementType == KWord13TypeIgnore )
121 else if ( m_currentLayout )
123 for (
int i = 0; i < attributes.count(); ++i )
125 TQString attrName ( name );
127 attrName += attributes.qName( i );
128 m_currentLayout->m_layoutProperties[ attrName ] = attributes.value( i );
129 kdDebug(30520) <<
"Layout Property: " << attrName <<
" = " << attributes.value( i ) << endl;
131 stackItem->elementType = KWord13TypeEmpty;
136 kdError(30520) <<
"No current layout for storing property: " << name << endl;
141bool KWord13Parser::startElementName(
const TQString&,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem )
143 if ( stackItem->elementType != KWord13TypeLayout )
146 stackItem->elementType = KWord13TypeIgnore;
150 stackItem->elementType = KWord13TypeEmpty;
152 if ( m_currentLayout )
154 m_currentLayout->m_name = attributes.value(
"value" );
159bool KWord13Parser::startElementFormat(
const TQString&,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem )
162 if ( stackItem->elementType == KWord13TypeIgnore )
166 else if ( stackItem->elementType == KWord13TypeLayout )
168 stackItem->elementType = KWord13TypeLayoutFormatOne;
171 else if ( stackItem->elementType != KWord13TypeFormatsPlural )
173 kdError(30520) <<
"<FORMAT> is child neither of <FORMATS> nor of <LAYOUT> nor of <STYLE>! Aborting!" << endl;
177 stackItem->elementType = KWord13TypeFormat;
179 if ( m_currentFormat )
181 kdWarning(30520) <<
"Current format already defined!" << endl;
182 delete m_currentFormat;
187 const int id = attributes.value(
"id" ).toInt( &ok );
192 const int len = attributes.value(
"len" ).toInt( &ok );
195 m_currentFormat = one;
197 else if (
id == 4 && ok )
199 stackItem->elementType = KWord13TypeVariable;
202 else if (
id == 6 && ok )
204 stackItem->elementType = KWord13TypeAnchor;
210 stackItem->elementType = KWord13TypeIgnore;
213 m_currentFormat->
m_id = id;
215 const int pos = attributes.value(
"pos" ).toInt( &ok );
218 m_currentFormat->m_pos = pos;
222 kdWarning(30520) <<
"Cannot set position of <FORMAT>: " << attributes.value(
"pos" ) << endl;
226 kdDebug(30520) <<
"<FORMAT id=\"" <<
id <<
"\" pos=\"" << pos <<
"\" len=\"" << attributes.value(
"len" ) <<
"\">" << endl;
231bool KWord13Parser::startElementLayout(
const TQString&,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem )
234 if ( stackItem->elementType == KWord13TypeIgnore )
239 stackItem->elementType = KWord13TypeLayout;
241 if ( m_currentFormat )
243 kdWarning(30520) <<
"Current format defined! (Layout)" << endl;
244 delete m_currentFormat;
247 if ( m_currentLayout )
250 kdWarning(30520) <<
"Current layout already defined!" << endl;
251 delete m_currentLayout;
255 m_currentLayout->m_outline = ( attributes.value(
"outline" ) ==
"true" );
260bool KWord13Parser::startElementParagraph(
const TQString&,
const TQXmlAttributes&, KWord13StackItem *stackItem )
262 if ( stackItem->elementType == KWord13TypeUnknownFrameset )
264 stackItem->elementType = KWord13TypeIgnore;
268 stackItem->elementType = KWord13TypeParagraph;
270 if ( m_currentParagraph )
273 kdWarning(30520) <<
"Current paragraph already defined!" << endl;
274 delete m_currentParagraph;
282bool KWord13Parser::startElementFrame(
const TQString& name,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem )
284 if ( stackItem->elementType == KWord13TypeFrameset || stackItem->elementType == KWord13TypePictureFrameset )
286 stackItem->elementType = KWord13TypeEmpty;
287 if ( stackItem->m_currentFrameset )
289 const int num = ++stackItem->m_currentFrameset->m_numFrames;
290 for (
int i = 0; i < attributes.count(); ++i )
292 TQString attrName ( name );
294 attrName += TQString::number( num );
296 attrName += attributes.qName( i );
297 stackItem->m_currentFrameset->m_frameData[ attrName ] = attributes.value( i );
298 kdDebug(30520) <<
"FrameData: " << attrName <<
" = " << attributes.value( i ) << endl;
304 kdError(30520) <<
"Data of <FRAMESET> not found" << endl;
308 else if ( stackItem->elementType != KWord13TypeUnknownFrameset )
310 kdError(30520) <<
"<FRAME> not child of <FRAMESET>" << endl;
316bool KWord13Parser::startElementFrameset(
const TQString& name,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem )
318 const TQString frameTypeStr( attributes.value(
"frameType" ) );
319 const TQString frameInfoStr( attributes.value(
"frameInfo" ) );
321 if ( frameTypeStr.isEmpty() || frameInfoStr.isEmpty() )
323 kdError(30520) <<
"<FRAMESET> without frameType or frameInfo attribute!" << endl;
327 const int frameType = frameTypeStr.toInt();
328 const int frameInfo = frameInfoStr.toInt();
330 if ( frameType == 1 )
332 stackItem->elementType = KWord13TypeFrameset;
333 KWordTextFrameset* frameset =
new KWordTextFrameset( frameType, frameInfo, attributes.value(
"name" ) );
336 if ( ( !frameInfo ) && attributes.value(
"grpMgr" ).isEmpty() )
338 m_kwordDocument->m_normalTextFramesetList.append( frameset );
339 stackItem->m_currentFrameset = m_kwordDocument->m_normalTextFramesetList.current();
341 else if ( !frameInfo )
345 m_kwordDocument->m_tableFramesetList.append( frameset );
346 stackItem->m_currentFrameset = m_kwordDocument->m_tableFramesetList.current();
348 else if ( frameInfo >= 1 && frameInfo <= 6 )
350 m_kwordDocument->m_headerFooterFramesetList.append( frameset );
351 stackItem->m_currentFrameset = m_kwordDocument->m_headerFooterFramesetList.current();
353 else if ( frameInfo == 7 )
355 m_kwordDocument->m_footEndNoteFramesetList.append( frameset );
356 stackItem->m_currentFrameset = m_kwordDocument->m_footEndNoteFramesetList.current();
360 kdError(30520) <<
"Unknown text frameset!" << endl;
361 m_kwordDocument->m_otherFramesetList.append( frameset );
362 stackItem->m_currentFrameset = m_kwordDocument->m_otherFramesetList.current();
365 else if ( ( frameType == 2 )
366 || ( frameType == 5 ) )
370 kdWarning(30520) <<
"Unknown FrameInfo for pictures: " << frameInfo << endl;
372 stackItem->elementType = KWord13TypePictureFrameset;
373 KWord13PictureFrameset* frameset =
new KWord13PictureFrameset( frameType, frameInfo, attributes.value(
"name" ) );
374 m_kwordDocument->m_otherFramesetList.append( frameset );
375 stackItem->m_currentFrameset = m_kwordDocument->m_otherFramesetList.current();
383 kdWarning(30520) <<
"Unknown/unsupported <FRAMESET> type! Type: " << frameTypeStr <<
" Info: " << frameInfoStr << endl;
384 stackItem->elementType = KWord13TypeUnknownFrameset;
385 KWord13Frameset* frameset =
new KWord13Frameset( frameType, frameInfo, attributes.value(
"name" ) );
386 m_kwordDocument->m_otherFramesetList.append( frameset );
387 stackItem->m_currentFrameset = m_kwordDocument->m_otherFramesetList.current();
393bool KWord13Parser::startElementDocumentAttributes(
const TQString& name,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem,
394 const KWord13StackItemType& allowedParentType,
const KWord13StackItemType& newType )
396 if ( parserStack.current()->elementType == allowedParentType )
398 stackItem->elementType = newType;
399 for (
int i = 0; i < attributes.count(); ++i )
401 TQString attrName ( name );
403 attrName += attributes.qName( i );
404 m_kwordDocument->m_documentProperties[ attrName ] = attributes.value( i );
405 kdDebug(30520) <<
"DocAttr: " << attrName <<
" = " << attributes.value( i ) << endl;
411 kdError(30520) <<
"Wrong parent!" << endl;
416bool KWord13Parser::startElementKey(
const TQString& name,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem )
418 const TQString key( calculatePictureKey( attributes.value(
"filename" ),
419 attributes.value(
"year" ), attributes.value(
"month" ), attributes.value(
"day" ),
420 attributes.value(
"hour" ), attributes.value(
"minute" ), attributes.value(
"second" ),
421 attributes.value(
"msec" ) ) );
422 kdDebug(30520) <<
"Picture key: " << key << endl;
424 if ( stackItem->elementType == KWord13TypePicturesPlural )
426 KWord13Picture* pic =
new KWord13Picture;
427 pic->m_storeName = attributes.value(
"name" );
428 if ( pic->m_storeName.isEmpty() )
430 kdError(30520) <<
"Picture defined without store name! Aborting!" << endl;
434 m_kwordDocument->m_pictureDict.insert( key, pic );
436 else if ( stackItem->elementType == KWord13TypePicture )
439 if ( stackItem->m_currentFrameset )
441 stackItem->m_currentFrameset->setKey( key );
453bool KWord13Parser::startElementAnchor(
const TQString& name,
const TQXmlAttributes& attributes, KWord13StackItem *stackItem )
455 if ( stackItem->elementType == KWord13TypeAnchor )
457 const TQString anchorType ( attributes.value(
"type" ) );
458 if ( anchorType ==
"grpMgr" )
459 kdWarning(30520) <<
"Anchor of type grpMgr! Not tested!" << endl;
460 else if ( anchorType !=
"frameset" )
462 kdError(30520) <<
"Unsupported anchor type: " << anchorType << endl;
465 const TQString frameset ( attributes.value(
"instance" ) );
466 if ( frameset.isEmpty() )
468 kdError(30520) <<
"Anchor to an empty frameset name! Aborting!" << endl;
471 if ( m_currentFormat )
477 if ( m_kwordDocument->m_anchoredFramesetNames.find( frameset ) == m_kwordDocument->m_anchoredFramesetNames.end() )
479 m_kwordDocument->m_anchoredFramesetNames.append( frameset );
484 kdError(30520) <<
"Anchor not child of <FORMAT id=\"6\"> Aborting!" << endl;
491bool KWord13Parser::startElement(
const TQString&,
const TQString&,
const TQString& name,
const TQXmlAttributes& attributes )
493 kdDebug(30520) << indent <<
"<" << name <<
">" << endl;
495 if (parserStack.isEmpty())
497 kdError(30520) <<
"Stack is empty!! Aborting! (in KWordParser::startElement)" << endl;
502 KWord13StackItem *stackItem=
new KWord13StackItem(*parserStack.current());
506 kdError(30520) <<
"Could not create Stack Item! Aborting! (in StructureParser::startElement)" << endl;
510 stackItem->itemName=name;
515 if ( name ==
"COLOR" || name ==
"FONT" || name ==
"SIZE"
516 || name ==
"WEIGHT" || name ==
"ITALIC" || name ==
"UNDERLINE"
517 || name ==
"STRIKEOUT" || name ==
"VERTALIGN" || name ==
"SHADOW"
518 || name ==
"FONTATTRIBUTE" || name ==
"LANGUAGE"
519 || name ==
"TEXTBACKGROUNDCOLOR" || name ==
"OFFSETFROMBASELINE" )
521 success = startElementFormatOneProperty( name, attributes, stackItem );
523 else if ( name ==
"FLOW" || name ==
"INDENTS" || name ==
"OFFSETS"
524 || name ==
"LINESPACING" || name ==
"PAGEBREAKING"
525 || name ==
"LEFTBORDER" || name ==
"RIGHTBORDER" || name ==
"FOLLOWING"
526 || name ==
"TOPBORDER" || name ==
"BOTTOMBORDER" || name ==
"COUNTER" )
528 success = startElementLayoutProperty( name, attributes, stackItem );
530 else if ( name ==
"TEXT" )
532 if ( stackItem->elementType == KWord13TypeParagraph && m_currentParagraph )
534 stackItem->elementType = KWord13TypeText;
535 m_currentParagraph->setText( TQString() );
539 stackItem->elementType = KWord13TypeIgnore;
543 else if ( name ==
"NAME" )
545 success = startElementName( name, attributes, stackItem );
547 else if ( name ==
"FORMATS" )
549 if ( stackItem->elementType == KWord13TypeParagraph && m_currentParagraph )
551 stackItem->elementType = KWord13TypeFormatsPlural;
555 stackItem->elementType = KWord13TypeIgnore;
559 else if ( name ==
"PARAGRAPH" )
561 success = startElementParagraph( name, attributes, stackItem );
563 else if ( name ==
"FORMAT" )
565 success = startElementFormat( name, attributes, stackItem );
567 else if (name ==
"LAYOUT" )
569 success = startElementLayout( name, attributes, stackItem );
571 else if ( name ==
"TYPE" )
574 if ( m_currentFormat && ( stackItem->elementType == KWord13TypeVariable ) )
580 else if ( name ==
"KEY" )
582 success = startElementKey( name, attributes, stackItem );
584 else if ( name ==
"ANCHOR" )
586 success = startElementAnchor( name, attributes, stackItem );
588 else if ( name ==
"PICTURE" || name ==
"IMAGE" || name ==
"CLIPART" )
591 if ( stackItem->elementType == KWord13TypePictureFrameset )
593 stackItem->elementType = KWord13TypePicture;
597 else if ( name ==
"FRAME" )
599 success = startElementFrame( name, attributes, stackItem );
601 else if ( name ==
"FRAMESET" )
603 success = startElementFrameset( name, attributes, stackItem );
605 else if (name ==
"STYLE" )
607 success = startElementLayout( name, attributes, stackItem );
609 else if ( name ==
"DOC" )
611 success = startElementDocumentAttributes( name, attributes, stackItem, KWord13TypeBottom, KWord13TypeDocument );
613 else if ( name ==
"PAPER")
615 success = startElementDocumentAttributes( name, attributes, stackItem, KWord13TypeDocument, KWord13TypePaper );
617 else if ( name ==
"PAPERBORDERS" )
619 success = startElementDocumentAttributes( name, attributes, stackItem, KWord13TypePaper, KWord13TypeEmpty );
621 else if ( ( name ==
"ATTRIBUTES" ) || ( name ==
"VARIABLESETTINGS" )
622 || ( name ==
"FOOTNOTESETTINGS" ) || ( name ==
"ENDNOTESETTINGS" ) )
624 success = startElementDocumentAttributes( name, attributes, stackItem, KWord13TypeDocument, KWord13TypeEmpty );
626 else if ( name ==
"FRAMESTYLE" )
629 stackItem->elementType = KWord13TypeIgnore;
632 else if ( name ==
"PICTURES" || name ==
"PIXMAPS" || name ==
"CLIPARTS" )
635 stackItem->elementType = KWord13TypePicturesPlural;
640 stackItem->elementType = KWord13TypeUnknown;
646 parserStack.push( stackItem );
656bool KWord13Parser :: endElement(
const TQString&,
const TQString& ,
const TQString& name)
658 indent.remove( 0, 1 );
660 if (parserStack.isEmpty())
662 kdError(30520) <<
"Stack is empty!! Aborting! (in StructureParser::endElement)" << endl;
668 KWord13StackItem *stackItem=parserStack.pop();
670 if ( name ==
"PARAGRAPH" )
672 if ( stackItem->m_currentFrameset && m_currentParagraph )
674 if ( stackItem->m_currentFrameset->addParagraph( *m_currentParagraph ) )
679 m_currentParagraph->m_formats.setAutoDelete(
false );
681 else if ( stackItem->elementType == KWord13TypeIgnore )
685 delete m_currentParagraph;
686 m_currentParagraph = 0;
688 else if ( name ==
"FORMAT" )
690 if ( stackItem->elementType == KWord13TypeFormat )
692 if ( m_currentParagraph )
694 m_currentParagraph->m_formats.append( m_currentFormat );
695 kdDebug(30520) <<
"Adding to <FORMATS>: " << ((
void*) m_currentFormat) << endl;
700 kdError(30520) <<
"No paragraph to store <FORMAT>! Aborting!" << endl;
701 delete m_currentFormat;
707 else if ( stackItem->elementType == KWord13TypeLayoutFormatOne )
713 else if ( name ==
"LAYOUT" )
715 if ( m_currentLayout && m_currentParagraph )
717 m_currentParagraph->m_layout = *m_currentLayout;
719 delete m_currentLayout;
723 else if ( name ==
"STYLE" )
725 if ( m_kwordDocument && m_currentLayout )
727 if ( m_currentLayout->m_name.isEmpty() )
730 kdError(30520) <<
"Anonymous style found! Aborting" << endl;
733 m_kwordDocument->m_styles.append( *m_currentLayout );
736 delete m_currentLayout;
739 else if ( name ==
"DOC" )
751 kdError(30506) <<
"Found closing tag name: " << name <<
" expected: " << stackItem->itemName << endl;
759bool KWord13Parser :: characters (
const TQString & ch )
765 kdDebug(30520) << indent <<
" (LINEFEED)" << endl;
767 else if (ch.length()> 40)
769 kdDebug(30520) << indent <<
" :" << ch.left(40) <<
"..." << endl;
773 kdDebug(30520) << indent <<
" :" << ch <<
":" << endl;
778 if (parserStack.isEmpty())
780 kdError(30520) <<
"Stack is empty!! Aborting! (in StructureParser::characters)" << endl;
786 KWord13StackItem *stackItem = parserStack.current();
788 if ( stackItem->elementType == KWord13TypeText )
791 if ( m_currentParagraph )
795 const uint length = ch.length();
796 for ( uint i = 0; i < length; ++i )
798 const ushort uni = ch.at(i).unicode();
801 else if ( uni == 9 || uni == 10 || uni == 13)
816 kdWarning(30520) <<
"Unexcepted control characters found in text!" << endl;
817 m_currentParagraph->appendText( ch );
822 kdError(30520) <<
"No current paragraph defined! Tag mismatch?" << endl;
826 else if (stackItem->elementType==KWord13TypeEmpty)
828 success=ch.stripWhiteSpace().isEmpty();
832 kdError(30520) <<
"Empty element "<< stackItem->itemName <<
" is not empty! Aborting! (in KWordParser::characters)" << endl;
843bool KWord13Parser::warning(
const TQXmlParseException& exception)
845 kdWarning(30520) <<
"XML parsing warning: line " << exception.lineNumber()
846 <<
" col " << exception.columnNumber() <<
" message: " << exception.message() << endl;
850bool KWord13Parser::error(
const TQXmlParseException& exception)
853 kdWarning(30520) <<
"XML parsing error: line " << exception.lineNumber()
854 <<
" col " << exception.columnNumber() <<
" message: " << exception.message() << endl;
858bool KWord13Parser::fatalError (
const TQXmlParseException& exception)
860 kdError(30520) <<
"XML parsing fatal error: line " << exception.lineNumber()
861 <<
" col " << exception.columnNumber() <<
" message: " << exception.message() << endl;
866TQString KWord13Parser::calculatePictureKey(
const TQString& filename,
867 const TQString& year,
const TQString& month,
const TQString& day,
868 const TQString& hour,
const TQString& minute,
const TQString& second,
869 const TQString& microsecond )
const
872 bool globalOk =
true;
875 const int iYear = year.toInt( & ok );
876 globalOk = globalOk && ok;
879 const int iMonth = month.toInt( & ok );
880 globalOk = globalOk && ok;
883 const int iDay = day.toInt( & ok );
884 globalOk = globalOk && ok;
887 const int iHour = hour.toInt( & ok );
888 globalOk = globalOk && ok;
891 const int iMinute = minute.toInt( & ok );
892 globalOk = globalOk && ok;
895 const int iSecond = second.toInt( & ok );
896 globalOk = globalOk && ok;
899 const int iMicrosecond = microsecond.toInt( & ok );
900 globalOk = globalOk && ok;
905 globalOk = globalOk && TQDate::isValid( iYear, iMonth, iDay );
906 globalOk = globalOk && TQTime::isValid( iHour, iMinute, iSecond, iMicrosecond );
913 dt = TQDateTime( TQDate( iYear, iMonth, iDay ), TQTime( iHour, iMinute, iSecond, iMicrosecond ) );
918 dt = TQDateTime( TQDate( 1970, 1, 1 ), TQTime( 0, 0, 0, 0 ) );
925 TQString result ( dt.toString(
"yyyyMMddhhmmsszzz" ) );