lib Library API Documentation

koOasisStore.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "koOasisStore.h"
00020 
00021 #include "koDocument.h"
00022 #include "koxmlns.h"
00023 #include "kodom.h"
00024 #include <koStore.h>
00025 #include <koStoreDevice.h>
00026 #include <koxmlwriter.h>
00027 
00028 #include <ktempfile.h>
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 
00032 #include <qfile.h>
00033 #include <qxml.h>
00034 #include <qbuffer.h>
00035 
00036 KoOasisStore::KoOasisStore( KoStore* store )
00037     : m_store( store ),
00038       m_storeDevice( 0 ),
00039       m_contentWriter( 0 ),
00040       m_bodyWriter( 0 ),
00041       m_manifestWriter( 0 ),
00042       m_contentTmpFile( 0 )
00043 {
00044 }
00045 
00046 KoOasisStore::~KoOasisStore()
00047 {
00048     // If all the right close methods were called, nothing should remain,
00049     // so those deletes are really just in case.
00050     Q_ASSERT( !m_contentWriter );
00051     delete m_contentWriter;
00052     Q_ASSERT( !m_bodyWriter );
00053     delete m_bodyWriter;
00054     Q_ASSERT( !m_storeDevice );
00055     delete m_storeDevice;
00056     Q_ASSERT( !m_contentTmpFile );
00057     delete m_contentTmpFile;
00058     Q_ASSERT( !m_manifestWriter );
00059     delete m_manifestWriter;
00060 }
00061 
00062 KoXmlWriter* KoOasisStore::contentWriter()
00063 {
00064     if ( !m_contentWriter )
00065     {
00066         if ( !m_store->open( "content.xml" ) )
00067             return 0;
00068         m_storeDevice = new KoStoreDevice( m_store );
00069         m_contentWriter = KoDocument::createOasisXmlWriter( m_storeDevice, "office:document-content" );
00070     }
00071     return m_contentWriter;
00072 }
00073 
00074 KoXmlWriter* KoOasisStore::bodyWriter()
00075 {
00076     if ( !m_bodyWriter )
00077     {
00078         Q_ASSERT( !m_contentTmpFile );
00079         m_contentTmpFile = new KTempFile;
00080         m_contentTmpFile->setAutoDelete( true );
00081         m_bodyWriter = new KoXmlWriter( m_contentTmpFile->file(), 1 );
00082     }
00083     return m_bodyWriter;
00084 }
00085 
00086 bool KoOasisStore::closeContentWriter()
00087 {
00088     Q_ASSERT( m_bodyWriter );
00089     Q_ASSERT( m_contentTmpFile );
00090 
00091     delete m_bodyWriter; m_bodyWriter = 0;
00092     // copy over the contents from the tempfile to the real one
00093     QFile* tmpFile = m_contentTmpFile->file();
00094     tmpFile->close();
00095     m_contentWriter->addCompleteElement( tmpFile );
00096     m_contentTmpFile->close();
00097     delete m_contentTmpFile; m_contentTmpFile = 0;
00098 
00099     Q_ASSERT( m_contentWriter );
00100     m_contentWriter->endElement(); // document-content
00101     m_contentWriter->endDocument();
00102     delete m_contentWriter; m_contentWriter = 0;
00103     delete m_storeDevice; m_storeDevice = 0;
00104     if ( !m_store->close() ) // done with content.xml
00105         return false;
00106     return true;
00107 }
00108 
00109 KoXmlWriter* KoOasisStore::manifestWriter( const char* mimeType )
00110 {
00111     if ( !m_manifestWriter )
00112     {
00113         // the pointer to the buffer is already stored in the KoXmlWriter, no need to store it here as well
00114         QBuffer *manifestBuffer = new QBuffer;
00115         manifestBuffer->open( IO_WriteOnly );
00116         m_manifestWriter = new KoXmlWriter( manifestBuffer );
00117         m_manifestWriter->startDocument( "manifest:manifest" );
00118         m_manifestWriter->startElement( "manifest:manifest" );
00119         m_manifestWriter->addAttribute( "xmlns:manifest", KoXmlNS::manifest );
00120         m_manifestWriter->addManifestEntry( "/", mimeType );
00121     }
00122     return m_manifestWriter;
00123 }
00124 
00125 bool KoOasisStore::closeManifestWriter()
00126 {
00127     m_manifestWriter->endElement();
00128     m_manifestWriter->endDocument();
00129     QBuffer* buffer = static_cast<QBuffer *>( m_manifestWriter->device() );
00130     delete m_manifestWriter; m_manifestWriter = 0;
00131     bool ok = false;
00132     if ( m_store->open( "META-INF/manifest.xml" ) )
00133     {
00134         Q_LONG written = m_store->write( buffer->buffer() );
00135         ok = ( written == (Q_LONG)buffer->buffer().size() && m_store->close() );
00136     }
00137     delete buffer;
00138     return ok;
00139 }
00140 
00141 bool KoOasisStore::loadAndParse( const QString& fileName, QDomDocument& doc, QString& errorMessage )
00142 {
00143     //kdDebug(30003) << "loadAndParse: Trying to open " << fileName << endl;
00144 
00145     if (!m_store->open(fileName))
00146     {
00147         kdWarning(30003) << "Entry " << fileName << " not found!" << endl;
00148         errorMessage = i18n( "Could not find %1" ).arg( fileName );
00149         return false;
00150     }
00151     // Error variables for QDomDocument::setContent
00152     QString errorMsg;
00153     int errorLine, errorColumn;
00154 
00155     // We need to be able to see the space in <text:span> </text:span>, this is why
00156     // we activate the "report-whitespace-only-CharData" feature.
00157     // Unfortunately this leads to lots of whitespace text nodes in between real
00158     // elements in the rest of the document, watch out for that.
00159     QXmlInputSource source( m_store->device() );
00160     // Copied from QDomDocumentPrivate::setContent, to change the whitespace thing
00161     QXmlSimpleReader reader;
00162     KoDocument::setupXmlReader( reader, true /*namespaceProcessing*/ );
00163 
00164     bool ok = doc.setContent( &source, &reader, &errorMsg, &errorLine, &errorColumn );
00165     if ( !ok )
00166     {
00167         kdError(30003) << "Parsing error in " << fileName << "! Aborting!" << endl
00168                        << " In line: " << errorLine << ", column: " << errorColumn << endl
00169                        << " Error message: " << errorMsg << endl;
00170         errorMessage = i18n( "Parsing error in the main document at line %1, column %2\nError message: %3" )
00171                        .arg( errorLine ).arg( errorColumn ).arg( i18n ( "QXml", errorMsg.utf8() ) );
00172     }
00173     else
00174     {
00175         kdDebug(30003) << "File " << fileName << " loaded and parsed" << endl;
00176     }
00177     m_store->close();
00178     return ok;
00179 }
00180 
00181 QString KoOasisStore::mimeForPath( const QDomDocument& doc, const QString& fullPath )
00182 {
00183     QDomElement docElem = doc.documentElement();
00184     QDomElement elem;
00185     forEachElement( elem, docElem )
00186     {
00187         if ( elem.localName() == "file-entry" && elem.namespaceURI() == KoXmlNS::manifest )
00188         {
00189             if ( elem.attributeNS( KoXmlNS::manifest, "full-path", QString::null ) == fullPath )
00190                 return elem.attributeNS( KoXmlNS::manifest, "media-type", QString::null );
00191         }
00192     }
00193     return QString::null;
00194 }
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:02 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003