filters

kword13import.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Nicolas GOUTTE <goutte@kde.org>
3
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.
8
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.
13
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., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqxml.h>
21#include <tqdom.h>
22
23#include <kdebug.h>
24#include <kgenericfactory.h>
25#include <kimageio.h>
26#include <tdetempfile.h>
27
28#include <KoFilterChain.h>
29#include <KoStoreDevice.h>
30
31#include "kword13parser.h"
32#include "kword13document.h"
33#include "kword13oasisgenerator.h"
34#include "kword13postparsing.h"
35#include "kword13import.h"
36
37typedef KGenericFactory<KWord13Import, KoFilter> KWord13ImportFactory;
38K_EXPORT_COMPONENT_FACTORY( libkwordkword1dot3import, KWord13ImportFactory( "kofficefilters" ) )
39
40
41KWord13Import::KWord13Import(KoFilter */*parent*/, const char */*name*/, const TQStringList &)
42 : KoFilter()
43{
44}
45
46bool KWord13Import::parseInfo( TQIODevice* io, KWord13Document& kwordDocument )
47{
48 kdDebug(30520) << "Starting KWord13Import::parseInfo" << endl;
49 TQDomDocument doc;
50 // Error variables for TQDomDocument::setContent
51 TQString errorMsg;
52 int errorLine, errorColumn;
53 if ( ! doc.setContent( io, &errorMsg, &errorLine, &errorColumn ) )
54 {
55 kdError(30520) << "Parsing error in documentinfo.xml! Aborting!" << endl
56 << " In line: " << errorLine << ", column: " << errorColumn << endl
57 << " Error message: " << errorMsg << endl;
58 // ### TODO: user message
59 return false;
60 }
61 TQDomElement docElement( doc.documentElement() );
62 // In documentinfo.xml, the text data is in the grand-children of the document element
63 for ( TQDomNode node = docElement.firstChild(); !node.isNull(); node = node.nextSibling() )
64 {
65 kdDebug(30520) << "Child " << node.nodeName() << endl;
66 if ( !node.isElement() )
67 continue; // Comment, PI...
68 const TQString nodeName( node.nodeName() );
69 for ( TQDomNode node2 = node.firstChild(); !node2.isNull(); node2 = node2.nextSibling() )
70 {
71 kdDebug(30520) << "Grand-child " << node2.nodeName() << endl;
72 if ( !node2.isElement() )
73 continue;
74 const TQString nodeName2 ( nodeName + ':' + node2.nodeName() );
75 TQDomElement element( node2.toElement() );
76 kwordDocument.m_documentInfo[ nodeName2 ] = element.text();
77 }
78 }
79 kdDebug(30520) << "Quitting KWord13Import::parseInfo" << endl;
80 return true;
81}
82
83bool KWord13Import::parseRoot( TQIODevice* io, KWord13Document& kwordDocument )
84{
85 KWord13Parser handler( &kwordDocument );
86
87 TQXmlSimpleReader reader;
88 reader.setContentHandler( &handler );
89 reader.setErrorHandler( &handler );
90
91 TQXmlInputSource source( io ); // Read the file
92
93 if (!reader.parse( source ))
94 {
95 kdWarning(30520) << "Parse Error" << endl;
96 return false;
97 }
98 return true;
99}
100
101bool KWord13Import::postParse( KoStore* store, KWord13Document& doc )
102{
103 KWord13PostParsing post;
104 return post.postParse( store, doc );
105}
106
107KoFilter::ConversionStatus KWord13Import::convert( const TQCString& from, const TQCString& to )
108{
109 if ( to != "application/vnd.oasis.opendocument.text"
110 || from != "application/x-kword" )
111 {
112 return KoFilter::NotImplemented;
113 }
114
115 // We need KimageIO's help in OOWriterWorker::convertUnknownImage
116 KImageIO::registerFormats();
117
118 KWord13Document kwordDocument;
119
120 const TQString fileName( m_chain->inputFile() );
121 if ( fileName.isEmpty() )
122 {
123 kdError(30520) << "No input file name!" << endl;
124 return KoFilter::StupidError;
125 }
126
127 KoStore* store = KoStore::createStore( fileName, KoStore::Read );
128 if ( store && store->hasFile( "maindoc.xml" ) )
129 {
130 kdDebug(30520) << "Maindoc.xml found in KoStore!" << endl;
131
132 // We do not really care about errors while reading/parsing documentinfo
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 ) )
138 {
139 kdWarning(30520) << "Parsing documentinfo.xml has failed. Ignoring!" << endl;
140 }
141 ioInfo.close();
142 store->close();
143
144 // ### TODO: error return values
145 if ( ! store->open( "maindoc.xml" ) )
146 {
147 kdError(30520) << "Opening root has failed" << endl;
148 delete store;
149 return KoFilter::StupidError;
150 }
151 KoStoreDevice ioMain( store );
152 ioMain.open( IO_ReadOnly );
153 kdDebug (30520) << "Processing root... " << endl;
154 if ( ! parseRoot ( &ioMain, kwordDocument ) )
155 {
156 kdWarning(30520) << "Parsing maindoc.xml has failed! Aborting!" << endl;
157 delete store;
158 return KoFilter::StupidError;
159 }
160 ioMain.close();
161 store->close();
162
163 if ( store->open( "preview.png" ) )
164 {
165
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() )
171 {
172 kdWarning(30520) << "Loading of preview failed! Ignoring!" << endl;
173 }
174 else
175 {
176 kwordDocument.m_previewFile = new KTempFile( TQString(), ".png" );
177 // ### TODO check KTempFile
178 kwordDocument.m_previewFile->setAutoDelete( true );
179 TQFile file( kwordDocument.m_previewFile->name() );
180 // ### TODO: check if file is correctly written
181 file.open( IO_WriteOnly );
182 file.writeBlock( image );
183 file.close();
184 }
185 ioPreview.close();
186 store->close();
187 }
188 else
189 {
190 kdDebug(30520) << "No preview found!" << endl;
191 }
192 }
193 else
194 {
195 kdWarning(30520) << "Opening store has failed. Trying raw XML file!" << endl;
196 // Be sure to undefine store
197 delete store;
198 store = 0;
199
200 TQFile file( fileName );
201 file.open( IO_ReadOnly );
202 if ( ! parseRoot( &file, kwordDocument ) )
203 {
204 kdError(30520) << "Could not process document! Aborting!" << endl;
205 file.close();
206 return KoFilter::StupidError;
207 }
208 file.close();
209 }
210
211 if ( ! postParse( store, kwordDocument ) )
212 {
213 kdError(30520) << "Error during post-parsing! Aborting!" << endl;
214 return KoFilter::StupidError;
215 }
216
217 // We have finished with the input store/file, so close the store (already done for a raw XML file)
218 kdDebug(30520) << "Deleting input store..." << endl;
219 delete store;
220 store = 0;
221 kdDebug(30520) << "Input store deleted!" << endl;
222
223 KWord13OasisGenerator generator;
224
225 kdDebug(30520) << __FILE__ << ":" << __LINE__ << endl;
226
227 if ( ! generator.prepare( kwordDocument ) )
228 {
229 kdError(30520) << "Could not prepare the OASIS document! Aborting!" << endl;
230 return KoFilter::StupidError;
231 }
232
233 const TQString filenameOut ( m_chain->outputFile() );
234
235 if ( filenameOut.isEmpty() )
236 {
237 kdError(30520) << "Empty file name for saving as OASIS! Aborting!" << endl;
238 return KoFilter::StupidError;
239 }
240
241 if ( ! generator.generate( filenameOut, kwordDocument ) )
242 {
243 kdError(30520) << "Could not save as OASIS! Aborting!" << endl;
244 return KoFilter::StupidError;
245 }
246
247 kdDebug(30520) << "Filter has finished!" << endl;
248
249 return KoFilter::OK;
250}
251
252#include "kword13import.moc"