xref: /aoo42x/main/editeng/source/xml/xmltxtimp.cxx (revision 190118d0)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_editeng.hxx"
26 #include <tools/debug.hxx>
27 #include <com/sun/star/io/XActiveDataControl.hpp>
28 #include <com/sun/star/io/XActiveDataSource.hpp>
29 #include <com/sun/star/xml/sax/XParser.hpp>
30 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 #include <com/sun/star/text/XText.hpp>
33 #include <comphelper/processfactory.hxx>
34 #include <unotools/streamwrap.hxx>
35 #include <rtl/ustrbuf.hxx>
36 #include <sot/storage.hxx>
37 #include <svl/itemprop.hxx>
38 #include <xmloff/xmlimp.hxx>
39 #include <xmloff/xmlmetae.hxx>
40 #include <xmloff/xmlictxt.hxx>
41 #include <xmloff/xmltoken.hxx>
42 #include <xmloff/xmlnmspe.hxx>
43 #include <xmloff/xmlstyle.hxx>
44 #include "editsource.hxx"
45 #include <editeng/editeng.hxx>
46 #include <editeng/unotext.hxx>
47 #include <editeng/unoprnms.hxx>
48 #include <editeng/unoipset.hxx>
49 
50 using namespace com::sun::star;
51 using namespace com::sun::star::document;
52 using namespace com::sun::star::uno;
53 using namespace com::sun::star::lang;
54 using namespace com::sun::star::xml::sax;
55 using namespace com::sun::star::text;
56 using namespace ::rtl;
57 using namespace cppu;
58 using namespace xmloff::token;
59 
60 
61 ///////////////////////////////////////////////////////////////////////
62 
63 class SvxXMLTextImportContext : public SvXMLImportContext
64 {
65 public:
66 	SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >& xAttrList, const uno::Reference< XText >& xText );
67 	virtual ~SvxXMLTextImportContext();
68 
69 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
70 
71 //	SvxXMLXTableImport& getImport() const { return *(SvxXMLXTableImport*)&GetImport(); }
72 
73 private:
74 	const uno::Reference< XText > mxText;
75 };
76 
77 ///////////////////////////////////////////////////////////////////////
78 
SvxXMLTextImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<XAttributeList> &,const uno::Reference<XText> & xText)79 SvxXMLTextImportContext::SvxXMLTextImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >&, const uno::Reference< XText >& xText )
80 : SvXMLImportContext( rImport, nPrfx, rLName ), mxText( xText )
81 {
82 }
83 
~SvxXMLTextImportContext()84 SvxXMLTextImportContext::~SvxXMLTextImportContext()
85 {
86 }
87 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<XAttributeList> & xAttrList)88 SvXMLImportContext *SvxXMLTextImportContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
89 {
90 	SvXMLImportContext* pContext = NULL;
91 	if(XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_BODY ) )
92 	{
93 		pContext = new SvxXMLTextImportContext( GetImport(), nPrefix, rLocalName, xAttrList, mxText );
94 	}
95 	else if( XML_NAMESPACE_OFFICE == nPrefix && IsXMLToken( rLocalName, XML_AUTOMATIC_STYLES ) )
96 	{
97 		pContext = new SvXMLStylesContext( GetImport(), nPrefix, rLocalName, xAttrList );
98 		GetImport().GetTextImport()->SetAutoStyles( (SvXMLStylesContext*)pContext );
99 
100 	}
101 	else
102 	{
103 		pContext = GetImport().GetTextImport()->CreateTextChildContext( GetImport(), nPrefix, rLocalName, xAttrList );
104 	}
105 
106 	if( NULL == pContext )
107 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
108 
109 	return pContext;
110 }
111 
112 ///////////////////////////////////////////////////////////////////////
113 
114 class SvxXMLXTextImportComponent : public SvXMLImport
115 {
116 public:
117 	// #110680#
118 	SvxXMLXTextImportComponent(
119 		const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
120 		const uno::Reference< XText > & xText );
121 
122 	virtual ~SvxXMLXTextImportComponent() throw ();
123 
124 	static sal_Bool load( const rtl::OUString& rUrl, const com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >& xTable ) throw();
125 protected:
126 	virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
127 
128 private:
129 	const uno::Reference< XText > mxText;
130 };
131 
132 // --------------------------------------------------------------------
133 
134 // #110680#
SvxXMLXTextImportComponent(const::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> xServiceFactory,const uno::Reference<XText> & xText)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 )
140 {
141 	GetTextImport()->SetCursor( mxText->createTextCursor() );
142 }
143 
~SvxXMLXTextImportComponent()144 SvxXMLXTextImportComponent::~SvxXMLXTextImportComponent() throw ()
145 {
146 }
147 
SvxReadXML(EditEngine & rEditEngine,SvStream & rStream,const ESelection & rSel)148 void SvxReadXML( EditEngine& rEditEngine, SvStream& rStream, const ESelection& rSel )
149 {
150 	SvxEditEngineSource aEditSource( &rEditEngine );
151 
152     static const SfxItemPropertyMapEntry SvxXMLTextImportComponentPropertyMap[] =
153 	{
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}
159 	};
160 	static SvxItemPropertySet aSvxXMLTextImportComponentPropertySet( SvxXMLTextImportComponentPropertyMap, EditEngine::GetGlobalItemPool() );
161 
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 );
166 
167 	try
168 	{
169 		do
170 		{
171 			uno::Reference<lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
172 			if( !xServiceFactory.is() )
173 			{
174 				DBG_ERROR( "SvxXMLXTableImport::load: got no service manager" );
175 				break;
176 			}
177 
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() )
180 			{
181 				DBG_ERROR( "com.sun.star.xml.sax.Parser service missing" );
182 				break;
183 			}
184 
185 			uno::Reference<io::XInputStream> xInputStream = new utl::OInputStreamWrapper( rStream );
186 
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() ) );
192 
193 			aMedium.GetInStream()->Seek( 0 );
194 			uno::Reference< io::XActiveDataSource > xSource( aMedium.GetDataSource() );
195 
196 			if( !xSource.is() )
197 			{
198 				DBG_ERROR( "got no data source from medium" );
199 				break;
200 			}
201 
202 			uno::Reference< XInterface > xPipe( xServiceFactory->createInstance(OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.Pipe") ) ) );
203 			if( !xPipe.is() )
204 			{
205 				DBG_ERROR( "XMLReader::Read: com.sun.star.io.Pipe service missing" );
206 				break;
207 			}
208 
209 			// connect pipe's output stream to the data source
210 			xSource->setOutputStream( uno::Reference< io::XOutputStream >::query( xPipe ) );
211 
212 			xml::sax::InputSource aParserInput;
213 			aParserInput.aInputStream =	uno::Reference< io::XInputStream >::query( xPipe );
214 			aParserInput.sSystemId = aMedium.GetName();
215 
216 
217 			if( xSource.is() )
218 			{
219 				uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY );
220 				xSourceControl->start();
221 			}
222 
223 */
224 
225 			// #110680#
226 			// uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xText ) );
227 			uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTextImportComponent( xServiceFactory, xText ) );
228 
229 			xParser->setDocumentHandler( xHandler );
230 
231 			xml::sax::InputSource aParserInput;
232 			aParserInput.aInputStream =	xInputStream;
233 //			aParserInput.sSystemId = aMedium.GetName();
234 			xParser->parseStream( aParserInput );
235 		}
236 		while(0);
237 	}
238 	catch( uno::Exception& )
239 	{
240 	}
241 }
242 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<XAttributeList> & xAttrList)243 SvXMLImportContext *SvxXMLXTextImportComponent::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
244 {
245 	SvXMLImportContext* pContext;
246 	if(XML_NAMESPACE_OFFICE == nPrefix && ( IsXMLToken( rLocalName, XML_DOCUMENT ) || IsXMLToken( rLocalName, XML_DOCUMENT_CONTENT ) ) )
247 	{
248 		 pContext = new SvxXMLTextImportContext(*this, nPrefix, rLocalName, xAttrList, mxText );
249 	}
250 	else
251 	{
252 		pContext = SvXMLImport::CreateContext(nPrefix, rLocalName, xAttrList);
253 	}
254 	return pContext;
255 }
256 
257