xref: /aoo42x/main/editeng/source/xml/xmltxtimp.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_editeng.hxx"
30*cdf0e10cSrcweir #include <tools/debug.hxx>
31*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataControl.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/text/XText.hpp>
37*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
38*cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
39*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40*cdf0e10cSrcweir #include <sot/storage.hxx>
41*cdf0e10cSrcweir #include <svl/itemprop.hxx>
42*cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
43*cdf0e10cSrcweir #include <xmloff/xmlmetae.hxx>
44*cdf0e10cSrcweir #include <xmloff/xmlictxt.hxx>
45*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
46*cdf0e10cSrcweir #include <xmloff/xmlnmspe.hxx>
47*cdf0e10cSrcweir #include <xmloff/xmlstyle.hxx>
48*cdf0e10cSrcweir #include "editsource.hxx"
49*cdf0e10cSrcweir #include <editeng/editeng.hxx>
50*cdf0e10cSrcweir #include <editeng/unotext.hxx>
51*cdf0e10cSrcweir #include <editeng/unoprnms.hxx>
52*cdf0e10cSrcweir #include <editeng/unoipset.hxx>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir using namespace com::sun::star;
55*cdf0e10cSrcweir using namespace com::sun::star::document;
56*cdf0e10cSrcweir using namespace com::sun::star::uno;
57*cdf0e10cSrcweir using namespace com::sun::star::lang;
58*cdf0e10cSrcweir using namespace com::sun::star::xml::sax;
59*cdf0e10cSrcweir using namespace com::sun::star::text;
60*cdf0e10cSrcweir using namespace ::rtl;
61*cdf0e10cSrcweir using namespace cppu;
62*cdf0e10cSrcweir using namespace xmloff::token;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir class SvxXMLTextImportContext : public SvXMLImportContext
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir public:
70*cdf0e10cSrcweir 	SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >& xAttrList, const uno::Reference< XText >& xText );
71*cdf0e10cSrcweir 	virtual ~SvxXMLTextImportContext();
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir //	SvxXMLXTableImport& getImport() const { return *(SvxXMLXTableImport*)&GetImport(); }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir private:
78*cdf0e10cSrcweir 	const uno::Reference< XText > mxText;
79*cdf0e10cSrcweir };
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >&, const uno::Reference< XText >& xText )
84*cdf0e10cSrcweir : SvXMLImportContext( rImport, nPrfx, rLName ), mxText( xText )
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir SvxXMLTextImportContext::~SvxXMLTextImportContext()
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir SvXMLImportContext *SvxXMLTextImportContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir 	SvXMLImportContext* pContext = NULL;
95*cdf0e10cSrcweir 	if(XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_BODY ) )
96*cdf0e10cSrcweir 	{
97*cdf0e10cSrcweir 		pContext = new SvxXMLTextImportContext( GetImport(), nPrefix, rLocalName, xAttrList, mxText );
98*cdf0e10cSrcweir 	}
99*cdf0e10cSrcweir 	else if( XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_AUTOMATIC_STYLES ) )
100*cdf0e10cSrcweir 	{
101*cdf0e10cSrcweir 		pContext = new SvXMLStylesContext( GetImport(), nPrefix, rLocalName, xAttrList );
102*cdf0e10cSrcweir 		GetImport().GetTextImport()->SetAutoStyles( (SvXMLStylesContext*)pContext );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	}
105*cdf0e10cSrcweir 	else
106*cdf0e10cSrcweir 	{
107*cdf0e10cSrcweir 		pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix, rLocalName, xAttrList );
108*cdf0e10cSrcweir 	}
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	if( NULL == pContext )
111*cdf0e10cSrcweir 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 	return pContext;
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////////
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir class SvxXMLXTextImportComponent : public SvXMLImport
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir public:
121*cdf0e10cSrcweir 	// #110680#
122*cdf0e10cSrcweir 	SvxXMLXTextImportComponent(
123*cdf0e10cSrcweir 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
124*cdf0e10cSrcweir 		const uno::Reference< XText > & xText );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	virtual ~SvxXMLXTextImportComponent() throw ();
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	static sal_Bool load( const rtl::OUString& rUrl, const com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >& xTable ) throw();
129*cdf0e10cSrcweir protected:
130*cdf0e10cSrcweir 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir private:
133*cdf0e10cSrcweir 	const uno::Reference< XText > mxText;
134*cdf0e10cSrcweir };
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir // --------------------------------------------------------------------
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir // #110680#
139*cdf0e10cSrcweir SvxXMLXTextImportComponent::SvxXMLXTextImportComponent(
140*cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
141*cdf0e10cSrcweir 	const uno::Reference< XText > & xText )
142*cdf0e10cSrcweir :	SvXMLImport(xServiceFactory),
143*cdf0e10cSrcweir 	mxText( xText )
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	GetTextImport()->SetCursor( mxText->createTextCursor() );
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir SvxXMLXTextImportComponent::~SvxXMLXTextImportComponent() throw ()
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir void SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir 	SvxEditEngineSource aEditSource( &rEditEngine );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap[] =
157*cdf0e10cSrcweir 	{
158*cdf0e10cSrcweir 		SVX_UNOEDIT_CHAR_PROPERTIES,
159*cdf0e10cSrcweir 		SVX_UNOEDIT_FONT_PROPERTIES,
160*cdf0e10cSrcweir //		SVX_UNOEDIT_OUTLINER_PROPERTIES,
161*cdf0e10cSrcweir 		SVX_UNOEDIT_PARA_PROPERTIES,
162*cdf0e10cSrcweir 		{0,0,0,0,0,0}
163*cdf0e10cSrcweir 	};
164*cdf0e10cSrcweir 	static SvxItemPropertySet aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	uno::Reference<text::XText > xParent;
167*cdf0e10cSrcweir 	SvxUnoText* pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextImportComponentPropertySet, xParent );
168*cdf0e10cSrcweir 	pUnoText->SetSelection( rSel );
169*cdf0e10cSrcweir 	uno::Reference<text::XText > xText( pUnoText );
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 	try
172*cdf0e10cSrcweir 	{
173*cdf0e10cSrcweir 		do
174*cdf0e10cSrcweir 		{
175*cdf0e10cSrcweir 			uno::Reference<lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
176*cdf0e10cSrcweir 			if( !xServiceFactory.is() )
177*cdf0e10cSrcweir 			{
178*cdf0e10cSrcweir 				DBG_ERROR( "SvxXMLXTableImport::load: got no service manager" );
179*cdf0e10cSrcweir 				break;
180*cdf0e10cSrcweir 			}
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 			uno::Reference< xml::sax::XParser > xParser( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY );
183*cdf0e10cSrcweir 			if( !xParser.is() )
184*cdf0e10cSrcweir 			{
185*cdf0e10cSrcweir 				DBG_ERROR( "com.sun.star.xml.sax.Parser service missing" );
186*cdf0e10cSrcweir 				break;
187*cdf0e10cSrcweir 			}
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 			uno::Reference<io::XInputStream> xInputStream = new utl::OInputStreamWrapper( rStream );
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir /* testcode
192*cdf0e10cSrcweir 			const OUString aURL( RTL_CONSTASCII_USTRINGPARAM( "file:///e:/test.xml" ) );
193*cdf0e10cSrcweir 			SfxMedium aMedium( aURL, STREAM_READ | STREAM_NOCREATE, sal_True );
194*cdf0e10cSrcweir 			aMedium.IsRemote();
195*cdf0e10cSrcweir 			uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 			aMedium.GetInStream()->Seek( 0 );
198*cdf0e10cSrcweir 			uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 			if( !xSource.is() )
201*cdf0e10cSrcweir 			{
202*cdf0e10cSrcweir 				DBG_ERROR( "got no data source from medium" );
203*cdf0e10cSrcweir 				break;
204*cdf0e10cSrcweir 			}
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 			uno::Reference< XInterface > xPipe( xServiceFactory->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe") ) ) );
207*cdf0e10cSrcweir 			if( !xPipe.is() )
208*cdf0e10cSrcweir 			{
209*cdf0e10cSrcweir 				DBG_ERROR( "XMLReader::Read: com.sun.star.io.Pipe service missing" );
210*cdf0e10cSrcweir 				break;
211*cdf0e10cSrcweir 			}
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 			// connect pipe's output stream to the data source
214*cdf0e10cSrcweir 			xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 			xml::sax::InputSource aParserInput;
217*cdf0e10cSrcweir 			aParserInput.aInputStream =	uno::Reference< io::XInputStream >::query( xPipe );
218*cdf0e10cSrcweir 			aParserInput.sSystemId = aMedium.GetName();
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 			if( xSource.is() )
222*cdf0e10cSrcweir 			{
223*cdf0e10cSrcweir 				uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
224*cdf0e10cSrcweir 				xSourceControl->start();
225*cdf0e10cSrcweir 			}
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir */
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 			// #110680#
230*cdf0e10cSrcweir 			// uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) );
231*cdf0e10cSrcweir 			uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xServiceFactory, xText ) );
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 			xParser->setDocumentHandler( xHandler );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 			xml::sax::InputSource aParserInput;
236*cdf0e10cSrcweir 			aParserInput.aInputStream =	xInputStream;
237*cdf0e10cSrcweir //			aParserInput.sSystemId = aMedium.GetName();
238*cdf0e10cSrcweir 			xParser->parseStream( aParserInput );
239*cdf0e10cSrcweir 		}
240*cdf0e10cSrcweir 		while(0);
241*cdf0e10cSrcweir 	}
242*cdf0e10cSrcweir 	catch( uno::Exception& )
243*cdf0e10cSrcweir 	{
244*cdf0e10cSrcweir 	}
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir SvXMLImportContext *SvxXMLXTextImportComponent::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
248*cdf0e10cSrcweir {
249*cdf0e10cSrcweir 	SvXMLImportContext* pContext;
250*cdf0e10cSrcweir 	if(XML_NAMESPACE_OFFICE == nPrefix && ( IsXMLToken( rLocalName, XML_DOCUMENT ) || IsXMLToken( rLocalName, XML_DOCUMENT_CONTENT ) ) )
251*cdf0e10cSrcweir 	{
252*cdf0e10cSrcweir 		 pContext = new SvxXMLTextImportContext(*this, nPrefix, rLocalName, xAttrList, mxText );
253*cdf0e10cSrcweir 	}
254*cdf0e10cSrcweir 	else
255*cdf0e10cSrcweir 	{
256*cdf0e10cSrcweir 		pContext = SvXMLImport::CreateContext(nPrefix, rLocalName, xAttrList);
257*cdf0e10cSrcweir 	}
258*cdf0e10cSrcweir 	return pContext;
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261