1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "swdocxreader.hxx"
22 #include <com/sun/star/document/XFilter.hpp>
23 #include <com/sun/star/document/XImporter.hpp>
24 #include <com/sun/star/frame/XModel.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/propertysequence.hxx>
30 #include <IDocumentStylePoolAccess.hxx>
32 #include <poolfmt.hxx>
34 #include <unotools/streamwrap.hxx>
35 #include <unotextrange.hxx>
36 #include <sfx2/docfile.hxx>
37 #include <tools/diagnose_ex.h>
39 #define AUTOTEXT_GALLERY "autoTxt"
43 extern "C" SAL_DLLPUBLIC_EXPORT Reader
* ImportDOCX()
45 return new SwDOCXReader
;
48 ErrCode
SwDOCXReader::Read(SwDoc
& rDoc
, const OUString
& /* rBaseURL */, SwPaM
& rPam
, const OUString
& /* FileName */ )
50 if (!m_pMedium
->GetInStream())
51 return ERR_SWG_READ_ERROR
;
53 // We want to work in an empty paragraph.
54 const SwPosition
* pPos
= rPam
.GetPoint();
55 rDoc
.getIDocumentContentOperations().SplitNode(*pPos
, false);
56 rDoc
.SetTextFormatColl(rPam
, rDoc
.getIDocumentStylePoolAccess().GetTextCollFromPool(RES_POOLCOLL_STANDARD
, false));
58 uno::Reference
<lang::XMultiServiceFactory
> xMultiServiceFactory(comphelper::getProcessServiceFactory());
59 uno::Reference
<uno::XInterface
> xInterface(xMultiServiceFactory
->createInstance("com.sun.star.comp.Writer.WriterFilter"), uno::UNO_SET_THROW
);
61 SwDocShell
* pDocShell(rDoc
.GetDocShell());
62 uno::Reference
<lang::XComponent
> xDstDoc(pDocShell
->GetModel(), uno::UNO_QUERY_THROW
);
63 uno::Reference
<document::XImporter
> xImporter(xInterface
, uno::UNO_QUERY_THROW
);
64 xImporter
->setTargetDocument(xDstDoc
);
66 const uno::Reference
<text::XTextRange
> xInsertTextRange
= SwXTextRange::CreateXTextRange(rDoc
, *rPam
.GetPoint(), nullptr);
67 uno::Reference
<io::XStream
> xStream(new utl::OStreamWrapper(*m_pMedium
->GetInStream()));
69 //SetLoading hack because the document properties will be re-initted
70 //by the xml filter and during the init, while it's considered uninitialized,
71 //setting a property will inform the document it's modified, which attempts
72 //to update the properties, which throws cause the properties are uninitialized
73 pDocShell
->SetLoading(SfxLoadedFlags::NONE
);
75 uno::Sequence
<beans::PropertyValue
> aDescriptor(comphelper::InitPropertySequence(
77 { "InputStream", uno::Any(xStream
) },
78 { "InsertMode", uno::Any(true) },
79 { "TextInsertModeRange", uno::Any(xInsertTextRange
) }
82 ErrCode ret
= ERRCODE_NONE
;
83 uno::Reference
<document::XFilter
> xFilter(xInterface
, uno::UNO_QUERY_THROW
);
86 xFilter
->filter(aDescriptor
);
88 catch (uno::Exception
const&)
90 TOOLS_WARN_EXCEPTION("sw.docx", "SwDOCXReader::Read()");
91 ret
= ERR_SWG_READ_ERROR
;
93 pDocShell
->SetLoading(SfxLoadedFlags::ALL
);
98 SwReaderType
SwDOCXReader::GetReaderType()
100 return SwReaderType::Storage
| SwReaderType::Stream
;
103 bool SwDOCXReader::HasGlossaries() const
109 bool SwDOCXReader::ReadGlossaries( SwTextBlocks
& rBlocks
, bool /* bSaveRelFiles */ ) const
111 uno::Reference
<lang::XMultiServiceFactory
> xMultiServiceFactory(
112 comphelper::getProcessServiceFactory() );
114 uno::Reference
<uno::XInterface
> xInterface(
115 xMultiServiceFactory
->createInstance( "com.sun.star.comp.Writer.WriterFilter" ),
116 uno::UNO_SET_THROW
);
118 uno::Reference
<document::XFilter
> xFilter( xInterface
, uno::UNO_QUERY_THROW
);
119 uno::Reference
<document::XImporter
> xImporter( xFilter
, uno::UNO_QUERY_THROW
);
121 SfxObjectShellLock
xDocSh( new SwDocShell( SfxObjectCreateMode::INTERNAL
) );
122 if( xDocSh
->DoInitNew() )
124 uno::Reference
<lang::XComponent
> xDstDoc( xDocSh
->GetModel(), uno::UNO_QUERY_THROW
);
125 xImporter
->setTargetDocument( xDstDoc
);
127 uno::Reference
<io::XStream
> xStream( new utl::OStreamWrapper( *m_pMedium
->GetInStream() ) );
129 uno::Sequence
<beans::PropertyValue
> aDescriptor( comphelper::InitPropertySequence({
130 { "InputStream", uno::Any(xStream
) },
131 { "ReadGlossaries", uno::Any(true) }
134 if( xFilter
->filter( aDescriptor
) )
136 if (rBlocks
.StartPutMuchBlockEntries())
138 bool bRet
= MakeEntries(static_cast<SwDocShell
*>(&xDocSh
)->GetDoc(), rBlocks
);
139 rBlocks
.EndPutMuchBlockEntries();
148 bool SwDOCXReader::MakeEntries( SwDoc
*pD
, SwTextBlocks
&rBlocks
)
150 const OUString
aOldURL( rBlocks
.GetBaseURL() );
151 rBlocks
.SetBaseURL( OUString() );
155 SwNodeIndex
aDocEnd( pD
->GetNodes().GetEndOfContent() );
156 SwNodeIndex
aStart( *aDocEnd
.GetNode().StartOfSectionNode(), 1 );
157 bool bIsAutoText
= false;
159 if( aStart
< aDocEnd
&& ( aDocEnd
.GetIndex() - aStart
.GetIndex() > 2 ) )
161 SwTextFormatColl
* pColl
= pD
->getIDocumentStylePoolAccess().GetTextCollFromPool
162 (RES_POOLCOLL_STANDARD
, false);
163 SwContentNode
* pCNd
= nullptr;
166 // Get name - first paragraph
169 SwPaM
aPam( aStart
);
170 SwNodeIndex
& rIdx
= aPam
.GetPoint()->nNode
;
172 aLNm
= aPam
.GetNode().GetTextNode()->GetText();
175 bIsAutoText
= aLNm
.startsWith(AUTOTEXT_GALLERY
);
176 aLNm
= aLNm
.copy(strlen(AUTOTEXT_GALLERY
) + 1);
183 SwPaM
aPam( aStart
);
185 SwNodeIndex
& rIdx
= aPam
.GetPoint()->nNode
;
187 pCNd
= rIdx
.GetNode().GetTextNode();
188 if( nullptr == pCNd
)
190 pCNd
= pD
->GetNodes().MakeTextNode( rIdx
, pColl
);
195 aPam
.GetPoint()->nContent
.Assign( pCNd
, 0 );
198 SwNodeIndex
& rIdx
= aPam
.GetPoint()->nNode
;
199 rIdx
= aStart
.GetNode().EndOfSectionIndex() - 1;
200 // don't add extra empty text node if exist (.dotx but not .dotm)
201 if( rIdx
.GetNode().GetTextNode() &&
202 rIdx
.GetNode().GetTextNode()->GetText().isEmpty() )
203 rIdx
= aStart
.GetNode().EndOfSectionIndex() - 2;
204 pCNd
= rIdx
.GetNode().GetContentNode();
205 if( nullptr == pCNd
)
208 pCNd
= pD
->GetNodes().MakeTextNode( rIdx
, pColl
);
212 aPam
.GetPoint()->nContent
.Assign( pCNd
, pCNd
->Len() );
216 // Now we have the right selection for one entry
219 OUString sShortcut
= aLNm
;
221 // Need to check make sure the shortcut is not already being used
222 sal_Int32 nStart
= 0;
223 sal_uInt16 nCurPos
= rBlocks
.GetIndex( sShortcut
);
225 while( sal_uInt16(-1) != nCurPos
)
227 // add a Number to it
228 sShortcut
= aLNm
+ OUString::number( ++nStart
);
229 nCurPos
= rBlocks
.GetIndex( sShortcut
);
232 if( rBlocks
.BeginPutDoc( sShortcut
, sShortcut
) )
234 SwDoc
* pGlDoc
= rBlocks
.GetDoc();
235 SwNodeIndex
aIdx( pGlDoc
->GetNodes().GetEndOfContent(), -1 );
236 pCNd
= aIdx
.GetNode().GetContentNode();
237 SwPosition
aPos( aIdx
, SwIndex( pCNd
, pCNd
? pCNd
->Len() : 0 ) );
238 pD
->getIDocumentContentOperations().CopyRange(aPam
, aPos
, SwCopyFlags::CheckPosInFly
);
247 aStart
= aStart
.GetNode().EndOfSectionIndex() + 1;
248 } while( aStart
< aDocEnd
&& aStart
.GetNode().IsStartNode() );
251 rBlocks
.SetBaseURL( aOldURL
);
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */