1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/processfactory.hxx>
28 #include <unotools/streamwrap.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <sot/storage.hxx>
31 #include <svl/itemprop.hxx>
32 #include <xmloff/xmlimp.hxx>
33 #include <xmloff/xmlmetae.hxx>
34 #include <xmloff/xmlictxt.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/xmlnmspe.hxx>
37 #include <xmloff/xmlstyle.hxx>
38 #include "editsource.hxx"
39 #include <editeng/editeng.hxx>
40 #include <editeng/unotext.hxx>
41 #include <editeng/unoprnms.hxx>
42 #include <editeng/unoipset.hxx>
44 using namespace com::sun::star
;
45 using namespace com::sun::star::document
;
46 using namespace com::sun::star::uno
;
47 using namespace com::sun::star::lang
;
48 using namespace com::sun::star::xml::sax
;
49 using namespace com::sun::star::text
;
50 using namespace ::rtl
;
52 using namespace xmloff::token
;
55 ///////////////////////////////////////////////////////////////////////
57 class SvxXMLTextImportContext
: public SvXMLImportContext
60 SvxXMLTextImportContext( SvXMLImport
& rImport
, sal_uInt16 nPrfx
, const OUString
& rLName
, const uno::Reference
< XAttributeList
>& xAttrList
, const uno::Reference
< XText
>& xText
);
61 virtual ~SvxXMLTextImportContext();
63 virtual SvXMLImportContext
*CreateChildContext( sal_uInt16 nPrefix
, const OUString
& rLocalName
, const uno::Reference
< XAttributeList
>& xAttrList
);
65 // SvxXMLXTableImport& getImport() const { return *(SvxXMLXTableImport*)&GetImport(); }
68 const uno::Reference
< XText
> mxText
;
71 ///////////////////////////////////////////////////////////////////////
73 SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport
& rImport
, sal_uInt16 nPrfx
, const OUString
& rLName
, const uno::Reference
< XAttributeList
>&, const uno::Reference
< XText
>& xText
)
74 : SvXMLImportContext( rImport
, nPrfx
, rLName
), mxText( xText
)
78 SvxXMLTextImportContext::~SvxXMLTextImportContext()
82 SvXMLImportContext
*SvxXMLTextImportContext::CreateChildContext( sal_uInt16 nPrefix
, const OUString
& rLocalName
, const uno::Reference
< XAttributeList
>& xAttrList
)
84 SvXMLImportContext
* pContext
= NULL
;
85 if(XML_NAMESPACE_OFFICE
== nPrefix
&& IsXMLToken( rLocalName
, XML_BODY
) )
87 pContext
= new SvxXMLTextImportContext( GetImport(), nPrefix
, rLocalName
, xAttrList
, mxText
);
89 else if( XML_NAMESPACE_OFFICE
== nPrefix
&& IsXMLToken( rLocalName
, XML_AUTOMATIC_STYLES
) )
91 pContext
= new SvXMLStylesContext( GetImport(), nPrefix
, rLocalName
, xAttrList
);
92 GetImport().GetTextImport()->SetAutoStyles( (SvXMLStylesContext
*)pContext
);
97 pContext
= GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix
, rLocalName
, xAttrList
);
100 if( NULL
== pContext
)
101 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
106 ///////////////////////////////////////////////////////////////////////
108 class SvxXMLXTextImportComponent
: public SvXMLImport
111 SvxXMLXTextImportComponent(
112 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> xContext
,
113 const uno::Reference
< XText
> & xText
);
115 virtual ~SvxXMLXTextImportComponent() throw ();
117 static sal_Bool
load( const OUString
& rUrl
, const com::sun::star::uno::Reference
< com::sun::star::container::XNameContainer
>& xTable
) throw();
119 virtual SvXMLImportContext
*CreateChildContext( sal_uInt16 nPrefix
, const OUString
& rLocalName
, const uno::Reference
< XAttributeList
>& xAttrList
);
122 const uno::Reference
< XText
> mxText
;
125 // --------------------------------------------------------------------
127 SvxXMLXTextImportComponent::SvxXMLXTextImportComponent(
128 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> xContext
,
129 const uno::Reference
< XText
> & xText
)
130 : SvXMLImport(xContext
),
133 GetTextImport()->SetCursor( mxText
->createTextCursor() );
136 SvxXMLXTextImportComponent::~SvxXMLXTextImportComponent() throw ()
140 void SvxReadXML( EditEngine
& rEditEngine
, SvStream
& rStream
, const ESelection
& rSel
)
142 SvxEditEngineSource
aEditSource( &rEditEngine
);
144 static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap
[] =
146 SVX_UNOEDIT_CHAR_PROPERTIES
,
147 SVX_UNOEDIT_FONT_PROPERTIES
,
148 // SVX_UNOEDIT_OUTLINER_PROPERTIES,
149 SVX_UNOEDIT_PARA_PROPERTIES
,
152 static SvxItemPropertySet
aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap
, EditEngine::GetGlobalItemPool() );
154 uno::Reference
<text::XText
> xParent
;
155 SvxUnoText
* pUnoText
= new SvxUnoText( &aEditSource
, &aSvxXMLTextImportComponentPropertySet
, xParent
);
156 pUnoText
->SetSelection( rSel
);
157 uno::Reference
<text::XText
> xText( pUnoText
);
163 uno::Reference
<uno::XComponentContext
> xContext( ::comphelper::getProcessComponentContext() );
165 uno::Reference
< xml::sax::XParser
> xParser
= xml::sax::Parser::create( xContext
);
167 uno::Reference
<io::XInputStream
> xInputStream
= new utl::OInputStreamWrapper( rStream
);
170 const OUString aURL( "file:///e:/test.xml" );
171 SfxMedium aMedium( aURL, STREAM_READ | STREAM_NOCREATE, sal_True );
173 uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
175 aMedium.GetInStream()->Seek( 0 );
176 uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
180 OSL_FAIL( "got no data source from medium" );
184 uno::Reference< XInterface > xPipe( Pipe::create(comphelper::getComponentContext(xServiceFactory)), UNO_QUERY );
186 // connect pipe's output stream to the data source
187 xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) );
189 xml::sax::InputSource aParserInput;
190 aParserInput.aInputStream = uno::Reference< io::XInputStream >::query( xPipe );
191 aParserInput.sSystemId = aMedium.GetName();
196 uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
197 xSourceControl->start();
202 // uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) );
203 uno::Reference
< XDocumentHandler
> xHandler( new SvxXMLXTextImportComponent( xContext
, xText
) );
205 xParser
->setDocumentHandler( xHandler
);
207 xml::sax::InputSource aParserInput
;
208 aParserInput
.aInputStream
= xInputStream
;
209 // aParserInput.sSystemId = aMedium.GetName();
210 xParser
->parseStream( aParserInput
);
214 catch( const uno::Exception
& )
219 SvXMLImportContext
*SvxXMLXTextImportComponent::CreateChildContext( sal_uInt16 nPrefix
, const OUString
& rLocalName
, const uno::Reference
< XAttributeList
>& xAttrList
)
221 SvXMLImportContext
* pContext
;
222 if(XML_NAMESPACE_OFFICE
== nPrefix
&& ( IsXMLToken( rLocalName
, XML_DOCUMENT
) || IsXMLToken( rLocalName
, XML_DOCUMENT_CONTENT
) ) )
224 pContext
= new SvxXMLTextImportContext(*this, nPrefix
, rLocalName
, xAttrList
, mxText
);
228 pContext
= SvXMLImport::CreateContext(nPrefix
, rLocalName
, xAttrList
);
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */