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/io/XActiveDataControl.hpp>
24 #include <com/sun/star/io/XActiveDataSource.hpp>
25 #include <com/sun/star/xml/sax/Parser.hpp>
26 #include <com/sun/star/container/XNameContainer.hpp>
27 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
28 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
29 #include <com/sun/star/drawing/LineDash.hpp>
30 #include <com/sun/star/awt/Gradient.hpp>
31 #include <com/sun/star/awt/XBitmap.hpp>
32 #include <com/sun/star/drawing/Hatch.hpp>
33 #include <com/sun/star/io/XOutputStream.hpp>
34 #include <com/sun/star/io/XSeekable.hpp>
35 #include <comphelper/processfactory.hxx>
36 #include <comphelper/storagehelper.hxx>
37 #include <unotools/streamwrap.hxx>
38 #include <rtl/ustrbuf.hxx>
39 #include <sax/tools/converter.hxx>
40 #include <sfx2/docfile.hxx>
41 #include <xmloff/xmlnmspe.hxx>
42 #include <xmloff/nmspmap.hxx>
43 
44 #include <xmloff/xmltoken.hxx>
45 #include <xmloff/xmlmetae.hxx>
46 #include <xmloff/DashStyle.hxx>
47 #include <xmloff/GradientStyle.hxx>
48 #include <xmloff/HatchStyle.hxx>
49 #include <xmloff/ImageStyle.hxx>
50 #include <xmloff/MarkerStyle.hxx>
51 #include <xmloff/xmlictxt.hxx>
52 #include <svx/xmlgrhlp.hxx>
53 #include <xmloff/attrlist.hxx>
54 
55 #include <xmlxtimp.hxx>
56 
57 #include <cstdio>
58 
59 using namespace com::sun::star;
60 using namespace com::sun::star::container;
61 using namespace com::sun::star::document;
62 using namespace com::sun::star::uno;
63 using namespace com::sun::star::awt;
64 using namespace com::sun::star::lang;
65 using namespace com::sun::star::xml::sax;
66 using namespace ::xmloff::token;
67 using namespace cppu;
68 
69 enum class SvxXMLTableImportContextEnum { Color, Marker, Dash, Hatch, Gradient, Bitmap };
70 
71 
72 class SvxXMLTableImportContext : public SvXMLImportContext
73 {
74 public:
75     SvxXMLTableImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, SvxXMLTableImportContextEnum eContext, const uno::Reference< XNameContainer >& xTable,
76         bool bOOoFormat );
77 
78     virtual SvXMLImportContextRef CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList ) override;
79 
80 protected:
81     void importColor( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
82     void importMarker( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
83     void importDash( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
84     void importHatch( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
85     void importGradient( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
86     void importBitmap( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
87 
88 private:
89     uno::Reference< XNameContainer > mxTable;
90     SvxXMLTableImportContextEnum const meContext;
91     bool const mbOOoFormat;
92 };
93 
94 
SvxXMLTableImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,SvxXMLTableImportContextEnum eContext,const uno::Reference<XNameContainer> & xTable,bool bOOoFormat)95 SvxXMLTableImportContext::SvxXMLTableImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName, SvxXMLTableImportContextEnum eContext, const uno::Reference< XNameContainer >& xTable, bool bOOoFormat )
96 : SvXMLImportContext( rImport, nPrfx, rLName ), mxTable( xTable ), meContext( eContext ),
97     mbOOoFormat( bOOoFormat )
98 {
99 }
100 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<XAttributeList> & rAttrList)101 SvXMLImportContextRef SvxXMLTableImportContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& rAttrList )
102 {
103     if( XML_NAMESPACE_DRAW == nPrefix )
104     {
105         uno::Reference< XAttributeList > xAttrList( rAttrList );
106         if( mbOOoFormat &&
107              (SvxXMLTableImportContextEnum::Dash == meContext || SvxXMLTableImportContextEnum::Hatch == meContext ||
108              SvxXMLTableImportContextEnum::Bitmap == meContext) )
109         {
110             SvXMLAttributeList *pAttrList = new SvXMLAttributeList( rAttrList );
111             xAttrList = pAttrList;
112             sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
113             for( sal_Int16 i=0; i < nAttrCount; i++ )
114             {
115                 const OUString& rAttrName = xAttrList->getNameByIndex( i );
116                 OUString aLocalName;
117                 sal_uInt16 nPrefix_ =
118                     GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
119                                                                 &aLocalName );
120                 if( XML_NAMESPACE_XLINK == nPrefix_ &&
121                     SvxXMLTableImportContextEnum::Bitmap == meContext &&
122                     IsXMLToken( aLocalName, XML_HREF ) )
123                 {
124                     const OUString rValue = xAttrList->getValueByIndex( i );
125                     if( !rValue.isEmpty() && '#' == rValue[0] )
126                         pAttrList->SetValueByIndex( i, rValue.copy( 1 ) );
127                 }
128                 else if( XML_NAMESPACE_DRAW == nPrefix_ &&
129                           ( ( SvxXMLTableImportContextEnum::Dash == meContext &&
130                               (IsXMLToken( aLocalName, XML_DOTS1_LENGTH ) ||
131                                IsXMLToken( aLocalName, XML_DOTS2_LENGTH ) ||
132                                IsXMLToken( aLocalName, XML_DISTANCE )) ) ||
133                             ( SvxXMLTableImportContextEnum::Hatch == meContext &&
134                               IsXMLToken( aLocalName, XML_HATCH_DISTANCE ) ) ) )
135                 {
136                     const OUString rValue = xAttrList->getValueByIndex( i );
137                     sal_Int32 nPos = rValue.getLength();
138                     while( nPos && rValue[nPos-1] <= ' ' )
139                         --nPos;
140                     if( nPos > 2 &&
141                         ('c'==rValue[nPos-2] || 'C'==rValue[nPos-2]) &&
142                         ('h'==rValue[nPos-1] || 'H'==rValue[nPos-1]) )
143                     {
144                         pAttrList->SetValueByIndex( i, rValue.copy( 0, nPos-2 ) );
145                     }
146                 }
147             }
148         }
149         try
150         {
151             Any aAny;
152             OUString aName;
153 
154             switch( meContext )
155             {
156             case SvxXMLTableImportContextEnum::Color:
157                 importColor( xAttrList, aAny, aName );
158                 break;
159             case SvxXMLTableImportContextEnum::Marker:
160                 importMarker( xAttrList, aAny, aName  );
161                 break;
162             case SvxXMLTableImportContextEnum::Dash:
163                 importDash( xAttrList, aAny, aName  );
164                 break;
165             case SvxXMLTableImportContextEnum::Hatch:
166                 importHatch( xAttrList, aAny, aName  );
167                 break;
168             case SvxXMLTableImportContextEnum::Gradient:
169                 importGradient( xAttrList, aAny, aName  );
170                 break;
171             case SvxXMLTableImportContextEnum::Bitmap:
172                 importBitmap( xAttrList, aAny, aName  );
173                 break;
174             }
175 
176             if( !aName.isEmpty() && aAny.hasValue() )
177             {
178                 if( mxTable->hasByName( aName ) )
179                 {
180                     mxTable->replaceByName( aName, aAny );
181                 }
182                 else
183                 {
184                     mxTable->insertByName( aName, aAny );
185                 }
186             }
187         }
188         catch (const uno::Exception&)
189         {
190         }
191     }
192 
193     return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
194 }
195 
importColor(const uno::Reference<XAttributeList> & xAttrList,Any & rAny,OUString & rName)196 void SvxXMLTableImportContext::importColor( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
197 {
198     const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
199     for( sal_Int16 i=0; i < nAttrCount; i++ )
200     {
201         const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
202         OUString aLocalName;
203         sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rFullAttrName, &aLocalName );
204 
205 
206         if( XML_NAMESPACE_DRAW == nPrefix )
207         {
208             if( aLocalName == GetXMLToken(XML_NAME) )
209             {
210                 rName = xAttrList->getValueByIndex( i );
211             }
212             else if( aLocalName == GetXMLToken(XML_COLOR) )
213             {
214                 sal_Int32 nColor(0);
215                 ::sax::Converter::convertColor(nColor,
216                         xAttrList->getValueByIndex( i ));
217                 rAny <<= nColor;
218             }
219         }
220     }
221 }
222 
importMarker(const uno::Reference<XAttributeList> & xAttrList,Any & rAny,OUString & rName)223 void SvxXMLTableImportContext::importMarker( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
224 {
225     try
226     {
227         XMLMarkerStyleImport aMarkerStyle( GetImport() );
228         aMarkerStyle.importXML( xAttrList, rAny, rName );
229     }
230     catch (const Exception&)
231     {
232         OSL_FAIL("SvxXMLTableImportContext::importMarker(), exception caught!");
233     }
234 }
235 
importDash(const uno::Reference<XAttributeList> & xAttrList,Any & rAny,OUString & rName)236 void SvxXMLTableImportContext::importDash( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
237 {
238     try
239     {
240         XMLDashStyleImport aDashStyle( GetImport() );
241         aDashStyle.importXML( xAttrList, rAny, rName );
242     }
243     catch (const Exception&)
244     {
245         OSL_FAIL("SvxXMLTableImportContext::importDash(), exception caught!");
246     }
247 }
248 
importHatch(const uno::Reference<XAttributeList> & xAttrList,Any & rAny,OUString & rName)249 void SvxXMLTableImportContext::importHatch( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
250 {
251     try
252     {
253         XMLHatchStyleImport aHatchStyle( GetImport() );
254         aHatchStyle.importXML( xAttrList, rAny, rName );
255     }
256     catch (const Exception&)
257     {
258         OSL_FAIL("SvxXMLTableImportContext::importHatch(), exception caught!");
259     }
260 }
261 
importGradient(const uno::Reference<XAttributeList> & xAttrList,Any & rAny,OUString & rName)262 void SvxXMLTableImportContext::importGradient( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
263 {
264     try
265     {
266         XMLGradientStyleImport aGradientStyle( GetImport() );
267         aGradientStyle.importXML( xAttrList, rAny, rName );
268     }
269     catch (const Exception&)
270     {
271         OSL_FAIL("SvxXMLTableImportContext::importGradient(), exception caught!");
272     }
273 }
274 
importBitmap(const uno::Reference<XAttributeList> & xAttrList,Any & rAny,OUString & rName)275 void SvxXMLTableImportContext::importBitmap( const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
276 {
277     try
278     {
279         uno::Any aGraphicAny;
280         XMLImageStyle::importXML(xAttrList, aGraphicAny, rName, GetImport());
281         if (aGraphicAny.has<uno::Reference<graphic::XGraphic>>())
282         {
283             auto xGraphic = aGraphicAny.get<uno::Reference<graphic::XGraphic>>();
284             uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
285             if (xBitmap.is())
286                 rAny <<= xBitmap;
287         }
288     }
289     catch (const Exception&)
290     {
291         OSL_FAIL("SvxXMLTableImportContext::importBitmap(), exception caught!");
292     }
293 }
294 
295 
SvxXMLXTableImport(const css::uno::Reference<css::uno::XComponentContext> & rContext,const uno::Reference<XNameContainer> & rTable,uno::Reference<XGraphicStorageHandler> const & xGraphicStorageHandler)296 SvxXMLXTableImport::SvxXMLXTableImport(
297     const css::uno::Reference< css::uno::XComponentContext >& rContext,
298     const uno::Reference< XNameContainer > & rTable,
299     uno::Reference<XGraphicStorageHandler> const & xGraphicStorageHandler)
300 :   SvXMLImport(rContext, "", SvXMLImportFlags::NONE),
301     mrTable( rTable )
302 {
303     SetGraphicStorageHandler(xGraphicStorageHandler);
304 
305     GetNamespaceMap().Add( "__ooo", GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
306     GetNamespaceMap().Add( "__office", GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
307     GetNamespaceMap().Add( "__draw", GetXMLToken(XML_N_DRAW), XML_NAMESPACE_DRAW );
308     GetNamespaceMap().Add( "__xlink", GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
309 
310     // OOo namespaces for reading OOo 1.1 files
311     GetNamespaceMap().Add( "___office",
312                         GetXMLToken(XML_N_OFFICE_OOO),
313                         XML_NAMESPACE_OFFICE );
314     GetNamespaceMap().Add( "___draw",
315                         GetXMLToken(XML_N_DRAW_OOO),
316                         XML_NAMESPACE_DRAW );
317 }
318 
~SvxXMLXTableImport()319 SvxXMLXTableImport::~SvxXMLXTableImport() throw ()
320 {
321 }
322 
openStorageStream(xml::sax::InputSource * pParserInput,rtl::Reference<SvXMLGraphicHelper> & rxGraphicHelper,const uno::Reference<embed::XStorage> & xStorage)323 static void openStorageStream( xml::sax::InputSource *pParserInput,
324                                rtl::Reference<SvXMLGraphicHelper>& rxGraphicHelper,
325                                const uno::Reference < embed::XStorage >& xStorage )
326 {
327     uno::Reference < io::XStream > xIStm( xStorage->openStreamElement( "Content.xml", embed::ElementModes::READ ), uno::UNO_SET_THROW );
328     pParserInput->aInputStream = xIStm->getInputStream();
329     rxGraphicHelper = SvXMLGraphicHelper::Create( xStorage, SvXMLGraphicHelperMode::Read );
330 }
331 
load(const OUString & rPath,const OUString & rReferer,const uno::Reference<embed::XStorage> & xStorage,const uno::Reference<XNameContainer> & xTable,bool * bOptLoadedFromStorage)332 bool SvxXMLXTableImport::load( const OUString &rPath, const OUString &rReferer,
333                                const uno::Reference < embed::XStorage > &xStorage,
334                                const uno::Reference< XNameContainer >& xTable,
335                                bool *bOptLoadedFromStorage ) throw()
336 {
337     bool bRet = true;
338     rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
339 
340     INetURLObject aURLObj( rPath );
341     bool bUseStorage = aURLObj.GetProtocol() == INetProtocol::NotValid; // a relative path
342 
343     try
344     {
345         uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
346 
347         uno::Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
348 
349         xml::sax::InputSource aParserInput;
350         comphelper::LifecycleProxy aNasty;
351 
352         if( !bUseStorage || !xStorage.is() )
353         {
354             SfxMedium aMedium( rPath, rReferer, StreamMode::READ | StreamMode::NOCREATE );
355             aParserInput.sSystemId = aMedium.GetName();
356 
357             if( aMedium.IsStorage() )
358             {
359                 uno::Reference < embed::XStorage > xMediumStorage( aMedium.GetStorage( false ), uno::UNO_SET_THROW );
360                 openStorageStream( &aParserInput, xGraphicHelper, xMediumStorage );
361             }
362             else
363                 aParserInput.aInputStream = aMedium.GetInputStream();
364         }
365         else // relative URL into a storage
366         {
367             uno::Reference< embed::XStorage > xSubStorage;
368             try
369             {
370                 xSubStorage = comphelper::OStorageHelper::GetStorageAtPath(
371                         xStorage, rPath, embed::ElementModes::READ, aNasty );
372             }
373             catch (const uno::Exception&)
374             {
375             }
376             if( xSubStorage.is() )
377                 openStorageStream( &aParserInput, xGraphicHelper, xSubStorage );
378             else
379             {
380                 css::uno::Reference< css::io::XStream > xStream = comphelper::OStorageHelper::GetStreamAtPath(
381                         xStorage, rPath, embed::ElementModes::READ, aNasty );
382                 if( !xStream.is() )
383                     return false;
384                 aParserInput.aInputStream = xStream->getInputStream();
385             }
386             if( bOptLoadedFromStorage )
387                 *bOptLoadedFromStorage = true;
388         }
389 
390         uno::Reference<XGraphicStorageHandler> xGraphicStorageHandler;
391         if (xGraphicHelper.is())
392             xGraphicStorageHandler = xGraphicHelper.get();
393 
394         try
395         {
396             uno::Reference< io::XSeekable > xSeek( aParserInput.aInputStream, uno::UNO_QUERY_THROW );
397             xSeek->seek( 0 );
398         }
399         catch (const uno::Exception&)
400         {
401         }
402 
403         uno::Reference<XDocumentHandler> xHandler(new SvxXMLXTableImport(xContext, xTable, xGraphicStorageHandler));
404         xParser->setDocumentHandler( xHandler );
405         xParser->parseStream( aParserInput );
406 
407         if( xGraphicHelper )
408             xGraphicHelper->dispose();
409     }
410     catch (...)
411     {
412 //      thrown each time you load a document with property tables that are not
413 //      on the current machine. FIXME: would be better to check a file exists
414 //      before importing ...
415         bRet = false;
416     }
417 
418     return bRet;
419 }
420 
CreateDocumentContext(sal_uInt16 const nPrefix,const OUString & rLocalName,const uno::Reference<XAttributeList> &)421 SvXMLImportContext *SvxXMLXTableImport::CreateDocumentContext(
422         sal_uInt16 const nPrefix, const OUString& rLocalName,
423         const uno::Reference< XAttributeList >& /*xAttrList*/)
424 {
425     if( XML_NAMESPACE_OOO == nPrefix ||
426         XML_NAMESPACE_OFFICE == nPrefix )
427     {
428         bool bOOoFormat = (XML_NAMESPACE_OFFICE == nPrefix);
429         Type aType = mrTable->getElementType();
430 
431         if ( rLocalName == "color-table" )
432         {
433             if( aType == ::cppu::UnoType<sal_Int32>::get() )
434                 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, SvxXMLTableImportContextEnum::Color, mrTable, bOOoFormat );
435         }
436         else if ( rLocalName == "marker-table" )
437         {
438             if( aType == cppu::UnoType<drawing::PolyPolygonBezierCoords>::get())
439                 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, SvxXMLTableImportContextEnum::Marker, mrTable, bOOoFormat );
440         }
441         else if ( rLocalName == "dash-table" )
442         {
443             if( aType == cppu::UnoType<drawing::LineDash>::get())
444                 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, SvxXMLTableImportContextEnum::Dash, mrTable, bOOoFormat );
445         }
446         else if ( rLocalName == "hatch-table" )
447         {
448             if( aType == cppu::UnoType<drawing::Hatch>::get())
449                 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, SvxXMLTableImportContextEnum::Hatch, mrTable, bOOoFormat );
450         }
451         else if ( rLocalName == "gradient-table" )
452         {
453             if( aType == cppu::UnoType<awt::Gradient>::get())
454                 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, SvxXMLTableImportContextEnum::Gradient, mrTable, bOOoFormat );
455         }
456         else if ( rLocalName == "bitmap-table" )
457         {
458             if( aType == ::cppu::UnoType<awt::XBitmap>::get())
459                 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, SvxXMLTableImportContextEnum::Bitmap, mrTable, bOOoFormat );
460         }
461     }
462 
463     return new SvXMLImportContext( *this, nPrefix, rLocalName );
464 }
465 
466 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
467