/************************************************************************* * * OpenOffice.org - a multi-platform office productivity suite * * $RCSfile: xmltxtimp.cxx,v $ * * $Revision: 1.10 $ * * last change: $Author: hr $ $Date: 2007-06-27 19:32:25 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. * * * GNU Lesser General Public License Version 2.1 * ============================================= * Copyright 2005 by Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, CA 94303, USA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * ************************************************************************/ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_svx.hxx" #ifndef _TOOLS_DEBUG_HXX #include #endif #ifndef _COM_SUN_STAR_IO_XACTIVEDATACONTROL_HPP_ #include #endif #ifndef _COM_SUN_STAR_IO_XACTIVEDATASOURCE_HPP_ #include #endif #ifndef _COM_SUN_STAR_XML_SAX_XPARSER_HPP_ #include #endif #ifndef _COM_SUN_STAR_XML_SAX_XDOCUMENTHANDLER_HPP_ #include #endif #ifndef _COM_SUN_STAR_IO_XOUTPUTSTREAM_HPP_ #include #endif #ifndef _COM_SUN_STAR_TEXT_XTEXT_HPP_ #include #endif #ifndef _COMPHELPER_PROCESSFACTORY_HXX_ #include #endif #ifndef _UTL_STREAM_WRAPPER_HXX_ #include #endif #ifndef _RTL_USTRBUF_HXX_ #include #endif #ifndef _SVSTOR_HXX #include #endif #ifndef _SFX_ITEMPROP_HXX #include #endif #ifndef _SFXDOCFILE_HXX #include #endif #ifndef _XMLOFF_XMLIMP_HXX #include #endif #ifndef _XMLOFF_XMLMETAE_HXX #include "xmloff/xmlmetae.hxx" #endif #ifndef _XMLOFF_XMLICTXT_HXX #include #endif #ifndef _XMLOFF_XMLTOKEN_HXX #include #endif #ifndef _XMLOFF_XMLNMSPE_HXX #include #endif #ifndef _XMLOFF_XMLSTYLE_HXX #include #endif #ifndef _SVX_EDITSOURCE_HXX #include "editsource.hxx" #endif #ifndef _SVX_UNOTEXT_HXX #include #endif using namespace com::sun::star; using namespace com::sun::star::document; using namespace com::sun::star::uno; using namespace com::sun::star::lang; using namespace com::sun::star::xml::sax; using namespace com::sun::star::text; using namespace ::rtl; using namespace cppu; using namespace xmloff::token; /////////////////////////////////////////////////////////////////////// class SvxXMLTextImportContext : public SvXMLImportContext { public: SvxXMLTextImportContext( SvXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >& xAttrList, const uno::Reference< XText >& xText ); virtual ~SvxXMLTextImportContext(); virtual SvXMLImportContext *CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList ); // SvxXMLXTableImport& getImport() const { return *(SvxXMLXTableImport*)&GetImport(); } private: const uno::Reference< XText > mxText; }; /////////////////////////////////////////////////////////////////////// SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >&, const uno::Reference< XText >& xText ) : SvXMLImportContext( rImport, nPrfx, rLName ), mxText( xText ) { } SvxXMLTextImportContext::~SvxXMLTextImportContext() { } SvXMLImportContext *SvxXMLTextImportContext::CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList ) { SvXMLImportContext* pContext = NULL; if(XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_BODY ) ) { pContext = new SvxXMLTextImportContext( GetImport(), nPrefix, rLocalName, xAttrList, mxText ); } else if( XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_AUTOMATIC_STYLES ) ) { pContext = new SvXMLStylesContext( GetImport(), nPrefix, rLocalName, xAttrList ); GetImport().GetTextImport()->SetAutoStyles( (SvXMLStylesContext*)pContext ); } else { pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix, rLocalName, xAttrList ); } if( NULL == pContext ) pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); return pContext; } /////////////////////////////////////////////////////////////////////// class SvxXMLXTextImportComponent : public SvXMLImport { public: // #110680# SvxXMLXTextImportComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory, const uno::Reference< XText > & xText ); virtual ~SvxXMLXTextImportComponent() throw (); static sal_Bool load( const rtl::OUString& rUrl, const com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >& xTable ) throw(); protected: virtual SvXMLImportContext *CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList ); private: const uno::Reference< XText > mxText; }; // -------------------------------------------------------------------- // #110680# SvxXMLXTextImportComponent::SvxXMLXTextImportComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory, const uno::Reference< XText > & xText ) : SvXMLImport(xServiceFactory), mxText( xText ) { GetTextImport()->SetCursor( mxText->createTextCursor() ); } SvxXMLXTextImportComponent::~SvxXMLXTextImportComponent() throw () { } void SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel ) { SvxEditEngineSource aEditSource( &rEditEngine ); static const SfxItemPropertyMap SvxXMLTextImportComponentPropertyMap[] = { SVX_UNOEDIT_CHAR_PROPERTIES, SVX_UNOEDIT_FONT_PROPERTIES, // SVX_UNOEDIT_OUTLINER_PROPERTIES, SVX_UNOEDIT_PARA_PROPERTIES, {0,0,0,0,0,0} }; uno::Reference xParent; SvxUnoText* pUnoText = new SvxUnoText( &aEditSource, SvxXMLTextImportComponentPropertyMap, xParent ); pUnoText->SetSelection( rSel ); uno::Reference xText( pUnoText ); try { do { uno::Reference xServiceFactory( ::comphelper::getProcessServiceFactory() ); if( !xServiceFactory.is() ) { DBG_ERROR( "SvxXMLXTableImport::load: got no service manager" ); break; } uno::Reference< xml::sax::XParser > xParser( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY ); if( !xParser.is() ) { DBG_ERROR( "com.sun.star.xml.sax.Parser service missing" ); break; } uno::Reference xInputStream = new utl::OInputStreamWrapper( rStream ); /* testcode const OUString aURL( RTL_CONSTASCII_USTRINGPARAM( "file:///e:/test.xml" ) ); SfxMedium aMedium( aURL, STREAM_READ | STREAM_NOCREATE, TRUE ); aMedium.IsRemote(); uno::Reference xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) ); aMedium.GetInStream()->Seek( 0 ); uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() ); if( !xSource.is() ) { DBG_ERROR( "got no data source from medium" ); break; } uno::Reference< XInterface > xPipe( xServiceFactory->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe") ) ) ); if( !xPipe.is() ) { DBG_ERROR( "XMLReader::Read: com.sun.star.io.Pipe service missing" ); break; } // connect pipe's output stream to the data source xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) ); xml::sax::InputSource aParserInput; aParserInput.aInputStream = uno::Reference< io::XInputStream >::query( xPipe ); aParserInput.sSystemId = aMedium.GetName(); if( xSource.is() ) { uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY ); xSourceControl->start(); } */ // #110680# // uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) ); uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xServiceFactory, xText ) ); xParser->setDocumentHandler( xHandler ); xml::sax::InputSource aParserInput; aParserInput.aInputStream = xInputStream; // aParserInput.sSystemId = aMedium.GetName(); xParser->parseStream( aParserInput ); } while(0); } catch( uno::Exception& ) { } } SvXMLImportContext *SvxXMLXTextImportComponent::CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList ) { SvXMLImportContext* pContext; if(XML_NAMESPACE_OFFICE == nPrefix && ( IsXMLToken( rLocalName, XML_DOCUMENT ) || IsXMLToken( rLocalName, XML_DOCUMENT_CONTENT ) ) ) { pContext = new SvxXMLTextImportContext(*this, nPrefix, rLocalName, xAttrList, mxText ); } else { pContext = SvXMLImport::CreateContext(nPrefix, rLocalName, xAttrList); } return pContext; }