Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/document/XImporter.hpp>
21 : #include <com/sun/star/util/XModifiable.hpp>
22 : #include <com/sun/star/util/XModifiable2.hpp>
23 : #include <com/sun/star/frame/XStorable.hpp>
24 : #include <tools/globname.hxx>
25 : #include <sot/clsids.hxx>
26 : #include <xmloff/nmspmap.hxx>
27 : #include <xmloff/xmlimp.hxx>
28 : #include "xmloff/xmlnmspe.hxx"
29 : #include <xmloff/xmltoken.hxx>
30 : #include "xmloff/xmlerror.hxx"
31 : #include <xmloff/attrlist.hxx>
32 : #include "xmloff/XMLFilterServiceNames.h"
33 : #include "XMLEmbeddedObjectImportContext.hxx"
34 :
35 : using ::rtl::OUString;
36 : using ::rtl::OUStringBuffer;
37 :
38 : using namespace ::com::sun::star::uno;
39 : using namespace ::com::sun::star::util;
40 : using namespace ::com::sun::star::beans;
41 : using namespace ::com::sun::star::lang;
42 : using namespace ::com::sun::star::frame;
43 : using namespace ::com::sun::star::document;
44 : using namespace ::com::sun::star::xml::sax;
45 : using namespace ::xmloff::token;
46 :
47 : struct XMLServiceMapEntry_Impl
48 : {
49 : enum XMLTokenEnum eClass;
50 : const sal_Char *sFilterService;
51 : sal_Int32 nFilterServiceLen;
52 : };
53 :
54 : #define SERVICE_MAP_ENTRY( cls, app ) \
55 : { XML_##cls, \
56 : XML_IMPORT_FILTER_##app, sizeof(XML_IMPORT_FILTER_##app)-1}
57 :
58 : const XMLServiceMapEntry_Impl aServiceMap[] =
59 : {
60 : SERVICE_MAP_ENTRY( TEXT, WRITER ),
61 : SERVICE_MAP_ENTRY( ONLINE_TEXT, WRITER ),
62 : SERVICE_MAP_ENTRY( SPREADSHEET, CALC ),
63 : SERVICE_MAP_ENTRY( DRAWING, DRAW ),
64 : SERVICE_MAP_ENTRY( GRAPHICS, DRAW ),
65 : SERVICE_MAP_ENTRY( PRESENTATION, IMPRESS ),
66 : SERVICE_MAP_ENTRY( CHART, CHART ),
67 : { XML_TOKEN_INVALID, 0, 0 }
68 : };
69 :
70 : class XMLEmbeddedObjectImportContext_Impl : public SvXMLImportContext
71 : {
72 : ::com::sun::star::uno::Reference<
73 : ::com::sun::star::xml::sax::XDocumentHandler > xHandler;
74 :
75 : public:
76 : TYPEINFO();
77 :
78 : XMLEmbeddedObjectImportContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
79 : const ::rtl::OUString& rLName,
80 : const ::com::sun::star::uno::Reference<
81 : ::com::sun::star::xml::sax::XDocumentHandler >& rHandler );
82 :
83 : virtual ~XMLEmbeddedObjectImportContext_Impl();
84 :
85 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
86 : const ::rtl::OUString& rLocalName,
87 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
88 :
89 : virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
90 :
91 : virtual void EndElement();
92 :
93 : virtual void Characters( const ::rtl::OUString& rChars );
94 : };
95 :
96 0 : TYPEINIT1( XMLEmbeddedObjectImportContext_Impl, SvXMLImportContext );
97 :
98 0 : XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
99 : SvXMLImport& rImport, sal_uInt16 nPrfx,
100 : const OUString& rLName,
101 : const Reference< XDocumentHandler >& rHandler ) :
102 : SvXMLImportContext( rImport, nPrfx, rLName ),
103 0 : xHandler( rHandler )
104 : {
105 0 : }
106 :
107 0 : XMLEmbeddedObjectImportContext_Impl::~XMLEmbeddedObjectImportContext_Impl()
108 : {
109 0 : }
110 :
111 0 : SvXMLImportContext *XMLEmbeddedObjectImportContext_Impl::CreateChildContext(
112 : sal_uInt16 nPrefix,
113 : const OUString& rLocalName,
114 : const Reference< XAttributeList >& )
115 : {
116 0 : return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
117 : nPrefix, rLocalName,
118 0 : xHandler );
119 : }
120 :
121 0 : void XMLEmbeddedObjectImportContext_Impl::StartElement(
122 : const Reference< XAttributeList >& xAttrList )
123 : {
124 0 : xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
125 0 : GetPrefix(), GetLocalName() ),
126 0 : xAttrList );
127 0 : }
128 :
129 0 : void XMLEmbeddedObjectImportContext_Impl::EndElement()
130 : {
131 0 : xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
132 0 : GetPrefix(), GetLocalName() ) );
133 0 : }
134 :
135 0 : void XMLEmbeddedObjectImportContext_Impl::Characters( const OUString& rChars )
136 : {
137 0 : xHandler->characters( rChars );
138 0 : }
139 :
140 : //-----------------------------------------------------------------------------
141 :
142 0 : TYPEINIT1( XMLEmbeddedObjectImportContext, SvXMLImportContext );
143 :
144 0 : sal_Bool XMLEmbeddedObjectImportContext::SetComponent(
145 : Reference< XComponent >& rComp )
146 : {
147 0 : if( !rComp.is() || sFilterService.isEmpty() )
148 0 : return sal_False;
149 :
150 :
151 0 : Sequence<Any> aArgs( 0 );
152 :
153 0 : Reference< XMultiServiceFactory > xServiceFactory = GetImport().getServiceFactory();
154 :
155 : xHandler = Reference < XDocumentHandler >(
156 0 : xServiceFactory->createInstanceWithArguments( sFilterService, aArgs),
157 0 : UNO_QUERY);
158 :
159 0 : if( !xHandler.is() )
160 0 : return sal_False;
161 :
162 : try
163 : {
164 0 : Reference < XModifiable2 > xModifiable2( rComp, UNO_QUERY_THROW );
165 0 : xModifiable2->disableSetModified();
166 : }
167 0 : catch( Exception& )
168 : {
169 : }
170 :
171 0 : Reference < XImporter > xImporter( xHandler, UNO_QUERY );
172 0 : xImporter->setTargetDocument( rComp );
173 :
174 0 : xComp = rComp; // keep ref to component only if there is a handler
175 :
176 0 : return sal_True;
177 : }
178 :
179 0 : XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
180 : SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
181 : const Reference< XAttributeList >& xAttrList ) :
182 0 : SvXMLImportContext( rImport, nPrfx, rLName )
183 : {
184 0 : SvGlobalName aName;
185 :
186 0 : if( nPrfx == XML_NAMESPACE_MATH &&
187 0 : IsXMLToken( rLName, XML_MATH ) )
188 : {
189 0 : sFilterService = OUString( XML_IMPORT_FILTER_MATH );
190 0 : aName = SvGlobalName(SO3_SM_CLASSID);
191 : }
192 0 : else if( nPrfx == XML_NAMESPACE_OFFICE &&
193 0 : IsXMLToken( rLName, XML_DOCUMENT ) )
194 : {
195 0 : OUString sMime;
196 :
197 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
198 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
199 : {
200 0 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
201 0 : OUString aLocalName;
202 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
203 0 : if( nPrefix == XML_NAMESPACE_OFFICE &&
204 0 : IsXMLToken( aLocalName, XML_MIMETYPE ) )
205 : {
206 0 : sMime = xAttrList->getValueByIndex( i );
207 : break;
208 : }
209 0 : }
210 :
211 0 : OUString sClass;
212 : static const char * aTmp[] =
213 : {
214 : "application/vnd.oasis.openoffice.",
215 : "application/x-vnd.oasis.openoffice.",
216 : "application/vnd.oasis.opendocument.",
217 : "application/x-vnd.oasis.opendocument.",
218 : NULL
219 : };
220 0 : for (int k=0; aTmp[k]; k++)
221 : {
222 0 : ::rtl::OUString sTmpString = ::rtl::OUString::createFromAscii(aTmp[k]);
223 0 : if( sMime.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
224 : {
225 0 : sClass = sMime.copy( sTmpString.getLength() );
226 : break;
227 : }
228 0 : }
229 :
230 0 : if( !sClass.isEmpty() )
231 : {
232 0 : const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
233 0 : while( pEntry->eClass != XML_TOKEN_INVALID )
234 : {
235 0 : if( IsXMLToken( sClass, pEntry->eClass ) )
236 : {
237 : sFilterService = OUString( pEntry->sFilterService,
238 : pEntry->nFilterServiceLen,
239 0 : RTL_TEXTENCODING_ASCII_US );
240 :
241 0 : switch( pEntry->eClass )
242 : {
243 0 : case XML_TEXT: aName = SvGlobalName(SO3_SW_CLASSID); break;
244 0 : case XML_ONLINE_TEXT: aName = SvGlobalName(SO3_SWWEB_CLASSID); break;
245 0 : case XML_SPREADSHEET: aName = SvGlobalName(SO3_SC_CLASSID); break;
246 : case XML_DRAWING:
247 : case XML_GRAPHICS:
248 0 : case XML_IMAGE: aName = SvGlobalName(SO3_SDRAW_CLASSID); break;
249 0 : case XML_PRESENTATION: aName = SvGlobalName(SO3_SIMPRESS_CLASSID); break;
250 0 : case XML_CHART: aName = SvGlobalName(SO3_SCH_CLASSID); break;
251 : default:
252 0 : break;
253 : }
254 :
255 0 : break;
256 : }
257 0 : pEntry++;
258 : }
259 0 : }
260 : }
261 :
262 0 : sCLSID = aName.GetHexName();
263 0 : }
264 :
265 0 : XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
266 : {
267 0 : }
268 :
269 0 : SvXMLImportContext *XMLEmbeddedObjectImportContext::CreateChildContext(
270 : sal_uInt16 nPrefix, const OUString& rLocalName,
271 : const Reference< XAttributeList >& )
272 : {
273 0 : if( xHandler.is() )
274 0 : return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
275 : nPrefix, rLocalName,
276 0 : xHandler );
277 : else
278 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
279 : }
280 :
281 0 : void XMLEmbeddedObjectImportContext::StartElement(
282 : const Reference< XAttributeList >& rAttrList )
283 : {
284 0 : if( xHandler.is() )
285 : {
286 0 : xHandler->startDocument();
287 : // #i34042: copy namepspace declarations
288 0 : SvXMLAttributeList *pAttrList = new SvXMLAttributeList( rAttrList );
289 0 : Reference< XAttributeList > xAttrList( pAttrList );
290 0 : const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap();
291 0 : sal_uInt16 nPos = rNamespaceMap.GetFirstKey();
292 0 : while( USHRT_MAX != nPos )
293 : {
294 0 : OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) );
295 0 : if( xAttrList->getValueByName( aAttrName ).isEmpty() )
296 : {
297 : pAttrList->AddAttribute( aAttrName,
298 0 : rNamespaceMap.GetNameByKey( nPos ) );
299 : }
300 0 : nPos = rNamespaceMap.GetNextKey( nPos );
301 0 : }
302 0 : xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
303 0 : GetPrefix(), GetLocalName() ),
304 0 : xAttrList );
305 : }
306 0 : }
307 :
308 0 : void XMLEmbeddedObjectImportContext::EndElement()
309 : {
310 0 : if( xHandler.is() )
311 : {
312 0 : xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
313 0 : GetPrefix(), GetLocalName() ) );
314 0 : xHandler->endDocument();
315 :
316 : try
317 : {
318 0 : Reference < XModifiable2 > xModifiable2( xComp, UNO_QUERY_THROW );
319 0 : xModifiable2->enableSetModified();
320 0 : xModifiable2->setModified( sal_True ); // trigger new replacement image generation
321 : }
322 0 : catch( Exception& )
323 : {
324 : }
325 : }
326 0 : }
327 :
328 0 : void XMLEmbeddedObjectImportContext::Characters( const ::rtl::OUString& rChars )
329 : {
330 0 : if( xHandler.is() )
331 0 : xHandler->characters( rChars );
332 0 : }
333 :
334 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|