filters
wpimport.cc00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #ifdef HAVE_UNISTD_H
00023 #include <unistd.h>
00024 #endif
00025
00026 #include <kdebug.h>
00027 #include <KoFilterChain.h>
00028 #include <kgenericfactory.h>
00029
00030 #include <wpimport.h>
00031
00032 #include <stdio.h>
00033
00034 typedef KGenericFactory<WPImport, KoFilter> WPImportFactory;
00035 K_EXPORT_COMPONENT_FACTORY( libwpimport, WPImportFactory( "kofficefilters" ) )
00036
00037 #include <libwpd/libwpd.h>
00038 #include <libwpd/WPXStream.h>
00039 #include <libwpd/WPXHLListenerImpl.h>
00040
00041
00042 class WPXMemoryInputStream : public WPXInputStream
00043 {
00044 public:
00045 WPXMemoryInputStream(uint8_t *data, size_t size);
00046 virtual ~WPXMemoryInputStream();
00047
00048 virtual bool isOLEStream() { return false; }
00049 virtual WPXInputStream * getDocumentOLEStream() { return NULL; }
00050
00051 const virtual uint8_t *read(size_t numBytes, size_t &numBytesRead);
00052 virtual int seek(long offset, WPX_SEEK_TYPE seekType);
00053 virtual long tell();
00054 virtual bool atEOS();
00055
00056 private:
00057 long m_offset;
00058 size_t m_size;
00059 uint8_t *m_data;
00060 uint8_t *m_tmpBuf;
00061 };
00062
00063
00064 WPXMemoryInputStream::WPXMemoryInputStream(uint8_t *data, size_t size) :
00065 WPXInputStream(false),
00066 m_offset(0),
00067 m_data(data),
00068 m_size(size),
00069 m_tmpBuf(NULL)
00070 {
00071 }
00072
00073 WPXMemoryInputStream::~WPXMemoryInputStream()
00074 {
00075 delete [] m_tmpBuf;
00076 delete [] m_data;
00077 }
00078
00079 const uint8_t * WPXMemoryInputStream::read(size_t numBytes, size_t &numBytesRead)
00080 {
00081 delete [] m_tmpBuf;
00082 int numBytesToRead;
00083
00084 if ((m_offset+numBytes) < m_size)
00085 numBytesToRead = numBytes;
00086 else
00087 numBytesToRead = m_size - m_offset;
00088
00089 numBytesRead = numBytesToRead;
00090
00091 if (numBytesToRead == 0)
00092 return NULL;
00093
00094 m_tmpBuf = new uint8_t[numBytesToRead];
00095 for (size_t i=0; i<numBytesToRead; i++)
00096 {
00097 m_tmpBuf[i] = m_data[m_offset];
00098 m_offset++;
00099 }
00100
00101 return m_tmpBuf;
00102 }
00103
00104 int WPXMemoryInputStream::seek(long offset, WPX_SEEK_TYPE seekType)
00105 {
00106 if (seekType == WPX_SEEK_CUR)
00107 m_offset += offset;
00108 else if (seekType == WPX_SEEK_SET)
00109 m_offset = offset;
00110
00111 if (m_offset < 0)
00112 m_offset = 0;
00113 else if (m_offset >= m_size)
00114 m_offset = m_size;
00115
00116 return 0;
00117 }
00118
00119 long WPXMemoryInputStream::tell()
00120 {
00121 return m_offset;
00122 }
00123
00124 bool WPXMemoryInputStream::atEOS()
00125 {
00126 if (m_offset >= m_size )
00127 return true;
00128
00129 return false;
00130 }
00131
00132
00133 class KWordListener : public WPXHLListenerImpl
00134 {
00135 public:
00136 KWordListener();
00137 virtual ~KWordListener();
00138
00139 virtual void setDocumentMetaData(const WPXPropertyList &propList) {}
00140
00141 virtual void startDocument() ;
00142 virtual void endDocument() ;
00143
00144 virtual void openPageSpan(const WPXPropertyList &propList) {}
00145 virtual void closePageSpan() {}
00146 virtual void openHeader(const WPXPropertyList &propList) {}
00147 virtual void closeHeader() {}
00148 virtual void openFooter(const WPXPropertyList &propList) {}
00149 virtual void closeFooter() {}
00150
00151 virtual void openSection(const WPXPropertyList &propList, const WPXPropertyListVector &columns) {}
00152 virtual void closeSection() {}
00153 virtual void openParagraph(const WPXPropertyList &propList, const WPXPropertyListVector &tabStops);
00154 virtual void closeParagraph();
00155 virtual void openSpan(const WPXPropertyList &propList) ;
00156 virtual void closeSpan() ;
00157
00158 virtual void insertTab();
00159 virtual void insertText(const WPXString &text);
00160 virtual void insertLineBreak();
00161
00162 virtual void defineOrderedListLevel(const WPXPropertyList &propList) {}
00163 virtual void defineUnorderedListLevel(const WPXPropertyList &propList) {}
00164 virtual void openOrderedListLevel(const WPXPropertyList &propList) {}
00165 virtual void openUnorderedListLevel(const WPXPropertyList &propList) {}
00166 virtual void closeOrderedListLevel() {}
00167 virtual void closeUnorderedListLevel() {}
00168 virtual void openListElement(const WPXPropertyList &propList, const WPXPropertyListVector &tabStops) {}
00169 virtual void closeListElement() {}
00170
00171 virtual void openFootnote(const WPXPropertyList &propList) {}
00172 virtual void closeFootnote() {}
00173 virtual void openEndnote(const WPXPropertyList &propList) {}
00174 virtual void closeEndnote() {}
00175
00176 virtual void openTable(const WPXPropertyList &propList, const WPXPropertyListVector &columns) {}
00177 virtual void openTableRow(const WPXPropertyList &propList) {}
00178 virtual void closeTableRow() {}
00179 virtual void openTableCell(const WPXPropertyList &propList) {}
00180 virtual void closeTableCell() {}
00181 virtual void insertCoveredTableCell(const WPXPropertyList &propList) {}
00182 virtual void closeTable() {}
00183
00184 QString root;
00185
00186 private:
00187 unsigned int m_currentListLevel;
00188 };
00189
00190
00191
00192 KWordListener::KWordListener()
00193 {
00194 }
00195
00196 KWordListener::~KWordListener()
00197 {
00198 }
00199
00200 void KWordListener::startDocument()
00201 {
00202 root = "<!DOCTYPE DOC>\n";
00203 root.append( "<DOC mime=\"application/x-kword\" syntaxVersion=\"2\" editor=\"KWord\">\n");
00204
00205
00206 root.append( "<PAPER width=\"595\" height=\"841\" format=\"1\" fType=\"0\" orientation=\"0\" hType=\"0\" columns=\"1\">\n" );
00207 root.append( "<PAPERBORDERS right=\"28\" left=\"28\" bottom=\"42\" top=\"42\" />" );
00208 root.append( "</PAPER>\n" );
00209
00210 root.append( "<ATTRIBUTES standardpage=\"1\" hasFooter=\"0\" hasHeader=\"0\" processing=\"0\" />\n" );
00211
00212 root.append( "<FRAMESETS>\n" );
00213 root.append( "<FRAMESET removable=\"0\" frameType=\"1\" frameInfo=\"0\" autoCreateNewFrame=\"1\">\n" );
00214 root.append( "<FRAME right=\"567\" left=\"28\" top=\"42\" bottom=\"799\" />\n" );
00215 }
00216
00217 void KWordListener::endDocument()
00218 {
00219 root.append( "</FRAMESET>\n" );
00220 root.append( "</FRAMESETS>\n" );
00221
00222 root.append( "</DOC>\n" );
00223 }
00224
00225 void KWordListener::openParagraph(const WPXPropertyList &propList, const WPXPropertyListVector &tabStops)
00226 {
00227 root.append( "<PARAGRAPH>\n" );
00228 root.append( "<TEXT>" );
00229 }
00230
00231 void KWordListener::closeParagraph()
00232 {
00233 root.append( "</TEXT>\n" );
00234 root.append( "<LAYOUT>\n");
00235 root.append( "<NAME value=\"Standard\" />\n");
00236 root.append( "<FLOW align=\"left\" />\n");
00237 root.append( "<FORMAT/>\n");
00238 root.append( "</LAYOUT>\n");
00239 root.append( "</PARAGRAPH>\n" );
00240 }
00241
00242 void KWordListener::insertTab()
00243 {
00244 }
00245
00246 void KWordListener::insertText(const WPXString &text)
00247 {
00248 root.append( QString::fromUtf8( text.cstr()) );
00249 }
00250
00251 void KWordListener::openSpan(const WPXPropertyList &propList)
00252 {
00253 }
00254
00255
00256 void KWordListener::closeSpan()
00257 {
00258 }
00259
00260 void KWordListener::insertLineBreak()
00261 {
00262 }
00263
00264 WPImport::WPImport( KoFilter *, const char *, const QStringList& ): KoFilter()
00265 {
00266 }
00267
00268 KoFilter::ConversionStatus WPImport::convert( const QCString& from, const QCString& to )
00269 {
00270
00271 if( to!= "application/x-kword" || from != "application/wordperfect" )
00272 return KoFilter::NotImplemented;
00273
00274
00275 const char* infile = m_chain->inputFile().latin1();
00276 FILE *f = fopen( infile, "rb" );
00277 if( !f )
00278 return KoFilter::StupidError;
00279
00280 fseek( f, 0, SEEK_END );
00281 long fsize = ftell( f );
00282 fseek( f, 0, SEEK_SET );
00283
00284 unsigned char* buf = new unsigned char[fsize];
00285 fread( buf, 1, fsize, f );
00286 fclose( f );
00287
00288
00289 WPXMemoryInputStream* instream = new WPXMemoryInputStream( buf, fsize );
00290
00291
00292 KWordListener listener;
00293 WPDResult error = WPDocument::parse( instream, static_cast<WPXHLListenerImpl *>(&listener));
00294 delete instream;
00295
00296 if( error != WPD_OK )
00297 return KoFilter::StupidError;
00298
00299 QString root = listener.root;
00300
00301
00302 if( root.isEmpty() ) return KoFilter::StupidError;
00303
00304
00305 KoStoreDevice* out = m_chain->storageFile( "root", KoStore::Write );
00306
00307 if( out )
00308 {
00309 QCString cstring = root.utf8();
00310 cstring.prepend( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
00311
00312 out->writeBlock( (const char*) cstring, cstring.length() );
00313 }
00314
00315 return KoFilter::OK;
00316 }
00317
00318 #include "wpimport.moc"
|