filters
kword13import.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qxml.h>
00021 #include <qdom.h>
00022
00023 #include <kdebug.h>
00024 #include <kgenericfactory.h>
00025 #include <kimageio.h>
00026 #include <ktempfile.h>
00027
00028 #include <KoFilterChain.h>
00029 #include <KoStoreDevice.h>
00030
00031 #include "kword13parser.h"
00032 #include "kword13document.h"
00033 #include "kword13oasisgenerator.h"
00034 #include "kword13postparsing.h"
00035 #include "kword13import.h"
00036
00037 typedef KGenericFactory<KWord13Import, KoFilter> KWord13ImportFactory;
00038 K_EXPORT_COMPONENT_FACTORY( libkwordkword1dot3import, KWord13ImportFactory( "kofficefilters" ) )
00039
00040
00041 KWord13Import::KWord13Import(KoFilter *, const char *, const QStringList &)
00042 : KoFilter()
00043 {
00044 }
00045
00046 bool KWord13Import::parseInfo( QIODevice* io, KWord13Document& kwordDocument )
00047 {
00048 kdDebug(30520) << "Starting KWord13Import::parseInfo" << endl;
00049 QDomDocument doc;
00050
00051 QString errorMsg;
00052 int errorLine, errorColumn;
00053 if ( ! doc.setContent( io, &errorMsg, &errorLine, &errorColumn ) )
00054 {
00055 kdError(30520) << "Parsing error in documentinfo.xml! Aborting!" << endl
00056 << " In line: " << errorLine << ", column: " << errorColumn << endl
00057 << " Error message: " << errorMsg << endl;
00058
00059 return false;
00060 }
00061 QDomElement docElement( doc.documentElement() );
00062
00063 for ( QDomNode node = docElement.firstChild(); !node.isNull(); node = node.nextSibling() )
00064 {
00065 kdDebug(30520) << "Child " << node.nodeName() << endl;
00066 if ( !node.isElement() )
00067 continue;
00068 const QString nodeName( node.nodeName() );
00069 for ( QDomNode node2 = node.firstChild(); !node2.isNull(); node2 = node2.nextSibling() )
00070 {
00071 kdDebug(30520) << "Grand-child " << node2.nodeName() << endl;
00072 if ( !node2.isElement() )
00073 continue;
00074 const QString nodeName2 ( nodeName + ':' + node2.nodeName() );
00075 QDomElement element( node2.toElement() );
00076 kwordDocument.m_documentInfo[ nodeName2 ] = element.text();
00077 }
00078 }
00079 kdDebug(30520) << "Quitting KWord13Import::parseInfo" << endl;
00080 return true;
00081 }
00082
00083 bool KWord13Import::parseRoot( QIODevice* io, KWord13Document& kwordDocument )
00084 {
00085 KWord13Parser handler( &kwordDocument );
00086
00087 QXmlSimpleReader reader;
00088 reader.setContentHandler( &handler );
00089 reader.setErrorHandler( &handler );
00090
00091 QXmlInputSource source( io );
00092
00093 if (!reader.parse( source ))
00094 {
00095 kdWarning(30520) << "Parse Error" << endl;
00096 return false;
00097 }
00098 return true;
00099 }
00100
00101 bool KWord13Import::postParse( KoStore* store, KWord13Document& doc )
00102 {
00103 KWord13PostParsing post;
00104 return post.postParse( store, doc );
00105 }
00106
00107 KoFilter::ConversionStatus KWord13Import::convert( const QCString& from, const QCString& to )
00108 {
00109 if ( to != "application/vnd.oasis.opendocument.text"
00110 || from != "application/x-kword" )
00111 {
00112 return KoFilter::NotImplemented;
00113 }
00114
00115
00116 KImageIO::registerFormats();
00117
00118 KWord13Document kwordDocument;
00119
00120 const QString fileName( m_chain->inputFile() );
00121 if ( fileName.isEmpty() )
00122 {
00123 kdError(30520) << "No input file name!" << endl;
00124 return KoFilter::StupidError;
00125 }
00126
00127 KoStore* store = KoStore::createStore( fileName, KoStore::Read );
00128 if ( store && store->hasFile( "maindoc.xml" ) )
00129 {
00130 kdDebug(30520) << "Maindoc.xml found in KoStore!" << endl;
00131
00132
00133 store->open( "documentinfo.xml" );
00134 KoStoreDevice ioInfo( store );
00135 ioInfo.open( IO_ReadOnly );
00136 kdDebug (30520) << "Processing document info... " << endl;
00137 if ( ! parseInfo ( &ioInfo, kwordDocument ) )
00138 {
00139 kdWarning(30520) << "Parsing documentinfo.xml has failed. Ignoring!" << endl;
00140 }
00141 ioInfo.close();
00142 store->close();
00143
00144
00145 if ( ! store->open( "maindoc.xml" ) )
00146 {
00147 kdError(30520) << "Opening root has failed" << endl;
00148 delete store;
00149 return KoFilter::StupidError;
00150 }
00151 KoStoreDevice ioMain( store );
00152 ioMain.open( IO_ReadOnly );
00153 kdDebug (30520) << "Processing root... " << endl;
00154 if ( ! parseRoot ( &ioMain, kwordDocument ) )
00155 {
00156 kdWarning(30520) << "Parsing maindoc.xml has failed! Aborting!" << endl;
00157 delete store;
00158 return KoFilter::StupidError;
00159 }
00160 ioMain.close();
00161 store->close();
00162
00163 if ( store->open( "preview.png" ) )
00164 {
00165
00166 kdDebug(30520) << "Preview found!" << endl;
00167 KoStoreDevice ioPreview( store );
00168 ioPreview.open( IO_ReadOnly );
00169 const QByteArray image ( ioPreview.readAll() );
00170 if ( image.isNull() )
00171 {
00172 kdWarning(30520) << "Loading of preview failed! Ignoring!" << endl;
00173 }
00174 else
00175 {
00176 kwordDocument.m_previewFile = new KTempFile( QString::null, ".png" );
00177
00178 kwordDocument.m_previewFile->setAutoDelete( true );
00179 QFile file( kwordDocument.m_previewFile->name() );
00180
00181 file.open( IO_WriteOnly );
00182 file.writeBlock( image );
00183 file.close();
00184 }
00185 ioPreview.close();
00186 store->close();
00187 }
00188 else
00189 {
00190 kdDebug(30520) << "No preview found!" << endl;
00191 }
00192 }
00193 else
00194 {
00195 kdWarning(30520) << "Opening store has failed. Trying raw XML file!" << endl;
00196
00197 delete store;
00198 store = 0;
00199
00200 QFile file( fileName );
00201 file.open( IO_ReadOnly );
00202 if ( ! parseRoot( &file, kwordDocument ) )
00203 {
00204 kdError(30520) << "Could not process document! Aborting!" << endl;
00205 file.close();
00206 return KoFilter::StupidError;
00207 }
00208 file.close();
00209 }
00210
00211 if ( ! postParse( store, kwordDocument ) )
00212 {
00213 kdError(30520) << "Error during post-parsing! Aborting!" << endl;
00214 return KoFilter::StupidError;
00215 }
00216
00217
00218 kdDebug(30520) << "Deleting input store..." << endl;
00219 delete store;
00220 store = 0;
00221 kdDebug(30520) << "Input store deleted!" << endl;
00222
00223 KWord13OasisGenerator generator;
00224
00225 kdDebug(30520) << __FILE__ << ":" << __LINE__ << endl;
00226
00227 if ( ! generator.prepare( kwordDocument ) )
00228 {
00229 kdError(30520) << "Could not prepare the OASIS document! Aborting!" << endl;
00230 return KoFilter::StupidError;
00231 }
00232
00233 const QString filenameOut ( m_chain->outputFile() );
00234
00235 if ( filenameOut.isEmpty() )
00236 {
00237 kdError(30520) << "Empty file name for saving as OASIS! Aborting!" << endl;
00238 return KoFilter::StupidError;
00239 }
00240
00241 if ( ! generator.generate( filenameOut, kwordDocument ) )
00242 {
00243 kdError(30520) << "Could not save as OASIS! Aborting!" << endl;
00244 return KoFilter::StupidError;
00245 }
00246
00247 kdDebug(30520) << "Filter has finished!" << endl;
00248
00249 return KoFilter::OK;
00250 }
00251
00252 #include "kword13import.moc"
|