Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/editeng/source/xml/xmltxtimp.cxx
Line
Count
Source
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>
21
#include <comphelper/processfactory.hxx>
22
#include <unotools/streamwrap.hxx>
23
#include <svl/itemprop.hxx>
24
#include <utility>
25
#include <xmloff/xmlimp.hxx>
26
#include <xmloff/xmlictxt.hxx>
27
#include <xmloff/xmltoken.hxx>
28
#include <xmloff/xmlnamespace.hxx>
29
#include <xmloff/xmlstyle.hxx>
30
#include "editsource.hxx"
31
#include <editxml.hxx>
32
#include <editdoc.hxx>
33
#include <EditSelection.hxx>
34
#include <editeng/editeng.hxx>
35
#include <editeng/unotext.hxx>
36
#include <editeng/unoprnms.hxx>
37
#include <editeng/unoipset.hxx>
38
#include <cassert>
39
#include <unomodel.hxx>
40
41
using namespace com::sun::star;
42
using namespace com::sun::star::uno;
43
using namespace com::sun::star::xml::sax;
44
using namespace com::sun::star::text;
45
using namespace cppu;
46
using namespace xmloff::token;
47
48
namespace {
49
50
class SvxXMLTextImportContext : public SvXMLImportContext
51
{
52
public:
53
    SvxXMLTextImportContext( SvXMLImport& rImport, uno::Reference< XText > xText );
54
55
    virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
56
            sal_Int32 nElement,
57
            const uno::Reference< xml::sax::XFastAttributeList >& xAttrList) override;
58
59
private:
60
    const uno::Reference< XText > mxText;
61
};
62
63
}
64
65
SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, uno::Reference< XText > xText )
66
0
: SvXMLImportContext( rImport ), mxText(std::move( xText ))
67
0
{
68
0
}
69
70
css::uno::Reference< css::xml::sax::XFastContextHandler > SvxXMLTextImportContext::createFastChildContext(
71
        sal_Int32 nElement,
72
        const uno::Reference< xml::sax::XFastAttributeList >& xAttrList)
73
0
{
74
0
    SvXMLImportContext* pContext = nullptr;
75
0
    if(nElement == XML_ELEMENT(OFFICE, XML_BODY ))
76
0
    {
77
0
        pContext = new SvxXMLTextImportContext( GetImport(), mxText );
78
0
    }
79
0
    else if( nElement == XML_ELEMENT(OFFICE, XML_AUTOMATIC_STYLES ) )
80
0
    {
81
0
        pContext = new SvXMLStylesContext( GetImport() );
82
0
        GetImport().GetTextImport()->SetAutoStyles( static_cast<SvXMLStylesContext*>(pContext) );
83
0
    }
84
0
    else
85
0
        pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nElement, xAttrList );
86
0
    return pContext;
87
0
}
88
89
namespace {
90
91
class SvxXMLXTextImportComponent : public SvXMLImport
92
{
93
public:
94
    SvxXMLXTextImportComponent(
95
        const css::uno::Reference< css::uno::XComponentContext >& rContext,
96
        uno::Reference< XText > xText );
97
98
    virtual SvXMLImportContext* CreateFastContext(sal_Int32 nElement,
99
        const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList >& xAttrList ) override;
100
101
private:
102
    const uno::Reference< XText > mxText;
103
};
104
105
}
106
107
SvXMLImportContext *SvxXMLXTextImportComponent::CreateFastContext(
108
        sal_Int32 nElement,
109
        const uno::Reference< xml::sax::XFastAttributeList >& /*xAttrList*/)
110
0
{
111
0
    SvXMLImportContext* pContext = nullptr;
112
113
0
    if(nElement == XML_ELEMENT(OFFICE, XML_DOCUMENT_CONTENT ) )
114
0
    {
115
0
        pContext = new SvxXMLTextImportContext( *this, mxText );
116
0
    }
117
118
0
    return pContext;
119
0
}
120
121
SvxXMLXTextImportComponent::SvxXMLXTextImportComponent(
122
    const css::uno::Reference< css::uno::XComponentContext >& xContext,
123
    uno::Reference< XText > xText )
