nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / filter / docx / swdocxreader.cxx
blob790f91f0d3440dcb5cd5d205ad9ab025c293b08d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
28 #include <doc.hxx>
29 #include <docsh.hxx>
30 #include <IDocumentStylePoolAccess.hxx>
31 #include <ndtxt.hxx>
32 #include <poolfmt.hxx>
33 #include <swerror.h>
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"
41 using namespace css;
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) }
80 }));
82 ErrCode ret = ERRCODE_NONE;
83 uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
84 try
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);
95 return ret;
98 SwReaderType SwDOCXReader::GetReaderType()
100 return SwReaderType::Storage | SwReaderType::Stream;
103 bool SwDOCXReader::HasGlossaries() const
105 // TODO
106 return true;
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) }
132 }));
134 if( xFilter->filter( aDescriptor ) )
136 if (rBlocks.StartPutMuchBlockEntries())
138 bool bRet = MakeEntries(static_cast<SwDocShell*>(&xDocSh)->GetDoc(), rBlocks);
139 rBlocks.EndPutMuchBlockEntries();
140 return bRet;
145 return false;
148 bool SwDOCXReader::MakeEntries( SwDoc *pD, SwTextBlocks &rBlocks )
150 const OUString aOldURL( rBlocks.GetBaseURL() );
151 rBlocks.SetBaseURL( OUString() );
153 bool bRet = false;
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;
164 bRet = true;
165 do {
166 // Get name - first paragraph
167 OUString aLNm;
169 SwPaM aPam( aStart );
170 SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
171 ++rIdx;
172 aLNm = aPam.GetNode().GetTextNode()->GetText();
174 // is AutoText?
175 bIsAutoText = aLNm.startsWith(AUTOTEXT_GALLERY);
176 aLNm = aLNm.copy(strlen(AUTOTEXT_GALLERY) + 1);
179 // Do not copy name
180 aStart++;
182 // Get content
183 SwPaM aPam( aStart );
185 SwNodeIndex& rIdx = aPam.GetPoint()->nNode;
186 ++rIdx;
187 pCNd = rIdx.GetNode().GetTextNode();
188 if( nullptr == pCNd )
190 pCNd = pD->GetNodes().MakeTextNode( rIdx, pColl );
191 rIdx = *pCNd;
195 aPam.GetPoint()->nContent.Assign( pCNd, 0 );
196 aPam.SetMark();
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 )
207 ++rIdx;
208 pCNd = pD->GetNodes().MakeTextNode( rIdx, pColl );
209 rIdx = *pCNd;
212 aPam.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
214 if( bIsAutoText )
216 // Now we have the right selection for one entry
217 rBlocks.ClearDoc();
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);
239 rBlocks.PutDoc();
241 else
243 bRet = false;
247 aStart = aStart.GetNode().EndOfSectionIndex() + 1;
248 } while( aStart < aDocEnd && aStart.GetNode().IsStartNode() );
251 rBlocks.SetBaseURL( aOldURL );
253 return bRet;
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */