Bump for 3.6-28
[LibreOffice.git] / editeng / source / xml / xmltxtimp.cxx
blob91d3224f174b34849ff55cdca1fb02c9b282d81d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <com/sun/star/io/XActiveDataControl.hpp>
30 #include <com/sun/star/io/XActiveDataSource.hpp>
31 #include <com/sun/star/xml/sax/XParser.hpp>
32 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33 #include <com/sun/star/io/XOutputStream.hpp>
34 #include <com/sun/star/text/XText.hpp>
35 #include <comphelper/processfactory.hxx>
36 #include <unotools/streamwrap.hxx>
37 #include <rtl/ustrbuf.hxx>
38 #include <sot/storage.hxx>
39 #include <svl/itemprop.hxx>
40 #include <xmloff/xmlimp.hxx>
41 #include <xmloff/xmlmetae.hxx>
42 #include <xmloff/xmlictxt.hxx>
43 #include <xmloff/xmltoken.hxx>
44 #include <xmloff/xmlnmspe.hxx>
45 #include <xmloff/xmlstyle.hxx>
46 #include "editsource.hxx"
47 #include <editeng/editeng.hxx>
48 #include <editeng/unotext.hxx>
49 #include <editeng/unoprnms.hxx>
50 #include <editeng/unoipset.hxx>
52 using namespace com::sun::star;
53 using namespace com::sun::star::document;
54 using namespace com::sun::star::uno;
55 using namespace com::sun::star::lang;
56 using namespace com::sun::star::xml::sax;
57 using namespace com::sun::star::text;
58 using namespace ::rtl;
59 using namespace cppu;
60 using namespace xmloff::token;
63 ///////////////////////////////////////////////////////////////////////
65 class SvxXMLTextImportContext : public SvXMLImportContext
67 public:
68 SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >& xAttrList, const uno::Reference< XText >& xText );
69 virtual ~SvxXMLTextImportContext();
71 virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
73 // SvxXMLXTableImport& getImport() const { return *(SvxXMLXTableImport*)&GetImport(); }
75 private:
76 const uno::Reference< XText > mxText;
79 ///////////////////////////////////////////////////////////////////////
81 SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >&, const uno::Reference< XText >& xText )
82 : SvXMLImportContext( rImport, nPrfx, rLName ), mxText( xText )
86 SvxXMLTextImportContext::~SvxXMLTextImportContext()
90 SvXMLImportContext *SvxXMLTextImportContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
92 SvXMLImportContext* pContext = NULL;
93 if(XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_BODY ) )
95 pContext = new SvxXMLTextImportContext( GetImport(), nPrefix, rLocalName, xAttrList, mxText );
97 else if( XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_AUTOMATIC_STYLES ) )
99 pContext = new SvXMLStylesContext( GetImport(), nPrefix, rLocalName, xAttrList );
100 GetImport().GetTextImport()->SetAutoStyles( (SvXMLStylesContext*)pContext );
103 else
105 pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix, rLocalName, xAttrList );
108 if( NULL == pContext )
109 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
111 return pContext;
114 ///////////////////////////////////////////////////////////////////////
116 class SvxXMLXTextImportComponent : public SvXMLImport
118 public:
119 SvxXMLXTextImportComponent(
120 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
121 const uno::Reference< XText > & xText );
123 virtual ~SvxXMLXTextImportComponent() throw ();
125 static sal_Bool load( const rtl::OUString& rUrl, const com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >& xTable ) throw();
126 protected:
127 virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
129 private:
130 const uno::Reference< XText > mxText;
133 // --------------------------------------------------------------------
135 SvxXMLXTextImportComponent::SvxXMLXTextImportComponent(
136 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
137 const uno::Reference< XText > & xText )
138 : SvXMLImport(xServiceFactory),
139 mxText( xText )
141 GetTextImport()->SetCursor( mxText->createTextCursor() );
144 SvxXMLXTextImportComponent::~SvxXMLXTextImportComponent() throw ()
148 void SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
150 SvxEditEngineSource aEditSource( &rEditEngine );
152 static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap[] =
154 SVX_UNOEDIT_CHAR_PROPERTIES,
155 SVX_UNOEDIT_FONT_PROPERTIES,
156 // SVX_UNOEDIT_OUTLINER_PROPERTIES,
157 SVX_UNOEDIT_PARA_PROPERTIES,
158 {0,0,0,0,0,0}
160 static SvxItemPropertySet aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
162 uno::Reference<text::XText > xParent;
163 SvxUnoText* pUnoText = new SvxUnoText( &aEditSource, &aSvxXMLTextImportComponentPropertySet, xParent );
164 pUnoText->SetSelection( rSel );
165 uno::Reference<text::XText > xText( pUnoText );
171 uno::Reference<lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
172 if( !xServiceFactory.is() )
174 OSL_FAIL( "SvxXMLXTableImport::load: got no service manager" );
175 break;
178 uno::Reference< xml::sax::XParser > xParser( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY );
179 if( !xParser.is() )
181 OSL_FAIL( "com.sun.star.xml.sax.Parser service missing" );
182 break;
185 uno::Reference<io::XInputStream> xInputStream = new utl::OInputStreamWrapper( rStream );
187 /* testcode
188 const OUString aURL( RTL_CONSTASCII_USTRINGPARAM( "file:///e:/test.xml" ) );
189 SfxMedium aMedium( aURL, STREAM_READ | STREAM_NOCREATE, sal_True );
190 aMedium.IsRemote();
191 uno::Reference<io::XOutputStream> xOut( new utl::OOutputStreamWrapper( *aMedium.GetOutStream() ) );
193 aMedium.GetInStream()->Seek( 0 );
194 uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
196 if( !xSource.is() )
198 OSL_FAIL( "got no data source from medium" );
199 break;
202 uno::Reference< XInterface > xPipe( xServiceFactory->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe") ) ) );
203 if( !xPipe.is() )
205 OSL_FAIL( "XMLReader::Read: com.sun.star.io.Pipe service missing" );
206 break;
209 // connect pipe's output stream to the data source
210 xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) );
212 xml::sax::InputSource aParserInput;
213 aParserInput.aInputStream = uno::Reference< io::XInputStream >::query( xPipe );
214 aParserInput.sSystemId = aMedium.GetName();
217 if( xSource.is() )
219 uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
220 xSourceControl->start();
225 // uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) );
226 uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xServiceFactory, xText ) );
228 xParser->setDocumentHandler( xHandler );
230 xml::sax::InputSource aParserInput;
231 aParserInput.aInputStream = xInputStream;
232 // aParserInput.sSystemId = aMedium.GetName();
233 xParser->parseStream( aParserInput );
235 while(0);
237 catch( const uno::Exception& )
242 SvXMLImportContext *SvxXMLXTextImportComponent::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
244 SvXMLImportContext* pContext;
245 if(XML_NAMESPACE_OFFICE == nPrefix && ( IsXMLToken( rLocalName, XML_DOCUMENT ) || IsXMLToken( rLocalName, XML_DOCUMENT_CONTENT ) ) )
247 pContext = new SvxXMLTextImportContext(*this, nPrefix, rLocalName, xAttrList, mxText );
249 else
251 pContext = SvXMLImport::CreateContext(nPrefix, rLocalName, xAttrList);
253 return pContext;
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */