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 <comphelper/classids.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 :
36 : using namespace ::com::sun::star::uno;
37 : using namespace ::com::sun::star::util;
38 : using namespace ::com::sun::star::beans;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::com::sun::star::frame;
41 : using namespace ::com::sun::star::document;
42 : using namespace ::com::sun::star::xml::sax;
43 : using namespace ::xmloff::token;
44 :
45 : struct XMLServiceMapEntry_Impl
46 : {
47 : enum XMLTokenEnum eClass;
48 : const sal_Char *sFilterService;
49 : sal_Int32 nFilterServiceLen;
50 : };
51 :
52 : #define SERVICE_MAP_ENTRY( cls, app ) \
53 : { XML_##cls, \
54 : XML_IMPORT_FILTER_##app, sizeof(XML_IMPORT_FILTER_##app)-1}
55 :
56 : const XMLServiceMapEntry_Impl aServiceMap[] =
57 : {
58 : SERVICE_MAP_ENTRY( TEXT, WRITER ),
59 : SERVICE_MAP_ENTRY( ONLINE_TEXT, WRITER ),
60 : SERVICE_MAP_ENTRY( SPREADSHEET, CALC ),
61 : SERVICE_MAP_ENTRY( DRAWING, DRAW ),
62 : SERVICE_MAP_ENTRY( GRAPHICS, DRAW ),
63 : SERVICE_MAP_ENTRY( PRESENTATION, IMPRESS ),
64 : SERVICE_MAP_ENTRY( CHART, CHART ),
65 : { XML_TOKEN_INVALID, 0, 0 }
66 : };
67 :
68 : class XMLEmbeddedObjectImportContext_Impl : public SvXMLImportContext
69 : {
70 : ::com::sun::star::uno::Reference<
71 : ::com::sun::star::xml::sax::XDocumentHandler > xHandler;
72 :
73 : public:
74 : TYPEINFO();
75 :
76 : XMLEmbeddedObjectImportContext_Impl( SvXMLImport& rImport, sal_uInt16 nPrfx,
77 : const OUString& rLName,
78 : const ::com::sun::star::uno::Reference<
79 : ::com::sun::star::xml::sax::XDocumentHandler >& rHandler );
80 :
81 : virtual ~XMLEmbeddedObjectImportContext_Impl();
82 :
83 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
84 : const OUString& rLocalName,
85 : const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
86 :
87 : virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
88 :
89 : virtual void EndElement();
90 :
91 : virtual void Characters( const OUString& rChars );
92 : };
93 :
94 0 : TYPEINIT1( XMLEmbeddedObjectImportContext_Impl, SvXMLImportContext );
95 :
96 0 : XMLEmbeddedObjectImportContext_Impl::XMLEmbeddedObjectImportContext_Impl(
97 : SvXMLImport& rImport, sal_uInt16 nPrfx,
98 : const OUString& rLName,
99 : const Reference< XDocumentHandler >& rHandler ) :
100 : SvXMLImportContext( rImport, nPrfx, rLName ),
101 0 : xHandler( rHandler )
102 : {
103 0 : }
104 :
105 0 : XMLEmbeddedObjectImportContext_Impl::~XMLEmbeddedObjectImportContext_Impl()
106 : {
107 0 : }
108 :
109 0 : SvXMLImportContext *XMLEmbeddedObjectImportContext_Impl::CreateChildContext(
110 : sal_uInt16 nPrefix,
111 : const OUString& rLocalName,
112 : const Reference< XAttributeList >& )
113 : {
114 0 : return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
115 : nPrefix, rLocalName,
116 0 : xHandler );
117 : }
118 :
119 0 : void XMLEmbeddedObjectImportContext_Impl::StartElement(
120 : const Reference< XAttributeList >& xAttrList )
121 : {
122 0 : xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
123 0 : GetPrefix(), GetLocalName() ),
124 0 : xAttrList );
125 0 : }
126 :
127 0 : void XMLEmbeddedObjectImportContext_Impl::EndElement()
128 : {
129 0 : xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
130 0 : GetPrefix(), GetLocalName() ) );
131 0 : }
132 :
133 0 : void XMLEmbeddedObjectImportContext_Impl::Characters( const OUString& rChars )
134 : {
135 0 : xHandler->characters( rChars );
136 0 : }
137 :
138 : //-----------------------------------------------------------------------------
139 :
140 0 : TYPEINIT1( XMLEmbeddedObjectImportContext, SvXMLImportContext );
141 :
142 0 : sal_Bool XMLEmbeddedObjectImportContext::SetComponent(
143 : Reference< XComponent >& rComp )
144 : {
145 0 : if( !rComp.is() || sFilterService.isEmpty() )
146 0 : return sal_False;
147 :
148 :
149 0 : Sequence<Any> aArgs( 0 );
150 :
151 0 : Reference< XComponentContext > xContext( GetImport().GetComponentContext() );
152 :
153 0 : xHandler = Reference < XDocumentHandler >(
154 0 : xContext->getServiceManager()->createInstanceWithArgumentsAndContext(sFilterService, aArgs, xContext),
155 0 : UNO_QUERY);
156 :
157 0 : if( !xHandler.is() )
158 0 : return sal_False;
159 :
160 : try
161 : {
162 0 : Reference < XModifiable2 > xModifiable2( rComp, UNO_QUERY_THROW );
163 0 : xModifiable2->disableSetModified();
164 : }
165 0 : catch( Exception& )
166 : {
167 : }
168 :
169 0 : Reference < XImporter > xImporter( xHandler, UNO_QUERY );
170 0 : xImporter->setTargetDocument( rComp );
171 :
172 0 : xComp = rComp; // keep ref to component only if there is a handler
173 :
174 0 : return sal_True;
175 : }
176 :
177 0 : XMLEmbeddedObjectImportContext::XMLEmbeddedObjectImportContext(
178 : SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
179 : const Reference< XAttributeList >& xAttrList ) :
180 0 : SvXMLImportContext( rImport, nPrfx, rLName )
181 : {
182 0 : SvGlobalName aName;
183 :
184 0 : if( nPrfx == XML_NAMESPACE_MATH &&
185 0 : IsXMLToken( rLName, XML_MATH ) )
186 : {
187 0 : sFilterService = OUString( XML_IMPORT_FILTER_MATH );
188 0 : aName = SvGlobalName(SO3_SM_CLASSID);
189 : }
190 0 : else if( nPrfx == XML_NAMESPACE_OFFICE &&
191 0 : IsXMLToken( rLName, XML_DOCUMENT ) )
192 : {
193 0 : OUString sMime;
194 :
195 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
196 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
197 : {
198 0 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
199 0 : OUString aLocalName;
200 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
201 0 : if( nPrefix == XML_NAMESPACE_OFFICE &&
202 0 : IsXMLToken( aLocalName, XML_MIMETYPE ) )
203 : {
204 0 : sMime = xAttrList->getValueByIndex( i );
205 0 : break;
206 : }
207 0 : }
208 :
209 0 : OUString sClass;
210 : static const char * aTmp[] =
211 : {
212 : "application/vnd.oasis.openoffice.",
213 : "application/x-vnd.oasis.openoffice.",
214 : "application/vnd.oasis.opendocument.",
215 : "application/x-vnd.oasis.opendocument.",
216 : NULL
217 : };
218 0 : for (int k=0; aTmp[k]; k++)
219 : {
220 0 : OUString sTmpString = OUString::createFromAscii(aTmp[k]);
221 0 : if( sMime.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
222 : {
223 0 : sClass = sMime.copy( sTmpString.getLength() );
224 0 : break;
225 : }
226 0 : }
227 :
228 0 : if( !sClass.isEmpty() )
229 : {
230 0 : const XMLServiceMapEntry_Impl *pEntry = aServiceMap;
231 0 : while( pEntry->eClass != XML_TOKEN_INVALID )
232 : {
233 0 : if( IsXMLToken( sClass, pEntry->eClass ) )
234 : {
235 0 : sFilterService = OUString( pEntry->sFilterService,
236 : pEntry->nFilterServiceLen,
237 0 : RTL_TEXTENCODING_ASCII_US );
238 :
239 0 : switch( pEntry->eClass )
240 : {
241 0 : case XML_TEXT: aName = SvGlobalName(SO3_SW_CLASSID); break;
242 0 : case XML_ONLINE_TEXT: aName = SvGlobalName(SO3_SWWEB_CLASSID); break;
243 0 : case XML_SPREADSHEET: aName = SvGlobalName(SO3_SC_CLASSID); break;
244 : case XML_DRAWING:
245 : case XML_GRAPHICS:
246 0 : case XML_IMAGE: aName = SvGlobalName(SO3_SDRAW_CLASSID); break;
247 0 : case XML_PRESENTATION: aName = SvGlobalName(SO3_SIMPRESS_CLASSID); break;
248 0 : case XML_CHART: aName = SvGlobalName(SO3_SCH_CLASSID); break;
249 : default:
250 0 : break;
251 : }
252 :
253 0 : break;
254 : }
255 0 : pEntry++;
256 : }
257 0 : }
258 : }
259 :
260 0 : sCLSID = aName.GetHexName();
261 0 : }
262 :
263 0 : XMLEmbeddedObjectImportContext::~XMLEmbeddedObjectImportContext()
264 : {
265 0 : }
266 :
267 0 : SvXMLImportContext *XMLEmbeddedObjectImportContext::CreateChildContext(
268 : sal_uInt16 nPrefix, const OUString& rLocalName,
269 : const Reference< XAttributeList >& )
270 : {
271 0 : if( xHandler.is() )
272 0 : return new XMLEmbeddedObjectImportContext_Impl( GetImport(),
273 : nPrefix, rLocalName,
274 0 : xHandler );
275 : else
276 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
277 : }
278 :
279 0 : void XMLEmbeddedObjectImportContext::StartElement(
280 : const Reference< XAttributeList >& rAttrList )
281 : {
282 0 : if( xHandler.is() )
283 : {
284 0 : xHandler->startDocument();
285 : // #i34042: copy namepspace declarations
286 0 : SvXMLAttributeList *pAttrList = new SvXMLAttributeList( rAttrList );
287 0 : Reference< XAttributeList > xAttrList( pAttrList );
288 0 : const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap();
289 0 : sal_uInt16 nPos = rNamespaceMap.GetFirstKey();
290 0 : while( USHRT_MAX != nPos )
291 : {
292 0 : OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) );
293 0 : if( xAttrList->getValueByName( aAttrName ).isEmpty() )
294 : {
295 : pAttrList->AddAttribute( aAttrName,
296 0 : rNamespaceMap.GetNameByKey( nPos ) );
297 : }
298 0 : nPos = rNamespaceMap.GetNextKey( nPos );
299 0 : }
300 0 : xHandler->startElement( GetImport().GetNamespaceMap().GetQNameByKey(
301 0 : GetPrefix(), GetLocalName() ),
302 0 : xAttrList );
303 : }
304 0 : }
305 :
306 0 : void XMLEmbeddedObjectImportContext::EndElement()
307 : {
308 0 : if( xHandler.is() )
309 : {
310 0 : xHandler->endElement( GetImport().GetNamespaceMap().GetQNameByKey(
311 0 : GetPrefix(), GetLocalName() ) );
312 0 : xHandler->endDocument();
313 :
314 : try
315 : {
316 0 : Reference < XModifiable2 > xModifiable2( xComp, UNO_QUERY_THROW );
317 0 : xModifiable2->enableSetModified();
318 0 : xModifiable2->setModified( sal_True ); // trigger new replacement image generation
319 : }
320 0 : catch( Exception& )
321 : {
322 : }
323 : }
324 0 : }
325 :
326 0 : void XMLEmbeddedObjectImportContext::Characters( const OUString& rChars )
327 : {
328 0 : if( xHandler.is() )
329 0 : xHandler->characters( rChars );
330 0 : }
331 :
332 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|