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