VikasMahato

Untitled

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