1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- 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/XStorage.hpp>
#include <com/sun/star/embed/XTransactedObject.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/xml/sax/Writer.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>

#include <sal/log.hxx>
#include <comphelper/storagehelper.hxx>
#include <unotools/mediadescriptor.hxx>
#include <xmloff/attrlist.hxx>

using namespace com::sun::star;

namespace writerperfect
{
EPUBPackage::EPUBPackage(uno::Reference<uno::XComponentContext> xContext,
                         const uno::Sequence<beans::PropertyValue>& rDescriptor)
    : mxContext(std::move(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();

    // MIME type must be uncompressed.
    uno::Reference<beans::XPropertySet> xPropertySet(mxOutputStream, uno::UNO_QUERY);
    xPropertySet->setPropertyValue("Compressed", uno::makeAny(false));
    mxOutputStream.clear();
}

EPUBPackage::~EPUBPackage()
{
    uno::Reference<embed::XTransactedObject> xTransactedObject(mxStorage, uno::UNO_QUERY);
    xTransactedObject->commit();
}

void EPUBPackage::openXMLFile(const char* pName)<--- The function 'openXMLFile' is never used.
{
    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)<--- The function 'openElement' is never used.
{
    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)<--- The function 'closeElement' is never used.
{
    assert(mxOutputWriter.is());

    mxOutputWriter->endElement(OUString::fromUtf8(pName));
}

void EPUBPackage::insertCharacters(const librevenge::RVNGString& rCharacters)<--- The function 'insertCharacters' is never used.
{
    mxOutputWriter->characters(OUString::fromUtf8(rCharacters.cstr()));
}

void EPUBPackage::closeXMLFile()<--- The function 'closeXMLFile' is never used.
{
    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)<--- The function 'openCSSFile' is never used.
{
    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()<--- The function 'closeCSSFile' is never used.
{
    assert(mxOutputStream.is());

    uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
    xTransactedObject->commit();
    mxOutputStream.clear();
}

void EPUBPackage::openBinaryFile(const char* pName)<--- The function 'openBinaryFile' is never used.
{
    assert(pName);
    assert(!mxOutputStream.is());

    mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName(
                           OUString::fromUtf8(pName), embed::ElementModes::READWRITE),
                       uno::UNO_QUERY);
}

void EPUBPackage::insertBinaryData(const librevenge::RVNGBinaryData& rData)<--- The function 'insertBinaryData' is never used.
{
    assert(mxOutputStream.is());

    if (rData.empty())
        return;

    uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(rData.getDataBuffer()),
                                  rData.size());
    mxOutputStream->writeBytes(aData);
}

void EPUBPackage::closeBinaryFile()<--- The function 'closeBinaryFile' is never used.
{
    assert(mxOutputStream.is());

    uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
    xTransactedObject->commit();
    mxOutputStream.clear();
}

void EPUBPackage::openTextFile(const char* pName)<--- The function 'openTextFile' is never used.
{
    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()<--- The function 'closeTextFile' is never used.
{
    SAL_WARN("writerperfect", "EPUBPackage::closeTextFile: implement me");
}

} // namespace writerperfect

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */