LibreOffice Module editeng (master) 1
xmltxtimp.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 <com/sun/star/text/XText.hpp>
23#include <svl/itemprop.hxx>
24#include <utility>
25#include <xmloff/xmlimp.hxx>
26#include <xmloff/xmlictxt.hxx>
27#include <xmloff/xmltoken.hxx>
29#include <xmloff/xmlstyle.hxx>
30#include "editsource.hxx"
31#include <editxml.hxx>
32#include <editdoc.hxx>
33#include <editeng/editeng.hxx>
34#include <editeng/unotext.hxx>
35#include <editeng/unoprnms.hxx>
36#include <editeng/unoipset.hxx>
37#include <cassert>
38#include <unomodel.hxx>
39
40using namespace com::sun::star;
41using namespace com::sun::star::document;
42using namespace com::sun::star::uno;
43using namespace com::sun::star::lang;
44using namespace com::sun::star::xml::sax;
45using namespace com::sun::star::text;
46using namespace cppu;
47using namespace xmloff::token;
48
49namespace {
50
51class SvxXMLTextImportContext : public SvXMLImportContext
52{
53public:
54 SvxXMLTextImportContext( SvXMLImport& rImport, uno::Reference< XText > xText );
55
56 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
57 sal_Int32 nElement,
58 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList) override;
59
60private:
61 const uno::Reference< XText > mxText;
62};
63
64}
65
66SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, uno::Reference< XText > xText )
67: SvXMLImportContext( rImport ), mxText(std::move( xText ))
68{
69}
70
71css::uno::Reference< css::xml::sax::XFastContextHandler > SvxXMLTextImportContext::createFastChildContext(
72 sal_Int32 nElement,
73 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
74{
75 SvXMLImportContext* pContext = nullptr;
76 if(nElement == XML_ELEMENT(OFFICE, XML_BODY ))
77 {
78 pContext = new SvxXMLTextImportContext( GetImport(), mxText );
79 }
80 else if( nElement == XML_ELEMENT(OFFICE, XML_AUTOMATIC_STYLES ) )
81 {
82 pContext = new SvXMLStylesContext( GetImport() );
83 GetImport().GetTextImport()->SetAutoStyles( static_cast<SvXMLStylesContext*>(pContext) );
84 }
85 else
86 pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nElement, xAttrList );
87 return pContext;
88}
89
90namespace {
91
92class SvxXMLXTextImportComponent : public SvXMLImport
93{
94public:
95 SvxXMLXTextImportComponent(
96 const css::uno::Reference< css::uno::XComponentContext >& rContext,
97 uno::Reference< XText > xText );
98
99 virtual SvXMLImportContext* CreateFastContext(sal_Int32 nElement,
100 const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList >& xAttrList ) override;
101
102private:
103 const uno::Reference< XText > mxText;
104};
105
106}
107
108SvXMLImportContext *SvxXMLXTextImportComponent::CreateFastContext(
109 sal_Int32 nElement,
110 const uno::Reference< xml::sax::XFastAttributeList >& /*xAttrList*/)
111{
112 SvXMLImportContext* pContext = nullptr;
113
114 if(nElement == XML_ELEMENT(OFFICE, XML_DOCUMENT_CONTENT ) )
115 {
116 pContext = new SvxXMLTextImportContext( *this, mxText );
117 }
118
119 return pContext;
120}
121
122SvxXMLXTextImportComponent::SvxXMLXTextImportComponent(
123 const css::uno::Reference< css::uno::XComponentContext >& xContext,
124 uno::Reference< XText > xText )
125: SvXMLImport(xContext, ""),
126 mxText(std::move( xText ))
127{
128 GetTextImport()->SetCursor( mxText->createTextCursor() );
129 SvXMLImport::setTargetDocument(new SvxSimpleUnoModel);
130}
131
132EditPaM SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
133{
134 SvxEditEngineSource aEditSource( &rEditEngine );
135
136 static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap[] =
137 {
140// bullets & numbering props, tdf#128046
145 };
146 static SvxItemPropertySet aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
147
148 assert(!rSel.HasRange());
149 //get the initial para count before paste
150 sal_uInt32 initialParaCount = rEditEngine.GetEditDoc().Count();
151 //insert para breaks before inserting the copied text
152 rEditEngine.InsertParaBreak( rEditEngine.CreateSelection( rSel ).Max() );
153 rEditEngine.InsertParaBreak( rEditEngine.CreateSelection( rSel ).Max() );
154
155 // Init return PaM.
156 EditPaM aPaM( rEditEngine.CreateSelection( rSel ).Max());
157
158 ESelection aSel(rSel.nStartPara+1, 0, rSel.nEndPara+1, 0);
159 uno::Reference<text::XText > xParent;
160 rtl::Reference<SvxUnoText> pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextImportComponentPropertySet, xParent );
161 pUnoText->SetSelection( aSel );
162
163 try
164 {
165 do
166 {
167 uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
168
169 uno::Reference<io::XInputStream> xInputStream = new utl::OInputStreamWrapper( rStream );
170
171/* testcode
172 static constexpr OUStringLiteral aURL( u"file:///e:/test.xml" );
173 SfxMedium aMedium( aURL, StreamMode::READ | STREAM_NOCREATE, sal_True );
174 uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
175
176 aMedium.GetInStream()->Seek( 0 );
177 uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
178
179 if( !xSource.is() )
180 {
181 OSL_FAIL( "got no data source from medium" );
182 break;
183 }
184
185 uno::Reference< XInterface > xPipe( Pipe::create(comphelper::getComponentContext(xServiceFactory)), UNO_QUERY );
186
187 // connect pipe's output stream to the data source
188 xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) );
189
190 xml::sax::InputSource aParserInput;
191 aParserInput.aInputStream.set( xPipe, UNO_QUERY );
192 aParserInput.sSystemId = aMedium.GetName();
193
194
195 if( xSource.is() )
196 {
197 uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
198 xSourceControl->start();
199 }
200
201*/
202
203 // uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) );
204 rtl::Reference< SvxXMLXTextImportComponent > xImport( new SvxXMLXTextImportComponent( xContext, pUnoText ) );
205
206 xml::sax::InputSource aParserInput;
207 aParserInput.aInputStream = xInputStream;
208// aParserInput.sSystemId = aMedium.GetName();
209 xImport->parseStream( aParserInput );
210 }
211 while(false);
212
213 //remove the extra para breaks
214 EditDoc& pDoc = rEditEngine.GetEditDoc();
215 rEditEngine.ParaAttribsToCharAttribs( pDoc.GetObject( rSel.nEndPara ) );
216 rEditEngine.ConnectParagraphs( pDoc.GetObject( rSel.nEndPara ),
217 pDoc.GetObject( rSel.nEndPara + 1 ), true );
218 rEditEngine.ParaAttribsToCharAttribs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara - 2 ) );
219 rEditEngine.ConnectParagraphs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara - 2 ),
220 pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara -1 ), true );
221
222 // The final join is to be returned.
223 aPaM = rEditEngine.ConnectParagraphs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara - 2 ),
224 pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara -1 ), true );
225 }
226 catch( const uno::Exception& )
227 {
228 }
229
230 return aPaM;
231}
232
233/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const ContentNode * GetObject(sal_Int32 nPos) const
Definition: editdoc.cxx:2091
sal_Int32 Count() const
Definition: editdoc.cxx:2143
EditSelection CreateSelection(const ESelection &rSel)
Definition: editeng.cxx:945
static SfxItemPool & GetGlobalItemPool()
Definition: editeng.cxx:2653
EditPaM InsertParaBreak(const EditSelection &rEditSelection)
Definition: editeng.cxx:2865
void ParaAttribsToCharAttribs(ContentNode *pNode)
Definition: editeng.cxx:819
EditPaM ConnectParagraphs(ContentNode *pLeft, ContentNode *pRight, bool bBackward)
Definition: editeng.cxx:829
EditDoc & GetEditDoc()
Definition: editeng.cxx:905
EditPaM & Max()
Definition: editdoc.hxx:706
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
css::uno::Type const & get()
constexpr TypedWhichId< SfxBoolItem > EE_PARA_BULLETSTATE(EE_PARA_START+9)
constexpr TypedWhichId< SfxInt16Item > EE_PARA_OUTLLEVEL(EE_PARA_START+11)
constexpr TypedWhichId< SvxNumBulletItem > EE_PARA_NUMBULLET(EE_PARA_START+5)
Reference< text::XText > mxText
bool HasRange() const
Definition: editdata.hxx:151
sal_Int32 nStartPara
Definition: editdata.hxx:113
sal_Int32 nEndPara
Definition: editdata.hxx:115
constexpr OUStringLiteral UNO_NAME_NUMBERING
Definition: unoprnms.hxx:275
constexpr OUStringLiteral UNO_NAME_NUMBERING_LEVEL
Definition: unoprnms.hxx:277
constexpr OUStringLiteral UNO_NAME_NUMBERING_RULES
Definition: unoprnms.hxx:276
#define SVX_UNOEDIT_CHAR_PROPERTIES
Definition: unotext.hxx:79
#define SVX_UNOEDIT_PARA_PROPERTIES
Definition: unotext.hxx:142
#define SVX_UNOEDIT_FONT_PROPERTIES
Definition: unotext.hxx:139
#define XML_ELEMENT(prefix, name)
EditPaM SvxReadXML(EditEngine &rEditEngine, SvStream &rStream, const ESelection &rSel)
this function imports xml from the stream into the selected of an edit engine
Definition: xmltxtimp.cxx:132