bump product version to 6.3.0.0.beta1
[LibreOffice.git] / editeng / source / xml / xmltxtimp.cxx
blob51994a3230805d50ab548d6cd8b4b744bd5c60cb
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 <com/sun/star/io/Pipe.hpp>
21 #include <com/sun/star/io/XActiveDataControl.hpp>
22 #include <com/sun/star/io/XActiveDataSource.hpp>
23 #include <com/sun/star/xml/sax/Parser.hpp>
24 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
25 #include <com/sun/star/io/XOutputStream.hpp>
26 #include <com/sun/star/text/XText.hpp>
27 #include <com/sun/star/frame/XModel.hpp>
28 #include <comphelper/processfactory.hxx>
29 #include <unotools/streamwrap.hxx>
30 #include <rtl/ustrbuf.hxx>
31 #include <sot/storage.hxx>
32 #include <svl/itemprop.hxx>
33 #include <xmloff/xmlimp.hxx>
34 #include <xmloff/xmlmetae.hxx>
35 #include <xmloff/xmlictxt.hxx>
36 #include <xmloff/xmltoken.hxx>
37 #include <xmloff/xmlnmspe.hxx>
38 #include <xmloff/xmlstyle.hxx>
39 #include "editsource.hxx"
40 #include <editxml.hxx>
41 #include <editdoc.hxx>
42 #include <editeng/editeng.hxx>
43 #include <editeng/unotext.hxx>
44 #include <editeng/unoprnms.hxx>
45 #include <editeng/unoipset.hxx>
46 #include <cassert>
47 #include <unomodel.hxx>
49 using namespace com::sun::star;
50 using namespace com::sun::star::document;
51 using namespace com::sun::star::uno;
52 using namespace com::sun::star::lang;
53 using namespace com::sun::star::xml::sax;
54 using namespace com::sun::star::text;
55 using namespace cppu;
56 using namespace xmloff::token;
59 class SvxXMLTextImportContext : public SvXMLImportContext
61 public:
62 SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XText >& xText );
64 virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList ) override;
66 private:
67 const uno::Reference< XText > mxText;
71 SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XText >& xText )
72 : SvXMLImportContext( rImport, nPrfx, rLName ), mxText( xText )
76 SvXMLImportContextRef SvxXMLTextImportContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
78 SvXMLImportContext* pContext = nullptr;
79 if(XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_BODY ) )
81 pContext = new SvxXMLTextImportContext( GetImport(), nPrefix, rLocalName, mxText );
83 else if( XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_AUTOMATIC_STYLES ) )
85 pContext = new SvXMLStylesContext( GetImport(), nPrefix, rLocalName, xAttrList );
86 GetImport().GetTextImport()->SetAutoStyles( static_cast<SvXMLStylesContext*>(pContext) );
89 else
91 pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix, rLocalName, xAttrList );
94 if( nullptr == pContext )
95 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
97 return pContext;
101 class SvxXMLXTextImportComponent : public SvXMLImport
103 public:
104 SvxXMLXTextImportComponent(
105 const css::uno::Reference< css::uno::XComponentContext >& rContext,
106 const uno::Reference< XText > & rText );
108 virtual SvXMLImportContext *CreateDocumentContext( sal_uInt16 nPrefix,
109 const OUString& rLocalName, const uno::Reference< xml::sax::XAttributeList >& xAttrList ) override;
111 private:
112 const uno::Reference< XText > mxText;
115 SvXMLImportContext *SvxXMLXTextImportComponent::CreateDocumentContext(
116 sal_uInt16 const nPrefix, const OUString& rLocalName,
117 const uno::Reference< xml::sax::XAttributeList >& /*xAttrList*/)
119 SvXMLImportContext* pContext = nullptr;
121 if(XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_DOCUMENT_CONTENT ) )
123 pContext = new SvxXMLTextImportContext( *this, nPrefix, rLocalName, mxText );
126 if( nullptr == pContext )
127 pContext = new SvXMLImportContext( *this, nPrefix, rLocalName );
129 return pContext;
132 SvxXMLXTextImportComponent::SvxXMLXTextImportComponent(
133 const css::uno::Reference< css::uno::XComponentContext >& xContext,
134 const uno::Reference< XText > & xText )
135 : SvXMLImport(xContext, ""),
136 mxText( xText )
138 GetTextImport()->SetCursor( mxText->createTextCursor() );
139 SvXMLImport::setTargetDocument(new SvxSimpleUnoModel);
142 EditPaM SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
144 SvxEditEngineSource aEditSource( &rEditEngine );
146 static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap[] =
148 SVX_UNOEDIT_CHAR_PROPERTIES,
149 SVX_UNOEDIT_FONT_PROPERTIES,
150 // SVX_UNOEDIT_OUTLINER_PROPERTIES,
151 SVX_UNOEDIT_PARA_PROPERTIES,
152 { OUString(), 0, css::uno::Type(), 0, 0 }
154 static SvxItemPropertySet aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
156 assert(!rSel.HasRange());
157 //get the initial para count before paste
158 sal_uInt32 initialParaCount = rEditEngine.GetEditDoc().Count();
159 //insert para breaks before inserting the copied text
160 rEditEngine.InsertParaBreak( rEditEngine.CreateSelection( rSel ).Max() );
161 rEditEngine.InsertParaBreak( rEditEngine.CreateSelection( rSel ).Max() );
163 // Init return PaM.
164 EditPaM aPaM( rEditEngine.CreateSelection( rSel ).Max());
166 ESelection aSel(rSel.nStartPara+1, 0, rSel.nEndPara+1, 0);
167 uno::Reference<text::XText > xParent;
168 SvxUnoText* pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextImportComponentPropertySet, xParent );
169 pUnoText->SetSelection( aSel );
170 uno::Reference<text::XText > xText( pUnoText );
176 uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
178 uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
180 uno::Reference<io::XInputStream> xInputStream = new utl::OInputStreamWrapper( rStream );
182 /* testcode
183 const OUString aURL( "file:///e:/test.xml" );
184 SfxMedium aMedium( aURL, StreamMode::READ | STREAM_NOCREATE, sal_True );
185 uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
187 aMedium.GetInStream()->Seek( 0 );
188 uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
190 if( !xSource.is() )
192 OSL_FAIL( "got no data source from medium" );
193 break;
196 uno::Reference< XInterface > xPipe( Pipe::create(comphelper::getComponentContext(xServiceFactory)), UNO_QUERY );
198 // connect pipe's output stream to the data source
199 xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) );
201 xml::sax::InputSource aParserInput;
202 aParserInput.aInputStream.set( xPipe, UNO_QUERY );
203 aParserInput.sSystemId = aMedium.GetName();
206 if( xSource.is() )
208 uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
209 xSourceControl->start();
214 // uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) );
215 uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xContext, xText ) );
217 xParser->setDocumentHandler( xHandler );
219 xml::sax::InputSource aParserInput;
220 aParserInput.aInputStream = xInputStream;
221 // aParserInput.sSystemId = aMedium.GetName();
222 xParser->parseStream( aParserInput );
224 while(false);
226 //remove the extra para breaks
227 EditDoc& pDoc = rEditEngine.GetEditDoc();
228 rEditEngine.ParaAttribsToCharAttribs( pDoc.GetObject( rSel.nEndPara ) );
229 rEditEngine.ConnectParagraphs( pDoc.GetObject( rSel.nEndPara ),
230 pDoc.GetObject( rSel.nEndPara + 1 ), true );
231 rEditEngine.ParaAttribsToCharAttribs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara - 2 ) );
232 rEditEngine.ConnectParagraphs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara - 2 ),
233 pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara -1 ), true );
235 // The final join is to be returned.
236 aPaM = rEditEngine.ConnectParagraphs( pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara - 2 ),
237 pDoc.GetObject( pDoc.Count() - initialParaCount + aSel.nEndPara -1 ), true );
239 catch( const uno::Exception& )
243 return aPaM;
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */