Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* -*- 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 "xmldataprovider.hxx"
- #include <datatransformation.hxx>
- #include <salhelper/thread.hxx>
- #include <comphelper/string.hxx>
- #include <orcusfiltersimpl.hxx>
- #include <orcusinterface.hxx>
- #include <orcusxml.hxx>
- #include <document.hxx>
- #include <svtools/treelistbox.hxx>
- #include <svtools/treelistentry.hxx>
- #include <ucbhelper/content.hxx>
- #include <o3tl/make_unique.hxx>
- #include <orcus/spreadsheet/import_interface.hpp>
- #include <orcus/xml_structure_tree.hpp>
- #include <orcus/xml_namespace.hpp>
- #include <orcus/orcus_xml.hpp>
- #include <orcus/global.hpp>
- #include <orcus/sax_parser_base.hpp>
- #include <orcus/stream.hpp>
- #include <com/sun/star/ucb/XCommandEnvironment.hpp>
- #include <comphelper/processfactory.hxx>
- #include <iostream>
- #include <string>
- #include <sstream>
- using namespace com::sun::star;
- namespace sc
- {
- class XMLFetchThread : public salhelper::Thread
- {
- ScDocument& mrDocument;
- OUString maURL;
- OUString maTreePath;
- ScOrcusImportXMLParam maParam;
- bool mbTerminate;
- osl::Mutex maMtxTerminate;
- const std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations;
- std::function<void()> maImportFinishedHdl;
- public:
- XMLFetchThread(ScDocument& rDoc, const OUString& rURL, ScOrcusImportXMLParam aParam, const OUString& rTreePath,
- std::function<void()> aImportFinishedHdl,
- const std::vector<std::shared_ptr<sc::DataTransformation>>& mrTransformations);
- ~XMLFetchThread();
- void RequestTerminate();
- bool IsRequestedTerminate();
- void Terminate();
- void EndThread();
- virtual void execute() override;
- };
- XMLFetchThread::XMLFetchThread(
- ScDocument& rDoc, const OUString& rURL, ScOrcusImportXMLParam aParam, const OUString& rTreePath,
- std::function<void()> aImportFinishedHdl,
- const std::vector<std::shared_ptr<sc::DataTransformation>>& mrTransformations)
- : salhelper::Thread("XML Fetch Thread")
- , mrDocument(rDoc)
- , maURL(rURL)
- , maTreePath(rTreePath)
- , maParam(aParam)
- , maDataTransformations(mrTransformations)
- , maImportFinishedHdl(aImportFinishedHdl)
- {
- }
- XMLFetchThread::~XMLFetchThread()
- {
- }
- bool XMLFetchThread::IsRequestedTerminate()
- {
- osl::MutexGuard aGuard(maMtxTerminate);
- return mbTerminate;
- }
- void XMLFetchThread::RequestTerminate()
- {
- osl::MutexGuard aGuard(maMtxTerminate);
- mbTerminate = true;
- }
- void XMLFetchThread::EndThread()
- {
- RequestTerminate();
- }
- void XMLFetchThread::execute()
- {
- OStringBuffer aBuffer(64000);
- std::unique_ptr<SvStream> pStream = DataProvider::FetchStreamFromURL(maURL, aBuffer);
- if (aBuffer.isEmpty())
- return;
- try
- {
- // Handle Import
- }
- catch (const std::exception&)
- {
- }
- for (auto& itr : maDataTransformations)
- {
- itr->Transform(mrDocument);
- }
- SolarMutexGuard aGuard;
- maImportFinishedHdl();
- }
- XMLDataProvider::XMLDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource)
- : DataProvider(rDataSource)
- , mpDocument(pDoc)
- {
- }
- XMLDataProvider::~XMLDataProvider()
- {
- if (mxXMLFetchThread.is())
- {
- SolarMutexReleaser aReleaser;
- mxXMLFetchThread->join();
- }
- }
- void XMLDataProvider::Import()
- {
- // already importing data
- if (mpDoc)
- return;
- mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
- mpDoc->ResetClip(mpDocument, SCTAB(0));
- mxXMLFetchThread = new XMLFetchThread(*mpDoc, mrDataSource.getURL(), mrDataSource.getXMLParam(),
- mrDataSource.getTreePath(),
- std::bind(&XMLDataProvider::ImportFinished, this),
- mrDataSource.getDataTransformation());
- mxXMLFetchThread->launch();
- if (mbDeterministic)
- {
- SolarMutexReleaser aReleaser;
- mxXMLFetchThread->join();
- }
- }
- std::map<OUString, OUString> XMLDataProvider::getDataSourcesForURL(const OUString& /*rURL*/)
- {
- std::map<OUString, OUString> aMap;
- OStringBuffer aBuffer(64000);
- std::unique_ptr<SvStream> pStream
- = DataProvider::FetchStreamFromURL(mrDataSource.getURL(), aBuffer);
- if (aBuffer.isEmpty())
- return std::map<OUString, OUString>();
- return aMap;
- }
- void XMLDataProvider::ImportFinished()
- {
- mrDataSource.getDBManager()->WriteToDoc(*mpDoc);
- mxXMLFetchThread.clear();
- mpDoc.reset();
- }
- const OUString& XMLDataProvider::GetURL() const { return mrDataSource.getURL(); }
- }
- /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Add Comment
Please, Sign In to add comment