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