Update ooo320-m1
[ooovba.git] / svx / source / xml / xmltxtimp.cxx
blob092540886a3b984eb76d238f6e176b09d2f1ad04
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmltxtimp.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <tools/debug.hxx>
34 #include <com/sun/star/io/XActiveDataControl.hpp>
35 #include <com/sun/star/io/XActiveDataSource.hpp>
36 #include <com/sun/star/xml/sax/XParser.hpp>
37 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
38 #include <com/sun/star/io/XOutputStream.hpp>
39 #include <com/sun/star/text/XText.hpp>
40 #include <comphelper/processfactory.hxx>
41 #include <unotools/streamwrap.hxx>
42 #include <rtl/ustrbuf.hxx>
44 #ifndef _SVSTOR_HXX
45 #include <sot/storage.hxx>
46 #endif
47 #include <svtools/itemprop.hxx>
48 #include <sfx2/docfile.hxx>
49 #include <xmloff/xmlimp.hxx>
50 #include "xmloff/xmlmetae.hxx"
51 #include <xmloff/xmlictxt.hxx>
52 #include <xmloff/xmltoken.hxx>
53 #include <xmloff/xmlnmspe.hxx>
54 #include <xmloff/xmlstyle.hxx>
55 #include "editsource.hxx"
56 #include <svx/unotext.hxx>
58 using namespace com::sun::star;
59 using namespace com::sun::star::document;
60 using namespace com::sun::star::uno;
61 using namespace com::sun::star::lang;
62 using namespace com::sun::star::xml::sax;
63 using namespace com::sun::star::text;
64 using namespace ::rtl;
65 using namespace cppu;
66 using namespace xmloff::token;
69 ///////////////////////////////////////////////////////////////////////
71 class SvxXMLTextImportContext : public SvXMLImportContext
73 public:
74 SvxXMLTextImportContext( SvXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >& xAttrList, const uno::Reference< XText >& xText );
75 virtual ~SvxXMLTextImportContext();
77 virtual SvXMLImportContext *CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
79 // SvxXMLXTableImport& getImport() const { return *(SvxXMLXTableImport*)&GetImport(); }
81 private:
82 const uno::Reference< XText > mxText;
85 ///////////////////////////////////////////////////////////////////////
87 SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >&, const uno::Reference< XText >& xText )
88 : SvXMLImportContext( rImport, nPrfx, rLName ), mxText( xText )
92 SvxXMLTextImportContext::~SvxXMLTextImportContext()
96 SvXMLImportContext *SvxXMLTextImportContext::CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
98 SvXMLImportContext* pContext = NULL;
99 if(XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_BODY ) )
101 pContext = new SvxXMLTextImportContext( GetImport(), nPrefix, rLocalName, xAttrList, mxText );
103 else if( XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_AUTOMATIC_STYLES ) )
105 pContext = new SvXMLStylesContext( GetImport(), nPrefix, rLocalName, xAttrList );
106 GetImport().GetTextImport()->SetAutoStyles( (SvXMLStylesContext*)pContext );
109 else
111 pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix, rLocalName, xAttrList );
114 if( NULL == pContext )
115 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
117 return pContext;
120 ///////////////////////////////////////////////////////////////////////
122 class SvxXMLXTextImportComponent : public SvXMLImport
124 public:
125 // #110680#
126 SvxXMLXTextImportComponent(
127 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
128 const uno::Reference< XText > & xText );
130 virtual ~SvxXMLXTextImportComponent() throw ();
132 static sal_Bool load( const rtl::OUString& rUrl, const com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >& xTable ) throw();
133 protected:
134 virtual SvXMLImportContext *CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
136 private:
137 const uno::Reference< XText > mxText;
140 // --------------------------------------------------------------------
142 // #110680#
143 SvxXMLXTextImportComponent::SvxXMLXTextImportComponent(
144 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
145 const uno::Reference< XText > & xText )
146 : SvXMLImport(xServiceFactory),
147 mxText( xText )
149 GetTextImport()->SetCursor( mxText->createTextCursor() );
152 SvxXMLXTextImportComponent::~SvxXMLXTextImportComponent() throw ()
156 void SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
158 SvxEditEngineSource aEditSource( &rEditEngine );
160 static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap[] =
162 SVX_UNOEDIT_CHAR_PROPERTIES,
163 SVX_UNOEDIT_FONT_PROPERTIES,
164 // SVX_UNOEDIT_OUTLINER_PROPERTIES,
165 SVX_UNOEDIT_PARA_PROPERTIES,
166 {0,0,0,0,0,0}
168 static SvxItemPropertySet aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap );
170 uno::Reference<text::XText > xParent;
171 SvxUnoText* pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextImportComponentPropertySet, xParent );
172 pUnoText->SetSelection( rSel );
173 uno::Reference<text::XText > xText( pUnoText );
179 uno::Reference<lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
180 if( !xServiceFactory.is() )
182 DBG_ERROR( "SvxXMLXTableImport::load: got no service manager" );
183 break;
186 uno::Reference< xml::sax::XParser > xParser( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY );
187 if( !xParser.is() )
189 DBG_ERROR( "com.sun.star.xml.sax.Parser service missing" );
190 break;
193 uno::Reference<io::XInputStream> xInputStream = new utl::OInputStreamWrapper( rStream );
195 /* testcode
196 const OUString aURL( RTL_CONSTASCII_USTRINGPARAM( "file:///e:/test.xml" ) );
197 SfxMedium aMedium( aURL, STREAM_READ | STREAM_NOCREATE, TRUE );
198 aMedium.IsRemote();
199 uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
201 aMedium.GetInStream()->Seek( 0 );
202 uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
204 if( !xSource.is() )
206 DBG_ERROR( "got no data source from medium" );
207 break;
210 uno::Reference< XInterface > xPipe( xServiceFactory->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe") ) ) );
211 if( !xPipe.is() )
213 DBG_ERROR( "XMLReader::Read: com.sun.star.io.Pipe service missing" );
214 break;
217 // connect pipe's output stream to the data source
218 xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) );
220 xml::sax::InputSource aParserInput;
221 aParserInput.aInputStream = uno::Reference< io::XInputStream >::query( xPipe );
222 aParserInput.sSystemId = aMedium.GetName();
225 if( xSource.is() )
227 uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
228 xSourceControl->start();
233 // #110680#
234 // uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) );
235 uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xServiceFactory, xText ) );
237 xParser->setDocumentHandler( xHandler );
239 xml::sax::InputSource aParserInput;
240 aParserInput.aInputStream = xInputStream;
241 // aParserInput.sSystemId = aMedium.GetName();
242 xParser->parseStream( aParserInput );
244 while(0);
246 catch( uno::Exception& )
251 SvXMLImportContext *SvxXMLXTextImportComponent::CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
253 SvXMLImportContext* pContext;
254 if(XML_NAMESPACE_OFFICE == nPrefix && ( IsXMLToken( rLocalName, XML_DOCUMENT ) || IsXMLToken( rLocalName, XML_DOCUMENT_CONTENT ) ) )
256 pContext = new SvxXMLTextImportContext(*this, nPrefix, rLocalName, xAttrList, mxText );
258 else
260 pContext = SvXMLImport::CreateContext(nPrefix, rLocalName, xAttrList);
262 return pContext;