office-gobmx/writerperfect/source/writer/EPUBPackage.cxx
Miklos Vajna 12b739aa2b libepubgen: backport 3 validation fixes
A hello world ODT is now exported without validation errors.

Change-Id: I2b160ee980f8ed9c80fd76945600d1edb1ee29bc
Reviewed-on: https://gerrit.libreoffice.org/40908
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
2017-08-09 11:53:53 +02:00

178 lines
5.9 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "EPUBPackage.hxx"
#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/embed/XTransactedObject.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/xml/sax/Writer.hpp>
#include <comphelper/storagehelper.hxx>
#include <unotools/mediadescriptor.hxx>
#include <xmloff/attrlist.hxx>
using namespace com::sun::star;
namespace writerperfect
{
EPUBPackage::EPUBPackage(const uno::Reference<uno::XComponentContext> &xContext, const uno::Sequence<beans::PropertyValue> &rDescriptor)
: mxContext(xContext)
{
// Extract the output stream from the descriptor.
utl::MediaDescriptor aMediaDesc(rDescriptor);
auto xStream = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_STREAMFOROUTPUT(), uno::Reference<io::XStream>());
const sal_Int32 nOpenMode = embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE;
mxStorage.set(comphelper::OStorageHelper::GetStorageOfFormatFromStream(ZIP_STORAGE_FORMAT_STRING, xStream, nOpenMode, mxContext), uno::UNO_QUERY);
// The zipped content represents an EPUB Publication.
mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName("mimetype", embed::ElementModes::READWRITE), uno::UNO_QUERY);
const OString aMimeType("application/epub+zip");
uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8 *>(aMimeType.getStr()), aMimeType.getLength());
mxOutputStream->writeBytes(aData);
uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
xTransactedObject->commit();
mxOutputStream.clear();
}
EPUBPackage::~EPUBPackage()
{
uno::Reference<embed::XTransactedObject> xTransactedObject(mxStorage, uno::UNO_QUERY);
xTransactedObject->commit();
}
void EPUBPackage::openXMLFile(const char *pName)
{
assert(pName);
assert(!mxOutputStream.is());
assert(!mxOutputWriter.is());
mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName(OUString::fromUtf8(pName), embed::ElementModes::READWRITE), uno::UNO_QUERY);
mxOutputWriter = xml::sax::Writer::create(mxContext);
mxOutputWriter->setOutputStream(mxOutputStream);
mxOutputWriter->startDocument();
}
void EPUBPackage::openElement(const char *pName, const librevenge::RVNGPropertyList &rAttributes)
{
assert(mxOutputWriter.is());
rtl::Reference<SvXMLAttributeList> pAttributeList(new SvXMLAttributeList());
librevenge::RVNGPropertyList::Iter it(rAttributes);
for (it.rewind(); it.next();)
pAttributeList->AddAttribute(OUString::fromUtf8(it.key()), OUString::fromUtf8(it()->getStr().cstr()));
mxOutputWriter->startElement(OUString::fromUtf8(pName), uno::Reference<xml::sax::XAttributeList>(pAttributeList.get()));
}
void EPUBPackage::closeElement(const char *pName)
{
assert(mxOutputWriter.is());
mxOutputWriter->endElement(OUString::fromUtf8(pName));
}
void EPUBPackage::insertCharacters(const librevenge::RVNGString &rCharacters)
{
mxOutputWriter->characters(OUString::fromUtf8(rCharacters.cstr()));
}
void EPUBPackage::closeXMLFile()
{
assert(mxOutputWriter.is());
assert(mxOutputStream.is());
mxOutputWriter->endDocument();
mxOutputWriter.clear();
uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
xTransactedObject->commit();
mxOutputStream.clear();
}
void EPUBPackage::openCSSFile(const char *pName)
{
assert(pName);
assert(!mxOutputStream.is());
mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName(OUString::fromUtf8(pName), embed::ElementModes::READWRITE), uno::UNO_QUERY);
}
void EPUBPackage::insertRule(const librevenge::RVNGString &rSelector, const librevenge::RVNGPropertyList &rProperties)
{
assert(mxOutputStream.is());
uno::Reference<io::XSeekable> xSeekable(mxOutputStream, uno::UNO_QUERY);
std::stringstream aStream;
if (xSeekable->getPosition() != 0)
aStream << '\n';
aStream << rSelector.cstr() << " {\n";
librevenge::RVNGPropertyList::Iter it(rProperties);
for (it.rewind(); it.next();)
{
if (it())
aStream << " " << it.key() << ": " << it()->getStr().cstr() << ";\n";
}
aStream << "}\n";
std::string aString = aStream.str();
uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8 *>(aString.c_str()), aString.size());
mxOutputStream->writeBytes(aData);
}
void EPUBPackage::closeCSSFile()
{
assert(mxOutputStream.is());
uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
xTransactedObject->commit();
mxOutputStream.clear();
}
void EPUBPackage::openBinaryFile(const char *pName)
{
SAL_WARN("writerperfect", "EPUBPackage::openBinaryFile, " << pName << ": implement me");
}
void EPUBPackage::insertBinaryData(const librevenge::RVNGBinaryData &/*rData*/)
{
SAL_WARN("writerperfect", "EPUBPackage::insertBinaryData: implement me");
}
void EPUBPackage::closeBinaryFile()
{
SAL_WARN("writerperfect", "EPUBPackage::closeBinaryFile: implement me");
}
void EPUBPackage::openTextFile(const char *pName)
{
SAL_WARN("writerperfect", "EPUBPackage::openTextFile, " << pName << ": implement me");
}
void EPUBPackage::insertText(const librevenge::RVNGString &/*rCharacters*/)
{
SAL_WARN("writerperfect", "EPUBPackage::insertText: implement me");
}
void EPUBPackage::insertLineBreak()
{
SAL_WARN("writerperfect", "EPUBPackage::insertLineBreak: implement me");
}
void EPUBPackage::closeTextFile()
{
SAL_WARN("writerperfect", "EPUBPackage::closeTextFile: implement me");
}
} // namespace writerperfect
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */