VikasMahato

Untitled

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