LibreOffice Module sw (master) 1
swparrtf.cxx
Go to the documentation of this file.
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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <poolfmt.hxx>
21#include <shellio.hxx>
22#include <ndtxt.hxx>
23#include <doc.hxx>
24#include <docsh.hxx>
26#include <swdll.hxx>
27#include <swerror.h>
28
29#include <unotextrange.hxx>
30
35
36#include <com/sun/star/document/XFilter.hpp>
37#include <com/sun/star/document/XImporter.hpp>
38#include <com/sun/star/frame/XModel.hpp>
39#include <com/sun/star/lang/XMultiServiceFactory.hpp>
40
41using namespace ::com::sun::star;
42
43namespace
44{
46class SwRTFReader : public Reader
47{
48 ErrCode Read(SwDoc& rDoc, const OUString& rBaseURL, SwPaM& rPam,
49 const OUString& rFileName) override;
50};
51}
52
53ErrCode SwRTFReader::Read(SwDoc& rDoc, const OUString& /*rBaseURL*/, SwPaM& rPam,
54 const OUString& /*rFileName*/)
55{
56 if (!m_pStream)
57 return ERR_SWG_READ_ERROR;
58
59 // We want to work in an empty paragraph.
60 // Step 1: XTextRange will be updated when content is inserted, so we know
61 // the end position.
62 const rtl::Reference<SwXTextRange> xInsertPosition
63 = SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), nullptr);
64 auto pSttNdIdx = std::make_shared<SwNodeIndex>(rDoc.GetNodes());
65 const SwPosition* pPos = rPam.GetPoint();
66
67 // Step 2: Split once and remember the node that has been split.
68 rDoc.getIDocumentContentOperations().SplitNode(*pPos, false);
69 *pSttNdIdx = pPos->GetNodeIndex() - 1;
70
71 // Step 3: Split again.
72 rDoc.getIDocumentContentOperations().SplitNode(*pPos, false);
73 auto pSttNdIdx2 = std::make_shared<SwNodeIndex>(rDoc.GetNodes());
74 *pSttNdIdx2 = pPos->GetNodeIndex();
75
76 // Step 4: Insert all content into the new node
77 rPam.Move(fnMoveBackward);
80
81 SwDocShell* pDocShell(rDoc.GetDocShell());
82 uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(
84 uno::Reference<uno::XInterface> xInterface(
85 xMultiServiceFactory->createInstance("com.sun.star.comp.Writer.RtfFilter"),
86 uno::UNO_SET_THROW);
87
88 uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
89 uno::Reference<lang::XComponent> xDstDoc(pDocShell->GetModel(), uno::UNO_QUERY_THROW);
90 xImporter->setTargetDocument(xDstDoc);
91
92 const rtl::Reference<SwXTextRange> xInsertTextRange
93 = SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), nullptr);
94
95 uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
96 uno::Sequence<beans::PropertyValue> aDescriptor(comphelper::InitPropertySequence(
97 { { "InputStream",
98 uno::Any(uno::Reference<io::XStream>(new utl::OStreamWrapper(*m_pStream))) },
99 { "InsertMode", uno::Any(true) },
100 { "TextInsertModeRange",
101 uno::Any(uno::Reference<text::XTextRange>(xInsertTextRange)) } }));
102 auto ret = ERRCODE_NONE;
103 try
104 {
105 xFilter->filter(aDescriptor);
106 }
107 catch (uno::Exception const&)
108 {
109 TOOLS_WARN_EXCEPTION("sw.rtf", "SwRTFReader::Read()");
110 ret = ERR_SWG_READ_ERROR;
111 }
112
113 // Clean up the fake paragraphs.
114 SwUnoInternalPaM aPam(rDoc);
115 ::sw::XTextRangeToSwPaM(aPam, xInsertPosition);
116 if (pSttNdIdx->GetIndex())
117 {
118 // If we are in insert mode, join the split node that is in front
119 // of the new content with the first new node. Or in other words:
120 // Revert the first split node.
121 SwTextNode* pTextNode = pSttNdIdx->GetNode().GetTextNode();
122 SwNodeIndex aNxtIdx(*pSttNdIdx);
123 if (pTextNode && pTextNode->CanJoinNext(&aNxtIdx)
124 && pSttNdIdx->GetIndex() + 1 == aNxtIdx.GetIndex())
125 {
126 // If the PaM points to the first new node, move the PaM to the
127 // end of the previous node.
128 if (aPam.GetPoint()->GetNode() == aNxtIdx.GetNode())
129 {
130 aPam.GetPoint()->Assign(*pTextNode, pTextNode->GetText().getLength());
131 }
132 // If the first new node isn't empty, convert the node's text
133 // attributes into hints. Otherwise, set the new node's
134 // paragraph style at the previous (empty) node.
135 SwTextNode* pDelNd = aNxtIdx.GetNode().GetTextNode();
136 if (pTextNode->GetText().getLength())
137 pDelNd->FormatToTextAttr(pTextNode);
138 else
139 {
140 pTextNode->ChgFormatColl(pDelNd->GetTextColl());
141 if (!pDelNd->GetNoCondAttr(RES_PARATR_LIST_ID, /*bInParents=*/false))
142 {
143 // Lists would need manual merging, but copy paragraph direct formatting
144 // otherwise.
145 pDelNd->CopyCollFormat(*pTextNode);
146 }
147 }
148 pTextNode->JoinNext();
149 }
150 }
151
152 if (pSttNdIdx2->GetIndex())
153 {
154 // If we are in insert mode, join the split node that is after
155 // the new content with the last new node. Or in other words:
156 // Revert the second split node.
157 SwTextNode* pTextNode = pSttNdIdx2->GetNode().GetTextNode();
158 SwNodeIndex aPrevIdx(*pSttNdIdx2);
159 if (pTextNode && pTextNode->CanJoinPrev(&aPrevIdx)
160 && pSttNdIdx2->GetIndex() - 1 == aPrevIdx.GetIndex())
161 {
162 // If the last new node isn't empty, convert the node's text
163 // attributes into hints. Otherwise, set the new node's
164 // paragraph style at the next (empty) node.
165 SwTextNode* pDelNd = aPrevIdx.GetNode().GetTextNode();
166 if (pTextNode->GetText().getLength())
167 pDelNd->FormatToTextAttr(pTextNode);
168 else
169 pTextNode->ChgFormatColl(pDelNd->GetTextColl());
170 pTextNode->JoinPrev();
171 }
172 }
173
174 return ret;
175}
176
177extern "C" SAL_DLLPUBLIC_EXPORT Reader* ImportRTF() { return new SwRTFReader; }
178
179extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportRTF(SvStream& rStream)
180{
182
183 SfxObjectShellLock xDocSh(new SwDocShell(SfxObjectCreateMode::INTERNAL));
184 xDocSh->DoInitNew();
185
186 uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(
188 uno::Reference<uno::XInterface> xInterface(
189 xMultiServiceFactory->createInstance("com.sun.star.comp.Writer.RtfFilter"),
190 uno::UNO_SET_THROW);
191
192 uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
193 uno::Reference<lang::XComponent> xDstDoc(xDocSh->GetModel(), uno::UNO_QUERY_THROW);
194 xImporter->setTargetDocument(xDstDoc);
195
196 uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
197 uno::Sequence<beans::PropertyValue> aDescriptor(comphelper::InitPropertySequence(
198 { { "InputStream",
199 uno::Any(uno::Reference<io::XStream>(new utl::OStreamWrapper(rStream))) } }));
200 bool bRet = true;
201 try
202 {
203 xFilter->filter(aDescriptor);
204 }
205 catch (...)
206 {
207 bRet = false;
208 }
209 return bRet;
210}
211
212/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool SplitNode(const SwPosition &rPos, bool bChkTableStart)=0
Split a node at rPos (implemented only for TextNode).
virtual SwTextFormatColl * GetTextCollFromPool(sal_uInt16 nId, bool bRegardLanguage=true)=0
Return "Auto-Collection with ID.
virtual ErrCode Read(SwDoc &, const OUString &rBaseURL, SwPaM &, const OUString &)=0
css::uno::Reference< css::frame::XModel3 > GetModel() const
bool CanJoinNext(SwNodeIndex *pIdx=nullptr) const
Is it possible to join two nodes? In pIdx the second position can be returned.
Definition: node.cxx:1844
bool CanJoinPrev(SwNodeIndex *pIdx=nullptr) const
Can we join two Nodes? We can return the 2nd position in pIdx.
Definition: node.cxx:1892
const SfxPoolItem * GetNoCondAttr(sal_uInt16 nWhich, bool bInParents) const
Obtains attribute that is not delivered via conditional style!
Definition: node.cxx:1804
Definition: doc.hxx:197
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:329
SwNodes & GetNodes()
Definition: doc.hxx:422
IDocumentStylePoolAccess const & getIDocumentStylePoolAccess() const
Definition: doc.cxx:440
bool SetTextFormatColl(const SwPaM &rRg, SwTextFormatColl *pFormat, const bool bReset=true, const bool bResetListAttrs=false, SwRootFrame const *pLayout=nullptr)
Add 4th optional parameter <bResetListAttrs>.
Definition: docfmt.cxx:1100
SwDocShell * GetDocShell()
Definition: doc.hxx:1370
Marks a node in the document model.
Definition: ndindex.hxx:31
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:901
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:188
bool Move(SwMoveFnCollection const &fnMove=fnMoveForward, SwGoInDoc fnGo=GoInContent)
Movement of cursor.
Definition: pam.cxx:657
const SwPosition * GetPoint() const
Definition: pam.hxx:253
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:112
virtual SwContentNode * JoinNext() override
Definition: ndtxt.cxx:1002
void FormatToTextAttr(SwTextNode *pNd)
Convey attributes of an AttrSet (AutoFormat) to SwpHintsArray.
Definition: thints.cxx:2539
void JoinPrev()
Definition: ndtxt.cxx:1119
virtual SwFormatColl * ChgFormatColl(SwFormatColl *) override
Definition: ndtxt.cxx:4060
void CopyCollFormat(SwTextNode &rDestNd, bool bUndoForChgFormatColl=true)
Copy collection with all auto formats to dest-node.
Definition: ndcopy.cxx:339
const OUString & GetText() const
Definition: ndtxt.hxx:244
SwTextFormatColl * GetTextColl() const
Definition: ndtxt.hxx:895
static rtl::Reference< SwXTextRange > CreateXTextRange(SwDoc &rDoc, const SwPosition &rPos, const SwPosition *const pMark)
Definition: unoobj2.cxx:1221
#define TOOLS_WARN_EXCEPTION(area, stream)
#define ERRCODE_NONE
constexpr TypedWhichId< SfxStringItem > RES_PARATR_LIST_ID(RES_PARATR_LIST_BEGIN)
static OUString Read(SvStream &rStream)
Definition: ww8scan.cxx:6697
void ensure()
Definition: swdll.cxx:71
Reference< XMultiServiceFactory > getProcessServiceFactory()
css::uno::Sequence< css::beans::PropertyValue > InitPropertySequence(::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit)
bool XTextRangeToSwPaM(SwUnoInternalPaM &rToFill, const uno::Reference< text::XTextRange > &xTextRange, ::sw::TextRangeMode const eMode)
Definition: unoobj2.cxx:1108
SwMoveFnCollection const & fnMoveBackward
Definition: paminit.cxx:60
@ RES_POOLCOLL_STANDARD
Standard.
Definition: poolfmt.hxx:250
Marks a position in the document model.
Definition: pam.hxx:38
SwNodeOffset GetNodeIndex() const
Definition: pam.hxx:78
#define ERR_SWG_READ_ERROR
Definition: swerror.h:25
SAL_DLLPUBLIC_EXPORT bool TestImportRTF(SvStream &rStream)
Definition: swparrtf.cxx:179
SAL_DLLPUBLIC_EXPORT Reader * ImportRTF()
Definition: swparrtf.cxx:177