LibreOffice Module svx (master) 1
xmlxtimp.cxx
Go to the documentation of this file.
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 <tools/urlobj.hxx>
21#include <com/sun/star/document/XGraphicStorageHandler.hpp>
22#include <com/sun/star/embed/ElementModes.hpp>
23#include <com/sun/star/container/XNameContainer.hpp>
24#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
25#include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
26#include <com/sun/star/drawing/LineDash.hpp>
27#include <com/sun/star/awt/Gradient.hpp>
28#include <com/sun/star/awt/XBitmap.hpp>
29#include <com/sun/star/drawing/Hatch.hpp>
30#include <com/sun/star/io/XSeekable.hpp>
34#include <sfx2/docfile.hxx>
35#include <utility>
38
39#include <xmloff/xmltoken.hxx>
40#include <xmloff/DashStyle.hxx>
42#include <xmloff/HatchStyle.hxx>
43#include <xmloff/ImageStyle.hxx>
45#include <xmloff/xmlictxt.hxx>
46#include <svx/xmlgrhlp.hxx>
47
48#include <xmlxtimp.hxx>
50
51using namespace com::sun::star;
52using namespace com::sun::star::container;
53using namespace com::sun::star::document;
54using namespace com::sun::star::uno;
55using namespace com::sun::star::awt;
56using namespace com::sun::star::lang;
57using namespace com::sun::star::xml::sax;
58using namespace ::xmloff::token;
59using namespace cppu;
60
61namespace {
62
63enum class SvxXMLTableImportContextEnum { Color, Marker, Dash, Hatch, Gradient, Bitmap };
64
65class SvxXMLTableImportContext : public SvXMLImportContext
66{
67public:
68 SvxXMLTableImportContext( SvXMLImport& rImport, SvxXMLTableImportContextEnum eContext, uno::Reference< XNameContainer > xTable,
69 bool bOOoFormat );
70
71 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL
72 createFastChildContext(sal_Int32 Element,
73 const css::uno::Reference< css::xml::sax::XFastAttributeList > & Attribs) override;
74
75protected:
76 static void importColor( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName );
77 void importMarker( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName );
78 void importDash( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName );
79 void importHatch( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName );
80 void importGradient( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName );
81 void importBitmap( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName );
82
83private:
84 uno::Reference< XNameContainer > mxTable;
85 SvxXMLTableImportContextEnum meContext;
86 bool mbOOoFormat;
87};
88
89}
90
91SvxXMLTableImportContext::SvxXMLTableImportContext( SvXMLImport& rImport, SvxXMLTableImportContextEnum eContext, uno::Reference< XNameContainer > xTable, bool bOOoFormat )
92: SvXMLImportContext( rImport ), mxTable(std::move( xTable )), meContext( eContext ),
93 mbOOoFormat( bOOoFormat )
94{
95}
96
97css::uno::Reference< css::xml::sax::XFastContextHandler >
98 SvxXMLTableImportContext::createFastChildContext(sal_Int32 nElement,
99 const css::uno::Reference< css::xml::sax::XFastAttributeList > & rAttrList)
100{
101 if( !(IsTokenInNamespace(nElement, XML_NAMESPACE_DRAW) ||
102 IsTokenInNamespace(nElement, XML_NAMESPACE_DRAW_OOO) ))
103 return nullptr;
104
105 std::vector<std::pair<sal_Int32, OString>> aTmpAttrList;
106 for (auto& aIter : sax_fastparser::castToFastAttributeList( rAttrList ))
107 aTmpAttrList.push_back({aIter.getToken(), OString(aIter.toCString())});
108 if( mbOOoFormat &&
109 (SvxXMLTableImportContextEnum::Dash == meContext || SvxXMLTableImportContextEnum::Hatch == meContext ||
110 SvxXMLTableImportContextEnum::Bitmap == meContext) )
111 {
112 for( auto & aIter : aTmpAttrList )
113 {
114 sal_Int32 aLocalAttr = aIter.first & TOKEN_MASK;
115 if( aIter.first == XML_ELEMENT(XLINK, XML_HREF) &&
116 SvxXMLTableImportContextEnum::Bitmap == meContext )
117 {
118 OString& rValue = aIter.second;
119 if( !rValue.isEmpty() && '#' == rValue[0] )
120 rValue = rValue.copy( 1 );
121 }
122 else if( (IsTokenInNamespace(aIter.first, XML_NAMESPACE_DRAW) || IsTokenInNamespace(aIter.first, XML_NAMESPACE_DRAW_OOO)) &&
123 ( ( SvxXMLTableImportContextEnum::Dash == meContext &&
124 ( aLocalAttr == XML_DOTS1_LENGTH ||
125 aLocalAttr == XML_DOTS2_LENGTH ||
126 aLocalAttr == XML_DISTANCE ) ) ||
127 ( SvxXMLTableImportContextEnum::Hatch == meContext &&
128 ( aLocalAttr == XML_DISTANCE ) ) ) )
129 {
130 OString& rValue = aIter.second;
131 sal_Int32 nPos = rValue.getLength();
132 while( nPos && rValue[nPos-1] <= ' ' )
133 --nPos;
134 if( nPos > 2 &&
135 ('c'==rValue[nPos-2] || 'C'==rValue[nPos-2]) &&
136 ('h'==rValue[nPos-1] || 'H'==rValue[nPos-1]) )
137 {
138 rValue = rValue.copy( 0, nPos-2 );
139 }
140 }
141 }
142 }
143 try
144 {
146 for (const auto& aIter : aTmpAttrList)
147 xFastList->add(aIter.first, aIter.second);
148
149 Any aAny;
150 OUString aName;
151
152 switch( meContext )
153 {
154 case SvxXMLTableImportContextEnum::Color:
155 importColor( xFastList, aAny, aName );
156 break;
157 case SvxXMLTableImportContextEnum::Marker:
158 importMarker( xFastList, aAny, aName );
159 break;
160 case SvxXMLTableImportContextEnum::Dash:
161 importDash( xFastList, aAny, aName );
162 break;
163 case SvxXMLTableImportContextEnum::Hatch:
164 importHatch( xFastList, aAny, aName );
165 break;
166 case SvxXMLTableImportContextEnum::Gradient:
167 importGradient( xFastList, aAny, aName );
168 break;
169 case SvxXMLTableImportContextEnum::Bitmap:
170 importBitmap( xFastList, aAny, aName );
171 break;
172 }
173
174 if( !aName.isEmpty() && aAny.hasValue() )
175 {
176 if( mxTable->hasByName( aName ) )
177 {
178 mxTable->replaceByName( aName, aAny );
179 }
180 else
181 {
182 mxTable->insertByName( aName, aAny );
183 }
184 }
185 }
186 catch (const uno::Exception&)
187 {
189 }
190 return new SvXMLImportContext( GetImport() );
191}
192
193void SvxXMLTableImportContext::importColor( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName )
194{
195 for (auto& aIter : sax_fastparser::castToFastAttributeList( xAttrList ))
196 {
197 switch (aIter.getToken())
198 {
199 case XML_ELEMENT(DRAW, XML_NAME):
200 case XML_ELEMENT(DRAW_OOO, XML_NAME):
201 rName = aIter.toString();
202 break;
203 case XML_ELEMENT(DRAW, XML_COLOR):
204 case XML_ELEMENT(DRAW_OOO, XML_COLOR):
205 {
206 sal_Int32 nColor(0);
207 ::sax::Converter::convertColor(nColor, aIter.toView());
208 rAny <<= nColor;
209 break;
210 }
211 default:
212 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
213 }
214 }
215}
216
217void SvxXMLTableImportContext::importMarker( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName )
218{
219 try
220 {
221 XMLMarkerStyleImport aMarkerStyle( GetImport() );
222 aMarkerStyle.importXML( xAttrList, rAny, rName );
223 }
224 catch (const Exception&)
225 {
226 TOOLS_WARN_EXCEPTION("svx", "");
227 }
228}
229
230void SvxXMLTableImportContext::importDash( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName )
231{
232 try
233 {
234 XMLDashStyleImport aDashStyle( GetImport() );
235 aDashStyle.importXML( xAttrList, rAny, rName );
236 }
237 catch (const Exception&)
238 {
239 TOOLS_WARN_EXCEPTION("svx", "");
240 }
241}
242
243void SvxXMLTableImportContext::importHatch( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName )
244{
245 try
246 {
247 XMLHatchStyleImport aHatchStyle( GetImport() );
248 aHatchStyle.importXML( xAttrList, rAny, rName );
249 }
250 catch (const Exception&)
251 {
252 TOOLS_WARN_EXCEPTION("svx", "");
253 }
254}
255
256void SvxXMLTableImportContext::importGradient( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName )
257{
258 try
259 {
260 XMLGradientStyleImport aGradientStyle( GetImport() );
261 aGradientStyle.importXML( xAttrList, rAny, rName );
262 }
263 catch (const Exception&)
264 {
265 TOOLS_WARN_EXCEPTION("svx", "");
266 }
267}
268
269void SvxXMLTableImportContext::importBitmap( const uno::Reference< XFastAttributeList >& xAttrList, Any& rAny, OUString& rName )
270{
271 try
272 {
273 uno::Any aGraphicAny;
274 XMLImageStyle::importXML(xAttrList, aGraphicAny, rName, GetImport());
275 if (aGraphicAny.has<uno::Reference<graphic::XGraphic>>())
276 {
277 auto xGraphic = aGraphicAny.get<uno::Reference<graphic::XGraphic>>();
278 uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
279 if (xBitmap.is())
280 rAny <<= xBitmap;
281 }
282 }
283 catch (const Exception&)
284 {
285 TOOLS_WARN_EXCEPTION("svx", "");
286 }
287}
288
289
291 const css::uno::Reference< css::uno::XComponentContext >& rContext,
292 const uno::Reference< XNameContainer > & rTable,
293 uno::Reference<XGraphicStorageHandler> const & xGraphicStorageHandler)
294: SvXMLImport(rContext, "", SvXMLImportFlags::NONE),
295 mrTable( rTable )
296{
297 SetGraphicStorageHandler(xGraphicStorageHandler);
298
303
306
307 // OOo namespaces for reading OOo 1.1 files
308 GetNamespaceMap().Add( "___office",
311 GetNamespaceMap().Add( "___draw",
314}
315
317{
318}
319
320static void openStorageStream( xml::sax::InputSource *pParserInput,
321 rtl::Reference<SvXMLGraphicHelper>& rxGraphicHelper,
322 const uno::Reference < embed::XStorage >& xStorage )
323{
324 uno::Reference < io::XStream > xIStm( xStorage->openStreamElement( "Content.xml", embed::ElementModes::READ ), uno::UNO_SET_THROW );
325 pParserInput->aInputStream = xIStm->getInputStream();
326 rxGraphicHelper = SvXMLGraphicHelper::Create( xStorage, SvXMLGraphicHelperMode::Read );
327}
328
329bool SvxXMLXTableImport::load( const OUString &rPath, const OUString &rReferer,
330 const uno::Reference < embed::XStorage > &xStorage,
331 const uno::Reference< XNameContainer >& xTable,
332 bool *bOptLoadedFromStorage ) noexcept
333{
334 bool bRet = true;
336
337 INetURLObject aURLObj( rPath );
338 bool bUseStorage = aURLObj.GetProtocol() == INetProtocol::NotValid; // a relative path
339
340 try
341 {
342 uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
343
344 xml::sax::InputSource aParserInput;
346
347 if( !bUseStorage || !xStorage.is() )
348 {
349 SfxMedium aMedium( rPath, rReferer, StreamMode::READ | StreamMode::NOCREATE );
350 aParserInput.sSystemId = aMedium.GetName();
351
352 if( aMedium.IsStorage() )
353 {
354 uno::Reference < embed::XStorage > xMediumStorage( aMedium.GetStorage( false ), uno::UNO_SET_THROW );
355 openStorageStream( &aParserInput, xGraphicHelper, xMediumStorage );
356 }
357 else
358 aParserInput.aInputStream = aMedium.GetInputStream();
359 }
360 else // relative URL into a storage
361 {
362 uno::Reference< embed::XStorage > xSubStorage;
363 try
364 {
366 xStorage, rPath, embed::ElementModes::READ, aNasty );
367 }
368 catch (const uno::Exception&)
369 {
370 }
371 if( xSubStorage.is() )
372 openStorageStream( &aParserInput, xGraphicHelper, xSubStorage );
373 else
374 {
375 css::uno::Reference< css::io::XStream > xStream = comphelper::OStorageHelper::GetStreamAtPath(
376 xStorage, rPath, embed::ElementModes::READ, aNasty );
377 if( !xStream.is() )
378 return false;
379 aParserInput.aInputStream = xStream->getInputStream();
380 }
381 if( bOptLoadedFromStorage )
382 *bOptLoadedFromStorage = true;
383 }
384
385 uno::Reference<XGraphicStorageHandler> xGraphicStorageHandler;
386 if (xGraphicHelper.is())
387 xGraphicStorageHandler = xGraphicHelper.get();
388
389 try
390 {
391 uno::Reference< io::XSeekable > xSeek( aParserInput.aInputStream, uno::UNO_QUERY_THROW );
392 xSeek->seek( 0 );
393 }
394 catch (const uno::Exception&)
395 {
396 }
397
398 rtl::Reference<SvxXMLXTableImport> xImport(new SvxXMLXTableImport(xContext, xTable, xGraphicStorageHandler));
399 xImport->parseStream( aParserInput );
400
401 if( xGraphicHelper )
402 xGraphicHelper->dispose();
403 }
404 catch (...)
405 {
406// thrown each time you load a document with property tables that are not
407// on the current machine. FIXME: would be better to check a file exists
408// before importing ...
409 bRet = false;
410 }
411
412 return bRet;
413}
414
416 const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList >& /*xAttrList*/ )
417{
418 if( IsTokenInNamespace(nElement, XML_NAMESPACE_OOO) ||
421 {
422 bool bOOoFormat = IsTokenInNamespace(nElement, XML_NAMESPACE_OFFICE) ||
424 Type aType = mrTable->getElementType();
425 sal_Int32 nToken = nElement & TOKEN_MASK;
426
427 if ( nToken == XML_COLOR_TABLE )
428 {
429 if( aType == ::cppu::UnoType<sal_Int32>::get() )
430 return new SvxXMLTableImportContext( *this, SvxXMLTableImportContextEnum::Color, mrTable, bOOoFormat );
431 }
432 else if ( nToken == XML_MARKER_TABLE )
433 {
435 return new SvxXMLTableImportContext( *this, SvxXMLTableImportContextEnum::Marker, mrTable, bOOoFormat );
436 }
437 else if ( nToken == XML_DASH_TABLE )
438 {
440 return new SvxXMLTableImportContext( *this, SvxXMLTableImportContextEnum::Dash, mrTable, bOOoFormat );
441 }
442 else if ( nToken == XML_HATCH_TABLE )
443 {
445 return new SvxXMLTableImportContext( *this, SvxXMLTableImportContextEnum::Hatch, mrTable, bOOoFormat );
446 }
447 else if ( nToken == XML_GRADIENT_TABLE )
448 {
450 return new SvxXMLTableImportContext( *this, SvxXMLTableImportContextEnum::Gradient, mrTable, bOOoFormat );
451 }
452 else if ( nToken == XML_BITMAP_TABLE )
453 {
455 return new SvxXMLTableImportContext( *this, SvxXMLTableImportContextEnum::Bitmap, mrTable, bOOoFormat );
456 }
457 }
458
459 return nullptr;
460}
461
462/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XInputStream > xStream
INetProtocol GetProtocol() const
const OUString & GetName() const
css::uno::Reference< css::embed::XStorage > GetStorage(bool bCreateTempFile=true)
css::uno::Reference< css::io::XInputStream > const & GetInputStream()
bool IsStorage()
static rtl::Reference< SvXMLGraphicHelper > Create(const css::uno::Reference< css::embed::XStorage > &rXMLStorage, SvXMLGraphicHelperMode eCreateMode)
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
SvXMLNamespaceMap & GetNamespaceMap()
void SetGraphicStorageHandler(css::uno::Reference< css::document::XGraphicStorageHandler > const &rxGraphicStorageHandler)
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
static bool load(const OUString &rPath, const OUString &rReferer, const css::uno::Reference< css::embed::XStorage > &xStorage, const css::uno::Reference< css::container::XNameContainer > &xTable, bool *bOptLoadedFromStorage) noexcept
Definition: xmlxtimp.cxx:329
virtual ~SvxXMLXTableImport() noexcept override
Definition: xmlxtimp.cxx:316
const css::uno::Reference< css::container::XNameContainer > & mrTable
Definition: xmlxtimp.hxx:50
SvxXMLXTableImport(const css::uno::Reference< css::uno::XComponentContext > &rContext, const css::uno::Reference< css::container::XNameContainer > &rTable, css::uno::Reference< css::document::XGraphicStorageHandler > const &rxGraphicStorageHandler)
Definition: xmlxtimp.cxx:290
virtual SvXMLImportContext * CreateFastContext(sal_Int32 Element, const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlxtimp.cxx:415
static css::uno::Reference< css::embed::XStorage > GetStorageAtPath(const css::uno::Reference< css::embed::XStorage > &xStorage, std::u16string_view aPath, sal_uInt32 nOpenMode, LifecycleProxy const &rNastiness)
static css::uno::Reference< css::io::XStream > GetStreamAtPath(const css::uno::Reference< css::embed::XStorage > &xStorage, std::u16string_view aPath, sal_uInt32 nOpenMode, LifecycleProxy const &rNastiness)
static bool convertColor(sal_Int32 &rColor, std::u16string_view rValue)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_UNHANDLED_EXCEPTION(...)
OUString aName
sal_uInt16 nPos
NONE
XMLOFF_DLLPUBLIC bool importXML(css::uno::Reference< css::xml::sax::XFastAttributeList > const &xAttrList, css::uno::Any &rValue, OUString &rStrName, SvXMLImport &rImport)
Type
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
OUString toString(OptionInfo const *info)
XML_DASH_TABLE
XML_N_XLINK
XML_N_OFFICE
XML_HATCH_TABLE
XML_NP_DRAW
XML_COLOR_TABLE
XML_MARKER_TABLE
XML_NP_XLINK
XML_GRADIENT_TABLE
XML_NP_OFFICE
XML_N_DRAW_OOO
XML_N_OFFICE_OOO
XML_NP_OOO
XML_NAME
XML_N_DRAW
XML_COLOR
XML_BITMAP_TABLE
XML_N_OOO
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
DefTokenId nToken
#define XMLOFF_WARN_UNKNOWN(area, rIter)
#define XML_ELEMENT(prefix, name)
constexpr bool IsTokenInNamespace(sal_Int32 nToken, sal_uInt16 nNamespacePrefix)
SvXMLImportFlags
constexpr sal_Int32 TOKEN_MASK
constexpr sal_uInt16 XML_NAMESPACE_DRAW
constexpr sal_uInt16 XML_NAMESPACE_XLINK
constexpr sal_uInt16 XML_NAMESPACE_OFFICE_OOO
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
constexpr sal_uInt16 XML_NAMESPACE_OOO
static void openStorageStream(xml::sax::InputSource *pParserInput, rtl::Reference< SvXMLGraphicHelper > &rxGraphicHelper, const uno::Reference< embed::XStorage > &xStorage)
Definition: xmlxtimp.cxx:320