diff options
author | Szymon Kłos <szymon.klos@collabora.com> | 2017-04-14 20:39:40 +0200 |
---|---|---|
committer | Szymon Kłos <szymon.klos@collabora.com> | 2017-04-18 12:44:45 +0200 |
commit | efc5643c9a11f68b4fd41ac1b019f373ada3d750 (patch) | |
tree | 240cdceafa8267165dcd2917bb309356143c1d48 /sw/source/filter/docx | |
parent | d08ea6b3d343fa852f9d1bec67082829ca023f91 (diff) |
AutoText: loading dotx documents
For testing purposes patch introduces simple
import. New AutoText entries are imported
if document contains single paragraph AutoTexts.
Change-Id: I3f0e17c63e109eac6514ae0cb8cc168e8282b55b
Reviewed-on: https://gerrit.libreoffice.org/36634
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Szymon Kłos <szymon.klos@collabora.com>
Diffstat (limited to 'sw/source/filter/docx')
-rw-r--r-- | sw/source/filter/docx/swdocxreader.cxx | 106 | ||||
-rw-r--r-- | sw/source/filter/docx/swdocxreader.hxx | 6 |
2 files changed, 109 insertions, 3 deletions
diff --git a/sw/source/filter/docx/swdocxreader.cxx b/sw/source/filter/docx/swdocxreader.cxx index 5975f1b862bd..b8ea965d3504 100644 --- a/sw/source/filter/docx/swdocxreader.cxx +++ b/sw/source/filter/docx/swdocxreader.cxx @@ -19,7 +19,22 @@ #include "swdocxreader.hxx" +#include <com/sun/star/document/XFilter.hpp> +#include <com/sun/star/document/XImporter.hpp> +#include <com/sun/star/xml/dom/XDocument.hpp> +#include <com/sun/star/xml/dom/XElement.hpp> +#include <com/sun/star/xml/dom/XNode.hpp> +#include <com/sun/star/xml/dom/XNodeList.hpp> +#include <comphelper/processfactory.hxx> +#include <comphelper/propertyvalue.hxx> +#include <comphelper/sequenceashashmap.hxx> +#include <docsh.hxx> #include <swerror.h> +#include <tools/ref.hxx> +#include <unotxdoc.hxx> +#include <unotools/streamwrap.hxx> + +using namespace css; extern "C" SAL_DLLPUBLIC_EXPORT Reader* SAL_CALL ImportDOCX() { @@ -42,10 +57,95 @@ bool SwDOCXReader::HasGlossaries() const return true; } -bool SwDOCXReader::ReadGlossaries( SwTextBlocks& /* rBlocks */, bool /* bSaveRelFiles */ ) const +bool SwDOCXReader::ReadGlossaries( SwTextBlocks& rBlocks, bool /* bSaveRelFiles */ ) const { - // TODO - return false; + bool bRet = false; + + uno::Reference<xml::dom::XDocument> xDoc = OpenDocument(); + + if( xDoc.is() ) + { + uno::Reference<xml::dom::XNodeList> xList = xDoc->getElementsByTagName( "docPartBody" ); + for( int i = 0; i < xList->getLength(); i++ ) + { + uno::Reference<xml::dom::XNode> xBody = xList->item( i ); + uno::Reference<xml::dom::XNode> xP = xBody->getFirstChild(); + uno::Reference<xml::dom::XNode> xR = xP->getFirstChild(); + uno::Reference<xml::dom::XNode> xT = xR->getFirstChild(); + uno::Reference<xml::dom::XNode> xText = xT->getFirstChild(); + OUString aText = xText->getNodeValue(); + if( !aText.isEmpty() ) + { + rBlocks.PutText( aText, aText, aText ); + bRet = true; + } + } + } + + return bRet; +} + +uno::Reference<xml::dom::XDocument> SwDOCXReader::OpenDocument() const +{ + uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory( + comphelper::getProcessServiceFactory() ); + + uno::Reference<uno::XInterface> xInterface( + xMultiServiceFactory->createInstance( "com.sun.star.comp.Writer.WriterFilter" ), + uno::UNO_QUERY_THROW ); + + uno::Reference<document::XFilter> xFilter( xInterface, uno::UNO_QUERY_THROW ); + uno::Reference<document::XImporter> xImporter( xFilter, uno::UNO_QUERY_THROW ); + + SfxObjectShellLock xDocSh( new SwDocShell( SfxObjectCreateMode::INTERNAL ) ); + xDocSh->DoInitNew(); + + uno::Reference<lang::XComponent> xDstDoc( xDocSh->GetModel(), uno::UNO_QUERY_THROW ); + xImporter->setTargetDocument( xDstDoc ); + + uno::Sequence<beans::PropertyValue> aDescriptor( 1 ); + aDescriptor[0].Name = "InputStream"; + uno::Reference<io::XStream> xStream( new utl::OStreamWrapper( *pMedium->GetInStream() ) ); + aDescriptor[0].Value <<= xStream; + + uno::Reference<xml::dom::XDocument> xDoc; + + try + { + xFilter->filter( aDescriptor ); + + comphelper::SequenceAsHashMap aGrabBag = GetGrabBag( xDstDoc ); + aGrabBag["OOXGlossary"] >>= xDoc; + } + catch (uno::Exception const& e) + { + SAL_WARN("sw.docx", "SwDOCXReader::OpenDocument(): exception: " << e.Message); + } + + return xDoc; +} + +comphelper::SequenceAsHashMap SwDOCXReader::GetGrabBag( const uno::Reference<lang::XComponent>& xDocument ) +{ + if( xDocument.is() ) + { + // get glossar document from the GrabBag + uno::Reference<beans::XPropertySet> xDocProps( xDocument, uno::UNO_QUERY ); + if( xDocProps.is() ) + { + uno::Reference<beans::XPropertySetInfo> xPropsInfo = xDocProps->getPropertySetInfo(); + + const OUString aGrabBagPropName = "InteropGrabBag"; + if( xPropsInfo.is() && xPropsInfo->hasPropertyByName( aGrabBagPropName ) ) + { + // get existing grab bag + comphelper::SequenceAsHashMap aGrabBag( xDocProps->getPropertyValue( aGrabBagPropName ) ); + return aGrabBag; + } + } + } + + return comphelper::SequenceAsHashMap(); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/filter/docx/swdocxreader.hxx b/sw/source/filter/docx/swdocxreader.hxx index 35fdee86a643..2646a963523f 100644 --- a/sw/source/filter/docx/swdocxreader.hxx +++ b/sw/source/filter/docx/swdocxreader.hxx @@ -21,6 +21,9 @@ #define INCLUDED_SW_SOURCE_FILTER_DOCX_SWDOCXREADER_HXX #include <shellio.hxx> +#include <comphelper/sequenceashashmap.hxx> +#include <com/sun/star/xml/dom/XDocument.hpp> +#include <tools/ref.hxx> /// Wrapper for the UNO DOCX import filter (in writerfilter) for autotext purposes. class SwDOCXReader : public StgReader @@ -33,6 +36,9 @@ public: private: virtual sal_uLong Read( SwDoc&, const OUString&, SwPaM&, const OUString& ) override; + + uno::Reference<css::xml::dom::XDocument> OpenDocument() const; + static comphelper::SequenceAsHashMap GetGrabBag( const uno::Reference<lang::XComponent>& xDocument ); }; #endif |