summaryrefslogtreecommitdiffstats
path: root/sc/source/filter/orcus/xmlcontext.cxx
blob: 1f3ee58220a002d5b2a3d9c560cefce3169998cd (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* -*- 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 <orcusfiltersimpl.hxx>
#include <orcusinterface.hxx>
#include <orcusxml.hxx>
#include <document.hxx>
#include <tokenarray.hxx>

#include <vcl/weld.hxx>
#include <ucbhelper/content.hxx>
#include <sal/log.hxx>
#include <osl/file.hxx>

#include <orcus/xml_structure_tree.hpp>
#include <orcus/xml_namespace.hpp>
#include <orcus/orcus_xml.hpp>
#include <orcus/sax_parser_base.hpp>
#include <orcus/stream.hpp>

#include <com/sun/star/io/XInputStream.hpp>
#include <comphelper/processfactory.hxx>

#include <string>
#include <sstream>

namespace com::sun::star::ucb { class XCommandEnvironment; }

#define BUFFER_SIZE 4096

using namespace com::sun::star;

namespace {

ScOrcusXMLTreeParam::EntryData& setUserDataToEntry(weld::TreeView& rControl,
    const weld::TreeIter& rEntry, ScOrcusXMLTreeParam::UserDataStoreType& rStore, ScOrcusXMLTreeParam::EntryType eType)
{
    rStore.push_back(std::make_unique<ScOrcusXMLTreeParam::EntryData>(eType));
    rControl.set_id(rEntry, OUString::number(reinterpret_cast<sal_Int64>(rStore.back().get())));
    return *rStore.back();
}

void setEntityNameToUserData(
    ScOrcusXMLTreeParam::EntryData& rEntryData,
    const orcus::xml_structure_tree::entity_name& entity, const orcus::xml_structure_tree::walker& walker)
{
    rEntryData.mnNamespaceID = walker.get_xmlns_index(entity.ns);
}

OUString toString(const orcus::xml_structure_tree::entity_name& entity, const orcus::xml_structure_tree::walker& walker)
{
    OUStringBuffer aBuf;
    if (entity.ns)
    {
        // Namespace exists.  Use the short version of the xml namespace name for display.
        std::string aShortName = walker.get_xmlns_short_name(entity.ns);
        aBuf.appendAscii(aShortName.c_str());
        aBuf.append(':');
    }
    aBuf.append(OUString(entity.name.get(), entity.name.size(), RTL_TEXTENCODING_UTF8));
    return aBuf.makeStringAndClear();
}

void populateTree(
   weld::TreeView& rTreeCtrl, orcus::xml_structure_tree::walker& rWalker,
   const orcus::xml_structure_tree::entity_name& rElemName, bool bRepeat,
   const weld::TreeIter* pParent, ScOrcusXMLTreeParam& rParam)
{
    OUString sEntry(toString(rElemName, rWalker));
    std::unique_ptr<weld::TreeIter> xEntry(rTreeCtrl.make_iterator());
    rTreeCtrl.insert(pParent, -1, &sEntry, nullptr, nullptr, nullptr, false, xEntry.get());
    rTreeCtrl.set_image(*xEntry, rParam.maImgElementDefault, -1);

    ScOrcusXMLTreeParam::EntryData& rEntryData = setUserDataToEntry(rTreeCtrl,
        *xEntry, rParam.m_UserDataStore,
        bRepeat ? ScOrcusXMLTreeParam::ElementRepeat : ScOrcusXMLTreeParam::ElementDefault);

    setEntityNameToUserData(rEntryData, rElemName, rWalker);

    if (bRepeat)
    {
        // Recurring elements use different icon.
       rTreeCtrl.set_image(*xEntry, rParam.maImgElementRepeat, -1);
    }

    orcus::xml_structure_tree::entity_names_type aNames = rWalker.get_attributes();

    // Insert attributes.
    for (const orcus::xml_structure_tree::entity_name& rAttrName : aNames)
    {
        OUString sAttr(toString(rAttrName, rWalker));
        std::unique_ptr<weld::TreeIter> xAttr(rTreeCtrl.make_iterator());
        rTreeCtrl.insert(xEntry.get(), -1, &sAttr, nullptr, nullptr, nullptr, false, xAttr.get());

        ScOrcusXMLTreeParam::EntryData& rAttrData =
            setUserDataToEntry(rTreeCtrl, *xAttr, rParam.m_UserDataStore, ScOrcusXMLTreeParam::Attribute);
        setEntityNameToUserData(rAttrData, rAttrName, rWalker);

        rTreeCtrl.set_image(*xAttr, rParam.maImgAttribute, -1);
    }

    aNames = rWalker.get_children();

    // Non-leaf if it has child elements, leaf otherwise.
    rEntryData.mbLeafNode = aNames.empty();

    // Insert child elements recursively.
    for (const auto& rName : aNames)
    {
        orcus::xml_structure_tree::element aElem = rWalker.descend(rName);
        populateTree(rTreeCtrl, rWalker, rName, aElem.repeat, xEntry.get(), rParam);
        rWalker.ascend();
    }
}

class TreeUpdateSwitch
{
    weld::TreeView& mrTreeCtrl;
public:
    explicit TreeUpdateSwitch(weld::TreeView& rTreeCtrl) : mrTreeCtrl(rTreeCtrl)
    {
        mrTreeCtrl.freeze();
    }

    ~TreeUpdateSwitch()
    {
        mrTreeCtrl.thaw();
    }
};

void loadContentFromURL(const OUString& rURL, std::string& rStrm)
{
    ucbhelper::Content aContent(
        rURL, uno::Reference<ucb::XCommandEnvironment>(), comphelper::getProcessComponentContext());
    uno::Reference<io::XInputStream> xStrm = aContent.openStream();

    std::ostringstream aStrmBuf;
    uno::Sequence<sal_Int8> aBytes;
    size_t nBytesRead = 0;
    do
    {
        nBytesRead = xStrm->readBytes(aBytes, BUFFER_SIZE);
        const sal_Int8* p = aBytes.getConstArray();
        aStrmBuf << std::string(p, p + nBytesRead);
    }
    while (nBytesRead == BUFFER_SIZE);

    rStrm = aStrmBuf.str();
}

}

ScOrcusXMLContextImpl::ScOrcusXMLContextImpl(ScDocument& rDoc, const OUString& rPath) :
    ScOrcusXMLContext(), mrDoc(rDoc), maPath(rPath) {}

ScOrcusXMLContextImpl::~ScOrcusXMLContextImpl() {}

void ScOrcusXMLContextImpl::loadXMLStructure(weld::TreeView& rTreeCtrl, ScOrcusXMLTreeParam& rParam)
{
    rParam.m_UserDataStore.clear();

    std::string aStrm;
    loadContentFromURL(maPath, aStrm);

    if (aStrm.empty())
        return;

    orcus::xmlns_context cxt = maNsRepo.create_context();
    orcus::xml_structure_tree aXmlTree(cxt);
    try
    {
        aXmlTree.parse(&aStrm[0], aStrm.size());

        TreeUpdateSwitch aSwitch(rTreeCtrl);
        rTreeCtrl.clear();

        orcus::xml_structure_tree::walker aWalker = aXmlTree.get_walker();

        // Root element.
        orcus::xml_structure_tree::element aElem = aWalker.root();
        populateTree(rTreeCtrl, aWalker, aElem.name, aElem.repeat, nullptr, rParam);
    }
    catch (const orcus::sax::malformed_xml_error& e)
    {
        SAL_WARN("sc.orcus", "Malformed XML error: " << e.what());
    }
    catch (const std::exception& e)
    {
        SAL_WARN("sc.orcus", "parsing failed with an unknown error " << e.what());
    }

    rTreeCtrl.all_foreach([&rTreeCtrl](weld::TreeIter& rEntry){
        rTreeCtrl.expand_row(rEntry);
        return false;
    });
}

namespace {

class SetNamespaceAlias
{
    orcus::orcus_xml& mrFilter;
    orcus::xmlns_repository& mrNsRepo;
public:
    SetNamespaceAlias(orcus::orcus_xml& filter, orcus::xmlns_repository& repo) :
        mrFilter(filter), mrNsRepo(repo) {}

    void operator() (size_t index)
    {
        orcus::xmlns_id_t nsid = mrNsRepo.get_identifier(index);
        if (nsid == orcus::XMLNS_UNKNOWN_ID)
            return;

        std::string alias = mrNsRepo.get_short_name(index);
        mrFilter.set_namespace_alias(alias.c_str(), nsid);
    }
};

}

void ScOrcusXMLContextImpl::importXML(const ScOrcusImportXMLParam& rParam)
{
    ScOrcusFactory aFactory(mrDoc, true);

    OUString aSysPath;
    if (osl::FileBase::getSystemPathFromFileURL(maPath, aSysPath) != osl::FileBase::E_None)
        return;

    OString aOSysPath = OUStringToOString(aSysPath, RTL_TEXTENCODING_UTF8);
    const char* path = aOSysPath.getStr();

    try
    {
        orcus::orcus_xml filter(maNsRepo, &aFactory, nullptr);

        // Define all used namespaces.
        std::for_each(rParam.maNamespaces.begin(), rParam.maNamespaces.end(), SetNamespaceAlias(filter, maNsRepo));

        // Set cell links.
        for (const ScOrcusImportXMLParam::CellLink& rLink : rParam.maCellLinks)
        {
            OUString aTabName;
            mrDoc.GetName(rLink.maPos.Tab(), aTabName);
            filter.set_cell_link(
                rLink.maPath.getStr(),
                OUStringToOString(aTabName, RTL_TEXTENCODING_UTF8).getStr(),
                rLink.maPos.Row(), rLink.maPos.Col());
        }

        // Set range links.
        for (const ScOrcusImportXMLParam::RangeLink& rLink : rParam.maRangeLinks)
        {
            OUString aTabName;
            mrDoc.GetName(rLink.maPos.Tab(), aTabName);
            filter.start_range(
                OUStringToOString(aTabName, RTL_TEXTENCODING_UTF8).getStr(),
                rLink.maPos.Row(), rLink.maPos.Col());

            std::for_each(rLink.maFieldPaths.begin(), rLink.maFieldPaths.end(),
                [&filter](const OString& rFieldPath)
                {
                    filter.append_field_link(rFieldPath.getStr(), orcus::pstring());
                }
            );

            std::for_each(rLink.maRowGroups.begin(), rLink.maRowGroups.end(),
                [&filter] (const OString& rRowGroup)
                {
                    filter.set_range_row_group(rRowGroup.getStr());
                }
            );

            filter.commit_range();
        }

        orcus::file_content content(path);
        filter.read_stream(content.data(), content.size());

        aFactory.finalize();
    }
    catch (const std::exception&)
    {
    }
}

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