koOasisStore.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00049
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
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();
00101 m_contentWriter->endDocument();
00102 delete m_contentWriter; m_contentWriter = 0;
00103 delete m_storeDevice; m_storeDevice = 0;
00104 if ( !m_store->close() )
00105 return false;
00106 return true;
00107 }
00108
00109 KoXmlWriter* KoOasisStore::manifestWriter( const char* mimeType )
00110 {
00111 if ( !m_manifestWriter )
00112 {
00113
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
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
00152 QString errorMsg;
00153 int errorLine, errorColumn;
00154
00155
00156
00157
00158
00159 QXmlInputSource source( m_store->device() );
00160
00161 QXmlSimpleReader reader;
00162 KoDocument::setupXmlReader( reader, true );
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 }
This file is part of the documentation for lib Library Version 1.4.2.