24#include <kgenericfactory.h>
26#include <tdetempfile.h>
28#include <KoFilterChain.h>
29#include <KoStoreDevice.h>
31#include "kword13parser.h"
32#include "kword13document.h"
33#include "kword13oasisgenerator.h"
34#include "kword13postparsing.h"
35#include "kword13import.h"
37typedef KGenericFactory<KWord13Import, KoFilter> KWord13ImportFactory;
38K_EXPORT_COMPONENT_FACTORY( libkwordkword1dot3import, KWord13ImportFactory(
"kofficefilters" ) )
41KWord13Import::KWord13Import(KoFilter *, const
char *, const TQStringList &)
46bool KWord13Import::parseInfo( TQIODevice* io, KWord13Document& kwordDocument )
48 kdDebug(30520) <<
"Starting KWord13Import::parseInfo" << endl;
52 int errorLine, errorColumn;
53 if ( ! doc.setContent( io, &errorMsg, &errorLine, &errorColumn ) )
55 kdError(30520) <<
"Parsing error in documentinfo.xml! Aborting!" << endl
56 <<
" In line: " << errorLine <<
", column: " << errorColumn << endl
57 <<
" Error message: " << errorMsg << endl;
61 TQDomElement docElement( doc.documentElement() );
63 for ( TQDomNode node = docElement.firstChild(); !node.isNull(); node = node.nextSibling() )
65 kdDebug(30520) <<
"Child " << node.nodeName() << endl;
66 if ( !node.isElement() )
68 const TQString nodeName( node.nodeName() );
69 for ( TQDomNode node2 = node.firstChild(); !node2.isNull(); node2 = node2.nextSibling() )
71 kdDebug(30520) <<
"Grand-child " << node2.nodeName() << endl;
72 if ( !node2.isElement() )
74 const TQString nodeName2 ( nodeName +
':' + node2.nodeName() );
75 TQDomElement element( node2.toElement() );
76 kwordDocument.m_documentInfo[ nodeName2 ] = element.text();
79 kdDebug(30520) <<
"Quitting KWord13Import::parseInfo" << endl;
83bool KWord13Import::parseRoot( TQIODevice* io, KWord13Document& kwordDocument )
85 KWord13Parser handler( &kwordDocument );
87 TQXmlSimpleReader reader;
88 reader.setContentHandler( &handler );
89 reader.setErrorHandler( &handler );
91 TQXmlInputSource source( io );
93 if (!reader.parse( source ))
95 kdWarning(30520) <<
"Parse Error" << endl;
101bool KWord13Import::postParse( KoStore* store, KWord13Document& doc )
103 KWord13PostParsing post;
104 return post.postParse( store, doc );
107KoFilter::ConversionStatus KWord13Import::convert(
const TQCString& from,
const TQCString& to )
109 if ( to !=
"application/vnd.oasis.opendocument.text"
110 || from !=
"application/x-kword" )
112 return KoFilter::NotImplemented;
116 KImageIO::registerFormats();
118 KWord13Document kwordDocument;
120 const TQString fileName( m_chain->inputFile() );
121 if ( fileName.isEmpty() )
123 kdError(30520) <<
"No input file name!" << endl;
124 return KoFilter::StupidError;
127 KoStore* store = KoStore::createStore( fileName, KoStore::Read );
128 if ( store && store->hasFile(
"maindoc.xml" ) )
130 kdDebug(30520) <<
"Maindoc.xml found in KoStore!" << endl;
133 store->open(
"documentinfo.xml" );
134 KoStoreDevice ioInfo( store );
135 ioInfo.open( IO_ReadOnly );
136 kdDebug (30520) <<
"Processing document info... " << endl;
137 if ( ! parseInfo ( &ioInfo, kwordDocument ) )
139 kdWarning(30520) <<
"Parsing documentinfo.xml has failed. Ignoring!" << endl;
145 if ( ! store->open(
"maindoc.xml" ) )
147 kdError(30520) <<
"Opening root has failed" << endl;
149 return KoFilter::StupidError;
151 KoStoreDevice ioMain( store );
152 ioMain.open( IO_ReadOnly );
153 kdDebug (30520) <<
"Processing root... " << endl;
154 if ( ! parseRoot ( &ioMain, kwordDocument ) )
156 kdWarning(30520) <<
"Parsing maindoc.xml has failed! Aborting!" << endl;
158 return KoFilter::StupidError;
163 if ( store->open(
"preview.png" ) )
166 kdDebug(30520) <<
"Preview found!" << endl;
167 KoStoreDevice ioPreview( store );
168 ioPreview.open( IO_ReadOnly );
169 const TQByteArray image ( ioPreview.readAll() );
170 if ( image.isNull() )
172 kdWarning(30520) <<
"Loading of preview failed! Ignoring!" << endl;
176 kwordDocument.m_previewFile =
new KTempFile( TQString(),
".png" );
178 kwordDocument.m_previewFile->setAutoDelete(
true );
179 TQFile file( kwordDocument.m_previewFile->name() );
181 file.open( IO_WriteOnly );
182 file.writeBlock( image );
190 kdDebug(30520) <<
"No preview found!" << endl;
195 kdWarning(30520) <<
"Opening store has failed. Trying raw XML file!" << endl;
200 TQFile file( fileName );
201 file.open( IO_ReadOnly );
202 if ( ! parseRoot( &file, kwordDocument ) )
204 kdError(30520) <<
"Could not process document! Aborting!" << endl;
206 return KoFilter::StupidError;
211 if ( ! postParse( store, kwordDocument ) )
213 kdError(30520) <<
"Error during post-parsing! Aborting!" << endl;
214 return KoFilter::StupidError;
218 kdDebug(30520) <<
"Deleting input store..." << endl;
221 kdDebug(30520) <<
"Input store deleted!" << endl;
223 KWord13OasisGenerator generator;
225 kdDebug(30520) << __FILE__ <<
":" << __LINE__ << endl;
227 if ( ! generator.prepare( kwordDocument ) )
229 kdError(30520) <<
"Could not prepare the OASIS document! Aborting!" << endl;
230 return KoFilter::StupidError;
233 const TQString filenameOut ( m_chain->outputFile() );
235 if ( filenameOut.isEmpty() )
237 kdError(30520) <<
"Empty file name for saving as OASIS! Aborting!" << endl;
238 return KoFilter::StupidError;
241 if ( ! generator.generate( filenameOut, kwordDocument ) )
243 kdError(30520) <<
"Could not save as OASIS! Aborting!" << endl;
244 return KoFilter::StupidError;
247 kdDebug(30520) <<
"Filter has finished!" << endl;
252#include "kword13import.moc"