1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmltxtimp.cxx,v $
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>
45 #include <sot/storage.hxx>
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
;
66 using namespace xmloff::token
;
69 ///////////////////////////////////////////////////////////////////////
71 class SvxXMLTextImportContext
: public SvXMLImportContext
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(); }
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
);
111 pContext
= GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix
, rLocalName
, xAttrList
);
114 if( NULL
== pContext
)
115 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
120 ///////////////////////////////////////////////////////////////////////
122 class SvxXMLXTextImportComponent
: public SvXMLImport
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();
134 virtual SvXMLImportContext
*CreateChildContext( USHORT nPrefix
, const OUString
& rLocalName
, const uno::Reference
< XAttributeList
>& xAttrList
);
137 const uno::Reference
< XText
> mxText
;
140 // --------------------------------------------------------------------
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
),
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
,
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" );
186 uno::Reference
< xml::sax::XParser
> xParser( xServiceFactory
->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY
);
189 DBG_ERROR( "com.sun.star.xml.sax.Parser service missing" );
193 uno::Reference
<io::XInputStream
> xInputStream
= new utl::OInputStreamWrapper( rStream
);
196 const OUString aURL( RTL_CONSTASCII_USTRINGPARAM( "file:///e:/test.xml" ) );
197 SfxMedium aMedium( aURL, STREAM_READ | STREAM_NOCREATE, TRUE );
199 uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
201 aMedium.GetInStream()->Seek( 0 );
202 uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
206 DBG_ERROR( "got no data source from medium" );
210 uno::Reference< XInterface > xPipe( xServiceFactory->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe") ) ) );
213 DBG_ERROR( "XMLReader::Read: com.sun.star.io.Pipe service missing" );
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();
227 uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
228 xSourceControl->start();
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
);
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
);
260 pContext
= SvXMLImport::CreateContext(nPrefix
, rLocalName
, xAttrList
);