VikasMahato

Untitled

Jun 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.94 KB | None | 0 0
  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 "xmldataprovider.hxx"
  11. #include <datatransformation.hxx>
  12. #include <salhelper/thread.hxx>
  13.  
  14. #include <comphelper/string.hxx>
  15. #include <orcusfiltersimpl.hxx>
  16. #include <orcusinterface.hxx>
  17. #include <orcusxml.hxx>
  18. #include <document.hxx>
  19.  
  20. #include <svtools/treelistbox.hxx>
  21. #include <svtools/treelistentry.hxx>
  22. #include <ucbhelper/content.hxx>
  23. #include <o3tl/make_unique.hxx>
  24.  
  25. #include <orcus/spreadsheet/import_interface.hpp>
  26. #include <orcus/xml_structure_tree.hpp>
  27. #include <orcus/xml_namespace.hpp>
  28. #include <orcus/orcus_xml.hpp>
  29. #include <orcus/global.hpp>
  30. #include <orcus/sax_parser_base.hpp>
  31.  
  32. #include <com/sun/star/ucb/XCommandEnvironment.hpp>
  33. #include <comphelper/processfactory.hxx>
  34.  
  35. #include <string>
  36. #include <sstream>
  37.  
  38. using namespace com::sun::star;
  39.  
  40. namespace sc
  41. {
  42. class XMLFetchThread : public salhelper::Thread
  43. {
  44.     ScDocument& mrDocument;
  45.     OUString maURL;
  46.     OUString maTreePath;
  47.     ScOrcusImportXMLParam maParam;
  48.  
  49.     bool mbTerminate;
  50.     osl::Mutex maMtxTerminate;
  51.  
  52.     const std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations;
  53.     std::function<void()> maImportFinishedHdl;
  54.  
  55. public:
  56.     XMLFetchThread(ScDocument& rDoc, const OUString& rURL, ScOrcusImportXMLParam aParam, const OUString& rTreePath,
  57.                    std::function<void()> aImportFinishedHdl,
  58.                    const std::vector<std::shared_ptr<sc::DataTransformation>>& mrTransformations);
  59.  
  60.     void RequestTerminate();
  61.     bool IsRequestedTerminate();
  62.     void Terminate();
  63.     void EndThread();
  64.  
  65.     virtual void execute() override;
  66. };
  67.  
  68. XMLFetchThread::XMLFetchThread(
  69.     ScDocument& rDoc, const OUString& rURL, ScOrcusImportXMLParam aParam, const OUString& rTreePath,
  70.     std::function<void()> aImportFinishedHdl,
  71.     const std::vector<std::shared_ptr<sc::DataTransformation>>& mrTransformations)
  72.     : salhelper::Thread("XML Fetch Thread")
  73.     , mrDocument(rDoc)
  74.     , maURL(rURL)
  75.     , maTreePath(rTreePath)
  76.     , maParam(aParam)
  77.     , maDataTransformations(mrTransformations)
  78.     , maImportFinishedHdl(aImportFinishedHdl)
  79. {
  80. }
  81.  
  82. XMLFetchThread::~XMLFetchThread()
  83. {
  84. }
  85.  
  86. bool XMLFetchThread::IsRequestedTerminate()
  87. {
  88.     osl::MutexGuard aGuard(maMtxTerminate);
  89.     return mbTerminate;
  90. }
  91.  
  92. void XMLFetchThread::RequestTerminate()
  93. {
  94.     osl::MutexGuard aGuard(maMtxTerminate);
  95.     mbTerminate = true;
  96. }
  97.  
  98. void XMLFetchThread::EndThread()
  99. {
  100.     RequestTerminate();
  101. }
  102.  
  103. void XMLFetchThread::execute()
  104. {
  105.     OStringBuffer aBuffer(64000);
  106.     std::unique_ptr<SvStream> pStream = DataProvider::FetchStreamFromURL(maURL, aBuffer);
  107.  
  108.     if (aBuffer.isEmpty())
  109.         return;
  110.  
  111.     ScOrcusFactory aFactory(mrDocument);
  112.  
  113.     try
  114.     {
  115.         // Handle Import
  116.  
  117.     }
  118.     catch (const std::exception&)
  119.     {
  120.     }
  121.  
  122.     for (auto& itr : maDataTransformations)
  123.     {
  124.         itr->Transform(mrDocument);
  125.     }
  126.  
  127.     SolarMutexGuard aGuard;
  128.     maImportFinishedHdl();
  129. }
  130.  
  131. XMLDataProvider::XMLDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource)
  132.     : DataProvider(rDataSource)
  133.     , mpDocument(pDoc)
  134. {
  135. }
  136.  
  137. XMLDataProvider::~XMLDataProvider()
  138. {
  139.     if (mxXMLFetchThread.is())
  140.     {
  141.         SolarMutexReleaser aReleaser;
  142.         mxXMLFetchThread->join();
  143.     }
  144. }
  145.  
  146. void XMLDataProvider::Import()
  147. {
  148.     // already importing data
  149.     if (mpDoc)
  150.         return;
  151.  
  152.     mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
  153.     mpDoc->ResetClip(mpDocument, SCTAB(0));
  154.  
  155.     mxXMLFetchThread = new XMLFetchThread(*mpDoc, mrDataSource.getURL(), mrDataSource.getXMLParam(),
  156.                                           mrDataSource.getTreePath(),
  157.                                           std::bind(&XMLDataProvider::ImportFinished, this),
  158.                                           mrDataSource.getDataTransformation());
  159.     mxXMLFetchThread->launch();
  160.  
  161.     if (mbDeterministic)
  162.     {
  163.         SolarMutexReleaser aReleaser;
  164.         mxXMLFetchThread->join();
  165.     }
  166. }
  167.  
  168. std::map<OUString, OUString> XMLDataProvider::getDataSourcesForURL(const OUString& /*rURL*/)
  169. {
  170.     std::map<OUString, OUString> aMap;
  171.  
  172.     OStringBuffer aBuffer(64000);
  173.     std::unique_ptr<SvStream> pStream
  174.         = DataProvider::FetchStreamFromURL(mrDataSource.getURL(), aBuffer);
  175.  
  176.     if (aBuffer.isEmpty())
  177.         return std::map<OUString, OUString>();
  178.  
  179.     return aMap;
  180. }
  181.  
  182. void XMLDataProvider::ImportFinished()
  183. {
  184.     mrDataSource.getDBManager()->WriteToDoc(*mpDoc);
  185.     mxXMLFetchThread.clear();
  186.     mpDoc.reset();
  187. }
  188.  
  189. const OUString& XMLDataProvider::GetURL() const { return mrDataSource.getURL(); }
  190. }
  191.  
  192. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Add Comment
Please, Sign In to add comment