Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/writerperfect/source/writer/EPUBPackage.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include "EPUBPackage.hxx"
11
12
#include <com/sun/star/embed/ElementModes.hpp>
13
#include <com/sun/star/embed/XStorage.hpp>
14
#include <com/sun/star/embed/XTransactedObject.hpp>
15
#include <com/sun/star/io/XSeekable.hpp>
16
#include <com/sun/star/xml/sax/Writer.hpp>
17
#include <com/sun/star/beans/XPropertySet.hpp>
18
#include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
19
20
#include <sal/log.hxx>
21
#include <comphelper/attributelist.hxx>
22
#include <comphelper/storagehelper.hxx>
23
#include <unotools/mediadescriptor.hxx>
24
25
using namespace com::sun::star;
26
27
namespace writerperfect
28
{
29
EPUBPackage::EPUBPackage(uno::Reference<uno::XComponentContext> xContext,
30
                         const uno::Sequence<beans::PropertyValue>& rDescriptor)
31
0
    : mxContext(std::move(xContext))
32
0
{
33
    // Extract the output stream from the descriptor.
34
0
    utl::MediaDescriptor aMediaDesc(rDescriptor);
35
0
    auto xStream = aMediaDesc.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_STREAMFOROUTPUT,
36
0
                                                        uno::Reference<io::XStream>());
37
0
    const sal_Int32 nOpenMode = embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE;
38
0
    mxStorage.set(comphelper::OStorageHelper::GetStorageOfFormatFromStream(
39
0
                      ZIP_STORAGE_FORMAT_STRING, xStream, nOpenMode, mxContext),
40
0
                  uno::UNO_QUERY);
41
42
    // The zipped content represents an EPUB Publication.
43
0
    mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName(
44
0
                           u"mimetype"_ustr, embed::ElementModes::READWRITE),
45
0
                       uno::UNO_QUERY);
46
0
    static constexpr OString aMimeType("application/epub+zip"_ostr);
47
0
    uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(aMimeType.getStr()),
48
0
                                  aMimeType.getLength());
49
0
    mxOutputStream->writeBytes(aData);
50
0
    uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
51
0
    xTransactedObject->commit();
52
53
    // MIME type must be uncompressed.
54
0
    uno::Reference<beans::XPropertySet> xPropertySet(mxOutputStream, uno::UNO_QUERY);
55
0
    xPropertySet->setPropertyValue(u"Compressed"_ustr, uno::Any(false));
56
0
    mxOutputStream.clear();
57
0
}
58
59
EPUBPackage::~EPUBPackage()
60
0
{
61
0
    uno::Reference<embed::XTransactedObject> xTransactedObject(mxStorage, uno::UNO_QUERY);
62
0
    xTransactedObject->commit();
63
0
}
64
65
void EPUBPackage::openXMLFile(const char* pName)
66
0
{
67
0
    assert(pName);
68
0
    assert(!mxOutputStream.is());
69
0
    assert(!mxOutputWriter.is());
70
71
0
    mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName(
72
0
                           OUString::fromUtf8(pName), embed::ElementModes::READWRITE),
73
0
                       uno::UNO_QUERY);
74
0
    mxOutputWriter = xml::sax::Writer::create(mxContext);
75
0
    mxOutputWriter->setOutputStream(mxOutputStream);
76
0
    mxOutputWriter->startDocument();
77
0
}
78
79
void EPUBPackage::openElement(const char* pName, const librevenge::RVNGPropertyList& rAttributes)
80
0
{
81
0
    assert(mxOutputWriter.is());
82
83
0
    rtl::Reference<comphelper::AttributeList> pAttributeList(new comphelper::AttributeList());
84
85
0
    librevenge::RVNGPropertyList::Iter it(rAttributes);
86
0
    for (it.rewind(); it.next();)
87
0
        pAttributeList->AddAttribute(OUString::fromUtf8(it.key()),
88
0
                                     OUString::fromUtf8(it()->getStr().cstr()));
89
90
0
    mxOutputWriter->startElement(OUString::fromUtf8(pName), pAttributeList);
91
0
}
92
93
void EPUBPackage::closeElement(const char* pName)
94
0
{
95
0
    assert(mxOutputWriter.is());
96
97
0
    mxOutputWriter->endElement(OUString::fromUtf8(pName));
98
0
}
99
100
void EPUBPackage::insertCharacters(const librevenge::RVNGString& rCharacters)
101
0
{
102
0
    mxOutputWriter->characters(OUString::fromUtf8(rCharacters.cstr()));
103
0
}
104
105
void EPUBPackage::closeXMLFile()
106
0
{
107
0
    assert(mxOutputWriter.is());
108
0
    assert(mxOutputStream.is());
109
110
0
    mxOutputWriter->endDocument();
111
0
    mxOutputWriter.clear();
112
113
0
    uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
114
0
    xTransactedObject->commit();
115
0
    mxOutputStream.clear();
116
0
}
117
118
void EPUBPackage::openCSSFile(const char* pName)
119
0
{
120
0
    assert(pName);
121
0
    assert(!mxOutputStream.is());
122
123
0
    mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName(
124
0
                           OUString::fromUtf8(pName), embed::ElementModes::READWRITE),
125
0
                       uno::UNO_QUERY);