124
0
:   SvXMLImport(xContext, u""_ustr),
125
0
    mxText(std::move( xText ))
126
0
{
127
0
    GetTextImport()->SetCursor( mxText->createTextCursor() );
128
0
    SvXMLImport::setTargetDocument(new SvxSimpleUnoModel);
129
0
}
130
131
EditPaM SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
132
0
{
133
0
    SvxEditEngineSource aEditSource( &rEditEngine );
134
135
0
    static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap[] =
136
0
    {
137
0
        SVX_UNOEDIT_CHAR_PROPERTIES,
138
0
        SVX_UNOEDIT_FONT_PROPERTIES,
139
//      bullets & numbering props, tdf#128046
140
0
        { UNO_NAME_NUMBERING_RULES,        EE_PARA_NUMBULLET,  cppu::UnoType<css::container::XIndexReplace>::get(), 0, 0 },
141
0
        { UNO_NAME_NUMBERING,              EE_PARA_BULLETSTATE,cppu::UnoType<bool>::get(), 0, 0 },
142
0
        { UNO_NAME_NUMBERING_LEVEL,        EE_PARA_OUTLLEVEL,  ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
143
0
        SVX_UNOEDIT_PARA_PROPERTIES,
144
0
    };
145
0
    static SvxItemPropertySet aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
146
147
0
    assert(!rSel.HasRange());
148
    //get the initial para count before paste
149
0
    sal_uInt32 initialParaCount = rEditEngine.GetEditDoc().Count();
150
    //insert para breaks before inserting the copied text
151
0
    rEditEngine.InsertParaBreak( EditSelection(rEditEngine.CreateSelection( rSel ).Max()) );
152
0
    rEditEngine.InsertParaBreak( EditSelection(rEditEngine.CreateSelection( rSel ).Max()) );
153
154
    // Init return PaM.
155
0
    EditPaM aPaM( rEditEngine.CreateSelection( rSel ).Max());
156
157
0
    ESelection aSel(rSel.start.nPara + 1, 0, rSel.end.nPara + 1, 0);
158
0
    uno::Reference<text::XText > xParent;
159
0
    rtl::Reference<SvxUnoText> pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextImportComponentPropertySet, xParent );
160
0
    pUnoText->SetSelection( aSel );
161
162
0
    try
163
0
    {
164
0
        do
165
0
        {
166
0
            const uno::Reference<uno::XComponentContext>& xContext( ::comphelper::getProcessComponentContext() );
167
168
0
            xml::sax::InputSource aParserInput;
169
0
            aParserInput.aInputStream = 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
0
            rtl::Reference< SvxXMLXTextImportComponent > xImport( new SvxXMLXTextImportComponent( xContext, pUnoText ) );
205
206
//          aParserInput.sSystemId = aMedium.GetName();
207
0
            xImport->parseStream( aParserInput );
208
0
        }
209
0
        while(false);
210
211
        //remove the extra para breaks
212
0
        EditDoc& pDoc = rEditEngine.GetEditDoc();
213
0
        rEditEngine.ParaAttribsToCharAttribs(pDoc.GetObject(rSel.end.nPara));
214
0
        rEditEngine.ConnectParagraphs(pDoc.GetObject(rSel.end.nPara),
215
0
                                      pDoc.GetObject(rSel.end.nPara + 1), true);
216
0
        rEditEngine.ParaAttribsToCharAttribs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.end.nPara - 2 ) );
217
0
        rEditEngine.ConnectParagraphs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.end.nPara - 2 ),
218
0
            pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.end.nPara -1 ), true );
219
220
        // The final join is to be returned.
221
0
        aPaM = rEditEngine.ConnectParagraphs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.end.nPara - 2 ),
222
0
            pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.end.nPara -1 ), true );
223
0
    }
224
0
    catch( const uno::Exception& )
225
0
    {
226
0
    }
227
228
0
    return aPaM;
229
0
}
230
231
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */