31#include <tqdatetime.h>
35#include <kfilterdev.h>
36#include <kgenericfactory.h>
37#include <tdemessagebox.h>
39#include <KoPageLayout.h>
41#include <KoFilterChain.h>
44#include "ImportFormatting.h"
45#include "ImportStyle.h"
46#include "ImportField.h"
48#include "abiwordimport.h"
50class ABIWORDImportFactory : KGenericFactory<ABIWORDImport, KoFilter>
53 ABIWORDImportFactory(
void) : KGenericFactory<ABIWORDImport, KoFilter> (
"kwordabiwordimport")
56 virtual void setupTranslations(
void )
58 TDEGlobal::locale()->insertCatalogue(
"kofficefilters" );
62K_EXPORT_COMPONENT_FACTORY( libabiwordimport, ABIWORDImportFactory() )
70class StructureParser : public TQXmlDefaultHandler
73 StructureParser(KoFilterChain* chain)
74 : m_chain(chain), m_pictureNumber(0), m_pictureFrameNumber(0), m_tableGroupNumber(0),
75 m_timepoint(TQDateTime::currentDateTime(TQt::UTC)), m_fatalerror(false)
78 structureStack.setAutoDelete(
true);
79 StackItem *stackItem=
new(StackItem);
80 stackItem->elementType=ElementTypeBottom;
81 stackItem->m_frameset=mainFramesetElement;
82 stackItem->stackElementText=mainFramesetElement;
83 structureStack.push(stackItem);
85 virtual ~StructureParser()
87 structureStack.clear();
90 virtual bool startDocument(
void);
91 virtual bool endDocument(
void);
92 virtual bool startElement(
const TQString&,
const TQString&,
const TQString& name,
const TQXmlAttributes& attributes);
93 virtual bool endElement(
const TQString&,
const TQString& ,
const TQString& qName);
94 virtual bool characters (
const TQString & ch );
95 virtual bool warning(
const TQXmlParseException& exception);
96 virtual bool error(
const TQXmlParseException& exception);
97 virtual bool fatalError(
const TQXmlParseException& exception);
99 inline TQDomDocument getDocInfo(
void)
const {
return m_info; }
100 inline TQDomDocument getDocument(
void)
const {
return mainDocument; }
101 inline bool wasFatalError(
void)
const {
return m_fatalerror; }
103 bool clearStackUntilParagraph(StackItemStack& auxilaryStack);
104 bool complexForcedPageBreak(StackItem* stackItem);
107 bool StartElementC(StackItem* stackItem, StackItem* stackCurrent,
108 const TQXmlAttributes& attributes);
109 bool StartElementA(StackItem* stackItem, StackItem* stackCurrent,
110 const TQXmlAttributes& attributes);
111 bool StartElementImage(StackItem* stackItem, StackItem* stackCurrent,
112 const TQXmlAttributes& attributes);
113 bool EndElementD (StackItem* stackItem);
114 bool EndElementM (StackItem* stackItem);
115 bool StartElementSection(StackItem* stackItem, StackItem* stackCurrent,
116 const TQXmlAttributes& attributes);
117 bool StartElementFoot(StackItem* stackItem, StackItem* stackCurrent,
118 const TQXmlAttributes& attributes);
119 bool StartElementTable(StackItem* stackItem, StackItem* stackCurrent,
const TQXmlAttributes& attributes);
120 bool StartElementCell(StackItem* stackItem, StackItem* stackCurrent,
const TQXmlAttributes& attributes);
122 void createDocument(
void);
123 void createDocInfo(
void);
125 StackItemStack structureStack;
126 TQDomDocument mainDocument;
127 TQDomDocument m_info;
128 TQDomElement framesetsPluralElement;
129 TQDomElement mainFramesetElement;
130 TQDomElement m_picturesElement;
131 TQDomElement m_paperElement;
132 TQDomElement m_paperBordersElement;
133 TQDomElement m_ignoreWordsElement;
134 StyleDataMap styleDataMap;
135 KoFilterChain* m_chain;
136 uint m_pictureNumber;
137 uint m_pictureFrameNumber;
138 uint m_tableGroupNumber;
139 TQMap<TQString,TQString> m_metadataMap;
140 TQDateTime m_timepoint;
146bool StructureParser::StartElementC(StackItem* stackItem, StackItem* stackCurrent,
const TQXmlAttributes& attributes)
152 if ((stackCurrent->elementType==ElementTypeParagraph)||(stackCurrent->elementType==ElementTypeContent))
156 TQString strStyleProps;
157 TQString strStyleName=attributes.value(
"style").stripWhiteSpace();
158 if (!strStyleName.isEmpty())
160 StyleDataMap::Iterator it=styleDataMap.find(strStyleName);
161 if (it!=styleDataMap.end())
163 strStyleProps=it.data().m_props;
167 AbiPropsMap abiPropsMap;
168 PopulateProperties(stackItem,strStyleProps,attributes,abiPropsMap,
true);
170 stackItem->elementType=ElementTypeContent;
171 stackItem->stackElementParagraph=stackCurrent->stackElementParagraph;
172 stackItem->stackElementText=stackCurrent->stackElementText;
173 stackItem->stackElementFormatsPlural=stackCurrent->stackElementFormatsPlural;
174 stackItem->pos=stackCurrent->pos;
177 else if ((stackCurrent->elementType==ElementTypeAnchor)||(stackCurrent->elementType==ElementTypeAnchorContent))
179 stackItem->elementType=ElementTypeAnchorContent;
183 kdError(30506) <<
"parse error <c> tag nested neither in <p> nor in <c> nor in <a> but in "
184 << stackCurrent->itemName << endl;
190bool charactersElementC (StackItem* stackItem, TQDomDocument& mainDocument,
const TQString & ch)
192 if (stackItem->elementType==ElementTypeContent)
194 TQDomElement elementText=stackItem->stackElementText;
195 TQDomElement elementFormatsPlural=stackItem->stackElementFormatsPlural;
196 elementText.appendChild(mainDocument.createTextNode(ch));
198 TQDomElement formatElementOut=mainDocument.createElement(
"FORMAT");
199 formatElementOut.setAttribute(
"id",1);
200 formatElementOut.setAttribute(
"pos",stackItem->pos);
201 formatElementOut.setAttribute(
"len",ch.length());
202 elementFormatsPlural.appendChild(formatElementOut);
203 stackItem->pos+=ch.length();
205 AddFormat(formatElementOut, stackItem, mainDocument);
207 else if (stackItem->elementType==ElementTypeAnchorContent)
210 stackItem->strTemp2+=ch;
215 kdError(30506) <<
"Internal error (in charactersElementC)" << endl;
221bool EndElementC (StackItem* stackItem, StackItem* stackCurrent)
223 if (stackItem->elementType==ElementTypeContent)
225 stackItem->stackElementText.normalize();
226 stackCurrent->pos=stackItem->pos;
228 else if (stackItem->elementType==ElementTypeAnchorContent)
230 stackCurrent->strTemp2+=stackItem->strTemp2;
234 kdError(30506) <<
"Wrong element type!! Aborting! (</c> in StructureParser::endElement)" << endl;
241bool StructureParser::StartElementA(StackItem* stackItem, StackItem* stackCurrent,
const TQXmlAttributes& attributes)
244 if (stackCurrent->elementType==ElementTypeParagraph)
250 stackItem->elementType=ElementTypeAnchor;
251 stackItem->stackElementParagraph=stackCurrent->stackElementParagraph;
252 stackItem->stackElementText=stackCurrent->stackElementText;
253 stackItem->stackElementFormatsPlural=stackCurrent->stackElementFormatsPlural;
254 stackItem->pos=stackCurrent->pos;
255 stackItem->strTemp1=attributes.value(
"xlink:href").stripWhiteSpace();
256 stackItem->strTemp2=TQString();
260 if (stackItem->strTemp1[0]==
'#')
262 kdWarning(30506) <<
"Anchor <a> to bookmark: " << stackItem->strTemp1 << endl
263 <<
" Processing <a> like <c>" << endl;
265 return StartElementC(stackItem, stackCurrent, attributes);
270 kdError(30506) <<
"parse error <a> tag not a child of <p> but of "
271 << stackCurrent->itemName << endl;
277static bool charactersElementA (StackItem* stackItem,
const TQString & ch)
280 stackItem->strTemp2+=ch;
284static bool EndElementA (StackItem* stackItem, StackItem* stackCurrent, TQDomDocument& mainDocument)
286 if (!stackItem->elementType==ElementTypeAnchor)
288 kdError(30506) <<
"Wrong element type!! Aborting! (</a> in StructureParser::endElement)" << endl;
292 TQDomElement elementText=stackItem->stackElementText;
293 elementText.appendChild(mainDocument.createTextNode(
"#"));
295 TQDomElement formatElement=mainDocument.createElement(
"FORMAT");
296 formatElement.setAttribute(
"id",4);
297 formatElement.setAttribute(
"pos",stackItem->pos);
298 formatElement.setAttribute(
"len",1);
300 TQDomElement variableElement=mainDocument.createElement(
"VARIABLE");
301 formatElement.appendChild(variableElement);
303 TQDomElement typeElement=mainDocument.createElement(
"TYPE");
304 typeElement.setAttribute(
"key",
"STRING");
305 typeElement.setAttribute(
"type",9);
306 typeElement.setAttribute(
"text",stackItem->strTemp2);
307 variableElement.appendChild(typeElement);
309 TQDomElement linkElement=mainDocument.createElement(
"LINK");
310 linkElement.setAttribute(
"hrefName",stackItem->strTemp1);
311 linkElement.setAttribute(
"linkName",stackItem->strTemp2);
312 variableElement.appendChild(linkElement);
315 stackCurrent->stackElementFormatsPlural.appendChild(formatElement);
323bool StartElementP(StackItem* stackItem, StackItem* stackCurrent,
324 TQDomDocument& mainDocument,
325 StyleDataMap& styleDataMap,
const TQXmlAttributes& attributes)
328 TQString strStyle=attributes.value(
"style");
329 if (strStyle.isEmpty())
333 StyleDataMap::ConstIterator it=styleDataMap.useOrCreateStyle(strStyle);
335 TQString strLevel=attributes.value(
"level");
337 if (strLevel.isEmpty())
340 level=it.data().m_level;
345 level=strStyle.toInt();
348 TQDomElement elementText=stackCurrent->stackElementText;
349 TQDomElement paragraphElementOut=mainDocument.createElement(
"PARAGRAPH");
350 stackCurrent->m_frameset.appendChild(paragraphElementOut);
352 TQDomElement textElementOut=mainDocument.createElement(
"TEXT");
353 paragraphElementOut.appendChild(textElementOut);
354 TQDomElement formatsPluralElementOut=mainDocument.createElement(
"FORMATS");
355 paragraphElementOut.appendChild(formatsPluralElementOut);
357 AbiPropsMap abiPropsMap;
358 PopulateProperties(stackItem,it.data().m_props,attributes,abiPropsMap,
false);
360 stackItem->elementType=ElementTypeParagraph;
361 stackItem->stackElementParagraph=paragraphElementOut;
362 stackItem->stackElementText=textElementOut;
363 stackItem->stackElementFormatsPlural=formatsPluralElementOut;
367 TQDomElement layoutElement=mainDocument.createElement(
"LAYOUT");
368 paragraphElementOut.appendChild(layoutElement);
370 AddLayout(strStyle,layoutElement, stackItem, mainDocument, abiPropsMap, level,
false);
375bool charactersElementP (StackItem* stackItem, TQDomDocument& mainDocument,
const TQString & ch)
377 TQDomElement elementText=stackItem->stackElementText;
379 elementText.appendChild(mainDocument.createTextNode(ch));
381 stackItem->pos+=ch.length();
386bool EndElementP (StackItem* stackItem)
388 if (!stackItem->elementType==ElementTypeParagraph)
390 kdError(30506) <<
"Wrong element type!! Aborting! (in endElementP)" << endl;
393 stackItem->stackElementText.normalize();
397static bool StartElementField(StackItem* stackItem, StackItem* stackCurrent,
398 TQDomDocument& mainDocument,
const TQXmlAttributes& attributes)
401 if (stackCurrent->elementType==ElementTypeParagraph)
403 TQString strType=attributes.value(
"type").stripWhiteSpace();
404 kdDebug(30506)<<
"<field> type:"<<strType<<endl;
406 AbiPropsMap abiPropsMap;
407 PopulateProperties(stackItem,TQString(),attributes,abiPropsMap,
true);
409 stackItem->elementType=ElementTypeEmpty;
412 TQDomElement variableElement=mainDocument.createElement(
"VARIABLE");
414 if (!ProcessField(mainDocument, variableElement, strType, attributes))
418 kdWarning(30506) <<
"Unknown <field> type: " << strType << endl;
419 TQDomElement formatElement=mainDocument.createElement(
"FORMAT");
420 formatElement.setAttribute(
"id",1);
421 formatElement.setAttribute(
"pos",stackItem->pos);
422 formatElement.setAttribute(
"len",strType.length());
424 formatElement.appendChild(variableElement);
427 stackCurrent->stackElementFormatsPlural.appendChild(formatElement);
428 stackCurrent->stackElementText.appendChild(mainDocument.createTextNode(strType));
429 stackCurrent->pos+=strType.length();
432 stackItem->fgColor.setRgb(255,0,0);
433 AddFormat(formatElement, stackItem, mainDocument);
438 TQDomElement formatElement=mainDocument.createElement(
"FORMAT");
439 formatElement.setAttribute(
"id",4);
440 formatElement.setAttribute(
"pos",stackItem->pos);
441 formatElement.setAttribute(
"len",1);
443 formatElement.appendChild(variableElement);
446 stackCurrent->stackElementFormatsPlural.appendChild(formatElement);
447 stackCurrent->stackElementText.appendChild(mainDocument.createTextNode(
"#"));
451 AddFormat(formatElement, stackItem, mainDocument);
456 kdError(30506) <<
"parse error <field> tag not nested in <p> but in "
457 << stackCurrent->itemName << endl;
464static bool StartElementS(StackItem* stackItem, StackItem* ,
465 const TQXmlAttributes& attributes, StyleDataMap& styleDataMap)
469 stackItem->elementType=ElementTypeEmpty;
471 TQString strStyleName=attributes.value(
"name").stripWhiteSpace();
473 if (strStyleName.isEmpty())
475 kdWarning(30506) <<
"Style has no name!" << endl;
479 TQString strLevel=attributes.value(
"level");
481 if (strLevel.isEmpty())
484 level=strLevel.toInt();
485 TQString strBasedOn=attributes.value(
"basedon").simplifyWhiteSpace();
486 styleDataMap.defineNewStyleFromOld(strStyleName,strBasedOn,level,attributes.value(
"props"));
487 kdDebug(30506) <<
" Style name: " << strStyleName << endl
488 <<
" Based on: " << strBasedOn << endl
489 <<
" Level: " << level << endl
490 <<
" Props: " << attributes.value(
"props") << endl;
497bool StructureParser::StartElementImage(StackItem* stackItem, StackItem* stackCurrent,
498 const TQXmlAttributes& attributes)
501 if ((stackCurrent->elementType!=ElementTypeParagraph) && (stackCurrent->elementType!=ElementTypeContent))
503 kdError(30506) <<
"parse error <image> tag nested neither in <p> nor in <c> but in "
504 << stackCurrent->itemName << endl;
507 stackItem->elementType=ElementTypeEmpty;
509 TQString strDataId=attributes.value(
"dataid").stripWhiteSpace();
511 AbiPropsMap abiPropsMap;
512 abiPropsMap.splitAndAddAbiProps(attributes.value(
"props"));
514 double height=ValueWithLengthUnit(abiPropsMap[
"height"].getValue());
515 double width =ValueWithLengthUnit(abiPropsMap[
"width" ].getValue());
517 kdDebug(30506) <<
"Image: " << strDataId <<
" height: " << height <<
" width: " << width << endl;
521 if (strDataId.isEmpty())
523 kdWarning(30506) <<
"Image has no data id!" << endl;
527 kdDebug(30506) <<
"Image: " << strDataId << endl;
530 TQString strPictureFrameName(i18n(
"Frameset name",
"Picture %1").arg(++m_pictureFrameNumber));
534 TQDomElement framesetElement=mainDocument.createElement(
"FRAMESET");
535 framesetElement.setAttribute(
"frameType",2);
536 framesetElement.setAttribute(
"frameInfo",0);
537 framesetElement.setAttribute(
"visible",1);
538 framesetElement.setAttribute(
"name",strPictureFrameName);
539 framesetsPluralElement.appendChild(framesetElement);
541 TQDomElement frameElementOut=mainDocument.createElement(
"FRAME");
542 frameElementOut.setAttribute(
"left",0);
543 frameElementOut.setAttribute(
"top",0);
544 frameElementOut.setAttribute(
"bottom",height);
545 frameElementOut.setAttribute(
"right" ,width );
546 frameElementOut.setAttribute(
"runaround",1);
548 framesetElement.appendChild(frameElementOut);
550 TQDomElement element=mainDocument.createElement(
"PICTURE");
551 element.setAttribute(
"keepAspectRatio",
"true");
552 framesetElement.setAttribute(
"frameType",2);
553 framesetElement.appendChild(element);
555 TQDomElement key=mainDocument.createElement(
"KEY");
556 key.setAttribute(
"filename",strDataId);
557 key.setAttribute(
"year",m_timepoint.date().year());
558 key.setAttribute(
"month",m_timepoint.date().month());
559 key.setAttribute(
"day",m_timepoint.date().day());
560 key.setAttribute(
"hour",m_timepoint.time().hour());
561 key.setAttribute(
"minute",m_timepoint.time().minute());
562 key.setAttribute(
"second",m_timepoint.time().second());
563 key.setAttribute(
"msec",m_timepoint.time().msec());
564 element.appendChild(key);
567 TQDomElement elementText=stackItem->stackElementText;
568 TQDomElement elementFormatsPlural=stackItem->stackElementFormatsPlural;
569 elementText.appendChild(mainDocument.createTextNode(
"#"));
571 TQDomElement formatElementOut=mainDocument.createElement(
"FORMAT");
572 formatElementOut.setAttribute(
"id",6);
573 formatElementOut.setAttribute(
"pos",stackItem->pos);
574 formatElementOut.setAttribute(
"len",1);
575 elementFormatsPlural.appendChild(formatElementOut);
580 TQDomElement anchor=mainDocument.createElement(
"ANCHOR");
582 anchor.setAttribute(
"type",
"frameset");
583 anchor.setAttribute(
"instance",strPictureFrameName);
584 formatElementOut.appendChild(anchor);
590static bool StartElementD(StackItem* stackItem, StackItem* ,
591 const TQXmlAttributes& attributes)
595 stackItem->elementType=ElementTypeRealData;
597 TQString strName=attributes.value(
"name").stripWhiteSpace();
598 kdDebug(30506) <<
"Data: " << strName << endl;
600 TQString strBase64=attributes.value(
"base64").stripWhiteSpace();
601 TQString strMime=attributes.value(
"mime").stripWhiteSpace();
603 if (strName.isEmpty())
605 kdWarning(30506) <<
"Data has no name!" << endl;
606 stackItem->elementType=ElementTypeEmpty;
610 if (strMime.isEmpty())
617 stackItem->fontName=strName;
618 stackItem->bold=(strBase64==
"yes");
619 stackItem->strTemp1=strMime;
620 stackItem->strTemp2=TQString();
625static bool CharactersElementD (StackItem* stackItem, TQDomDocument& ,
const TQString & ch)
628 stackItem->strTemp2+=ch;
632bool StructureParser::EndElementD (StackItem* stackItem)
634 if (!stackItem->elementType==ElementTypeRealData)
636 kdError(30506) <<
"Wrong element type!! Aborting! (in endElementD)" << endl;
641 kdError(30506) <<
"No filter chain! Aborting! (in endElementD)" << endl;
650 if (stackItem->strTemp1==
"image/png")
654 else if (stackItem->strTemp1==
"image/jpeg")
658 else if (stackItem->strTemp1==
"image/svg-xml")
665 kdWarning(30506) <<
"Unknown or unsupported mime type: "
666 << stackItem->strTemp1 << endl;
670 TQString strStoreName;
671 strStoreName=
"pictures/picture";
672 strStoreName+=TQString::number(++m_pictureNumber);
673 strStoreName+=extension;
675 TQString strDataId=stackItem->fontName;
676 TQDomElement key=mainDocument.createElement(
"KEY");
677 key.setAttribute(
"filename",strDataId);
678 key.setAttribute(
"year",m_timepoint.date().year());
679 key.setAttribute(
"month",m_timepoint.date().month());
680 key.setAttribute(
"day",m_timepoint.date().day());
681 key.setAttribute(
"hour",m_timepoint.time().hour());
682 key.setAttribute(
"minute",m_timepoint.time().minute());
683 key.setAttribute(
"second",m_timepoint.time().second());
684 key.setAttribute(
"msec",m_timepoint.time().msec());
685 key.setAttribute(
"name",strStoreName);
686 m_picturesElement.appendChild(key);
688 KoStoreDevice* out=m_chain->storageFile(strStoreName, KoStore::Write);
691 kdError(30506) <<
"Unable to open output file for: " << stackItem->fontName <<
" Storage: " << strStoreName << endl;
697 kdDebug(30506) <<
"Decode and write base64 stream: " << stackItem->fontName << endl;
700 TQByteArray base64Stream=stackItem->strTemp2.utf8();
701 TQByteArray binaryStream;
702 KCodecs::base64Decode(base64Stream, binaryStream);
703 out->writeBlock(binaryStream, binaryStream.count());
708 kdDebug(30506) <<
"Write character stream: " << stackItem->fontName << endl;
710 TQCString strOut=stackItem->strTemp2.stripWhiteSpace().utf8();
711 out->writeBlock(strOut,strOut.length());
718static bool StartElementM(StackItem* stackItem, StackItem* ,
719 const TQXmlAttributes& attributes)
722 stackItem->elementType=ElementTypeRealMetaData;
724 TQString strKey=attributes.value(
"key").stripWhiteSpace();
725 kdDebug(30506) <<
"Metadata key: " << strKey << endl;
727 if (strKey.isEmpty())
729 kdWarning(30506) <<
"Metadata has no key!" << endl;
730 stackItem->elementType=ElementTypeIgnore;
734 stackItem->strTemp1=strKey;
735 stackItem->strTemp2=TQString();
740static bool CharactersElementM (StackItem* stackItem,
const TQString & ch)
743 stackItem->strTemp2+=ch;
747bool StructureParser::EndElementM (StackItem* stackItem)
749 if (!stackItem->elementType==ElementTypeRealData)
751 kdError(30506) <<
"Wrong element type!! Aborting! (in endElementM)" << endl;
755 if (stackItem->strTemp1.isEmpty())
758 kdError(30506) <<
"Key name was erased! Aborting! (in endElementM)" << endl;
763 m_metadataMap[stackItem->strTemp1]=stackItem->strTemp2;
769static bool StartElementBR(StackItem* stackItem, StackItem* stackCurrent,
770 TQDomDocument& mainDocument)
773 if ((stackCurrent->elementType==ElementTypeParagraph)
774 || (stackCurrent->elementType==ElementTypeContent))
776 stackItem->elementType=ElementTypeEmpty;
780 if (stackCurrent->elementType==ElementTypeContent)
783 TQDomElement formatElement=mainDocument.createElement(
"FORMAT");
784 formatElement.setAttribute(
"id",1);
785 formatElement.setAttribute(
"pos",stackCurrent->pos);
786 formatElement.setAttribute(
"len",1);
787 AddFormat(formatElement, stackCurrent, mainDocument);
788 stackCurrent->stackElementFormatsPlural.appendChild(formatElement);
791 stackCurrent->stackElementText.appendChild(mainDocument.createTextNode(TQChar(10)));
797 kdError(30506) <<
"parse error <br> tag not nested in <p> or <c> but in "
798 << stackCurrent->itemName << endl;
806static bool StartElementPBR(StackItem* , StackItem* stackCurrent,
807 TQDomDocument& mainDocument)
813 TQDomElement paragraphElementOut=mainDocument.createElement(
"PARAGRAPH");
814 stackCurrent->m_frameset.appendChild(paragraphElementOut);
815 TQDomElement textElementOut=mainDocument.createElement(
"TEXT");
816 paragraphElementOut.appendChild(textElementOut);
817 TQDomElement formatsPluralElementOut=mainDocument.createElement(
"FORMATS");
818 paragraphElementOut.appendChild(formatsPluralElementOut);
822 TQDomNodeList nodeList=stackCurrent->stackElementParagraph.elementsByTagName(
"LAYOUT");
824 if (!nodeList.count())
826 kdError(30506) <<
"Unable to find <LAYOUT> element! Aborting! (in StartElementPBR)" <<endl;
831 TQDomNode newNode=nodeList.item(0).cloneNode(
true);
832 if (newNode.isNull())
834 kdError(30506) <<
"Unable to clone <LAYOUT> element! Aborting! (in StartElementPBR)" <<endl;
837 paragraphElementOut.appendChild(newNode);
840 TQDomElement oldLayoutElement=nodeList.item(0).toElement();
841 if (oldLayoutElement.isNull())
843 kdError(30506) <<
"Cannot find old <LAYOUT> element! Aborting! (in StartElementPBR)" <<endl;
848 TQDomElement pagebreakingElement=mainDocument.createElement(
"PAGEBREAKING");
849 pagebreakingElement.setAttribute(
"linesTogether",
"false");
850 pagebreakingElement.setAttribute(
"hardFrameBreak",
"false");
851 pagebreakingElement.setAttribute(
"hardFrameBreakAfter",
"true");
852 oldLayoutElement.appendChild(pagebreakingElement);
857 stackCurrent->elementType=ElementTypeParagraph;
858 stackCurrent->stackElementParagraph=paragraphElementOut;
859 stackCurrent->stackElementText=textElementOut;
860 stackCurrent->stackElementFormatsPlural=formatsPluralElementOut;
867static bool StartElementPageSize(TQDomElement& paperElement,
const TQXmlAttributes& attributes)
869 if (attributes.value(
"page-scale").toDouble()!=1.0)
871 kdWarning(30506) <<
"Ignoring unsupported page scale: " << attributes.value(
"page-scale") << endl;
874 int kwordOrientation;
875 TQString strOrientation=attributes.value(
"orientation").stripWhiteSpace();
877 if (strOrientation==
"portrait")
881 else if (strOrientation==
"landscape")
887 kdWarning(30506) <<
"Unknown page orientation: " << strOrientation <<
"! Ignoring! " << endl;
894 TQString strPageType=attributes.value(
"pagetype").stripWhiteSpace();
899 KoFormat kwordFormat = KoPageFormat::formatFromString(strPageType);
901 if (kwordFormat==PG_CUSTOM)
903 kdDebug(30506) <<
"Custom or other page format found: " << strPageType << endl;
905 double height = attributes.value(
"height").toDouble();
906 double width = attributes.value(
"width" ).toDouble();
908 TQString strUnits = attributes.value(
"units").stripWhiteSpace();
910 kdDebug(30506) <<
"Explicit page size: "
911 << height <<
" " << strUnits <<
" x " << width <<
" " << strUnits
916 kwordHeight = CentimetresToPoints(height);
917 kwordWidth = CentimetresToPoints(width);
919 else if (strUnits==
"inch")
921 kwordHeight = InchesToPoints(height);
922 kwordWidth = InchesToPoints(width);
924 else if (strUnits==
"mm")
926 kwordHeight = MillimetresToPoints(height);
927 kwordWidth = MillimetresToPoints(width);
933 kdWarning(30506) <<
"Unknown unit type: " << strUnits << endl;
939 kwordHeight = MillimetresToPoints(KoPageFormat::height(kwordFormat,PG_PORTRAIT));
940 kwordWidth = MillimetresToPoints(KoPageFormat::width (kwordFormat,PG_PORTRAIT));
943 if ((kwordHeight <= 1.0) || (kwordWidth <= 1.0))
946 kdWarning(30506) <<
"Page width or height is too small: "
947 << kwordHeight <<
"x" << kwordWidth << endl;
949 kwordFormat = PG_DIN_A4;
950 kwordHeight = CentimetresToPoints(29.7);
951 kwordWidth = CentimetresToPoints(21.0);
956 if (paperElement.isNull())
958 kdError(30506) <<
"<PAPER> element cannot be accessed! Aborting!" << endl;
962 paperElement.setAttribute(
"format",kwordFormat);
963 paperElement.setAttribute(
"width",kwordWidth);
964 paperElement.setAttribute(
"height",kwordHeight);
965 paperElement.setAttribute(
"orientation",kwordOrientation);
971bool StructureParser::complexForcedPageBreak(StackItem* stackItem)
975 StackItemStack auxilaryStack;
977 if (!clearStackUntilParagraph(auxilaryStack))
979 kdError(30506) <<
"Could not clear stack until a paragraph!" << endl;
985 bool success=StartElementPBR(stackItem,structureStack.current(),mainDocument);
989 StackItem* stackCurrent=structureStack.current();
991 while (auxilaryStack.count()>0)
993 item=auxilaryStack.pop();
997 item->stackElementParagraph=stackCurrent->stackElementParagraph;
998 item->stackElementText=stackCurrent->stackElementText;
999 item->stackElementFormatsPlural=stackCurrent->stackElementFormatsPlural;
1000 structureStack.push(item);
1008bool StructureParser::StartElementSection(StackItem* stackItem, StackItem* ,
1009 const TQXmlAttributes& attributes)
1012 stackItem->elementType=ElementTypeSection;
1014 AbiPropsMap abiPropsMap;
1016 kdDebug(30506)<<
"========== props=\"" << attributes.value(
"props") <<
"\"" << endl;
1017 abiPropsMap.splitAndAddAbiProps(attributes.value(
"props"));
1018 abiPropsMap.splitAndAddAbiProps(attributes.value(
"PROPS"));
1022 if (
true && (!m_paperBordersElement.isNull()))
1025 str=abiPropsMap[
"page-margin-top"].getValue();
1028 m_paperBordersElement.setAttribute(
"top",ValueWithLengthUnit(str));
1030 str=abiPropsMap[
"page-margin-left"].getValue();
1033 m_paperBordersElement.setAttribute(
"left",ValueWithLengthUnit(str));
1035 str=abiPropsMap[
"page-margin-bottom"].getValue();
1038 m_paperBordersElement.setAttribute(
"bottom",ValueWithLengthUnit(str));
1040 str=abiPropsMap[
"page-margin-right"].getValue();
1043 m_paperBordersElement.setAttribute(
"right",ValueWithLengthUnit(str));
1051static bool EndElementIW(StackItem* stackItem, StackItem* ,
1052 TQDomDocument& mainDocument, TQDomElement& m_ignoreWordsElement)
1054 if (!stackItem->elementType==ElementTypeIgnoreWord)
1056 kdError(30506) <<
"Wrong element type!! Aborting! (in endElementIW)" << endl;
1059 TQDomElement wordElement=mainDocument.createElement(
"SPELLCHECKIGNOREWORD");
1060 wordElement.setAttribute(
"word",stackItem->strTemp2.stripWhiteSpace());
1061 m_ignoreWordsElement.appendChild(wordElement);
1066bool StructureParser::StartElementFoot(StackItem* stackItem, StackItem* ,
1067 const TQXmlAttributes& )
1070 stackItem->elementType=ElementTypeFoot;
1072 const TQString id(attributes.value(
"endnote-id").stripWhiteSpace());
1073 kdDebug(30506) <<
"Foot note id: " <<
id << endl;
1077 kdWarning(30506) <<
"Footnote has no id!" << endl;
1078 stackItem->elementType=ElementTypeIgnore;
1083 TQDomElement framesetElement(mainDocument.createElement(
"FRAMESET"));
1084 framesetElement.setAttribute(
"frameType",1);
1085 framesetElement.setAttribute(
"frameInfo",7);
1086 framesetElement.setAttribute(
"visible",1);
1087 framesetElement.setAttribute(
"name",getFootnoteFramesetName(
id));
1088 framesetsPluralElement.appendChild(framesetElement);
1090 TQDomElement frameElementOut(mainDocument.createElement(
"FRAME"));
1095 frameElementOut.setAttribute(
"runaround",1);
1097 framesetElement.appendChild(frameElementOut);
1099 stackItem->m_frameset=framesetElement;
1101 stackItem->elementType=ElementTypeIgnore;
1107bool StructureParser::StartElementTable(StackItem* stackItem, StackItem* stackCurrent,
1108 const TQXmlAttributes& attributes)
1114 TQStringList widthList;
1115 widthList.split(
'/', attributes.value(
"table-column-props"),
false);
1116 const uint columns = widthList.size();
1117 stackItem->m_doubleArray.detach();
1118 stackItem->m_doubleArray.resize(columns+1);
1119 stackItem->m_doubleArray[0] = 0.0;
1120 TQStringList::ConstIterator it;
1122 for ( i=0, it=widthList.begin(); i<columns; ++i, ++it )
1124 kdDebug(30506) <<
"Column width: " << (*it) <<
" cooked " << ValueWithLengthUnit(*it) << endl;
1125 stackItem->m_doubleArray[i+1] = ValueWithLengthUnit(*it) + stackItem->m_doubleArray[i];
1129 const uint tableNumber(++m_tableGroupNumber);
1130 const TQString tableName(i18n(
"Table %1").arg(tableNumber));
1132 TQDomElement elementText=stackCurrent->stackElementText;
1133 TQDomElement paragraphElementOut=mainDocument.createElement(
"PARAGRAPH");
1134 stackCurrent->m_frameset.appendChild(paragraphElementOut);
1136 TQDomElement textElementOut(mainDocument.createElement(
"TEXT"));
1137 textElementOut.appendChild(mainDocument.createTextNode(
"#"));
1138 paragraphElementOut.appendChild(textElementOut);
1140 TQDomElement formatsPluralElementOut=mainDocument.createElement(
"FORMATS");
1141 paragraphElementOut.appendChild(formatsPluralElementOut);
1143 TQDomElement elementFormat(mainDocument.createElement(
"FORMAT"));
1144 elementFormat.setAttribute(
"id",6);
1145 elementFormat.setAttribute(
"pos",0);
1146 elementFormat.setAttribute(
"len",1);
1147 formatsPluralElementOut.appendChild(elementFormat);
1149 TQDomElement elementAnchor(mainDocument.createElement(
"ANCHOR"));
1150 elementAnchor.setAttribute(
"type",
"frameset");
1151 elementAnchor.setAttribute(
"instance",tableName);
1152 elementFormat.appendChild(elementAnchor);
1154 stackItem->elementType=ElementTypeTable;
1155 stackItem->stackElementParagraph=paragraphElementOut;
1156 stackItem->stackElementText=textElementOut;
1157 stackItem->stackElementFormatsPlural=formatsPluralElementOut;
1158 stackItem->strTemp1=tableName;
1159 stackItem->strTemp2=TQString::number(tableNumber);
1163 TQDomElement layoutElement=mainDocument.createElement(
"LAYOUT");
1164 paragraphElementOut.appendChild(layoutElement);
1166 AbiPropsMap abiPropsMap;
1167 styleDataMap.useOrCreateStyle(
"Normal");
1168 AddLayout(
"Normal", layoutElement, stackItem, mainDocument, abiPropsMap, 0,
false);
1170 stackItem->elementType=ElementTypeIgnore;
1176bool StructureParser::StartElementCell(StackItem* stackItem, StackItem* stackCurrent,
1177 const TQXmlAttributes& attributes)
1180 if (stackCurrent->elementType!=ElementTypeTable)
1182 kdError(30506) <<
"Wrong element type!! Aborting! (in StructureParser::endElementCell)" << endl;
1186 stackItem->elementType=ElementTypeCell;
1188 const TQString tableName(stackCurrent->strTemp1);
1189 kdDebug(30506) <<
"Table name: " << tableName << endl;
1191 if (tableName.isEmpty())
1193 kdError(30506) <<
"Table name is empty! Aborting!" << endl;
1197 AbiPropsMap abiPropsMap;
1198 abiPropsMap.splitAndAddAbiProps(attributes.value(
"props"));
1201 const uint row=abiPropsMap[
"top-attach"].getValue().toUInt();
1202 const uint col=abiPropsMap[
"left-attach"].getValue().toUInt();
1204 if ( col >= stackItem->m_doubleArray.size() )
1209 stackItem->m_doubleArray.resize( stackItem->m_doubleArray.size() + 1, TQGArray::SpeedOptim );
1210 stackItem->m_doubleArray[col+1] = stackItem->m_doubleArray[col] + 72;
1213 const TQString frameName(i18n(
"Frameset name",
"Table %3, row %1, column %2")
1214 .arg(row).arg(col).arg(stackCurrent->strTemp2));
1217 TQDomElement framesetElement(mainDocument.createElement(
"FRAMESET"));
1218 framesetElement.setAttribute(
"frameType",1);
1219 framesetElement.setAttribute(
"frameInfo",0);
1220 framesetElement.setAttribute(
"visible",1);
1221 framesetElement.setAttribute(
"name",frameName);
1222 framesetElement.setAttribute(
"row",row);
1223 framesetElement.setAttribute(
"col",col);
1224 framesetElement.setAttribute(
"rows",1);
1225 framesetElement.setAttribute(
"cols",1);
1226 framesetElement.setAttribute(
"grpMgr",tableName);
1227 framesetsPluralElement.appendChild(framesetElement);
1229 TQDomElement frameElementOut(mainDocument.createElement(
"FRAME"));
1230 frameElementOut.setAttribute(
"left", stackItem->m_doubleArray[col] );
1231 frameElementOut.setAttribute(
"right", stackItem->m_doubleArray[col+1] );
1232 frameElementOut.setAttribute(
"top",0);
1233 frameElementOut.setAttribute(
"bottom",0);
1234 frameElementOut.setAttribute(
"runaround",1);
1235 frameElementOut.setAttribute(
"autoCreateNewFrame",0);
1237 framesetElement.appendChild(frameElementOut);
1239 stackItem->m_frameset=framesetElement;
1240 TQDomElement nullDummy;
1241 stackItem->stackElementParagraph=nullDummy;
1242 stackItem->stackElementText=nullDummy;
1243 stackItem->stackElementFormatsPlural=nullDummy;
1246 stackItem->elementType=ElementTypeIgnore;
1253bool StructureParser :: startElement(
const TQString&,
const TQString&,
const TQString& name,
const TQXmlAttributes& attributes)
1256 kdDebug(30506) << indent <<
" <" << name <<
">" << endl;
1259 if (structureStack.isEmpty())
1261 kdError(30506) <<
"Stack is empty!! Aborting! (in StructureParser::startElement)" << endl;
1266 StackItem *stackItem=
new StackItem(*structureStack.current());
1270 kdError(30506) <<
"Could not create Stack Item! Aborting! (in StructureParser::startElement)" << endl;
1274 stackItem->itemName=name;
1278 if ((name==
"c")||(name==
"C"))
1280 success=StartElementC(stackItem,structureStack.current(),attributes);
1282 else if ((name==
"p")||(name==
"P"))
1284 success=StartElementP(stackItem,structureStack.current(),mainDocument,
1285 styleDataMap,attributes);
1287 else if ((name==
"section")||(name==
"SECTION"))
1289 success=StartElementSection(stackItem,structureStack.current(),attributes);
1293 success=StartElementA(stackItem,structureStack.current(),attributes);
1295 else if (name==
"br")
1298 StackItem* stackCurrent=structureStack.current();
1299 success=StartElementBR(stackItem,stackCurrent,mainDocument);
1301 else if (name==
"cbr")
1304 stackItem->elementType=ElementTypeEmpty;
1305 StackItem* stackCurrent=structureStack.current();
1306 if (stackCurrent->elementType==ElementTypeContent)
1308 kdWarning(30506) <<
"Forced column break found! Transforming to forced page break" << endl;
1309 success=complexForcedPageBreak(stackItem);
1311 else if (stackCurrent->elementType==ElementTypeParagraph)
1313 kdWarning(30506) <<
"Forced column break found! Transforming to forced page break" << endl;
1314 success=StartElementPBR(stackItem,stackCurrent,mainDocument);
1318 kdError(30506) <<
"Forced column break found out of turn! Aborting! Parent: "
1319 << stackCurrent->itemName <<endl;
1323 else if (name==
"pbr")
1326 stackItem->elementType=ElementTypeEmpty;
1327 StackItem* stackCurrent=structureStack.current();
1328 if (stackCurrent->elementType==ElementTypeContent)
1330 success=complexForcedPageBreak(stackItem);
1332 else if (stackCurrent->elementType==ElementTypeParagraph)
1334 success=StartElementPBR(stackItem,stackCurrent,mainDocument);
1338 kdError(30506) <<
"Forced page break found out of turn! Aborting! Parent: "
1339 << stackCurrent->itemName <<endl;
1343 else if (name==
"pagesize")
1346 stackItem->elementType=ElementTypeEmpty;
1347 stackItem->stackElementText=structureStack.current()->stackElementText;
1348 success=StartElementPageSize(m_paperElement,attributes);
1350 else if ((name==
"field")
1353 success=StartElementField(stackItem,structureStack.current(),mainDocument,attributes);
1357 success=StartElementS(stackItem,structureStack.current(),attributes,styleDataMap);
1359 else if ((name==
"image")
1362 success=StartElementImage(stackItem,structureStack.current(),attributes);
1366 success=StartElementD(stackItem,structureStack.current(),attributes);
1368 else if (name==
"iw")
1370 stackItem->elementType=ElementTypeIgnoreWord;
1375 success=StartElementM(stackItem,structureStack.current(),attributes);
1377 else if (name==
"foot")
1379 success=StartElementFoot(stackItem,structureStack.current(),attributes);
1381 else if (name==
"table")
1383 success=StartElementTable(stackItem,structureStack.current(), attributes);
1385 else if (name==
"cell")
1387 success=StartElementCell(stackItem,structureStack.current(),attributes);
1391 stackItem->elementType=ElementTypeUnknown;
1392 stackItem->stackElementText=structureStack.current()->stackElementText;
1397 structureStack.push(stackItem);
1406bool StructureParser :: endElement(
const TQString&,
const TQString& ,
const TQString& name)
1408 indent.remove( 0, 1 );
1409 kdDebug(30506) << indent <<
" </" << name <<
">" << endl;
1411 if (structureStack.isEmpty())
1413 kdError(30506) <<
"Stack is empty!! Aborting! (in StructureParser::endElement)" << endl;
1419 StackItem *stackItem=structureStack.pop();
1420 if ((name==
"c")||(name==
"C"))
1422 success=EndElementC(stackItem,structureStack.current());
1424 else if ((name==
"p")||(name==
"P"))
1426 success=EndElementP(stackItem);
1430 if (stackItem->elementType==ElementTypeContent)
1433 success=EndElementC(stackItem,structureStack.current());
1438 success=EndElementA(stackItem,structureStack.current(), mainDocument);
1443 success=EndElementD(stackItem);
1445 else if (name==
"iw")
1447 success=EndElementIW(stackItem,structureStack.current(), mainDocument, m_ignoreWordsElement);
1451 success=EndElementM(stackItem);
1460 kdError(30506) <<
"Found tag name: " << name
1461 <<
" expected: " << stackItem->itemName << endl;
1467bool StructureParser :: characters (
const TQString & ch )
1472 kdDebug(30506) << indent <<
" (LINEFEED)" << endl;
1474 else if (ch.length()> 40)
1476 kdDebug(30506) << indent <<
" :" << ch.left(40) <<
"..." << endl;
1480 kdDebug(30506) << indent <<
" :" << ch <<
":" << endl;
1483 if (structureStack.isEmpty())
1485 kdError(30506) <<
"Stack is empty!! Aborting! (in StructureParser::characters)" << endl;
1491 StackItem *stackItem=structureStack.current();
1493 if ((stackItem->elementType==ElementTypeContent)
1494 || (stackItem->elementType==ElementTypeAnchorContent))
1496 success=charactersElementC(stackItem,mainDocument,ch);
1498 else if (stackItem->elementType==ElementTypeParagraph)
1500 success=charactersElementP(stackItem,mainDocument,ch);
1502 else if (stackItem->elementType==ElementTypeAnchor)
1504 success=charactersElementA(stackItem,ch);
1506 else if (stackItem->elementType==ElementTypeEmpty)
1508 success=ch.stripWhiteSpace().isEmpty();
1512 kdError(30506) <<
"Empty element "<< stackItem->itemName
1513 <<
" is not empty! Aborting! (in StructureParser::characters)" << endl;
1516 else if (stackItem->elementType==ElementTypeRealData)
1518 success=CharactersElementD(stackItem,mainDocument,ch);
1520 else if (stackItem->elementType==ElementTypeIgnoreWord)
1522 stackItem->strTemp2+=ch;
1525 else if (stackItem->elementType==ElementTypeRealMetaData)
1527 success=CharactersElementM(stackItem,ch);
1537bool StructureParser::startDocument(
void)
1539 indent = TQString();
1540 styleDataMap.defineDefaultStyles();
1544void StructureParser::createDocInfo(
void)
1546 TQDomImplementation implementation;
1547 TQDomDocument doc(implementation.createDocumentType(
"document-info",
1548 "-//KDE//DTD document-info 1.2//EN",
"http://www.koffice.org/DTD/document-info-1.2.dtd"));
1553 mainDocument.createProcessingInstruction(
1554 "xml",
"version=\"1.0\" encoding=\"UTF-8\""));
1556 TQDomElement elementDoc(mainDocument.createElement(
"document-info"));
1557 elementDoc.setAttribute(
"xmlns",
"http://www.koffice.org/DTD/document-info");
1558 m_info.appendChild(elementDoc);
1560 TQDomElement about(mainDocument.createElement(
"about"));
1561 elementDoc.appendChild(about);
1563 TQDomElement abstract(mainDocument.createElement(
"abstract"));
1564 about.appendChild(abstract);
1565 abstract.appendChild(mainDocument.createTextNode(m_metadataMap[
"dc.description"]));
1567 TQDomElement title(mainDocument.createElement(
"title"));
1568 about.appendChild(title);
1569 title.appendChild(mainDocument.createTextNode(m_metadataMap[
"dc.title"]));
1571 TQDomElement keyword(mainDocument.createElement(
"keyword"));
1572 about.appendChild(keyword);
1573 keyword.appendChild(mainDocument.createTextNode(m_metadataMap[
"abiword.keywords"]));
1575 TQDomElement subject(mainDocument.createElement(
"subject"));
1576 about.appendChild(subject);
1577 subject.appendChild(mainDocument.createTextNode(m_metadataMap[
"dc.subject"]));
1580bool StructureParser::endDocument(
void)
1582 TQDomElement stylesPluralElement=mainDocument.createElement(
"STYLES");
1584 mainDocument.documentElement().insertBefore(stylesPluralElement,m_picturesElement);
1586 kdDebug(30506) <<
"###### Start Style List ######" << endl;
1587 StyleDataMap::ConstIterator it;
1590 it=styleDataMap.find(
"Normal");
1591 if (it!=styleDataMap.end())
1593 kdDebug(30506) <<
"\"" << it.key() <<
"\" => " << it.data().m_props << endl;
1594 TQDomElement styleElement=mainDocument.createElement(
"STYLE");
1595 stylesPluralElement.appendChild(styleElement);
1596 AddStyle(styleElement, it.key(),it.data(),mainDocument);
1599 kdWarning(30506) <<
"No 'Normal' style" << endl;
1601 for (it=styleDataMap.begin();it!=styleDataMap.end();++it)
1603 if (it.key()==
"Normal")
1606 kdDebug(30506) <<
"\"" << it.key() <<
"\" => " << it.data().m_props << endl;
1608 TQDomElement styleElement=mainDocument.createElement(
"STYLE");
1609 stylesPluralElement.appendChild(styleElement);
1611 AddStyle(styleElement, it.key(),it.data(),mainDocument);
1613 kdDebug(30506) <<
"###### End Style List ######" << endl;
1620bool StructureParser::warning(
const TQXmlParseException& exception)
1622 kdWarning(30506) <<
"XML parsing warning: line " << exception.lineNumber()
1623 <<
" col " << exception.columnNumber() <<
" message: " << exception.message() << endl;
1627bool StructureParser::error(
const TQXmlParseException& exception)
1630 kdWarning(30506) <<
"XML parsing error: line " << exception.lineNumber()
1631 <<
" col " << exception.columnNumber() <<
" message: " << exception.message() << endl;
1635bool StructureParser::fatalError (
const TQXmlParseException& exception)
1637 kdError(30506) <<
"XML parsing fatal error: line " << exception.lineNumber()
1638 <<
" col " << exception.columnNumber() <<
" message: " << exception.message() << endl;
1640 KMessageBox::error(NULL, i18n(
"An error has occurred while parsing the AbiWord file.\nAt line: %1, column %2\nError message: %3")
1641 .arg(exception.lineNumber()).arg(exception.columnNumber())
1642 .arg( i18n(
"TQXml", exception.message().utf8() ) ),
1643 i18n(
"AbiWord Import Filter"),0);
1647void StructureParser :: createDocument(
void)
1649 TQDomImplementation implementation;
1650 TQDomDocument doc(implementation.createDocumentType(
"DOC",
1651 "-//KDE//DTD kword 1.2//EN",
"http://www.koffice.org/DTD/kword-1.2.dtd"));
1655 mainDocument.appendChild(
1656 mainDocument.createProcessingInstruction(
1657 "xml",
"version=\"1.0\" encoding=\"UTF-8\""));
1659 TQDomElement elementDoc;
1660 elementDoc=mainDocument.createElement(
"DOC");
1661 elementDoc.setAttribute(
"xmlns",
"http://www.koffice.org/DTD/kword");
1662 elementDoc.setAttribute(
"editor",
"AbiWord Import Filter");
1663 elementDoc.setAttribute(
"mime",
"application/x-kword");
1664 elementDoc.setAttribute(
"syntaxVersion", 3 );
1665 mainDocument.appendChild(elementDoc);
1667 TQDomElement element;
1668 element=mainDocument.createElement(
"ATTRIBUTES");
1669 element.setAttribute(
"processing",0);
1670 element.setAttribute(
"standardpage",1);
1671 element.setAttribute(
"hasHeader",0);
1672 element.setAttribute(
"hasFooter",0);
1674 element.setAttribute(
"tabStopValue",36);
1675 elementDoc.appendChild(element);
1679 m_paperElement=mainDocument.createElement(
"PAPER");
1680 m_paperElement.setAttribute(
"format",PG_US_LETTER);
1681 m_paperElement.setAttribute(
"width",MillimetresToPoints(KoPageFormat::width (PG_US_LETTER,PG_PORTRAIT)));
1682 m_paperElement.setAttribute(
"height",MillimetresToPoints(KoPageFormat::height(PG_US_LETTER,PG_PORTRAIT)));
1683 m_paperElement.setAttribute(
"orientation",PG_PORTRAIT);
1684 m_paperElement.setAttribute(
"columns",1);
1685 m_paperElement.setAttribute(
"columnspacing",2);
1686 m_paperElement.setAttribute(
"hType",0);
1687 m_paperElement.setAttribute(
"fType",0);
1688 m_paperElement.setAttribute(
"spHeadBody",9);
1689 m_paperElement.setAttribute(
"spFootBody",9);
1690 m_paperElement.setAttribute(
"zoom",100);
1691 elementDoc.appendChild(m_paperElement);
1693 m_paperBordersElement=mainDocument.createElement(
"PAPERBORDERS");
1694 m_paperBordersElement.setAttribute(
"left",28);
1695 m_paperBordersElement.setAttribute(
"top",42);
1696 m_paperBordersElement.setAttribute(
"right",28);
1697 m_paperBordersElement.setAttribute(
"bottom",42);
1698 m_paperElement.appendChild(m_paperBordersElement);
1700 framesetsPluralElement=mainDocument.createElement(
"FRAMESETS");
1701 mainDocument.documentElement().appendChild(framesetsPluralElement);
1703 mainFramesetElement=mainDocument.createElement(
"FRAMESET");
1704 mainFramesetElement.setAttribute(
"frameType",1);
1705 mainFramesetElement.setAttribute(
"frameInfo",0);
1706 mainFramesetElement.setAttribute(
"visible",1);
1707 mainFramesetElement.setAttribute(
"name",i18n(
"Frameset name",
"Main Text Frameset"));
1708 framesetsPluralElement.appendChild(mainFramesetElement);
1710 TQDomElement frameElementOut=mainDocument.createElement(
"FRAME");
1711 frameElementOut.setAttribute(
"left",28);
1712 frameElementOut.setAttribute(
"top",42);
1713 frameElementOut.setAttribute(
"bottom",566);
1714 frameElementOut.setAttribute(
"right",798);
1715 frameElementOut.setAttribute(
"runaround",1);
1717 mainFramesetElement.appendChild(frameElementOut);
1720 m_ignoreWordsElement=mainDocument.createElement(
"SPELLCHECKIGNORELIST");
1721 mainDocument.documentElement().appendChild(m_ignoreWordsElement);
1722 m_picturesElement=mainDocument.createElement(
"PICTURES");
1723 mainDocument.documentElement().appendChild(m_picturesElement);
1726bool StructureParser::clearStackUntilParagraph(StackItemStack& auxilaryStack)
1730 StackItem* item=structureStack.pop();
1731 switch (item->elementType)
1733 case ElementTypeContent:
1736 auxilaryStack.push(item);
1739 case ElementTypeParagraph:
1742 structureStack.push(item);
1748 kdError(30506) <<
"Cannot clear this element: "
1749 << item->itemName << endl;
1756ABIWORDImport::ABIWORDImport(KoFilter *,
const char *,
const TQStringList &) :
1760KoFilter::ConversionStatus ABIWORDImport::convert(
const TQCString& from,
const TQCString& to )
1762 if ((to !=
"application/x-kword") || (from !=
"application/x-abiword"))
1763 return KoFilter::NotImplemented;
1765 kdDebug(30506)<<
"AbiWord to KWord Import filter"<<endl;
1767 StructureParser handler(m_chain);
1770 TQXmlSimpleReader reader;
1771 reader.setContentHandler( &handler );
1772 reader.setErrorHandler( &handler );
1776 TQString fileIn = m_chain->inputFile();
1777 const int result=fileIn.findRev(
'.');
1780 strExt=fileIn.mid(result);
1783 kdDebug(30506) <<
"File extension: -" << strExt <<
"-" << endl;
1787 if ((strExt==
".gz")||(strExt==
".GZ")
1788 ||(strExt==
".zabw")||(strExt==
".ZABW"))
1791 strMime=
"application/x-gzip";
1792 kdDebug(30506) <<
"Compression: gzip" << endl;
1794 else if ((strExt==
".bz2")||(strExt==
".BZ2")
1795 ||(strExt==
".bzabw")||(strExt==
".BZABW"))
1798 strMime=
"application/x-bzip2";
1799 kdDebug(30506) <<
"Compression: bzip2" << endl;
1802 TQIODevice* in = KFilterDev::deviceForFile(fileIn,strMime);
1806 kdError(30506) <<
"Cannot create device for uncompressing! Aborting!" << endl;
1807 return KoFilter::FileNotFound;
1810 if (!in->open(IO_ReadOnly))
1812 kdError(30506) <<
"Cannot open file for uncompressing! Aborting!" << endl;
1814 return KoFilter::FileNotFound;
1817 TQXmlInputSource source(in);
1821 if (!reader.parse( source ))
1823 kdError(30506) <<
"Import: Parsing unsuccessful. Aborting!" << endl;
1825 if (!handler.wasFatalError())
1828 KMessageBox::error(NULL, i18n(
"An error occurred during the load of the AbiWord file: %1").arg(from.data()),
1829 i18n(
"AbiWord Import Filter"),0);
1831 return KoFilter::ParsingError;
1838 kdDebug(30506) <<
"Creating documentinfo.xml" << endl;
1839 out=m_chain->storageFile(
"documentinfo.xml", KoStore::Write );
1842 kdError(30506) <<
"AbiWord Import unable to open output file! (Documentinfo)" << endl;
1843 KMessageBox::error(NULL, i18n(
"Unable to save document information."),i18n(
"AbiWord Import Filter"),0);
1844 return KoFilter::StorageCreationError;
1848 strOut=handler.getDocInfo().toCString();
1850 out->writeBlock(strOut,strOut.length());
1852 kdDebug(30506) <<
"Creating maindoc.xml" << endl;
1853 out=m_chain->storageFile(
"root", KoStore::Write );
1856 kdError(30506) <<
"AbiWord Import unable to open output file! (Root)" << endl;
1857 KMessageBox::error(NULL, i18n(
"Unable to save main document."),i18n(
"AbiWord Import Filter"),0);
1858 return KoFilter::StorageCreationError;
1862 strOut=handler.getDocument().toCString();
1864 out->writeBlock(strOut,strOut.length());
1867 kdDebug(30506) << documentOut.toString();
1870 kdDebug(30506) <<
"Now importing to KWord!" << endl;
1872 return KoFilter::OK;
1875#include "abiwordimport.moc"
Helpers for the AbiWord import filter.