126
0
}
127
128
void EPUBPackage::insertRule(const librevenge::RVNGString& rSelector,
129
                             const librevenge::RVNGPropertyList& rProperties)
130
0
{
131
0
    assert(mxOutputStream.is());
132
133
0
    uno::Reference<io::XSeekable> xSeekable(mxOutputStream, uno::UNO_QUERY);
134
0
    std::stringstream aStream;
135
0
    if (xSeekable->getPosition() != 0)
136
0
        aStream << '\n';
137
0
    aStream << rSelector.cstr() << " {\n";
138
139
0
    librevenge::RVNGPropertyList::Iter it(rProperties);
140
0
    for (it.rewind(); it.next();)
141
0
    {
142
0
        if (it())
143
0
            aStream << "  " << it.key() << ": " << it()->getStr().cstr() << ";\n";
144
0
    }
145
146
0
    aStream << "}\n";
147
0
    std::string aString = aStream.str();
148
0
    uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(aString.c_str()),
149
0
                                  aString.size());
150
0
    mxOutputStream->writeBytes(aData);
151
0
}
152
153
void EPUBPackage::closeCSSFile()
154
0
{
155
0
    assert(mxOutputStream.is());
156
157
0
    uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
158
0
    xTransactedObject->commit();
159
0
    mxOutputStream.clear();
160
0
}
161
162
void EPUBPackage::openBinaryFile(const char* pName)
163
0
{
164
0
    assert(pName);
165
0
    assert(!mxOutputStream.is());
166
167
0
    mxOutputStream.set(mxStorage->openStreamElementByHierarchicalName(
168
0
                           OUString::fromUtf8(pName), embed::ElementModes::READWRITE),
169
0
                       uno::UNO_QUERY);
170
0
}
171
172
void EPUBPackage::insertBinaryData(const librevenge::RVNGBinaryData& rData)
173
0
{
174
0
    assert(mxOutputStream.is());
175
176
0
    if (rData.empty())
177
0
        return;
178
179
0
    uno::Sequence<sal_Int8> aData(reinterpret_cast<const sal_Int8*>(rData.getDataBuffer()),
180
0
                                  rData.size());
181
0
    mxOutputStream->writeBytes(aData);
182
0
}
183
184
void EPUBPackage::closeBinaryFile()
185
0
{
186
0
    assert(mxOutputStream.is());
187
188
0
    uno::Reference<embed::XTransactedObject> xTransactedObject(mxOutputStream, uno::UNO_QUERY);
189
0
    xTransactedObject->commit();
190
0
    mxOutputStream.clear();
191
0
}
192
193
void EPUBPackage::openTextFile(const char* pName)
194
0
{
195
0
    SAL_WARN("writerperfect", "EPUBPackage::openTextFile, " << pName << ": implement me");
196
0
}
197
198
void EPUBPackage::insertText(const librevenge::RVNGString& /*rCharacters*/)
199
0
{
200
0
    SAL_WARN("writerperfect", "EPUBPackage::insertText: implement me");
201
0
}
202
203
void EPUBPackage::insertLineBreak()
204
0
{
205
0
    SAL_WARN("writerperfect", "EPUBPackage::insertLineBreak: implement me");
206
0
}
207
208
void EPUBPackage::closeTextFile()
209
0
{
210
0
    SAL_WARN("writerperfect", "EPUBPackage::closeTextFile: implement me");
211
0
}
212
213
} // namespace writerperfect
214
215
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */